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