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