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