]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/cache.c
fix format for new logging
[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         Entry           e, *ep;
547         ID                      id;
548         int count = 0;
549
550         e.e_dn = (char *) dn;
551         e.e_ndn = ch_strdup( dn );
552         (void) dn_normalize( e.e_ndn );
553
554 try_again:
555         /* set cache mutex */
556         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
557
558         if ( (ep = (Entry *) avl_find( cache->c_dntree, (caddr_t) &e,
559                 (AVL_CMP) entry_dn_cmp )) != NULL )
560         {
561                 int state;
562                 count++;
563
564                 /*
565                  * ep now points to an unlocked entry
566                  * we do not need to lock the entry if we only
567                  * check the state, refcnt, LRU, and id.
568                  */
569
570                 assert( ep->e_private );
571
572                 /* save id */
573                 id = ep->e_id;
574                 state = LEI(ep)->lei_state;
575
576                 /*
577                  * entry is deleted or not fully created yet
578                  */
579                 if ( state != CACHE_ENTRY_READY ) {
580                         assert(state != CACHE_ENTRY_UNDEFINED);
581
582                         /* free cache mutex */
583                         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
584
585 #ifdef NEW_LOGGING
586                         LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
587                                    "cache_find_entry_dn2id: (%s)%ld not ready: %d\n",
588                                    dn, id, state ));
589 #else
590                         Debug(LDAP_DEBUG_TRACE,
591                                 "====> cache_find_entry_dn2id(\"%s\"): %ld (not ready) %d\n",
592                                 dn, id, state);
593 #endif
594
595
596                         ldap_pvt_thread_yield();
597                         goto try_again;
598                 }
599
600                 /* lru */
601                 LRU_DELETE( cache, ep );
602                 LRU_ADD( cache, ep );
603                 
604                 /* free cache mutex */
605                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
606
607 #ifdef NEW_LOGGING
608                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
609                            "cache_find_entry_dn2id: (%s)%ld %d tries\n",
610                            dn, id, count ));
611 #else
612                 Debug(LDAP_DEBUG_TRACE,
613                         "====> cache_find_entry_dn2id(\"%s\"): %ld (%d tries)\n",
614                         dn, id, count);
615 #endif
616
617
618         } else {
619                 /* free cache mutex */
620                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
621
622                 id = NOID;
623         }
624
625         free(e.e_ndn);
626
627         return( id );
628 }
629
630 /*
631  * cache_find_entry_id - find an entry in the cache, given id
632  */
633
634 Entry *
635 cache_find_entry_id(
636         Cache   *cache,
637         ID                              id,
638         int                             rw
639 )
640 {
641         Entry   e;
642         Entry   *ep;
643         int     count = 0;
644
645         e.e_id = id;
646
647 try_again:
648         /* set cache mutex */
649         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
650
651         if ( (ep = (Entry *) avl_find( cache->c_idtree, (caddr_t) &e,
652                 (AVL_CMP) entry_id_cmp )) != NULL )
653         {
654                 int state;
655                 ID      ep_id;
656
657                 count++;
658
659                 assert( ep->e_private );
660
661                 ep_id = ep->e_id; 
662                 state = LEI(ep)->lei_state;
663
664                 /*
665                  * entry is deleted or not fully created yet
666                  */
667                 if ( state != CACHE_ENTRY_READY ) {
668
669                         assert(state != CACHE_ENTRY_UNDEFINED);
670
671                         /* free cache mutex */
672                         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
673
674 #ifdef NEW_LOGGING
675                         LDAP_LOG(( "caceh", LDAP_LEVEL_INFO,
676                                    "cache_find_entry_id: (%ld)->%ld not ready (%d).\n",
677                                    id, ep_id, state ));
678                                    
679 #else
680                         Debug(LDAP_DEBUG_TRACE,
681                                 "====> cache_find_entry_id( %ld ): %ld (not ready) %d\n",
682                                 id, ep_id, state);
683 #endif
684
685
686                         ldap_pvt_thread_yield();
687                         goto try_again;
688                 }
689
690                 /* acquire reader lock */
691                 if ( cache_entry_rdwr_trylock(ep, rw) == LDAP_PVT_THREAD_EBUSY ) {
692                         /* could not acquire entry lock...
693                          * owner cannot free as we have the cache locked.
694                          * so, unlock the cache, yield, and try again.
695                          */
696
697                         /* free cache mutex */
698                         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
699
700 #ifdef NEW_LOGGING
701                         LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
702                                    "cache_find_entry_id: %ld -> %ld (busy) %d.\n",
703                                    id, ep_id, state ));
704 #else
705                         Debug(LDAP_DEBUG_TRACE,
706                                 "====> cache_find_entry_id( %ld ): %ld (busy) %d\n",
707                                 id, ep_id, state);
708 #endif
709
710
711                         ldap_pvt_thread_yield();
712                         goto try_again;
713                 }
714
715                 /* lru */
716                 LRU_DELETE( cache, ep );
717                 LRU_ADD( cache, ep );
718                 
719                 LEI(ep)->lei_refcnt++;
720
721                 /* free cache mutex */
722                 ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
723
724 #ifdef NEW_LOGGING
725                 LDAP_LOG(( "cache", LDAP_LEVEL_DETAIL1,
726                            "cache_find_entry_id: %ld -> %s  found %d tries.\n",
727                            ep_id, ep->e_dn, count ));
728 #else
729                 Debug(LDAP_DEBUG_TRACE,
730                         "====> cache_find_entry_id( %ld ) \"%s\" (found) (%d tries)\n",
731                         ep_id, ep->e_dn, count);
732 #endif
733
734
735                 return( ep );
736         }
737
738         /* free cache mutex */
739         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
740
741         return( NULL );
742 }
743
744 /*
745  * cache_delete_entry - delete the entry e from the cache.  the caller
746  * should have obtained e (increasing its ref count) via a call to one
747  * of the cache_find_* routines.  the caller should *not* call the
748  * cache_return_entry() routine prior to calling cache_delete_entry().
749  * it performs this function.
750  *
751  * returns:     0       e was deleted ok
752  *              1       e was not in the cache
753  *              -1      something bad happened
754  */
755 int
756 cache_delete_entry(
757     Cache       *cache,
758     Entry               *e
759 )
760 {
761         int     rc;
762
763         /* set cache mutex */
764         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
765
766         assert( e->e_private );
767
768 #ifdef NEW_LOGGING
769         LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
770                    "cache_delete_entry: delete %ld.\n", e->e_id ));
771 #else
772         Debug( LDAP_DEBUG_TRACE, "====> cache_delete_entry( %ld )\n",
773                 e->e_id, 0, 0 );
774 #endif
775
776
777         rc = cache_delete_entry_internal( cache, e );
778
779         /* free cache mutex */
780         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
781         return( rc );
782 }
783
784 static int
785 cache_delete_entry_internal(
786     Cache       *cache,
787     Entry               *e
788 )
789 {
790         int rc = 0;     /* return code */
791
792         /* dn tree */
793         if ( avl_delete( &cache->c_dntree, (caddr_t) e, (AVL_CMP) entry_dn_cmp )
794                 == NULL )
795         {
796                 rc = -1;
797         }
798
799         /* id tree */
800         if ( avl_delete( &cache->c_idtree, (caddr_t) e, (AVL_CMP) entry_id_cmp )
801                 == NULL )
802         {
803                 rc = -1;
804         }
805
806         if (rc != 0) {
807                 return rc;
808         }
809
810         /* lru */
811         LRU_DELETE( cache, e );
812         cache->c_cursize--;
813
814         /*
815          * flag entry to be freed later by a call to cache_return_entry()
816          */
817         LEI(e)->lei_state = CACHE_ENTRY_DELETED;
818
819         return( 0 );
820 }
821
822 void
823 cache_release_all( Cache *cache )
824 {
825         Entry *e;
826         int rc;
827
828         /* set cache mutex */
829         ldap_pvt_thread_mutex_lock( &cache->c_mutex );
830
831 #ifdef NEW_LOGGING
832         LDAP_LOG(( "cache", LDAP_LEVEL_ENTRY,
833                    "cache_release_all: enter\n" ));
834 #else
835         Debug( LDAP_DEBUG_TRACE, "====> cache_release_all\n", 0, 0, 0 );
836 #endif
837
838
839         while ( (e = cache->c_lrutail) != NULL && LEI(e)->lei_refcnt == 0 ) {
840 #ifdef LDAP_RDWR_DEBUG
841                 assert(!ldap_pvt_thread_rdwr_active(&LEI(e)->lei_rdwr));
842 #endif
843
844                 /* delete from cache and lru q */
845                 /* XXX do we need rc ? */
846                 rc = cache_delete_entry_internal( cache, e );
847                 cache_entry_private_destroy( e );
848                 entry_free( e );
849         }
850
851         if ( cache->c_cursize ) {
852 #ifdef NEW_LOGGING
853                 LDAP_LOG(( "cache", LDAP_LEVEL_INFO,
854                            "cache_release_all: Entry cache could not be emptied.\n" ));
855 #else
856                 Debug( LDAP_DEBUG_TRACE, "Entry-cache could not be emptied\n", 0, 0, 0 );
857 #endif
858
859         }
860
861         /* free cache mutex */
862         ldap_pvt_thread_mutex_unlock( &cache->c_mutex );
863 }
864
865 #ifdef LDAP_DEBUG
866
867 static void
868 lru_print( Cache *cache )
869 {
870         Entry   *e;
871
872         fprintf( stderr, "LRU queue (head to tail):\n" );
873         for ( e = cache->c_lruhead; e != NULL; e = LEI(e)->lei_lrunext ) {
874                 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
875                         e->e_dn, e->e_id, LEI(e)->lei_refcnt );
876         }
877         fprintf( stderr, "LRU queue (tail to head):\n" );
878         for ( e = cache->c_lrutail; e != NULL; e = LEI(e)->lei_lruprev ) {
879                 fprintf( stderr, "\tdn \"%20s\" id %ld refcnt %d\n",
880                         e->e_dn, e->e_id, LEI(e)->lei_refcnt );
881         }
882 }
883
884 #endif
885