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