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