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