]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/cache.c
Prevent ldbm_sync from being called by ldbm_cache_close when the new
[openldap] / servers / slapd / back-ldbm / cache.c
1 /* cache.c - routines to maintain an in-core cache of entries */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 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-ldbm.h"
19
20 /* LDBM backend specific entry info -- visible only to the cache */
21 typedef struct ldbm_entry_info {
22         ldap_pvt_thread_rdwr_t  lei_rdwr;       /* reader/writer lock */
23
24         /*
25          * remaining fields require backend cache lock to access
26          * These items are specific to the LDBM backend and should
27          * be hidden.
28          */
29         int             lei_state;      /* for the cache */
30 #define CACHE_ENTRY_UNDEFINED   0
31 #define CACHE_ENTRY_CREATING    1
32 #define CACHE_ENTRY_READY               2
33 #define CACHE_ENTRY_DELETED             3
34
35         int             lei_refcnt;     /* # threads ref'ing this entry */
36         Entry   *lei_lrunext;   /* for cache lru list */
37         Entry   *lei_lruprev;
38 } EntryInfo;
39 #undef LEI
40 #define LEI(e)  ((EntryInfo *) ((e)->e_private))
41
42 static int      cache_delete_entry_internal(Cache *cache, Entry *e);
43 #ifdef LDAP_DEBUG
44 static void     lru_print(Cache *cache);
45 #endif
46
47 static int
48 cache_entry_rdwr_lock(Entry *e, int rw)
49 {
50 #ifdef NEW_LOGGING
51         LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
52                    "cache_entry_rdwr_lock: %s lock on ID %ld\n",
53                    rw ? "w" : "r", e->e_id ));
54 #else
55         Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%slock: ID: %ld\n",
56                 rw ? "w" : "r", e->e_id, 0);
57 #endif
58
59
60         if (rw)
61                 return ldap_pvt_thread_rdwr_wlock(&LEI(e)->lei_rdwr);
62         else
63                 return ldap_pvt_thread_rdwr_rlock(&LEI(e)->lei_rdwr);
64 }
65
66 static int
67 cache_entry_rdwr_trylock(Entry *e, int rw)
68 {
69 #ifdef NEW_LOGGING
70         LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
71                    "cache_entry_rdwr_trylock: try %s lock on ID: %ld.\n",
72                    rw ? "w" : "r", e->e_id ));
73 #else
74         Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%strylock: ID: %ld\n",
75                 rw ? "w" : "r", e->e_id, 0);
76 #endif
77
78
79         if (rw)
80                 return ldap_pvt_thread_rdwr_wtrylock(&LEI(e)->lei_rdwr);
81         else
82                 return ldap_pvt_thread_rdwr_rtrylock(&LEI(e)->lei_rdwr);
83 }
84
85 static int
86 cache_entry_rdwr_unlock(Entry *e, int rw)
87 {
88 #ifdef NEW_LOGGING
89         LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
90                    "cache_entry_rdwr_unlock: remove %s lock on ID %ld.\n",
91                    rw ? "w" : "r", e->e_id ));
92 #else
93         Debug( LDAP_DEBUG_ARGS, "entry_rdwr_%sunlock: ID: %ld\n",
94                 rw ? "w" : "r", e->e_id, 0);
95 #endif
96
97
98         if (rw)
99                 return ldap_pvt_thread_rdwr_wunlock(&LEI(e)->lei_rdwr);
100         else
101                 return ldap_pvt_thread_rdwr_runlock(&LEI(e)->lei_rdwr);
102 }
103
104 static int
105 cache_entry_rdwr_init(Entry *e)
106 {
107         return ldap_pvt_thread_rdwr_init( &LEI(e)->lei_rdwr );
108 }
109
110 static int
111 cache_entry_rdwr_destroy(Entry *e)
112 {
113         return ldap_pvt_thread_rdwr_destroy( &LEI(e)->lei_rdwr );
114 }
115
116 static int
117 cache_entry_private_init( Entry*e )
118 {
119         assert( e->e_private == NULL );
120
121         if( e->e_private != NULL ) {
122                 /* this should never happen */
123                 return 1;
124         }
125
126         e->e_private = ch_calloc(1, sizeof(struct ldbm_entry_info));
127
128         if( cache_entry_rdwr_init( e ) != 0 ) {
129                 free( LEI(e) );
130                 e->e_private = NULL;
131                 return 1;
132         } 
133
134         return 0;
135 }
136
137 static int
138 cache_entry_private_destroy( Entry*e )
139 {
140         assert( e->e_private );
141
142         cache_entry_rdwr_destroy( e );
143
144         free( e->e_private );
145         e->e_private = NULL;
146         return 0;
147 }
148
149 void
150 cache_return_entry_rw( Cache *cache, Entry *e, int rw )
151 {
152         ID id;
153         int refcnt;
154
155         /* set cache mutex */
156         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
157
158         assert( e->e_private );
159
160         cache_entry_rdwr_unlock(e, rw);
161
162         id = e->e_id;
163         refcnt = --LEI(e)->lei_refcnt;
164
165         if ( LEI(e)->lei_state == CACHE_ENTRY_CREATING ) {
166                 LEI(e)->lei_state = CACHE_ENTRY_READY;
167
168                 /* free cache mutex */
169                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
170
171 #ifdef NEW_LOGGING
172                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
173                            "cache_return_entry_rw: return (%ld):%s, refcnt=%d\n",
174                            id, rw ? "w" : "r", refcnt ));
175 #else
176                 Debug( LDAP_DEBUG_TRACE,
177                         "====> cache_return_entry_%s( %ld ): created (%d)\n",
178                         rw ? "w" : "r", id, refcnt );
179 #endif
180
181
182         } else if ( LEI(e)->lei_state == CACHE_ENTRY_DELETED ) {
183                 if( refcnt > 0 ) {
184                         /* free cache mutex */
185                         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
186
187 #ifdef NEW_LOGGING
188                         LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
189                                    "cache_return_entry_rw: %ld, delete pending (%d).\n",
190                                    id, refcnt ));
191 #else
192                         Debug( LDAP_DEBUG_TRACE,
193                                 "====> cache_return_entry_%s( %ld ): delete pending (%d)\n",
194                                 rw ? "w" : "r", id, refcnt );
195 #endif
196
197
198                 } else {
199                         cache_entry_private_destroy( e );
200                         entry_free( e );
201
202                         /* free cache mutex */
203                         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
204
205 #ifdef NEW_LOGGING
206                         LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
207                                    "cache_return_entry_rw: (%ld): deleted (%d)\n",
208                                    id, refcnt ));
209 #else
210                         Debug( LDAP_DEBUG_TRACE,
211                                 "====> cache_return_entry_%s( %ld ): deleted (%d)\n",
212                                 rw ? "w" : "r", id, refcnt );
213 #endif
214
215                 }
216
217         } else {
218                 /* free cache mutex */
219                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
220
221 #ifdef NEW_LOGGING
222                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
223                            "cache_return_entry_rw: ID %ld:%s returned (%d)\n",
224                            id, rw ? "w": "r", refcnt ));
225 #else
226                 Debug( LDAP_DEBUG_TRACE,
227                         "====> cache_return_entry_%s( %ld ): returned (%d)\n",
228                         rw ? "w" : "r", id, refcnt);
229 #endif
230
231         }
232 }
233
234 #define LRU_DELETE( cache, e ) do { \
235         if ( LEI(e)->lei_lruprev != NULL ) { \
236                 LEI(LEI(e)->lei_lruprev)->lei_lrunext = LEI(e)->lei_lrunext; \
237         } else { \
238                 (cache)->c_lruhead = LEI(e)->lei_lrunext; \
239         } \
240         if ( LEI(e)->lei_lrunext != NULL ) { \
241                 LEI(LEI(e)->lei_lrunext)->lei_lruprev = LEI(e)->lei_lruprev; \
242         } else { \
243                 (cache)->c_lrutail = LEI(e)->lei_lruprev; \
244         } \
245 } while(0)
246
247 #define LRU_ADD( cache, e ) do { \
248         LEI(e)->lei_lrunext = (cache)->c_lruhead; \
249         if ( LEI(e)->lei_lrunext != NULL ) { \
250                 LEI(LEI(e)->lei_lrunext)->lei_lruprev = (e); \
251         } \
252         (cache)->c_lruhead = (e); \
253         LEI(e)->lei_lruprev = NULL; \
254         if ( (cache)->c_lrutail == NULL ) { \
255                 (cache)->c_lrutail = (e); \
256         } \
257 } while(0)
258
259 /*
260  * cache_add_entry_rw - create and lock an entry in the cache
261  * returns:     0       entry has been created and locked
262  *              1       entry already existed
263  *              -1      something bad happened
264  */
265 int
266 cache_add_entry_rw(
267     Cache       *cache,
268     Entry               *e,
269         int             rw
270 )
271 {
272         int     i, rc;
273         Entry   *ee;
274
275 #ifdef NEW_LOGGING
276         LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
277                    "cache_add_entry_rw: add (%s):%s to cache\n",
278                    e->e_dn, rw ? "w" : "r" ));
279 #endif
280         /* set cache mutex */
281         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
282
283         assert( e->e_private == NULL );
284
285         if( cache_entry_private_init(e) != 0 ) {
286                 /* free cache mutex */
287                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
288
289 #ifdef NEW_LOGGING
290                 LDAP_LOG(( "cache", LDAP_LEVEL_ERR,
291                            "cache_add_entry_rw: add (%s):%ld private init failed!\n",
292                            e->e_dn, e->e_id ));
293 #else
294                 Debug( LDAP_DEBUG_ANY,
295                         "====> cache_add_entry( %ld ): \"%s\": private init failed!\n",
296                     e->e_id, e->e_dn, 0 );
297 #endif
298
299
300                 return( -1 );
301         }
302
303         if ( avl_insert( &cache->c_dntree, (caddr_t) e,
304                 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
305         {
306                 /* free cache mutex */
307                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
308
309 #ifdef NEW_LOGGING
310                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
311                            "cache_add_entry: (%s):%ld already in cache.\n",
312                            e->e_dn, e->e_id ));
313 #else
314                 Debug( LDAP_DEBUG_TRACE,
315                         "====> cache_add_entry( %ld ): \"%s\": already in dn cache\n",
316                     e->e_id, e->e_dn, 0 );
317 #endif
318
319
320                 cache_entry_private_destroy(e);
321
322                 return( 1 );
323         }
324
325         /* id tree */
326         if ( avl_insert( &cache->c_idtree, (caddr_t) e,
327                 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
328         {
329 #ifdef NEW_LOGGING
330                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
331                            "cache_add_entry: (%s):%ls already in cache.\n",
332                            e->e_dn, e->e_id ));
333 #else
334                 Debug( LDAP_DEBUG_ANY,
335                         "====> cache_add_entry( %ld ): \"%s\": already in id cache\n",
336                     e->e_id, e->e_dn, 0 );
337 #endif
338
339
340
341                 /* delete from dn tree inserted above */
342                 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
343                         (AVL_CMP) entry_dn_cmp ) == NULL )
344                 {
345 #ifdef NEW_LOGGING
346                         LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
347                                    "cache_add_entry: can't delete (%s) from cache.\n",
348                                    e->e_dn ));
349 #else
350                         Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
351                             0, 0, 0 );
352 #endif
353
354                 }
355
356                 cache_entry_private_destroy(e);
357
358                 /* free cache mutex */
359                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
360                 return( -1 );
361         }
362
363         cache_entry_rdwr_lock( e, rw );
364
365         /* put the entry into 'CREATING' state */
366         /* will be marked after when entry is returned */
367         LEI(e)->lei_state = CACHE_ENTRY_CREATING;
368         LEI(e)->lei_refcnt = 1;
369
370         /* lru */
371         LRU_ADD( cache, e );
372         if ( ++cache->c_cursize > cache->c_maxsize ) {
373                 /*
374                  * find the lru entry not currently in use and delete it.
375                  * in case a lot of entries are in use, only look at the
376                  * first 10 on the tail of the list.
377                  */
378                 i = 0;
379                 while ( cache->c_lrutail != NULL &&
380                         LEI(cache->c_lrutail)->lei_refcnt != 0 &&
381                         i < 10 )
382                 {
383                         /* move this in-use entry to the front of the q */
384                         ee = cache->c_lrutail;
385                         LRU_DELETE( cache, ee );
386                         LRU_ADD( cache, ee );
387                         i++;
388                 }
389
390                 /*
391                  * found at least one to delete - try to get back under
392                  * the max cache size.
393                  */
394                 while ( cache->c_lrutail != NULL &&
395                         LEI(cache->c_lrutail)->lei_refcnt == 0 &&
396                         cache->c_cursize > cache->c_maxsize )
397                 {
398                         e = cache->c_lrutail;
399
400                         /* delete from cache and lru q */
401                         /* XXX do we need rc ? */
402                         rc = cache_delete_entry_internal( cache, e );
403                         cache_entry_private_destroy( e );
404                         entry_free( e );
405                 }
406         }
407
408         /* free cache mutex */
409         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
410         return( 0 );
411 }
412
413 /*
414  * cache_update_entry - update a LOCKED entry which has been deleted.
415  * returns:     0       entry has been created and locked
416  *              1       entry already existed
417  *              -1      something bad happened
418  */
419 int
420 cache_update_entry(
421     Cache       *cache,
422     Entry               *e
423 )
424 {
425         int     i, rc;
426         Entry   *ee;
427
428         /* set cache mutex */
429         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
430
431         assert( e->e_private );
432
433         if ( avl_insert( &cache->c_dntree, (caddr_t) e,
434                 (AVL_CMP) entry_dn_cmp, avl_dup_error ) != 0 )
435         {
436 #ifdef NEW_LOGGING
437                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
438                            "cache_update_entry: (%s):%ld already in dn cache\n",
439                            e->e_dn, e->e_id ));
440 #else
441                 Debug( LDAP_DEBUG_TRACE,
442                         "====> cache_update_entry( %ld ): \"%s\": already in dn cache\n",
443                     e->e_id, e->e_dn, 0 );
444 #endif
445
446
447                 /* free cache mutex */
448                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
449                 return( 1 );
450         }
451
452         /* id tree */
453         if ( avl_insert( &cache->c_idtree, (caddr_t) e,
454                 (AVL_CMP) entry_id_cmp, avl_dup_error ) != 0 )
455         {
456 #ifdef NEW_LOGGING
457                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
458                            "cache_update_entry: (%s)%ld already in id cache\n",
459                            e->e_dn, e->e_id ));
460 #else
461                 Debug( LDAP_DEBUG_ANY,
462                         "====> cache_update_entry( %ld ): \"%s\": already in id cache\n",
463                     e->e_id, e->e_dn, 0 );
464 #endif
465
466
467                 /* delete from dn tree inserted above */
468                 if ( avl_delete( &cache->c_dntree, (caddr_t) e,
469                         (AVL_CMP) entry_dn_cmp ) == NULL )
470                 {
471 #ifdef NEW_LOGGING
472                         LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
473                                    "cache_update_entry: can't delete (%s)%ld from dn cache.\n",
474                                    e->e_dn, e->e_id ));
475 #else
476                         Debug( LDAP_DEBUG_ANY, "====> can't delete from dn cache\n",
477                             0, 0, 0 );
478 #endif
479
480                 }
481
482                 /* free cache mutex */
483                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
484                 return( -1 );
485         }
486
487
488         /* put the entry into 'CREATING' state */
489         /* will be marked after when entry is returned */
490         LEI(e)->lei_state = CACHE_ENTRY_CREATING;
491
492         /* lru */
493         LRU_ADD( cache, e );
494         if ( ++cache->c_cursize > cache->c_maxsize ) {
495                 /*
496                  * find the lru entry not currently in use and delete it.
497                  * in case a lot of entries are in use, only look at the
498                  * first 10 on the tail of the list.
499                  */
500                 i = 0;
501                 while ( cache->c_lrutail != NULL &&
502                         LEI(cache->c_lrutail)->lei_refcnt != 0 &&
503                         i < 10 )
504                 {
505                         /* move this in-use entry to the front of the q */
506                         ee = cache->c_lrutail;
507                         LRU_DELETE( cache, ee );
508                         LRU_ADD( cache, ee );
509                         i++;
510                 }
511
512                 /*
513                  * found at least one to delete - try to get back under
514                  * the max cache size.
515                  */
516                 while ( cache->c_lrutail != NULL &&
517                         LEI(cache->c_lrutail)->lei_refcnt == 0 &&
518                         cache->c_cursize > cache->c_maxsize )
519                 {
520                         e = cache->c_lrutail;
521
522                         /* delete from cache and lru q */
523                         /* XXX do we need rc ? */
524                         rc = cache_delete_entry_internal( cache, e );
525                         cache_entry_private_destroy( e );
526                         entry_free( e );
527                 }
528         }
529
530         /* free cache mutex */
531         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
532         return( 0 );
533 }
534
535 /*
536  * cache_find_entry_dn2id - find an entry in the cache, given dn
537  */
538
539 ID
540 cache_find_entry_dn2id(
541         Backend         *be,
542     Cache       *cache,
543     const char          *dn
544 )
545 {
546         char                    *ndn;
547         ID                      id;
548
549         ndn = ch_strdup( dn );
550         (void) dn_normalize( ndn );
551
552         id = cache_find_entry_ndn2id( be, cache, ndn );
553
554         free( ndn );
555
556         return ( id );
557 }
558
559 ID
560 cache_find_entry_ndn2id(
561         Backend         *be,
562     Cache       *cache,
563     const char          *ndn
564 )
565 {
566         Entry           e, *ep;
567         ID                      id;
568         int count = 0;
569
570         /* this function is always called with normalized DN */
571         e.e_ndn = (char *)ndn;
572
573 try_again:
574         /* set cache mutex */
575         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
576
577         if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
578                 (AVL_CMP) entry_dn_cmp )) != NULL )
579         {
580                 int state;
581                 count++;
582
583                 /*
584                  * ep now points to an unlocked entry
585                  * we do not need to lock the entry if we only
586                  * check the state, refcnt, LRU, and id.
587                  */
588
589                 assert( ep->e_private );
590
591                 /* save id */
592                 id = ep->e_id;
593                 state = LEI(ep)->lei_state;
594
595                 /*
596                  * entry is deleted or not fully created yet
597                  */
598                 if ( state != CACHE_ENTRY_READY ) {
599                         assert(state != CACHE_ENTRY_UNDEFINED);
600
601                         /* free cache mutex */
602                         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
603
604 #ifdef NEW_LOGGING
605                         LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
606                                    "cache_find_entry_dn2id: (%s)%ld not ready: %d\n",
607                                    dn, id, state ));
608 #else
609                         Debug(LDAP_DEBUG_TRACE,
610                                 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
611                                 ndn, id, state);
612 #endif
613
614
615                         ldap_pvt_thread_yield();
616                         goto try_again;
617                 }
618
619                 /* lru */
620                 LRU_DELETE( cache, ep );
621                 LRU_ADD( cache, ep );
622                 
623                 /* free cache mutex */
624                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
625
626 #ifdef NEW_LOGGING
627                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
628                            "cache_find_entry_dn2id: (%s)%ld %d tries\n",
629                            dn, id, count ));
630 #else
631                 Debug(LDAP_DEBUG_TRACE,
632                         "====> cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
633                         ndn, id, count);
634 #endif
635
636
637         } else {
638                 /* free cache mutex */
639                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
640
641                 id = NOID;
642         }
643
644         return( id );
645 }
646
647 /*
648  * cache_find_entry_id - find an entry in the cache, given id
649  */
650
651 Entry *
652 cache_find_entry_id(
653         Cache   *cache,
654         ID                              id,
655         int                             rw
656 )
657 {
658         Entry   e;
659         Entry   *ep;
660         int     count = 0;
661
662         e.e_id = id;
663
664 try_again:
665         /* set cache mutex */
666         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
667
668         if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
669                 (AVL_CMP) entry_id_cmp )) != NULL )
670         {
671                 int state;
672                 ID      ep_id;
673
674                 count++;
675
676                 assert( ep->e_private );
677
678                 ep_id = ep->e_id; 
679                 state = LEI(ep)->lei_state;
680
681                 /*
682                  * entry is deleted or not fully created yet
683                  */
684                 if ( state != CACHE_ENTRY_READY ) {
685
686                         assert(state != CACHE_ENTRY_UNDEFINED);
687
688                         /* free cache mutex */
689                         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
690
691 #ifdef NEW_LOGGING
692                         LDAP_LOG(( "caceh", LDAP_LEVEL_INFO,
693                                    "cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
694                                    id, ep_id, state ));
695                                    
696 #else
697                         Debug(LDAP_DEBUG_TRACE,
698                                 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
699                                 id, ep_id, state);
700 #endif
701
702
703                         ldap_pvt_thread_yield();
704                         goto try_again;
705                 }
706
707                 /* acquire reader lock */
708                 if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
709                         /* could not acquire entry lock...
710                          * owner cannot free as we have the cache locked.
711                          * so, unlock the cache, yield, and try again.
712                          */
713
714                         /* free cache mutex */
715                         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
716
717 #ifdef NEW_LOGGING
718                         LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
719                                    "cache_find_entry_id: %ld -> %ld (busy) %d.\n",
720                                    id, ep_id, state ));
721 #else
722                         Debug(LDAP_DEBUG_TRACE,
723                                 "====> cache_find_entry_id( %ld ): %ld (busy) %d\n",
724                                 id, ep_id, state);
725 #endif
726
727
728                         ldap_pvt_thread_yield();
729                         goto try_again;
730                 }
731
732                 /* lru */
733                 LRU_DELETE( cache, ep );
734                 LRU_ADD( cache, ep );
735                 
736                 LEI(ep)->lei_refcnt++;
737
738                 /* free cache mutex */
739                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
740
741 #ifdef NEW_LOGGING
742                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
743                            "cache_find_entry_id: %ld -> %s  found %d tries.\n",
744                            ep_id, ep->e_dn, count ));
745 #else
746                 Debug(LDAP_DEBUG_TRACE,
747                         "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
748                         ep_id, ep->e_dn, count);
749 #endif
750
751
752                 return( ep );
753         }
754
755         /* free cache mutex */
756         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
757
758         return( NULL );
759 }
760
761 /*
762  * cache_delete_entry - delete the entry e from the cache.  the caller
763  * should have obtained e (increasing its ref count) via a call to one
764  * of the cache_find_* routines.  the caller should *not* call the
765  * cache_return_entry() routine prior to calling cache_delete_entry().
766  * it performs this function.
767  *
768  * returns:     0       e was deleted ok
769  *              1       e was not in the cache
770  *              -1      something bad happened
771  */
772 int
773 cache_delete_entry(
774     Cache       *cache,
775     Entry               *e
776 )
777 {
778         int     rc;
779
780         /* set cache mutex */
781         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
782
783         assert( e->e_private );
784
785 #ifdef NEW_LOGGING
786         LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
787                    "cache_delete_entry: delete %ld.\n", e->e_id ));
788 #else
789         Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
790                 e->e_id, 0, 0 );
791 #endif
792
793
794         rc = cache_delete_entry_internal( cache, e );
795
796         /* free cache mutex */
797         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
798         return( rc );
799 }
800
801 static int
802 cache_delete_entry_internal(
803     Cache       *cache,
804     Entry               *e
805 )
806 {
807         int rc = 0;     /* return code */
808
809         /* dn tree */
810         if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
811                 == NULL )
812         {
813                 rc = -1;
814         }
815
816         /* id tree */
817         if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
818                 == NULL )
819         {
820                 rc = -1;
821         }
822
823         if (rc != 0) {
824                 return rc;
825         }
826
827         /* lru */
828         LRU_DELETE( cache, e );
829         cache->c_cursize--;
830
831         /*
832          * flag entry to be freed later by a call to cache_return_entry()
833          */
834         LEI(e)->lei_state = CACHE_ENTRY_DELETED;
835
836         return( 0 );
837 }
838
839 void
840 cache_release_all( Cache *cache )
841 {
842         Entry *e;
843         int rc;
844
845         /* set cache mutex */
846         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
847
848 #ifdef NEW_LOGGING
849         LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
850                    "cache_release_all: enter\n" ));
851 #else
852         Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
853 #endif
854
855
856         while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
857 #ifdef LDAP_RDWR_DEBUG
858                 assert(!ldap_pvt_thread_rdwr_active(&LEI(e)->lei_rdwr));
859 #endif
860
861                 /* delete from cache and lru q */
862                 /* XXX do we need rc ? */
863                 rc = cache_delete_entry_internal( cache, e );
864                 cache_entry_private_destroy( e );
865                 entry_free( e );
866         }
867
868         if ( cache->c_cursize ) {
869 #ifdef NEW_LOGGING
870                 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
871                            "cache_release_all: Entry cache could not be emptied.\n" ));
872 #else
873                 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
874 #endif
875
876         }
877
878         /* free cache mutex */
879         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
880 }
881
882 #ifdef LDAP_DEBUG
883
884 static void
885 lru_print( Cache *cache )
886 {
887         Entry   *e;
888
889         fprintf( stderr, "LRU queue (head to tail):\n" );
890         for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
891                 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
892                         e->e_dn, e->e_id, LEI(e)->lei_refcnt );
893         }
894         fprintf( stderr, "LRU queue (tail to head):\n" );
895         for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
896                 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
897                         e->e_dn, e->e_id, LEI(e)->lei_refcnt );
898         }
899 }
900
901 #endif
902