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