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