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