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