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