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