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