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