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