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