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