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