]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/cache.c
b4d0c9f2313b2f6190a91b6b49f7b66abad35883
[openldap] / servers / slapd / back-bdb / cache.c
1 /* cache.c - routines to maintain an in-core cache of entries */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/errno.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15
16 #include "slap.h"
17
18 #include "back-bdb.h"
19
20 static int      bdb_cache_delete_internal(Cache *cache, EntryInfo *e);
21 #ifdef LDAP_DEBUG
22 static void     bdb_lru_print(Cache *cache);
23 #endif
24
25 static EntryInfo *
26 bdb_cache_entryinfo_new( )
27 {
28         EntryInfo *ei;
29
30         ei = ch_calloc(1, sizeof(struct bdb_entry_info));
31         ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
32
33         return ei;
34 }
35
36 /* Atomically release and reacquire a lock */
37 static int
38 bdb_cache_entry_db_relock(
39         DB_ENV *env,
40         u_int32_t locker,
41         EntryInfo *ei,
42         int rw,
43         int tryOnly,
44         DB_LOCK *lock )
45 {
46 #ifdef NO_THREADS
47         return 0;
48 #else
49         int     rc;
50         DBT     lockobj;
51         DB_LOCKREQ list[2];
52
53         if ( !lock ) return 0;
54
55         lockobj.data = ei;
56         lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
57
58         list[0].op = DB_LOCK_PUT;
59         list[0].lock = *lock;
60         list[1].op = DB_LOCK_GET;
61         list[1].lock = *lock;
62         list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
63         list[1].obj = &lockobj;
64         rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
65                 list, 2, NULL );
66
67         if (rc) {
68 #ifdef NEW_LOGGING
69                 LDAP_LOG( CACHE, DETAIL1, 
70                         "bdb_cache_entry_db_relock: entry %d, rw %d, rc %d\n",
71                         ei->bei_id, rw, rc );
72 #else
73                 Debug( LDAP_DEBUG_TRACE,
74                         "bdb_cache_entry_db_relock: entry %d, rw %d, rc %d\n",
75                         ei->bei_id, rw, rc );
76 #endif
77         } else {
78                 *lock = list[1].lock;
79         }
80         return rc;
81 #endif
82 }
83 static int
84 bdb_cache_entry_db_lock
85 ( DB_ENV *env, u_int32_t locker, EntryInfo *ei, int rw, int tryOnly, DB_LOCK *lock )
86 {
87 #ifdef NO_THREADS
88         return 0;
89 #else
90         int       rc;
91         DBT       lockobj;
92         int       db_rw;
93
94         if ( !lock ) return 0;
95
96         if (rw)
97                 db_rw = DB_LOCK_WRITE;
98         else
99                 db_rw = DB_LOCK_READ;
100
101         lockobj.data = ei;
102         lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
103
104         rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
105                                         &lockobj, db_rw, lock);
106         if (rc) {
107 #ifdef NEW_LOGGING
108                 LDAP_LOG( CACHE, DETAIL1, 
109                         "bdb_cache_entry_db_lock: entry %d, rw %d, rc %d\n",
110                         ei->bei_id, rw, rc );
111 #else
112                 Debug( LDAP_DEBUG_TRACE,
113                         "bdb_cache_entry_db_lock: entry %d, rw %d, rc %d\n",
114                         ei->bei_id, rw, rc );
115 #endif
116         }
117         return rc;
118 #endif /* NO_THREADS */
119 }
120
121 int
122 bdb_cache_entry_db_unlock
123 ( DB_ENV *env, DB_LOCK *lock )
124 {
125 #ifdef NO_THREADS
126         return 0;
127 #else
128         int rc;
129
130         rc = LOCK_PUT ( env, lock );
131         return rc;
132 #endif
133 }
134
135 static int
136 bdb_cache_entryinfo_destroy( EntryInfo *e )
137 {
138         ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
139         free( e->bei_nrdn.bv_val );
140 #ifdef BDB_HIER
141         free( e->bei_rdn.bv_val );
142 #endif
143         free( e );
144         return 0;
145 }
146
147 #define LRU_DELETE( cache, ei ) do { \
148         if ( (ei)->bei_lruprev != NULL ) { \
149                 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
150         } else { \
151                 (cache)->c_lruhead = (ei)->bei_lrunext; \
152         } \
153         if ( (ei)->bei_lrunext != NULL ) { \
154                 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
155         } else { \
156                 (cache)->c_lrutail = (ei)->bei_lruprev; \
157         } \
158 } while(0)
159
160 #define LRU_ADD( cache, ei ) do { \
161         (ei)->bei_lrunext = (cache)->c_lruhead; \
162         if ( (ei)->bei_lrunext != NULL ) { \
163                 (ei)->bei_lrunext->bei_lruprev = (ei); \
164         } \
165         (cache)->c_lruhead = (ei); \
166         (ei)->bei_lruprev = NULL; \
167         if ( (cache)->c_lrutail == NULL ) { \
168                 (cache)->c_lrutail = (ei); \
169         } \
170 } while(0)
171
172 /* Do a lexical sort on normalized RDNs */
173 static int
174 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
175 {
176         const EntryInfo *e1 = v_e1, *e2 = v_e2;
177         int rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val, e1->bei_nrdn.bv_len );
178         if (rc == 0) rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
179         return rc;
180 }
181
182 static int
183 bdb_id_cmp( const void *v_e1, const void *v_e2 )
184 {
185         const EntryInfo *e1 = v_e1, *e2 = v_e2;
186         return e1->bei_id - e2->bei_id;
187 }
188
189 /* Create an entryinfo in the cache. Caller must release the locks later.
190  */
191 static int
192 bdb_entryinfo_add_internal(
193         struct bdb_info *bdb,
194         EntryInfo *ei,
195         EntryInfo **res,
196         u_int32_t locker
197 )
198 {
199         Cache *cache = &bdb->bi_cache;
200         DB_ENV *env = bdb->bi_dbenv;
201         EntryInfo *ei2 = NULL;
202         int incr = 1;
203         int addkid = 1;
204         int rc;
205         DB_LOCK lock;
206
207         *res = NULL;
208
209         ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
210         bdb_cache_entryinfo_lock( ei->bei_parent );
211
212         /* if parent was previously considered a leaf node,
213          * it was on the LRU list. Now it's going to have
214          * kids, take it off the LRU list.
215          */
216         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
217         if ( ei->bei_parent->bei_id && !ei->bei_parent->bei_kids ) {
218                 LRU_DELETE( cache, ei->bei_parent );
219                 incr = 0;
220         }
221
222         cache->c_cursize += incr;
223
224         /* See if we're above the cache size limit */
225         if ( cache->c_cursize > cache->c_maxsize ) {
226                 EntryInfo *elru, *elprev;
227                 int i = 0;
228
229                 /* Look for an unused entry to remove */
230                 for (elru = cache->c_lrutail; elru; elru = elprev, i++ ) {
231                         elprev = elru->bei_lruprev;
232
233                         /* Too many probes, not enough idle, give up */
234                         if (i > 10) break;
235
236                         /* If we can successfully writelock it, then
237                          * the object is idle.
238                          */
239                         if ( bdb_cache_entry_db_lock( env, locker, elru, 1, 1,
240                                 &lock ) == 0 ) {
241                                 /* Need to lock parent to delete child */
242                                 if ( ldap_pvt_thread_mutex_trylock(
243                                         &elru->bei_parent->bei_kids_mutex )) {
244                                         bdb_cache_entry_db_unlock( env, &lock );
245                                         continue;
246                                 }
247                                 bdb_cache_delete_internal( cache, elru );
248                                 bdb_cache_entryinfo_unlock( elru->bei_parent );
249                                 elru->bei_e->e_private = NULL;
250                                 bdb_entry_return( elru->bei_e );
251                                 bdb_cache_entry_db_unlock( env, &lock );
252                                 if (ei2) {
253                                         bdb_cache_entryinfo_destroy( elru );
254                                 } else {
255                                         /* re-use this one */
256                                         ch_free(elru->bei_nrdn.bv_val);
257                                         elru->bei_nrdn.bv_val = NULL;
258                                         elru->bei_e = NULL;
259                                         elru->bei_kids = NULL;
260                                         elru->bei_lrunext = NULL;
261                                         elru->bei_lruprev = NULL;
262                                         elru->bei_state = 0;
263 #ifdef BDB_HIER
264                                         elru->bei_modrdns = 0;
265 #endif
266                                         ei2 = elru;
267                                 }
268                                 if (cache->c_cursize < cache->c_maxsize)
269                                         break;
270                         }
271                 }
272         }
273         if (!ei2) {
274                 ei2 = bdb_cache_entryinfo_new();
275         }
276         ei2->bei_id = ei->bei_id;
277         ei2->bei_parent = ei->bei_parent;
278 #ifdef BDB_HIER
279         ei2->bei_rdn = ei->bei_rdn;
280 #endif
281
282         /* Add to cache ID tree */
283         if (avl_insert( &cache->c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
284                 EntryInfo *eix;
285                 eix = avl_find( cache->c_idtree, ei2, bdb_id_cmp );
286                 bdb_cache_entryinfo_destroy( ei2 );
287                 ei2 = eix;
288                 addkid = 0;
289                 cache->c_cursize -= incr;
290 #ifdef BDB_HIER
291                 if ( ei->bei_rdn.bv_val )
292                         ber_memfree_x( ei->bei_rdn.bv_val, NULL );
293 #endif
294         } else {
295                 LRU_ADD( cache, ei2 );
296                 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
297         }
298
299         if ( addkid ) {
300                 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
301                         avl_dup_error );
302         }
303
304         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
305
306         *res = ei2;
307         return 0;
308 }
309
310 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
311  * the info for its closest ancestor. *res should be NULL to process a
312  * complete DN starting from the tree root. Otherwise *res must be the
313  * immediate parent of the requested DN, and only the RDN will be searched.
314  * The EntryInfo is locked upon return and must be unlocked by the caller.
315  */
316 int
317 bdb_cache_find_ndn(
318         Backend         *be,
319         DB_TXN          *txn,
320         struct berval   *ndn,
321         EntryInfo       **res,
322         u_int32_t       locker,
323         void            *ctx
324 )
325 {
326         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
327         EntryInfo       ei, *eip, *ei2;
328         int rc = 0;
329         char *ptr;
330
331         /* this function is always called with normalized DN */
332         if ( *res ) {
333                 /* we're doing a onelevel search for an RDN */
334                 ei.bei_nrdn.bv_val = ndn->bv_val;
335                 ei.bei_nrdn.bv_len = dn_rdnlen( be, ndn );
336                 eip = *res;
337         } else {
338                 /* we're searching a full DN from the root */
339                 ptr = ndn->bv_val + ndn->bv_len - be->be_nsuffix[0].bv_len;
340                 ei.bei_nrdn.bv_val = ptr;
341                 ei.bei_nrdn.bv_len = be->be_nsuffix[0].bv_len;
342                 eip = &bdb->bi_cache.c_dntree;
343         }
344         
345         for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
346                 ei.bei_parent = eip;
347                 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
348                 if ( !ei2 ) {
349                         int len = ei.bei_nrdn.bv_len;
350                                 
351                         ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
352                         bdb_cache_entryinfo_unlock( eip );
353
354                         rc = bdb_dn2id( be, txn, &ei.bei_nrdn, &ei, ctx );
355                         if (rc) {
356                                 bdb_cache_entryinfo_lock( eip );
357                                 *res = eip;
358                                 return rc;
359                         }
360
361                         /* DN exists but needs to be added to cache */
362                         ei.bei_nrdn.bv_len = len;
363                         rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2,
364                                 locker );
365                         /* add_internal left eip and c_rwlock locked */
366                         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
367                         if ( rc ) {
368                                 *res = eip;
369                                 return rc;
370                         }
371                 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
372                         /* In the midst of deleting? Give it a chance to
373                          * complete.
374                          */
375                         bdb_cache_entryinfo_unlock( eip );
376                         ldap_pvt_thread_yield();
377                         bdb_cache_entryinfo_lock( eip );
378                         *res = eip;
379                         return DB_NOTFOUND;
380                 }
381                 bdb_cache_entryinfo_unlock( eip );
382                 bdb_cache_entryinfo_lock( ei2 );
383
384                 eip = ei2;
385
386                 /* Advance to next lower RDN */
387                 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
388                         && !DN_SEPARATOR(*ptr); ptr--);
389                 if ( ptr >= ndn->bv_val ) {
390                         if (DN_SEPARATOR(*ptr)) ptr++;
391                         ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
392                         ei.bei_nrdn.bv_val = ptr;
393                 }
394                 if ( ptr < ndn->bv_val ) {
395                         *res = eip;
396                         break;
397                 }
398         }
399
400         return rc;
401 }
402
403 #ifdef BDB_HIER
404 /* Walk up the tree from a child node, looking for an ID that's already
405  * been linked into the cache.
406  */
407 static int
408 bdb_cache_find_parent(
409         Backend *be,
410         DB_TXN *txn,
411         ID id,
412         EntryInfo **res,
413         void *ctx
414 )
415 {
416         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
417         EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
418         ID parent;
419         int rc;
420
421         ei.bei_id = id;
422         ei.bei_kids = NULL;
423
424         for (;;) {
425                 rc = bdb_dn2id_parent( be, txn, &ei, &eip.bei_id, ctx );
426                 if ( rc ) break;
427
428                 /* Save the previous node, if any */
429                 ei2 = ein;
430
431                 /* Create a new node for the current ID */
432                 ein = bdb_cache_entryinfo_new();
433                 ein->bei_id = ei.bei_id;
434                 ein->bei_kids = ei.bei_kids;
435                 ein->bei_nrdn = ei.bei_nrdn;
436                 ein->bei_rdn = ei.bei_rdn;
437                 
438                 /* This node is not fully connected yet */
439                 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
440
441                 /* If this is the first time, save this node
442                  * to be returned later.
443                  */
444                 if ( eir == NULL ) eir = ein;
445
446                 /* Insert this node into the ID tree */
447                 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
448                 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
449                         bdb_id_cmp, avl_dup_error ) ) {
450
451                         /* Hm, can this really happen? */
452                         bdb_cache_entryinfo_destroy( ein );
453                         ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
454                                 (caddr_t) &ei, bdb_id_cmp );
455                         bdb_cache_entryinfo_lock( ein );
456                         avl_insert( &ein->bei_kids, (caddr_t)ei2, bdb_rdn_cmp,
457                                 avl_dup_error );
458                         bdb_cache_entryinfo_unlock( ein );
459                 }
460
461                 /* If there was a previous node, link it to this one */
462                 if ( ei2 ) ei2->bei_parent = ein;
463
464                 if ( eip.bei_id ) {
465                         ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
466                                         (caddr_t) &eip, bdb_id_cmp );
467                 } else {
468                         ei2 = &bdb->bi_cache.c_dntree;
469                 }
470
471                 if ( ei2 ) {
472                         ein->bei_parent = ei2;
473                         bdb_cache_entryinfo_lock( ei2 );
474                         avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
475                                 avl_dup_error);
476                         bdb_cache_entryinfo_unlock( ei2 );
477                         *res = eir;
478                         bdb_cache_entryinfo_lock( eir );
479                 }
480                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
481                 if ( ei2 ) {
482                         /* Found a link. Reset all the state info */
483                         for (ein = eir; ein != ei2; ein=ein->bei_parent)
484                                 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
485                         break;
486                 }
487                 ei.bei_kids = NULL;
488                 ei.bei_id = eip.bei_id;
489                 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
490                         avl_dup_error );
491         }
492         return rc;
493 }
494 #endif
495
496 /*
497  * cache_find_id - find an entry in the cache, given id.
498  * The entry is locked for Read upon return. Call with islocked TRUE if
499  * the supplied *eip was already locked.
500  */
501
502 int
503 bdb_cache_find_id(
504         Backend *be,
505         DB_TXN  *tid,
506         ID                              id,
507         EntryInfo       **eip,
508         int             islocked,
509         u_int32_t       locker,
510         DB_LOCK         *lock,
511         void            *ctx
512 )
513 {
514         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
515         Entry   *ep = NULL;
516         int     rc = 0;
517         EntryInfo ei;
518
519         ei.bei_id = id;
520
521         /* If we weren't given any info, see if we have it already cached */
522         if ( !*eip ) {
523                 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
524                 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
525                                         (caddr_t) &ei, bdb_id_cmp );
526                 if ( *eip ) {
527                         bdb_cache_entryinfo_lock( *eip );
528                         islocked = 1;
529                 }
530                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
531         }
532
533         /* See if the ID exists in the database; add it to the cache if so */
534         if ( !*eip ) {
535 #ifndef BDB_HIER
536                 rc = bdb_id2entry( be, tid, id, &ep );
537                 if ( rc == 0 ) {
538                         rc = bdb_cache_find_ndn( be, tid,
539                                 &ep->e_nname, eip, locker, ctx );
540                         if ( *eip )
541                                 islocked = 1;
542                         if ( rc ) {
543                                 bdb_entry_return( ep );
544                                 ep = NULL;
545                         }
546                 }
547 #else
548                 rc = bdb_cache_find_parent(be, tid, id, eip, ctx );
549                 if ( rc == 0 && *eip )
550                         islocked = 1;
551 #endif
552         }
553
554         /* Ok, we found the info, do we have the entry? */
555         if ( *eip && rc == 0 ) {
556                 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
557                         rc = DB_NOTFOUND;
558                 } else if (!(*eip)->bei_e ) {
559                         if (!ep) {
560                                 rc = bdb_id2entry( be, tid, id, &ep );
561                         }
562                         if ( rc == 0 ) {
563                                 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
564                                         *eip, 1, 0, lock );
565                                 ep->e_private = *eip;
566 #ifdef BDB_HIER
567                                 bdb_fix_dn( ep, 0 );
568 #endif
569                                 (*eip)->bei_e = ep;
570                                 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
571                                         *eip, 0, 0, lock );
572                         }
573                 } else {
574 #ifdef BDB_HIER
575                         rc = bdb_fix_dn( (*eip)->bei_e, 1 );
576                         if ( rc ) {
577                                 bdb_cache_entry_db_lock( bdb->bi_dbenv,
578                                         locker, *eip, 1, 0, lock );
579                                 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
580                                 bdb_cache_entry_db_relock( bdb->bi_dbenv,
581                                         locker, *eip, 0, 0, lock );
582                         } else {
583                                 bdb_cache_entry_db_lock( bdb->bi_dbenv,
584                                         locker, *eip, 0, 0, lock );
585                         }
586 #else
587                         bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
588                                         *eip, 0, 0, lock );
589 #endif
590                 }
591         }
592         if ( rc == 0 && (*eip)->bei_kids == NULL ) {
593                 /* set lru mutex */
594                 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
595                 LRU_DELETE( &bdb->bi_cache, *eip );
596                 LRU_ADD( &bdb->bi_cache, *eip );
597                 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
598         }
599
600         if ( islocked ) {
601                 bdb_cache_entryinfo_unlock( *eip );
602         }
603         return rc;
604 }
605
606 int
607 bdb_cache_children(
608         Operation *op,
609         DB_TXN *txn,
610         Entry *e
611 )
612 {
613         int rc;
614
615         if ( BEI(e)->bei_kids ) {
616                 return 0;
617         }
618         if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
619                 return DB_NOTFOUND;
620         }
621         rc = bdb_dn2id_children( op, txn, e );
622         if ( rc == DB_NOTFOUND ) {
623                 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
624         }
625         return rc;
626 }
627
628 /* Update the cache after a successful database Add. */
629 int
630 bdb_cache_add(
631         struct bdb_info *bdb,
632         EntryInfo *eip,
633         Entry *e,
634         struct berval *nrdn,
635         u_int32_t locker
636 )
637 {
638         EntryInfo *new, ei;
639         struct berval rdn = e->e_name;
640         int rc;
641
642         ei.bei_id = e->e_id;
643         ei.bei_parent = eip;
644         ei.bei_nrdn = *nrdn;
645 #ifdef BDB_HIER
646         if ( nrdn->bv_len != e->e_nname.bv_len ) {
647                 char *ptr = strchr( rdn.bv_val, ',' );
648                 rdn.bv_len = ptr - rdn.bv_val;
649         }
650         ber_dupbv( &ei.bei_rdn, &rdn );
651 #endif
652         rc = bdb_entryinfo_add_internal( bdb, &ei, &new, locker );
653         new->bei_e = e;
654         e->e_private = new;
655         new->bei_state = CACHE_ENTRY_NO_KIDS;
656         eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
657         bdb_cache_entryinfo_unlock( eip );
658         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
659         return rc;
660 }
661
662 int
663 bdb_cache_modify(
664         Entry *e,
665         Attribute *newAttrs,
666         DB_ENV *env,
667         u_int32_t locker,
668         DB_LOCK *lock
669 )
670 {
671         EntryInfo *ei = BEI(e);
672         
673         /* Get write lock on data */
674         bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
675
676         /* If we've done repeated mods on a cached entry, then e_attrs
677          * is no longer contiguous with the entry, and must be freed.
678          */
679         if ( (void *)e->e_attrs != (void *)(e+1) ) {
680                 attrs_free( e->e_attrs );
681         }
682         e->e_attrs = newAttrs;
683
684         return 0;
685 }
686
687 /*
688  * Change the rdn in the entryinfo. Also move to a new parent if needed.
689  */
690 int
691 bdb_cache_modrdn(
692         Entry *e,
693         struct berval *nrdn,
694         Entry *new,
695         EntryInfo *ein,
696         DB_ENV *env,
697         u_int32_t locker,
698         DB_LOCK *lock
699 )
700 {
701         EntryInfo *ei = BEI(e), *pei;
702         struct berval rdn;
703         int rc = 0;
704
705         /* Get write lock on data */
706         bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
707
708         /* If we've done repeated mods on a cached entry, then e_attrs
709          * is no longer contiguous with the entry, and must be freed.
710          */
711         if ( (void *)e->e_attrs != (void *)(e+1) ) {
712                 attrs_free( e->e_attrs );
713         }
714         e->e_attrs = new->e_attrs;
715 #ifdef BDB_HIER
716         ch_free(e->e_name.bv_val);
717 #else
718         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
719                 e->e_bv.bv_val + e->e_bv.bv_len ) {
720                 ch_free(e->e_name.bv_val);
721                 ch_free(e->e_nname.bv_val);
722         }
723 #endif
724         e->e_name = new->e_name;
725         e->e_nname = new->e_nname;
726
727         /* Lock the parent's kids AVL tree */
728         pei = ei->bei_parent;
729         bdb_cache_entryinfo_lock( pei );
730         avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
731         free( ei->bei_nrdn.bv_val );
732         ber_dupbv( &ei->bei_nrdn, nrdn );
733 #ifdef BDB_HIER
734         free( ei->bei_rdn.bv_val );
735
736         rdn = e->e_name;
737         if ( nrdn->bv_len != e->e_nname.bv_len ) {
738                 char *ptr = strchr(rdn.bv_val, ',');
739                 rdn.bv_len = ptr - rdn.bv_val;
740         }
741         ber_dupbv( &ei->bei_rdn, &rdn );
742 #endif
743
744         if (!ein) {
745                 ein = ei->bei_parent;
746         } else {
747                 ei->bei_parent = ein;
748                 bdb_cache_entryinfo_unlock( pei );
749                 bdb_cache_entryinfo_lock( ein );
750         }
751 #ifdef BDB_HIER
752         { int max = ei->bei_modrdns;
753         /* Record the generation number of this change */
754                 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
755                         if ( pei->bei_modrdns > max )
756                                 max = pei->bei_modrdns;
757                 }
758                 ei->bei_modrdns = max + 1;
759         }
760 #endif
761         avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
762         bdb_cache_entryinfo_unlock( ein );
763         return rc;
764 }
765 /*
766  * cache_delete - delete the entry e from the cache. 
767  *
768  * returns:     0       e was deleted ok
769  *              1       e was not in the cache
770  *              -1      something bad happened
771  */
772 int
773 bdb_cache_delete(
774     Cache       *cache,
775     Entry               *e,
776     DB_ENV      *env,
777     u_int32_t   locker,
778     DB_LOCK     *lock
779 )
780 {
781         EntryInfo *ei = BEI(e);
782         int     rc;
783
784         assert( e->e_private );
785
786         /* Set this early, warn off any queriers */
787         ei->bei_state |= CACHE_ENTRY_DELETED;
788
789         /* Get write lock on the data */
790         bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
791
792         /* set cache write lock */
793         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
794
795         /* Lock the parent's kids tree */
796         bdb_cache_entryinfo_lock( ei->bei_parent );
797
798 #ifdef NEW_LOGGING
799         LDAP_LOG( CACHE, ENTRY, 
800                 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
801 #else
802         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
803                 e->e_id, 0, 0 );
804 #endif
805
806         /* set lru mutex */
807         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
808         rc = bdb_cache_delete_internal( cache, e->e_private );
809         /* free lru mutex */
810         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
811
812         /* free cache write lock */
813         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
814         bdb_cache_entryinfo_unlock( ei->bei_parent );
815         bdb_cache_entryinfo_destroy( ei );
816         e->e_private = NULL;
817         return( rc );
818 }
819
820 static int
821 bdb_cache_delete_internal(
822     Cache       *cache,
823     EntryInfo           *e
824 )
825 {
826         int rc = 0;     /* return code */
827
828         /* dn tree */
829         if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
830         {
831                 rc = -1;
832         }
833
834         /* If parent has no more kids, put in on LRU list */
835         if ( e->bei_parent->bei_kids == NULL ) {
836                 LRU_ADD( cache, e->bei_parent );
837                 cache->c_cursize++;
838         }
839
840         /* id tree */
841         if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
842         {
843                 rc = -1;
844         }
845
846         if (rc != 0) {
847                 return rc;
848         }
849
850         /* lru */
851         LRU_DELETE( cache, e );
852         cache->c_cursize--;
853
854         /*
855          * flag entry to be freed later by a call to cache_return_entry()
856          */
857         e->bei_state |= CACHE_ENTRY_DELETED;
858
859         return( 0 );
860 }
861
862 static void
863 bdb_entryinfo_release( void *data )
864 {
865         EntryInfo *ei = (EntryInfo *)data;
866         if ( ei->bei_kids ) {
867                 avl_free( ei->bei_kids, NULL );
868         }
869         if ( ei->bei_e ) {
870                 ei->bei_e->e_private = NULL;
871                 bdb_entry_return( ei->bei_e );
872         }
873         bdb_cache_entryinfo_destroy( ei );
874 }
875
876 void
877 bdb_cache_release_all( Cache *cache )
878 {
879         /* set cache write lock */
880         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
881         /* set lru mutex */
882         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
883
884 #ifdef NEW_LOGGING
885         LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
886 #else
887         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
888 #endif
889
890         avl_free( cache->c_dntree.bei_kids, NULL );
891         avl_free( cache->c_idtree, bdb_entryinfo_release );
892         cache->c_lruhead = NULL;
893         cache->c_lrutail = NULL;
894
895         /* free lru mutex */
896         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
897         /* free cache write lock */
898         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
899 }
900
901 #ifdef LDAP_DEBUG
902 static void
903 bdb_lru_print( Cache *cache )
904 {
905         EntryInfo       *e;
906
907         fprintf( stderr, "LRU queue (head to tail):\n" );
908         for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
909                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
910                         e->bei_nrdn.bv_val, e->bei_id );
911         }
912         fprintf( stderr, "LRU queue (tail to head):\n" );
913         for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
914                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
915                         e->bei_nrdn.bv_val, e->bei_id );
916         }
917 }
918 #endif
919
920 #ifdef BDB_REUSE_LOCKERS
921 static void
922 bdb_locker_id_free( void *key, void *data )
923 {
924         DB_ENV *env = key;
925         int lockid = (int) data;
926
927         XLOCK_ID_FREE( env, lockid );
928 }
929
930 int
931 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
932 {
933         int i, rc, lockid;
934         void *data;
935         void *ctx;
936
937         if ( !env || !locker ) return -1;
938
939         /* If no op was provided, try to find the ctx anyway... */
940         if ( op ) {
941                 ctx = op->o_threadctx;
942         } else {
943                 ctx = ldap_pvt_thread_pool_context();
944         }
945
946         /* Shouldn't happen unless we're single-threaded */
947         if ( !ctx ) {
948                 *locker = 0;
949                 return 0;
950         }
951
952         if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
953                 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
954                         rc = XLOCK_ID( env, &lockid );
955                         if (rc) ldap_pvt_thread_yield();
956                 }
957                 if ( rc != 0) {
958                         return rc;
959                 }
960                 data = (void *)lockid;
961                 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
962                         data, bdb_locker_id_free ) ) ) {
963                         XLOCK_ID_FREE( env, lockid );
964 #ifdef NEW_LOGGING
965                         LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
966                                 db_strerror(rc), rc, 0 );
967 #else
968                         Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
969                                 db_strerror(rc), rc, 0 );
970 #endif
971
972                         return rc;
973                 }
974         } else {
975                 lockid = (int)data;
976         }
977         *locker = lockid;
978         return 0;
979 }
980 #endif