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