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