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