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