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