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