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