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