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