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