]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/cache.c
Move MSVC port to the Attic
[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 && !tryOnly) {
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 && !tryOnly) {
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 if we need to read from the DB,
683                                          * we use a long-lived per-thread txn for this step.
684                                          */
685                                         if ( !ep && !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                                     if ( rc == 0 ) {
696                                                 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker2,
697                                                         *eip, 1, 0, lock );
698                                         }
699                                         if ( rc == 0 && !ep) {
700                                                 rc = bdb_id2entry( op->o_bd, ltid, id, &ep );
701                                         }
702                                         if ( rc == 0 ) {
703                                                 ep->e_private = *eip;
704 #ifdef BDB_HIER
705                                                 bdb_fix_dn( ep, 0 );
706 #endif
707                                                 (*eip)->bei_e = ep;
708                                                 ep = NULL;
709                                         }
710                                         (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
711                                         if ( rc == 0 ) {
712                                                 /* If we succeeded, downgrade back to a readlock. */
713                                                 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
714                                                         *eip, 0, 0, lock );
715                                         } else {
716                                                 /* Otherwise, release the lock. */
717                                                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
718                                         }
719                                         if ( locker2 != locker ) {
720                                                 /* If we're using the per-thread txn, release all
721                                                  * of its page locks now.
722                                                  */
723                                                 DB_LOCKREQ list;
724                                                 list.op = DB_LOCK_PUT_ALL;
725                                                 list.obj = NULL;
726                                                 bdb->bi_dbenv->lock_vec( bdb->bi_dbenv, locker2,
727                                                         0, &list, 1, NULL );
728                                         }
729                                 } else if ( !(*eip)->bei_e ) {
730                                         /* Some other thread is trying to load the entry,
731                                          * give it a chance to finish.
732                                          */
733                                         bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
734                                         ldap_pvt_thread_yield();
735                                         bdb_cache_entryinfo_lock( *eip );
736                                         islocked = 1;
737                                         goto load1;
738 #ifdef BDB_HIER
739                                 } else {
740                                         /* Check for subtree renames
741                                          */
742                                         rc = bdb_fix_dn( (*eip)->bei_e, 1 );
743                                         if ( rc ) {
744                                                 bdb_cache_entry_db_relock( bdb->bi_dbenv,
745                                                         locker, *eip, 1, 0, lock );
746                                                 /* check again in case other modifier did it already */
747                                                 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
748                                                         rc = bdb_fix_dn( (*eip)->bei_e, 2 );
749                                                 bdb_cache_entry_db_relock( bdb->bi_dbenv,
750                                                         locker, *eip, 0, 0, lock );
751                                         }
752 #endif
753                                 }
754
755                         }
756                 }
757         }
758         if ( islocked ) {
759                 bdb_cache_entryinfo_unlock( *eip );
760         }
761         if ( ep ) {
762                 bdb_entry_return( ep );
763         }
764         if ( rc == 0 ) {
765                 /* set lru mutex */
766                 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
767                 /* if entry is on LRU list, remove from old spot */
768                 if ( (*eip)->bei_lrunext || (*eip)->bei_lruprev ) {
769                         LRU_DELETE( &bdb->bi_cache, *eip );
770                 } else {
771                 /* if entry is new, bump cache size */
772                         bdb->bi_cache.c_cursize++;
773                 }
774                 /* lru_mutex is unlocked for us */
775                 bdb_cache_lru_add( bdb, locker, *eip );
776         }
777
778         return rc;
779 }
780
781 int
782 bdb_cache_children(
783         Operation *op,
784         DB_TXN *txn,
785         Entry *e )
786 {
787         int rc;
788
789         if ( BEI(e)->bei_kids ) {
790                 return 0;
791         }
792         if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
793                 return DB_NOTFOUND;
794         }
795         rc = bdb_dn2id_children( op, txn, e );
796         if ( rc == DB_NOTFOUND ) {
797                 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
798         }
799         return rc;
800 }
801
802 /* Update the cache after a successful database Add. */
803 int
804 bdb_cache_add(
805         struct bdb_info *bdb,
806         EntryInfo *eip,
807         Entry *e,
808         struct berval *nrdn,
809         u_int32_t locker )
810 {
811         EntryInfo *new, ei;
812         struct berval rdn = e->e_name;
813         DB_LOCK lock;
814         int rc;
815
816         ei.bei_id = e->e_id;
817         ei.bei_parent = eip;
818         ei.bei_nrdn = *nrdn;
819         ei.bei_lockpad = 0;
820
821         /* Lock this entry so that bdb_add can run to completion.
822          * It can only fail if BDB has run out of lock resources.
823          */
824         rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, &ei, 1, 0, &lock );
825         if ( rc ) {
826                 bdb_cache_entryinfo_unlock( eip );
827                 return rc;
828         }
829
830 #ifdef BDB_HIER
831         if ( nrdn->bv_len != e->e_nname.bv_len ) {
832                 char *ptr = strchr( rdn.bv_val, ',' );
833                 rdn.bv_len = ptr - rdn.bv_val;
834         }
835         ber_dupbv( &ei.bei_rdn, &rdn );
836         if ( eip->bei_dkids ) eip->bei_dkids++;
837 #endif
838
839         rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
840         /* bdb_csn_commit can cause this when adding the database root entry */
841         if ( new->bei_e ) {
842                 new->bei_e->e_private = NULL;
843                 bdb_entry_return( new->bei_e );
844         }
845         new->bei_e = e;
846         e->e_private = new;
847         new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
848         eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
849         if (eip->bei_parent) {
850                 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
851         }
852
853         /* set lru mutex */
854         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
855         ++bdb->bi_cache.c_cursize;
856         /* lru_mutex is unlocked for us */
857         bdb_cache_lru_add( bdb, locker, new );
858
859         bdb_cache_entryinfo_unlock( eip );
860         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
861         return rc;
862 }
863
864 int
865 bdb_cache_modify(
866         Entry *e,
867         Attribute *newAttrs,
868         DB_ENV *env,
869         u_int32_t locker,
870         DB_LOCK *lock )
871 {
872         EntryInfo *ei = BEI(e);
873         int rc;
874         /* Get write lock on data */
875         rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
876
877         /* If we've done repeated mods on a cached entry, then e_attrs
878          * is no longer contiguous with the entry, and must be freed.
879          */
880         if ( ! rc ) {
881                 if ( (void *)e->e_attrs != (void *)(e+1) ) {
882                         attrs_free( e->e_attrs ); 
883                 }
884                 e->e_attrs = newAttrs;
885         }
886         return rc;
887 }
888
889 /*
890  * Change the rdn in the entryinfo. Also move to a new parent if needed.
891  */
892 int
893 bdb_cache_modrdn(
894         Entry *e,
895         struct berval *nrdn,
896         Entry *new,
897         EntryInfo *ein,
898         DB_ENV *env,
899         u_int32_t locker,
900         DB_LOCK *lock )
901 {
902         EntryInfo *ei = BEI(e), *pei;
903         struct berval rdn;
904         int rc;
905
906         /* Get write lock on data */
907         rc =  bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
908         if ( rc ) return rc;
909
910         /* If we've done repeated mods on a cached entry, then e_attrs
911          * is no longer contiguous with the entry, and must be freed.
912          */
913         if ( (void *)e->e_attrs != (void *)(e+1) ) {
914                 attrs_free( e->e_attrs );
915         }
916         e->e_attrs = new->e_attrs;
917         if( e->e_nname.bv_val < e->e_bv.bv_val ||
918                 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
919         {
920                 ch_free(e->e_name.bv_val);
921                 ch_free(e->e_nname.bv_val);
922         }
923         e->e_name = new->e_name;
924         e->e_nname = new->e_nname;
925
926         /* Lock the parent's kids AVL tree */
927         pei = ei->bei_parent;
928         bdb_cache_entryinfo_lock( pei );
929         avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
930         free( ei->bei_nrdn.bv_val );
931         ber_dupbv( &ei->bei_nrdn, nrdn );
932 #ifdef BDB_HIER
933         free( ei->bei_rdn.bv_val );
934
935         rdn = e->e_name;
936         if ( nrdn->bv_len != e->e_nname.bv_len ) {
937                 char *ptr = strchr(rdn.bv_val, ',');
938                 rdn.bv_len = ptr - rdn.bv_val;
939         }
940         ber_dupbv( &ei->bei_rdn, &rdn );
941 #endif
942
943         if (!ein) {
944                 ein = ei->bei_parent;
945         } else {
946                 ei->bei_parent = ein;
947                 bdb_cache_entryinfo_unlock( pei );
948                 bdb_cache_entryinfo_lock( ein );
949         }
950 #ifdef BDB_HIER
951         {
952                 int max = ei->bei_modrdns;
953                 /* Record the generation number of this change */
954                 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
955                         if ( pei->bei_modrdns > max ) max = pei->bei_modrdns;
956                 }
957                 ei->bei_modrdns = max + 1;
958         }
959 #endif
960         avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
961         bdb_cache_entryinfo_unlock( ein );
962         return rc;
963 }
964 /*
965  * cache_delete - delete the entry e from the cache. 
966  *
967  * returns:     0       e was deleted ok
968  *              1       e was not in the cache
969  *              -1      something bad happened
970  */
971 int
972 bdb_cache_delete(
973     Cache       *cache,
974     Entry               *e,
975     DB_ENV      *env,
976     u_int32_t   locker,
977     DB_LOCK     *lock )
978 {
979         EntryInfo *ei = BEI(e);
980         int     rc;
981
982         assert( e->e_private );
983
984         /* Set this early, warn off any queriers */
985         ei->bei_state |= CACHE_ENTRY_DELETED;
986
987         /* Lock the entry's info */
988         bdb_cache_entryinfo_lock( ei );
989
990         /* Get write lock on the data */
991         rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
992         if ( rc ) {
993                 /* couldn't lock, undo and give up */
994                 ei->bei_state ^= CACHE_ENTRY_DELETED;
995                 bdb_cache_entryinfo_unlock( ei );
996                 return rc;
997         }
998
999         /* set cache write lock */
1000         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1001
1002         /* Lock the parent's kids tree */
1003         bdb_cache_entryinfo_lock( ei->bei_parent );
1004
1005 #ifdef NEW_LOGGING
1006         LDAP_LOG( CACHE, ENTRY, 
1007                 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
1008 #else
1009         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1010                 e->e_id, 0, 0 );
1011 #endif
1012
1013         /* set lru mutex */
1014         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1015         rc = bdb_cache_delete_internal( cache, e->e_private );
1016         /* free lru mutex */
1017         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1018
1019         /* free cache write lock */
1020         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1021         bdb_cache_entryinfo_unlock( ei->bei_parent );
1022
1023         /* Leave entry info locked */
1024
1025         return( rc );
1026 }
1027
1028 void
1029 bdb_cache_delete_cleanup(
1030         Cache *cache,
1031         Entry *e )
1032 {
1033         EntryInfo *ei = BEI(e);
1034
1035         ei->bei_e = NULL;
1036         e->e_private = NULL;
1037         bdb_entry_return( e );
1038
1039         free( ei->bei_nrdn.bv_val );
1040         ei->bei_nrdn.bv_val = NULL;
1041 #ifdef BDB_HIER
1042         free( ei->bei_rdn.bv_val );
1043         ei->bei_rdn.bv_val = NULL;
1044         ei->bei_modrdns = 0;
1045         ei->bei_ckids = 0;
1046         ei->bei_dkids = 0;
1047 #endif
1048         ei->bei_parent = NULL;
1049         ei->bei_kids = NULL;
1050         ei->bei_lruprev = NULL;
1051
1052         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1053         ei->bei_lrunext = cache->c_eifree;
1054         cache->c_eifree = ei;
1055         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1056         bdb_cache_entryinfo_unlock( ei );
1057 }
1058
1059 static int
1060 bdb_cache_delete_internal(
1061     Cache       *cache,
1062     EntryInfo           *e )
1063 {
1064         int rc = 0;     /* return code */
1065
1066 #ifdef BDB_HIER
1067         e->bei_parent->bei_ckids--;
1068         if ( e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1069 #endif
1070         /* dn tree */
1071         if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1072                 == NULL )
1073         {
1074                 rc = -1;
1075         }
1076
1077         /* id tree */
1078         if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) {
1079                 rc = -1;
1080         }
1081
1082         if (rc != 0) {
1083                 return rc;
1084         }
1085
1086         /* lru */
1087         LRU_DELETE( cache, e );
1088         cache->c_cursize--;
1089
1090         /*
1091          * flag entry to be freed later by a call to cache_return_entry()
1092          */
1093         e->bei_state |= CACHE_ENTRY_DELETED;
1094
1095         return( 0 );
1096 }
1097
1098 static void
1099 bdb_entryinfo_release( void *data )
1100 {
1101         EntryInfo *ei = (EntryInfo *)data;
1102         if ( ei->bei_kids ) {
1103                 avl_free( ei->bei_kids, NULL );
1104         }
1105         if ( ei->bei_e ) {
1106                 ei->bei_e->e_private = NULL;
1107                 bdb_entry_return( ei->bei_e );
1108         }
1109         bdb_cache_entryinfo_destroy( ei );
1110 }
1111
1112 void
1113 bdb_cache_release_all( Cache *cache )
1114 {
1115         /* set cache write lock */
1116         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1117         /* set lru mutex */
1118         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1119
1120 #ifdef NEW_LOGGING
1121         LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
1122 #else
1123         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1124 #endif
1125
1126         avl_free( cache->c_dntree.bei_kids, NULL );
1127         avl_free( cache->c_idtree, bdb_entryinfo_release );
1128         for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1129                 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1130                 bdb_cache_entryinfo_destroy(cache->c_eifree);
1131         }
1132         cache->c_lruhead = NULL;
1133         cache->c_lrutail = NULL;
1134
1135         /* free lru mutex */
1136         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1137         /* free cache write lock */
1138         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1139 }
1140
1141 #ifdef LDAP_DEBUG
1142 static void
1143 bdb_lru_print( Cache *cache )
1144 {
1145         EntryInfo       *e;
1146
1147         fprintf( stderr, "LRU queue (head to tail):\n" );
1148         for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1149                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1150                         e->bei_nrdn.bv_val, e->bei_id );
1151         }
1152         fprintf( stderr, "LRU queue (tail to head):\n" );
1153         for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1154                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1155                         e->bei_nrdn.bv_val, e->bei_id );
1156         }
1157 }
1158 #endif
1159
1160 static void
1161 bdb_txn_free( void *key, void *data )
1162 {
1163         DB_TXN *txn = data;
1164         TXN_ABORT( txn );
1165 }
1166
1167 /* Obtain a long-lived transaction for the current thread */
1168 static int
1169 bdb_txn_get( Operation *op, DB_ENV *env, DB_TXN **txn )
1170 {
1171         int i, rc, lockid;
1172         void *ctx, *data;
1173
1174         /* If no op was provided, try to find the ctx anyway... */
1175         if ( op ) {
1176                 ctx = op->o_threadctx;
1177         } else {
1178                 ctx = ldap_pvt_thread_pool_context();
1179         }
1180
1181         /* Shouldn't happen unless we're single-threaded */
1182         if ( !ctx ) {
1183                 *txn = NULL;
1184                 return 0;
1185         }
1186
1187         if ( ldap_pvt_thread_pool_getkey( ctx, ((char *)env)+1, &data, NULL ) ) {
1188                 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1189                         rc = TXN_BEGIN( env, NULL, txn, 0 );
1190                         if (rc) ldap_pvt_thread_yield();
1191                 }
1192                 if ( rc != 0) {
1193                         return rc;
1194                 }
1195                 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, ((char *)env)+1,
1196                         *txn, bdb_txn_free ) ) ) {
1197                         TXN_ABORT( *txn );
1198 #ifdef NEW_LOGGING
1199                         LDAP_LOG( BACK_BDB, ERR, "bdb_txn_get: err %s(%d)\n",
1200                                 db_strerror(rc), rc, 0 );
1201 #else
1202                         Debug( LDAP_DEBUG_ANY, "bdb_txn_get: err %s(%d)\n",
1203                                 db_strerror(rc), rc, 0 );
1204 #endif
1205
1206                         return rc;
1207                 }
1208         } else {
1209                 *txn = data;
1210         }
1211         return 0;
1212 }
1213
1214 #ifdef BDB_REUSE_LOCKERS
1215 static void
1216 bdb_locker_id_free( void *key, void *data )
1217 {
1218         DB_ENV *env = key;
1219         int lockid = (int) data;
1220         int rc;
1221
1222         rc = XLOCK_ID_FREE( env, lockid );
1223         if ( rc == EINVAL ) {
1224                 DB_LOCKREQ lr;
1225 #ifdef NEW_LOGGING
1226                 LDAP_LOG( BACK_BDB, ERR,
1227                         "bdb_locker_id_free: %d err %s(%d)\n",
1228                         lockid, db_strerror(rc), rc );
1229 #else
1230                 Debug( LDAP_DEBUG_ANY,
1231                         "bdb_locker_id_free: %d err %s(%d)\n",
1232                         lockid, db_strerror(rc), rc );
1233 #endif
1234                 /* release all locks held by this locker. */
1235                 lr.op = DB_LOCK_PUT_ALL;
1236                 lr.obj = NULL;
1237                 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1238                 XLOCK_ID_FREE( env, lockid );
1239         }
1240 }
1241
1242 int
1243 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
1244 {
1245         int i, rc, lockid;
1246         void *data;
1247         void *ctx;
1248
1249         if ( !env || !locker ) return -1;
1250
1251         /* If no op was provided, try to find the ctx anyway... */
1252         if ( op ) {
1253                 ctx = op->o_threadctx;
1254         } else {
1255                 ctx = ldap_pvt_thread_pool_context();
1256         }
1257
1258         /* Shouldn't happen unless we're single-threaded */
1259         if ( !ctx ) {
1260                 *locker = 0;
1261                 return 0;
1262         }
1263
1264         if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1265                 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1266                         rc = XLOCK_ID( env, &lockid );
1267                         if (rc) ldap_pvt_thread_yield();
1268                 }
1269                 if ( rc != 0) {
1270                         return rc;
1271                 }
1272                 data = (void *)lockid;
1273                 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1274                         data, bdb_locker_id_free ) ) ) {
1275                         XLOCK_ID_FREE( env, lockid );
1276 #ifdef NEW_LOGGING
1277                         LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1278                                 db_strerror(rc), rc, 0 );
1279 #else
1280                         Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1281                                 db_strerror(rc), rc, 0 );
1282 #endif
1283
1284                         return rc;
1285                 }
1286         } else {
1287                 lockid = (int)data;
1288         }
1289         *locker = lockid;
1290         return 0;
1291 }
1292 #endif
1293
1294 void
1295 bdb_cache_delete_entry(
1296         struct bdb_info *bdb,
1297         EntryInfo *ei,
1298         u_int32_t locker,
1299         DB_LOCK *lock )
1300 {
1301         ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1302         if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, ei, 1, 1, lock ) == 0 )
1303         {
1304                 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1305                         LRU_DELETE( &bdb->bi_cache, ei );
1306                         ei->bei_e->e_private = NULL;
1307                         bdb_entry_return( ei->bei_e );
1308                         ei->bei_e = NULL;
1309                         --bdb->bi_cache.c_cursize;
1310                 }
1311                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1312         }
1313         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1314 }