]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/cache.c
use LDAP_SIZELIMIT_EXCEEDED; rework the return values of slap_send_search_entry(...
[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 #ifdef SLAPD_UNUSED
36 static void     bdb_lru_print(Cache *cache);
37 #endif
38 #endif
39
40 static EntryInfo *
41 bdb_cache_entryinfo_new( Cache *cache )
42 {
43         EntryInfo *ei = NULL;
44
45         if ( cache->c_eifree ) {
46                 ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
47                 if ( cache->c_eifree ) {
48                         ei = cache->c_eifree;
49                         cache->c_eifree = ei->bei_lrunext;
50                 }
51                 ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
52         }
53         if ( ei ) {
54                 ei->bei_lrunext = NULL;
55                 ei->bei_state = 0;
56         } else {
57                 ei = ch_calloc(1, sizeof(struct bdb_entry_info));
58                 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
59         }
60
61         return ei;
62 }
63
64 /* Atomically release and reacquire a lock */
65 int
66 bdb_cache_entry_db_relock(
67         DB_ENV *env,
68         u_int32_t locker,
69         EntryInfo *ei,
70         int rw,
71         int tryOnly,
72         DB_LOCK *lock )
73 {
74 #ifdef NO_THREADS
75         return 0;
76 #else
77         int     rc;
78         DBT     lockobj;
79         DB_LOCKREQ list[2];
80
81         if ( !lock ) return 0;
82
83         lockobj.data = &ei->bei_id;
84         lockobj.size = sizeof(ei->bei_id) + 1;
85
86         list[0].op = DB_LOCK_PUT;
87         list[0].lock = *lock;
88         list[1].op = DB_LOCK_GET;
89         list[1].lock = *lock;
90         list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
91         list[1].obj = &lockobj;
92         rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
93                 list, 2, NULL );
94
95         if (rc && !tryOnly) {
96                 Debug( LDAP_DEBUG_TRACE,
97                         "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
98                         ei->bei_id, rw, rc );
99         } else {
100                 *lock = list[1].lock;
101         }
102         return rc;
103 #endif
104 }
105
106 static int
107 bdb_cache_entry_db_lock( DB_ENV *env, u_int32_t locker, EntryInfo *ei,
108         int rw, int tryOnly, DB_LOCK *lock )
109 {
110 #ifdef NO_THREADS
111         return 0;
112 #else
113         int       rc;
114         DBT       lockobj;
115         int       db_rw;
116
117         if ( !lock ) return 0;
118
119         if (rw)
120                 db_rw = DB_LOCK_WRITE;
121         else
122                 db_rw = DB_LOCK_READ;
123
124         lockobj.data = &ei->bei_id;
125         lockobj.size = sizeof(ei->bei_id) + 1;
126
127         rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
128                                         &lockobj, db_rw, lock);
129         if (rc && !tryOnly) {
130                 Debug( LDAP_DEBUG_TRACE,
131                         "bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
132                         ei->bei_id, rw, rc );
133         }
134         return rc;
135 #endif /* NO_THREADS */
136 }
137
138 int
139 bdb_cache_entry_db_unlock ( DB_ENV *env, DB_LOCK *lock )
140 {
141 #ifdef NO_THREADS
142         return 0;
143 #else
144         int rc;
145
146         if ( !lock ) return 0;
147
148         rc = LOCK_PUT ( env, lock );
149         return rc;
150 #endif
151 }
152
153 static int
154 bdb_cache_entryinfo_destroy( EntryInfo *e )
155 {
156         ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
157         free( e->bei_nrdn.bv_val );
158 #ifdef BDB_HIER
159         free( e->bei_rdn.bv_val );
160 #endif
161         free( e );
162         return 0;
163 }
164
165 #define LRU_DELETE( cache, ei ) do { \
166         if ( (ei)->bei_lruprev != NULL ) { \
167                 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
168         } else { \
169                 (cache)->c_lruhead = (ei)->bei_lrunext; \
170         } \
171         if ( (ei)->bei_lrunext != NULL ) { \
172                 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
173         } else { \
174                 (cache)->c_lrutail = (ei)->bei_lruprev; \
175         } \
176         (ei)->bei_lrunext = (ei)->bei_lruprev = NULL; \
177 } while(0)
178
179 #define LRU_ADD( cache, ei ) do { \
180         (ei)->bei_lrunext = (cache)->c_lruhead; \
181         if ( (ei)->bei_lrunext != NULL ) { \
182                 (ei)->bei_lrunext->bei_lruprev = (ei); \
183         } \
184         (cache)->c_lruhead = (ei); \
185         (ei)->bei_lruprev = NULL; \
186         if ( (cache)->c_lrutail == NULL ) { \
187                 (cache)->c_lrutail = (ei); \
188         } \
189 } while(0)
190
191 /* Do a length-ordered sort on normalized RDNs */
192 static int
193 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
194 {
195         const EntryInfo *e1 = v_e1, *e2 = v_e2;
196         int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
197         if (rc == 0) {
198                 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
199                         e1->bei_nrdn.bv_len );
200         }
201         return rc;
202 }
203
204 static int
205 bdb_id_cmp( const void *v_e1, const void *v_e2 )
206 {
207         const EntryInfo *e1 = v_e1, *e2 = v_e2;
208         return e1->bei_id - e2->bei_id;
209 }
210
211 /* Create an entryinfo in the cache. Caller must release the locks later.
212  */
213 static int
214 bdb_entryinfo_add_internal(
215         struct bdb_info *bdb,
216         EntryInfo *ei,
217         EntryInfo **res )
218 {
219         EntryInfo *ei2 = NULL;
220
221         *res = NULL;
222
223         ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
224
225         ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
226         bdb_cache_entryinfo_lock( ei->bei_parent );
227
228         ei2->bei_id = ei->bei_id;
229         ei2->bei_parent = ei->bei_parent;
230 #ifdef BDB_HIER
231         ei2->bei_rdn = ei->bei_rdn;
232 #endif
233 #ifdef SLAP_ZONE_ALLOC
234         ei2->bei_bdb = bdb;
235 #endif
236
237         /* Add to cache ID tree */
238         if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
239                 EntryInfo *eix;
240                 eix = avl_find( bdb->bi_cache.c_idtree, ei2, bdb_id_cmp );
241                 bdb_cache_entryinfo_destroy( ei2 );
242                 ei2 = eix;
243 #ifdef BDB_HIER
244                 /* It got freed above because its value was
245                  * assigned to ei2.
246                  */
247                 ei->bei_rdn.bv_val = NULL;
248 #endif
249         } else {
250                 bdb->bi_cache.c_eiused++;
251                 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
252
253                 /* This is a new leaf node. But if parent had no kids, then it was
254                  * a leaf and we would be decrementing that. So, only increment if
255                  * the parent already has kids.
256                  */
257                 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
258                         bdb->bi_cache.c_leaves++;
259                 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
260                         avl_dup_error );
261 #ifdef BDB_HIER
262                 ei->bei_parent->bei_ckids++;
263 #endif
264         }
265
266         *res = ei2;
267         return 0;
268 }
269
270 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
271  * the info for its closest ancestor. *res should be NULL to process a
272  * complete DN starting from the tree root. Otherwise *res must be the
273  * immediate parent of the requested DN, and only the RDN will be searched.
274  * The EntryInfo is locked upon return and must be unlocked by the caller.
275  */
276 int
277 bdb_cache_find_ndn(
278         Operation       *op,
279         DB_TXN          *txn,
280         struct berval   *ndn,
281         EntryInfo       **res )
282 {
283         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
284         EntryInfo       ei, *eip, *ei2;
285         int rc = 0;
286         char *ptr;
287
288         /* this function is always called with normalized DN */
289         if ( *res ) {
290                 /* we're doing a onelevel search for an RDN */
291                 ei.bei_nrdn.bv_val = ndn->bv_val;
292                 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
293                 eip = *res;
294         } else {
295                 /* we're searching a full DN from the root */
296                 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
297                 ei.bei_nrdn.bv_val = ptr;
298                 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
299                 /* Skip to next rdn if suffix is empty */
300                 if ( ei.bei_nrdn.bv_len == 0 ) {
301                         for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
302                                 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
303                         if ( ptr >= ndn->bv_val ) {
304                                 if (DN_SEPARATOR(*ptr)) ptr++;
305                                 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
306                                 ei.bei_nrdn.bv_val = ptr;
307                         }
308                 }
309                 eip = &bdb->bi_cache.c_dntree;
310         }
311         
312         for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
313                 ei.bei_parent = eip;
314                 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
315                 if ( !ei2 ) {
316                         int len = ei.bei_nrdn.bv_len;
317                                 
318                         if ( BER_BVISEMPTY( ndn )) {
319                                 *res = eip;
320                                 return LDAP_SUCCESS;
321                         }
322
323                         ei.bei_nrdn.bv_len = ndn->bv_len -
324                                 (ei.bei_nrdn.bv_val - ndn->bv_val);
325                         bdb_cache_entryinfo_unlock( eip );
326
327                         rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
328                         if (rc) {
329                                 bdb_cache_entryinfo_lock( eip );
330                                 *res = eip;
331                                 return rc;
332                         }
333
334                         /* DN exists but needs to be added to cache */
335                         ei.bei_nrdn.bv_len = len;
336                         rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
337                         /* add_internal left eip and c_rwlock locked */
338                         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
339                         if ( rc ) {
340                                 *res = eip;
341                                 return rc;
342                         }
343                 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
344                         /* In the midst of deleting? Give it a chance to
345                          * complete.
346                          */
347                         bdb_cache_entryinfo_unlock( eip );
348                         ldap_pvt_thread_yield();
349                         bdb_cache_entryinfo_lock( eip );
350                         *res = eip;
351                         return DB_NOTFOUND;
352                 }
353                 bdb_cache_entryinfo_unlock( eip );
354                 bdb_cache_entryinfo_lock( ei2 );
355
356                 eip = ei2;
357
358                 /* Advance to next lower RDN */
359                 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
360                         && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
361                 if ( ptr >= ndn->bv_val ) {
362                         if (DN_SEPARATOR(*ptr)) ptr++;
363                         ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
364                         ei.bei_nrdn.bv_val = ptr;
365                 }
366                 if ( ptr < ndn->bv_val ) {
367                         *res = eip;
368                         break;
369                 }
370         }
371
372         return rc;
373 }
374
375 #ifdef BDB_HIER
376 /* Walk up the tree from a child node, looking for an ID that's already
377  * been linked into the cache.
378  */
379 int
380 hdb_cache_find_parent(
381         Operation *op,
382         DB_TXN *txn,
383         u_int32_t       locker,
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 = 0;
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, locker, &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                         addlru = 0;
439
440                 }
441                 if ( addlru ) {
442                         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
443                         LRU_ADD( &bdb->bi_cache, ein );
444                         ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
445                 }
446                 addlru = 1;
447
448                 /* If this is the first time, save this node
449                  * to be returned later.
450                  */
451                 if ( eir == NULL ) eir = ein;
452
453                 /* If there was a previous node, link it to this one */
454                 if ( ei2 ) ei2->bei_parent = ein;
455
456                 /* Look for this node's parent */
457                 if ( eip.bei_id ) {
458                         ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
459                                         (caddr_t) &eip, bdb_id_cmp );
460                 } else {
461                         ei2 = &bdb->bi_cache.c_dntree;
462                 }
463                 bdb->bi_cache.c_eiused++;
464                 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
465                                 bdb->bi_cache.c_leaves++;
466                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
467
468                 /* Got the parent, link in and we're done. */
469                 if ( ei2 ) {
470                         bdb_cache_entryinfo_lock( ei2 );
471                         ein->bei_parent = ei2;
472                         avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
473                                 avl_dup_error);
474                         ei2->bei_ckids++;
475                         bdb_cache_entryinfo_unlock( ei2 );
476                         bdb_cache_entryinfo_lock( eir );
477
478                         /* Reset all the state info */
479                         for (ein = eir; ein != ei2; ein=ein->bei_parent)
480                                 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
481                         *res = eir;
482                         break;
483                 }
484                 ei.bei_kids = NULL;
485                 ei.bei_id = eip.bei_id;
486                 ei.bei_ckids = 1;
487                 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
488                         avl_dup_error );
489         }
490         return rc;
491 }
492
493 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
494  * of a given node
495  */
496 int hdb_cache_load(
497         struct bdb_info *bdb,
498         EntryInfo *ei,
499         EntryInfo **res )
500 {
501         EntryInfo *ei2;
502         int rc;
503
504         /* See if we already have this one */
505         bdb_cache_entryinfo_lock( ei->bei_parent );
506         ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
507         bdb_cache_entryinfo_unlock( ei->bei_parent );
508
509         if ( !ei2 ) {
510                 /* Not found, add it */
511                 struct berval bv;
512
513                 /* bei_rdn was not malloc'd before, do it now */
514                 ber_dupbv( &bv, &ei->bei_rdn );
515                 ei->bei_rdn = bv;
516
517                 rc = bdb_entryinfo_add_internal( bdb, ei, res );
518                 bdb_cache_entryinfo_unlock( ei->bei_parent );
519                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
520         } else {
521                 /* Found, return it */
522                 *res = ei2;
523                 return 0;
524         }
525         return rc;
526 }
527 #endif
528
529 /* caller must have lru_mutex locked. mutex
530  * will be unlocked on return.
531  */
532 static void
533 bdb_cache_lru_add(
534         struct bdb_info *bdb,
535         u_int32_t       locker,
536         EntryInfo *ei )
537 {
538         DB_LOCK         lock, *lockp;
539
540         if ( locker ) {
541                 lockp = &lock;
542         } else {
543                 lockp = NULL;
544         }
545
546         /* See if we're above the cache size limit */
547         if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
548                 EntryInfo *elru, *elprev;
549                 int i = 0;
550
551                 /* Look for an unused entry to remove */
552                 for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev, i++ ) {
553                         elprev = elru->bei_lruprev;
554
555                         /* Too many probes, not enough idle, give up */
556                         if (i > 10)
557                                 break;
558
559                         /* If we can successfully writelock it, then
560                          * the object is idle.
561                          */
562                         if ( bdb_cache_entry_db_lock( bdb->bi_dbenv,
563                                         bdb->bi_cache.c_locker, elru, 1, 1, lockp ) == 0 ) {
564
565                                 int stop = 0, decr = 0;
566
567                                 /* If this node is in the process of linking into the cache,
568                                  * or this node is being deleted, skip it.
569                                  */
570                                 if ( elru->bei_state &
571                                         ( CACHE_ENTRY_NOT_LINKED | CACHE_ENTRY_DELETED )) {
572                                         bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
573                                         continue;
574                                 }
575                                 /* Free entry for this node if it's present */
576                                 if ( elru->bei_e ) {
577                                         elru->bei_e->e_private = NULL;
578 #ifdef SLAP_ZONE_ALLOC
579                                         bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
580 #else
581                                         bdb_entry_return( elru->bei_e );
582 #endif
583                                         elru->bei_e = NULL;
584                                         decr = 1;
585                                 }
586                                 /* ITS#4010 if we're in slapcat, and this node is a leaf
587                                  * node, free it.
588                                  *
589                                  * FIXME: we need to do this for slapd as well, (which is
590                                  * why we compute bi_cache.c_leaves now) but at the moment
591                                  * we can't because it causes unresolvable deadlocks. 
592                                  */
593                                 if ( slapMode & SLAP_TOOL_READONLY ) {
594                                         if ( !elru->bei_kids ) {
595                                                 /* This does LRU_DELETE for us */
596                                                 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
597                                                 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
598                                         }
599                                         /* Leave node on LRU list for a future pass */
600                                 } else {
601                                         LRU_DELETE( &bdb->bi_cache, elru );
602                                 }
603                                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
604
605                                 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
606                                 if ( decr )
607                                         --bdb->bi_cache.c_cursize;
608                                 if (bdb->bi_cache.c_cursize <= bdb->bi_cache.c_maxsize)
609                                         stop = 1;
610                                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
611                                 if (stop) break;
612                         }
613                 }
614         }
615         LRU_ADD( &bdb->bi_cache, ei );
616         ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
617 }
618
619 EntryInfo *
620 bdb_cache_find_info(
621         struct bdb_info *bdb,
622         ID id )
623 {
624         EntryInfo       ei = { 0 },
625                         *ei2;
626
627         ei.bei_id = id;
628
629         ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
630         ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
631                                         (caddr_t) &ei, bdb_id_cmp );
632         ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
633         return ei2;
634 }
635
636 /*
637  * cache_find_id - find an entry in the cache, given id.
638  * The entry is locked for Read upon return. Call with islocked TRUE if
639  * the supplied *eip was already locked.
640  */
641
642 int
643 bdb_cache_find_id(
644         Operation *op,
645         DB_TXN  *tid,
646         ID                              id,
647         EntryInfo       **eip,
648         int             islocked,
649         u_int32_t       locker,
650         DB_LOCK         *lock )
651 {
652         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
653         Entry   *ep = NULL;
654         int     rc = 0, load = 0;
655         EntryInfo ei = { 0 };
656
657         ei.bei_id = id;
658
659 #ifdef SLAP_ZONE_ALLOC
660         slap_zh_rlock(bdb->bi_cache.c_zctx);
661 #endif
662         /* If we weren't given any info, see if we have it already cached */
663         if ( !*eip ) {
664 again:  ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
665                 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
666                         (caddr_t) &ei, bdb_id_cmp );
667                 if ( *eip ) {
668                         /* If the lock attempt fails, the info is in use */
669                         if ( ldap_pvt_thread_mutex_trylock(
670                                         &(*eip)->bei_kids_mutex )) {
671                                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
672                                 /* If this node is being deleted, treat
673                                  * as if the delete has already finished
674                                  */
675                                 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
676                                         return DB_NOTFOUND;
677                                 }
678                                 /* otherwise, wait for the info to free up */
679                                 ldap_pvt_thread_yield();
680                                 goto again;
681                         }
682                         /* If this info isn't hooked up to its parent yet,
683                          * unlock and wait for it to be fully initialized
684                          */
685                         if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
686                                 bdb_cache_entryinfo_unlock( *eip );
687                                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
688                                 ldap_pvt_thread_yield();
689                                 goto again;
690                         }
691                         islocked = 1;
692                 }
693                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
694         }
695
696         /* See if the ID exists in the database; add it to the cache if so */
697         if ( !*eip ) {
698 #ifndef BDB_HIER
699                 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
700                 if ( rc == 0 ) {
701                         rc = bdb_cache_find_ndn( op, tid,
702                                 &ep->e_nname, eip );
703                         if ( *eip ) islocked = 1;
704                         if ( rc ) {
705 #ifdef SLAP_ZONE_ALLOC
706                                 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
707 #else
708                                 bdb_entry_return( ep );
709 #endif
710                                 ep = NULL;
711                         }
712                 }
713 #else
714                 rc = hdb_cache_find_parent(op, tid, locker, id, eip );
715                 if ( rc == 0 && *eip ) islocked = 1;
716 #endif
717         }
718
719         /* Ok, we found the info, do we have the entry? */
720         if ( *eip && rc == 0 ) {
721                 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
722                         rc = DB_NOTFOUND;
723                 } else {
724                         /* Make sure only one thread tries to load the entry */
725 load1:
726 #ifdef SLAP_ZONE_ALLOC
727                         if ((*eip)->bei_e && !slap_zn_validate(
728                                         bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
729                                 (*eip)->bei_e = NULL;
730                                 (*eip)->bei_zseq = 0;
731                         }
732 #endif
733                         if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
734                                 load = 1;
735                                 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
736                         }
737                         if ( islocked ) {
738                                 bdb_cache_entryinfo_unlock( *eip );
739                                 islocked = 0;
740                         }
741                         rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, *eip, 0, 0, lock );
742                         if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
743                                 rc = DB_NOTFOUND;
744                                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
745                         } else if ( rc == 0 ) {
746                                 if ( load ) {
747                                         /* Give up original read lock, obtain write lock
748                                          */
749                                     if ( rc == 0 ) {
750                                                 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
751                                                         *eip, 1, 0, lock );
752                                         }
753                                         if ( rc == 0 && !ep) {
754                                                 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
755                                         }
756                                         if ( rc == 0 ) {
757                                                 ep->e_private = *eip;
758 #ifdef BDB_HIER
759                                                 bdb_fix_dn( ep, 0 );
760 #endif
761                                                 (*eip)->bei_e = ep;
762 #ifdef SLAP_ZONE_ALLOC
763                                                 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
764 #endif
765                                                 ep = NULL;
766                                         }
767                                         (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
768                                         if ( rc == 0 ) {
769                                                 /* If we succeeded, downgrade back to a readlock. */
770                                                 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
771                                                         *eip, 0, 0, lock );
772                                         } else {
773                                                 /* Otherwise, release the lock. */
774                                                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
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         DB_LOCK lock;
883         int rc;
884 #ifdef BDB_HIER
885         struct berval rdn = e->e_name;
886 #endif
887
888         ei.bei_id = e->e_id;
889         ei.bei_parent = eip;
890         ei.bei_nrdn = *nrdn;
891         ei.bei_lockpad = 0;
892
893         /* Lock this entry so that bdb_add can run to completion.
894          * It can only fail if BDB has run out of lock resources.
895          */
896         rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, &ei, 1, 0, &lock );
897         if ( rc ) {
898                 bdb_cache_entryinfo_unlock( eip );
899                 return rc;
900         }
901
902 #ifdef BDB_HIER
903         if ( nrdn->bv_len != e->e_nname.bv_len ) {
904                 char *ptr = ber_bvchr( &rdn, ',' );
905                 assert( ptr != NULL );
906                 rdn.bv_len = ptr - rdn.bv_val;
907         }
908         ber_dupbv( &ei.bei_rdn, &rdn );
909         if ( eip->bei_dkids ) eip->bei_dkids++;
910 #endif
911
912         rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
913         /* bdb_csn_commit can cause this when adding the database root entry */
914         if ( new->bei_e ) {
915                 new->bei_e->e_private = NULL;
916 #ifdef SLAP_ZONE_ALLOC
917                 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
918 #else
919                 bdb_entry_return( new->bei_e );
920 #endif
921         }
922         new->bei_e = e;
923         e->e_private = new;
924         new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
925         eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
926         if (eip->bei_parent) {
927                 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
928         }
929         bdb_cache_entryinfo_unlock( eip );
930
931         ++bdb->bi_cache.c_cursize;
932         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
933
934         /* set lru mutex */
935         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
936
937         /* lru_mutex is unlocked for us */
938         bdb_cache_lru_add( bdb, locker, new );
939
940         return rc;
941 }
942
943 int
944 bdb_cache_modify(
945         Entry *e,
946         Attribute *newAttrs,
947         DB_ENV *env,
948         u_int32_t locker,
949         DB_LOCK *lock )
950 {
951         EntryInfo *ei = BEI(e);
952         int rc;
953         /* Get write lock on data */
954         rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
955
956         /* If we've done repeated mods on a cached entry, then e_attrs
957          * is no longer contiguous with the entry, and must be freed.
958          */
959         if ( ! rc ) {
960                 if ( (void *)e->e_attrs != (void *)(e+1) ) {
961                         attrs_free( e->e_attrs ); 
962                 }
963                 e->e_attrs = newAttrs;
964         }
965         return rc;
966 }
967
968 /*
969  * Change the rdn in the entryinfo. Also move to a new parent if needed.
970  */
971 int
972 bdb_cache_modrdn(
973         struct bdb_info *bdb,
974         Entry *e,
975         struct berval *nrdn,
976         Entry *new,
977         EntryInfo *ein,
978         u_int32_t locker,
979         DB_LOCK *lock )
980 {
981         EntryInfo *ei = BEI(e), *pei;
982         int rc;
983 #ifdef BDB_HIER
984         struct berval rdn;
985 #endif
986
987         /* Get write lock on data */
988         rc =  bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ei, 1, 0, lock );
989         if ( rc ) return rc;
990
991         /* If we've done repeated mods on a cached entry, then e_attrs
992          * is no longer contiguous with the entry, and must be freed.
993          */
994         if ( (void *)e->e_attrs != (void *)(e+1) ) {
995                 attrs_free( e->e_attrs );
996         }
997         e->e_attrs = new->e_attrs;
998         if( e->e_nname.bv_val < e->e_bv.bv_val ||
999                 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1000         {
1001                 ch_free(e->e_name.bv_val);
1002                 ch_free(e->e_nname.bv_val);
1003         }
1004         e->e_name = new->e_name;
1005         e->e_nname = new->e_nname;
1006
1007         /* Lock the parent's kids AVL tree */
1008         pei = ei->bei_parent;
1009         bdb_cache_entryinfo_lock( pei );
1010         avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1011         free( ei->bei_nrdn.bv_val );
1012         ber_dupbv( &ei->bei_nrdn, nrdn );
1013 #ifdef BDB_HIER
1014         free( ei->bei_rdn.bv_val );
1015
1016         rdn = e->e_name;
1017         if ( nrdn->bv_len != e->e_nname.bv_len ) {
1018                 char *ptr = ber_bvchr(&rdn, ',');
1019                 assert( ptr != NULL );
1020                 rdn.bv_len = ptr - rdn.bv_val;
1021         }
1022         ber_dupbv( &ei->bei_rdn, &rdn );
1023 #endif
1024
1025         if (!ein) {
1026                 ein = ei->bei_parent;
1027         } else {
1028                 ei->bei_parent = ein;
1029                 bdb_cache_entryinfo_unlock( pei );
1030                 bdb_cache_entryinfo_lock( ein );
1031         }
1032 #ifdef BDB_HIER
1033         {
1034                 /* Record the generation number of this change */
1035                 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1036                 bdb->bi_modrdns++;
1037                 ei->bei_modrdns = bdb->bi_modrdns;
1038                 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1039         }
1040 #endif
1041         avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1042         bdb_cache_entryinfo_unlock( ein );
1043         return rc;
1044 }
1045 /*
1046  * cache_delete - delete the entry e from the cache. 
1047  *
1048  * returns:     0       e was deleted ok
1049  *              1       e was not in the cache
1050  *              -1      something bad happened
1051  */
1052 int
1053 bdb_cache_delete(
1054     Cache       *cache,
1055     Entry               *e,
1056     DB_ENV      *env,
1057     u_int32_t   locker,
1058     DB_LOCK     *lock )
1059 {
1060         EntryInfo *ei = BEI(e);
1061         int     rc;
1062
1063         assert( e->e_private != NULL );
1064
1065         /* Set this early, warn off any queriers */
1066         ei->bei_state |= CACHE_ENTRY_DELETED;
1067
1068         /* Lock the entry's info */
1069         bdb_cache_entryinfo_lock( ei );
1070
1071         /* Get write lock on the data */
1072         rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
1073         if ( rc ) {
1074                 /* couldn't lock, undo and give up */
1075                 ei->bei_state ^= CACHE_ENTRY_DELETED;
1076                 bdb_cache_entryinfo_unlock( ei );
1077                 return rc;
1078         }
1079
1080         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1081                 e->e_id, 0, 0 );
1082
1083         /* set lru mutex */
1084         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1085
1086         /* set cache write lock */
1087         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1088
1089         rc = bdb_cache_delete_internal( cache, e->e_private, 1 );
1090
1091         /* free cache write lock */
1092         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1093
1094         /* free lru mutex */
1095         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1096
1097         /* Leave entry info locked */
1098
1099         return( rc );
1100 }
1101
1102 void
1103 bdb_cache_delete_cleanup(
1104         Cache *cache,
1105         EntryInfo *ei )
1106 {
1107         if ( ei->bei_e ) {
1108                 ei->bei_e->e_private = NULL;
1109 #ifdef SLAP_ZONE_ALLOC
1110                 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1111 #else
1112                 bdb_entry_return( ei->bei_e );
1113 #endif
1114                 ei->bei_e = NULL;
1115         }
1116
1117         free( ei->bei_nrdn.bv_val );
1118         ei->bei_nrdn.bv_val = NULL;
1119 #ifdef BDB_HIER
1120         free( ei->bei_rdn.bv_val );
1121         ei->bei_rdn.bv_val = NULL;
1122         ei->bei_modrdns = 0;
1123         ei->bei_ckids = 0;
1124         ei->bei_dkids = 0;
1125 #endif
1126         ei->bei_parent = NULL;
1127         ei->bei_kids = NULL;
1128         ei->bei_lruprev = NULL;
1129
1130         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1131         ei->bei_lrunext = cache->c_eifree;
1132         cache->c_eifree = ei;
1133         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1134         bdb_cache_entryinfo_unlock( ei );
1135 }
1136
1137 static int
1138 bdb_cache_delete_internal(
1139     Cache       *cache,
1140     EntryInfo           *e,
1141     int         decr )
1142 {
1143         int rc = 0;     /* return code */
1144
1145         /* Lock the parent's kids tree */
1146         bdb_cache_entryinfo_lock( e->bei_parent );
1147
1148 #ifdef BDB_HIER
1149         e->bei_parent->bei_ckids--;
1150         if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1151 #endif
1152         /* dn tree */
1153         if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1154                 == NULL )
1155         {
1156                 rc = -1;
1157         }
1158         if ( e->bei_parent->bei_kids )
1159                 cache->c_leaves--;
1160
1161         /* id tree */
1162         if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) {
1163                 rc = -1;
1164         }
1165
1166         if ( rc == 0 ){
1167                 cache->c_eiused--;
1168
1169                 /* lru */
1170                 LRU_DELETE( cache, e );
1171                 if ( e->bei_e ) cache->c_cursize--;
1172         }
1173
1174         bdb_cache_entryinfo_unlock( e->bei_parent );
1175
1176         return( rc );
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_leaves = 0;
1216         cache->c_idtree = NULL;
1217         cache->c_lruhead = NULL;
1218         cache->c_lrutail = NULL;
1219         cache->c_dntree.bei_kids = NULL;
1220
1221         /* free lru mutex */
1222         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1223         /* free cache write lock */
1224         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1225 }
1226
1227 #ifdef LDAP_DEBUG
1228 #ifdef SLAPD_UNUSED
1229 static void
1230 bdb_lru_print( Cache *cache )
1231 {
1232         EntryInfo       *e;
1233
1234         fprintf( stderr, "LRU queue (head to tail):\n" );
1235         for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1236                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1237                         e->bei_nrdn.bv_val, e->bei_id );
1238         }
1239         fprintf( stderr, "LRU queue (tail to head):\n" );
1240         for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1241                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1242                         e->bei_nrdn.bv_val, e->bei_id );
1243         }
1244 }
1245 #endif
1246 #endif
1247
1248 #ifdef BDB_REUSE_LOCKERS
1249 static void
1250 bdb_locker_id_free( void *key, void *data )
1251 {
1252         DB_ENV *env = key;
1253         u_int32_t lockid = (long)data;
1254         int rc;
1255
1256         rc = XLOCK_ID_FREE( env, lockid );
1257         if ( rc == EINVAL ) {
1258                 DB_LOCKREQ lr;
1259                 Debug( LDAP_DEBUG_ANY,
1260                         "bdb_locker_id_free: %lu err %s(%d)\n",
1261                         (unsigned long) lockid, db_strerror(rc), rc );
1262                 /* release all locks held by this locker. */
1263                 lr.op = DB_LOCK_PUT_ALL;
1264                 lr.obj = NULL;
1265                 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1266                 XLOCK_ID_FREE( env, lockid );
1267         }
1268 }
1269
1270 int
1271 bdb_locker_id( Operation *op, DB_ENV *env, u_int32_t *locker )
1272 {
1273         int i, rc;
1274         u_int32_t lockid;
1275         void *data;
1276         void *ctx;
1277
1278         if ( !env || !locker ) return -1;
1279
1280         /* If no op was provided, try to find the ctx anyway... */
1281         if ( op ) {
1282                 ctx = op->o_threadctx;
1283         } else {
1284                 ctx = ldap_pvt_thread_pool_context();
1285         }
1286
1287         /* Shouldn't happen unless we're single-threaded */
1288         if ( !ctx ) {
1289                 *locker = 0;
1290                 return 0;
1291         }
1292
1293         if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1294                 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1295                         rc = XLOCK_ID( env, &lockid );
1296                         if (rc) ldap_pvt_thread_yield();
1297                 }
1298                 if ( rc != 0) {
1299                         return rc;
1300                 }
1301                 data = (void *)((long)lockid);
1302                 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1303                         data, bdb_locker_id_free ) ) ) {
1304                         XLOCK_ID_FREE( env, lockid );
1305                         Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1306                                 db_strerror(rc), rc, 0 );
1307
1308                         return rc;
1309                 }
1310         } else {
1311                 lockid = (long)data;
1312         }
1313         *locker = lockid;
1314         return 0;
1315 }
1316 #endif /* BDB_REUSE_LOCKERS */
1317
1318 void
1319 bdb_cache_delete_entry(
1320         struct bdb_info *bdb,
1321         EntryInfo *ei,
1322         u_int32_t locker,
1323         DB_LOCK *lock )
1324 {
1325         ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1326         if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, ei, 1, 1, lock ) == 0 )
1327         {
1328                 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1329                         LRU_DELETE( &bdb->bi_cache, ei );
1330                         ei->bei_e->e_private = NULL;
1331 #ifdef SLAP_ZONE_ALLOC
1332                         bdb_entry_return( bdb, ei->bei_e, ei->bei_zseq );
1333 #else
1334                         bdb_entry_return( ei->bei_e );
1335 #endif
1336                         ei->bei_e = NULL;
1337                         --bdb->bi_cache.c_cursize;
1338                 }
1339                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1340         }
1341         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1342 }