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