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