]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/cache.c
ab1ad2940f80e0df57dc4cff0e860c72aec9310d
[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         int rc = 0;
318         char *ptr;
319
320         /* this function is always called with normalized DN */
321         if ( *res ) {
322                 /* we're doing a onelevel search for an RDN */
323                 ei.bei_nrdn.bv_val = ndn->bv_val;
324                 ei.bei_nrdn.bv_len = dn_rdnlen( be, ndn );
325                 eip = *res;
326         } else {
327                 /* we're searching a full DN from the root */
328                 ptr = ndn->bv_val + ndn->bv_len - be->be_nsuffix[0].bv_len;
329                 ei.bei_nrdn.bv_val = ptr;
330                 ei.bei_nrdn.bv_len = be->be_nsuffix[0].bv_len;
331                 eip = &bdb->bi_cache.c_dntree;
332         }
333         
334         for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
335                 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
336                 if ( !ei2 ) {
337                         int len = ei.bei_nrdn.bv_len;
338                                 
339                         ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
340                         bdb_cache_entryinfo_unlock( eip );
341
342                         rc = bdb_dn2id( be, txn, &ei.bei_nrdn, &ei, ctx );
343                         if (rc) {
344                                 bdb_cache_entryinfo_lock( eip );
345                                 *res = eip;
346                                 return rc;
347                         }
348
349                         /* DN exists but needs to be added to cache */
350                         ei.bei_nrdn.bv_len = len;
351                         rc = bdb_entryinfo_add_internal( bdb,
352                                 eip, ei.bei_id, &ei.bei_nrdn, &ei2, locker );
353                         /* add_internal left eip and c_rwlock locked */
354                         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
355                         if ( rc ) {
356                                 *res = eip;
357                                 return rc;
358                         }
359                 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
360                         /* In the midst of deleting? Give it a chance to
361                          * complete.
362                          */
363                         bdb_cache_entryinfo_unlock( eip );
364                         ldap_pvt_thread_yield();
365                         bdb_cache_entryinfo_lock( eip );
366                         *res = eip;
367                         return DB_NOTFOUND;
368                 }
369                 bdb_cache_entryinfo_unlock( eip );
370                 bdb_cache_entryinfo_lock( ei2 );
371
372                 eip = ei2;
373
374                 /* Advance to next lower RDN */
375                 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
376                         && !DN_SEPARATOR(*ptr); ptr--);
377                 if ( ptr >= ndn->bv_val ) {
378                         if (DN_SEPARATOR(*ptr)) ptr++;
379                         ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
380                         ei.bei_nrdn.bv_val = ptr;
381                 }
382                 if ( ptr < ndn->bv_val ) {
383                         *res = eip;
384                         break;
385                 }
386         }
387
388         return rc;
389 }
390
391 /*
392  * cache_find_entry_id - find an entry in the cache, given id.
393  * The entry is locked for Read upon return. Call with islocked TRUE if
394  * the supplied *eip was already locked.
395  */
396
397 int
398 bdb_cache_find_entry_id(
399         Backend *be,
400         DB_TXN  *tid,
401         ID                              id,
402         EntryInfo       **eip,
403         int             islocked,
404         u_int32_t       locker,
405         DB_LOCK         *lock,
406         void            *ctx
407 )
408 {
409         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
410         Entry   *ep = NULL;
411         int     rc = 0;
412         EntryInfo ei;
413
414         ei.bei_id = id;
415
416         /* If we weren't given any info, see if we have it already cached */
417         if ( !*eip ) {
418                 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
419                 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
420                                         (caddr_t) &ei, bdb_id_cmp );
421                 if ( *eip ) {
422                         bdb_cache_entryinfo_lock( *eip );
423                         islocked = 1;
424                 }
425                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
426         }
427
428         /* See if the ID exists in the database; add it to the cache if so */
429         if ( !*eip ) {
430                 rc = bdb_id2entry( be, tid, id, &ep );
431                 if ( rc == 0 ) {
432                         rc = bdb_cache_find_entry_ndn2id( be, tid,
433                                 &ep->e_nname, eip, locker, ctx );
434                         if ( *eip )
435                                 islocked = 1;
436                         if ( rc ) {
437                                 bdb_entry_return( ep );
438                                 ep = NULL;
439                         }
440                 }
441         }
442
443         /* Ok, we found the info, do we have the entry? */
444         if ( *eip && rc == 0 ) {
445                 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
446                         rc = DB_NOTFOUND;
447                 } else if (!(*eip)->bei_e ) {
448                         if (!ep) {
449                                 rc = bdb_id2entry( be, tid, id, &ep );
450                         }
451                         if ( rc == 0 ) {
452                                 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
453                                         *eip, 1, 0, lock );
454                                 (*eip)->bei_e = ep;
455                                 ep->e_private = *eip;
456                                 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
457                                         *eip, 0, 0, lock );
458                         }
459                 } else {
460                         bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
461                                         *eip, 0, 0, lock );
462                 }
463         }
464         if ( rc == 0 && (*eip)->bei_kids == NULL ) {
465                 /* set lru mutex */
466                 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
467                 LRU_DELETE( &bdb->bi_cache, *eip );
468                 LRU_ADD( &bdb->bi_cache, *eip );
469                 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
470         }
471
472         if ( islocked ) {
473                 bdb_cache_entryinfo_unlock( *eip );
474         }
475         return rc;
476 }
477
478 int
479 bdb_cache_children(
480         Operation *op,
481         DB_TXN *txn,
482         Entry *e
483 )
484 {
485         int rc;
486
487         if ( BEI(e)->bei_kids ) {
488                 return 0;
489         }
490         if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
491                 return DB_NOTFOUND;
492         }
493         rc = bdb_dn2id_children( op, txn, e );
494         if ( rc == DB_NOTFOUND ) {
495                 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
496         }
497         return rc;
498 }
499
500 /* Update the cache after a successful database Add. */
501 int
502 bdb_cache_add(
503         struct bdb_info *bdb,
504         EntryInfo *ei,
505         Entry *e,
506         struct berval *nrdn,
507         u_int32_t locker
508 )
509 {
510         EntryInfo *new;
511         int rc;
512
513         rc = bdb_entryinfo_add_internal( bdb, ei, e->e_id, nrdn, &new, locker );
514         new->bei_e = e;
515         e->e_private = new;
516         new->bei_state = CACHE_ENTRY_NO_KIDS;
517         ei->bei_state &= ~CACHE_ENTRY_NO_KIDS;
518         bdb_cache_entryinfo_unlock( ei );
519         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
520         return rc;
521 }
522
523 int
524 bdb_cache_modify(
525         Entry *e,
526         Attribute *newAttrs,
527         DB_ENV *env,
528         u_int32_t locker,
529         DB_LOCK *lock
530 )
531 {
532         EntryInfo *ei = BEI(e);
533         
534         /* Get write lock on data */
535         bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
536
537         /* If we've done repeated mods on a cached entry, then e_attrs
538          * is no longer contiguous with the entry, and must be freed.
539          */
540         if ( (void *)e->e_attrs != (void *)(e+1) ) {
541                 attrs_free( e->e_attrs );
542         }
543         e->e_attrs = newAttrs;
544
545         return 0;
546 }
547
548 /*
549  * Change the rdn in the entryinfo. Also move to a new parent if needed.
550  */
551 int
552 bdb_cache_modrdn(
553         Entry *e,
554         struct berval *nrdn,
555         Entry *new,
556         EntryInfo *ein,
557         DB_ENV *env,
558         u_int32_t locker,
559         DB_LOCK *lock
560 )
561 {
562         EntryInfo *ei = BEI(e), *pei;
563         int rc = 0;
564
565         /* Get write lock on data */
566         bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
567
568         /* If we've done repeated mods on a cached entry, then e_attrs
569          * is no longer contiguous with the entry, and must be freed.
570          */
571         if ( (void *)e->e_attrs != (void *)(e+1) ) {
572                 attrs_free( e->e_attrs );
573         }
574         e->e_attrs = new->e_attrs;
575 #ifdef BDB_HIER
576         ch_free(e->e_name.bv_val);
577 #else
578         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
579                 e->e_bv.bv_val + e->e_bv.bv_len ) {
580                 ch_free(e->e_name.bv_val);
581                 ch_free(e->e_nname.bv_val);
582         }
583 #endif
584         e->e_name = new->e_name;
585         e->e_nname = new->e_nname;
586
587         /* Lock the parent's kids AVL tree */
588         pei = ei->bei_parent;
589         bdb_cache_entryinfo_lock( pei );
590         avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
591         free( ei->bei_nrdn.bv_val );
592         ber_dupbv( &ei->bei_nrdn, nrdn );
593         if (!ein) {
594                 ein = ei->bei_parent;
595         } else {
596                 ei->bei_parent = ein;
597                 bdb_cache_entryinfo_unlock( pei );
598                 bdb_cache_entryinfo_lock( ein );
599         }
600         avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
601         bdb_cache_entryinfo_unlock( ein );
602         return rc;
603 }
604 /*
605  * cache_delete_entry - delete the entry e from the cache. 
606  *
607  * returns:     0       e was deleted ok
608  *              1       e was not in the cache
609  *              -1      something bad happened
610  */
611 int
612 bdb_cache_delete_entry(
613     Cache       *cache,
614     Entry               *e,
615     DB_ENV      *env,
616     u_int32_t   locker,
617     DB_LOCK     *lock
618 )
619 {
620         EntryInfo *ei = BEI(e);
621         int     rc;
622
623         assert( e->e_private );
624
625         /* Set this early, warn off any queriers */
626         ei->bei_state |= CACHE_ENTRY_DELETED;
627
628         /* Get write lock on the data */
629         bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
630
631         /* set cache write lock */
632         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
633
634         /* Lock the parent's kids tree */
635         bdb_cache_entryinfo_lock( ei->bei_parent );
636
637 #ifdef NEW_LOGGING
638         LDAP_LOG( CACHE, ENTRY, 
639                 "bdb_cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 );
640 #else
641         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n",
642                 e->e_id, 0, 0 );
643 #endif
644
645         /* set lru mutex */
646         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
647         rc = bdb_cache_delete_entry_internal( cache, e->e_private );
648         /* free lru mutex */
649         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
650
651         /* free cache write lock */
652         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
653         bdb_cache_entryinfo_unlock( ei->bei_parent );
654         bdb_cache_entryinfo_destroy( ei );
655         e->e_private = NULL;
656         return( rc );
657 }
658
659 static int
660 bdb_cache_delete_entry_internal(
661     Cache       *cache,
662     EntryInfo           *e
663 )
664 {
665         int rc = 0;     /* return code */
666
667         /* dn tree */
668         if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
669         {
670                 rc = -1;
671         }
672
673         /* If parent has no more kids, put in on LRU list */
674         if ( e->bei_parent->bei_kids == NULL ) {
675                 LRU_ADD( cache, e->bei_parent );
676                 cache->c_cursize++;
677         }
678
679         /* id tree */
680         if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
681         {
682                 rc = -1;
683         }
684
685         if (rc != 0) {
686                 return rc;
687         }
688
689         /* lru */
690         LRU_DELETE( cache, e );
691         cache->c_cursize--;
692
693         /*
694          * flag entry to be freed later by a call to cache_return_entry()
695          */
696         e->bei_state |= CACHE_ENTRY_DELETED;
697
698         return( 0 );
699 }
700
701 static void
702 bdb_entryinfo_release( void *data )
703 {
704         EntryInfo *ei = (EntryInfo *)data;
705         avl_free( ei->bei_kids, NULL );
706         if ( ei->bei_e ) {
707                 ei->bei_e->e_private = NULL;
708                 bdb_entry_return( ei->bei_e );
709         }
710         bdb_cache_entryinfo_destroy( ei );
711 }
712
713 void
714 bdb_cache_release_all( Cache *cache )
715 {
716         /* set cache write lock */
717         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
718         /* set lru mutex */
719         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
720
721 #ifdef NEW_LOGGING
722         LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
723 #else
724         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
725 #endif
726
727         avl_free( cache->c_dntree.bei_kids, NULL );
728         avl_free( cache->c_idtree, bdb_entryinfo_release );
729         cache->c_lruhead = NULL;
730         cache->c_lrutail = NULL;
731
732         /* free lru mutex */
733         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
734         /* free cache write lock */
735         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
736 }
737
738 #ifdef LDAP_DEBUG
739 static void
740 bdb_lru_print( Cache *cache )
741 {
742         EntryInfo       *e;
743
744         fprintf( stderr, "LRU queue (head to tail):\n" );
745         for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
746                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
747                         e->bei_nrdn.bv_val, e->bei_id );
748         }
749         fprintf( stderr, "LRU queue (tail to head):\n" );
750         for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
751                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
752                         e->bei_nrdn.bv_val, e->bei_id );
753         }
754 }
755 #endif
756
757 #ifdef BDB_REUSE_LOCKERS
758 void
759 bdb_locker_id_free( void *key, void *data )
760 {
761         DB_ENV *env = key;
762         int lockid = (int) data;
763
764         XLOCK_ID_FREE( env, lockid );
765 }
766
767 int
768 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
769 {
770         int i, rc, lockid;
771         void *data;
772         void *ctx;
773
774         if ( !env || !locker ) return -1;
775
776         /* If no op was provided, try to find the ctx anyway... */
777         if ( op ) {
778                 ctx = op->o_threadctx;
779         } else {
780                 ctx = ldap_pvt_thread_pool_context();
781         }
782
783         /* Shouldn't happen unless we're single-threaded */
784         if ( !ctx ) {
785                 *locker = 0;
786                 return 0;
787         }
788
789         if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
790                 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
791                         rc = XLOCK_ID( env, &lockid );
792                         if (rc) ldap_pvt_thread_yield();
793                 }
794                 if ( rc != 0) {
795                         return rc;
796                 }
797                 data = (void *)lockid;
798                 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
799                         data, bdb_locker_id_free ) ) ) {
800                         XLOCK_ID_FREE( env, lockid );
801 #ifdef NEW_LOGGING
802                         LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
803                                 db_strerror(rc), rc, 0 );
804 #else
805                         Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
806                                 db_strerror(rc), rc, 0 );
807 #endif
808
809                         return rc;
810                 }
811         } else {
812                 lockid = (int)data;
813         }
814         *locker = lockid;
815         return 0;
816 }
817 #endif