]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/cache.c
85d4d052d4e1654100987793919095b3e0fe6bd7
[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->bei_id;
78         lockobj.size = sizeof(ei->bei_id) + 1;
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->bei_id;
125         lockobj.size = sizeof(ei->bei_id) + 1;
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, bdb->bi_cache.c_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                         rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, *eip, 0, 0, lock );
659                         /* entry is protected now, we don't need to hold the entryinfo */
660                         if ( islocked ) {
661                                 bdb_cache_entryinfo_unlock( *eip );
662                                 islocked = 0;
663                         }
664                         if ( rc == 0 ) {
665                                 if ( !(*eip)->bei_e ) {
666                                         if (!ep) {
667                                                 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
668                                         }
669                                         if ( rc == 0 ) {
670                                                 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
671                                                         *eip, 1, 0, lock );
672                                                 /* Make sure no other modifier beat us to it */
673                                                 if ( (*eip)->bei_e ) {
674                                                         bdb_entry_return( ep );
675                                                         ep = NULL;
676                                                 } else {
677                                                         ep->e_private = *eip;
678 #ifdef BDB_HIER
679                                                         bdb_fix_dn( ep, 0 );
680 #endif
681                                                         (*eip)->bei_e = ep;
682                                                 }
683                                                 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
684                                                         *eip, 0, 0, lock );
685                                         }
686                                 } else {
687                                         /* If we had the entry already, this item
688                                          * is on the LRU list.
689                                          */
690                                         lru_del = 1;
691 #ifdef BDB_HIER
692                                         rc = bdb_fix_dn( (*eip)->bei_e, 1 );
693                                         if ( rc ) {
694                                                 bdb_cache_entry_db_relock( bdb->bi_dbenv,
695                                                         locker, *eip, 1, 0, lock );
696                                                 /* check again in case other modifier did it already */
697                                                 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
698                                                         rc = bdb_fix_dn( (*eip)->bei_e, 2 );
699                                                 bdb_cache_entry_db_relock( bdb->bi_dbenv,
700                                                         locker, *eip, 0, 0, lock );
701                                         }
702 #endif
703                                 }
704
705                         }
706                 }
707         }
708         if ( rc == 0 ) {
709                 /* set lru mutex */
710                 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
711                 /* if entry is old, remove from old spot on LRU list */
712                 if ( lru_del ) {
713                         LRU_DELETE( &bdb->bi_cache, *eip );
714                 } else {
715                 /* if entry is new, bump cache size */
716                         bdb->bi_cache.c_cursize++;
717                 }
718                 /* lru_mutex is unlocked for us */
719                 bdb_cache_lru_add( bdb, locker, *eip );
720         }
721
722         if ( islocked ) {
723                 bdb_cache_entryinfo_unlock( *eip );
724         }
725         return rc;
726 }
727
728 int
729 bdb_cache_children(
730         Operation *op,
731         DB_TXN *txn,
732         Entry *e )
733 {
734         int rc;
735
736         if ( BEI(e)->bei_kids ) {
737                 return 0;
738         }
739         if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
740                 return DB_NOTFOUND;
741         }
742         rc = bdb_dn2id_children( op, txn, e );
743         if ( rc == DB_NOTFOUND ) {
744                 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
745         }
746         return rc;
747 }
748
749 /* Update the cache after a successful database Add. */
750 int
751 bdb_cache_add(
752         struct bdb_info *bdb,
753         EntryInfo *eip,
754         Entry *e,
755         struct berval *nrdn,
756         u_int32_t locker )
757 {
758         EntryInfo *new, ei;
759         struct berval rdn = e->e_name;
760         DB_LOCK lock;
761         int rc;
762
763         ei.bei_id = e->e_id;
764         ei.bei_parent = eip;
765         ei.bei_nrdn = *nrdn;
766         ei.bei_lockpad = 0;
767
768         /* Lock this entry so that bdb_add can run to completion.
769          * It can only fail if BDB has run out of lock resources.
770          */
771         rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, &ei, 1, 0, &lock );
772         if ( rc ) {
773                 bdb_cache_entryinfo_unlock( eip );
774                 return rc;
775         }
776
777 #ifdef BDB_HIER
778         if ( nrdn->bv_len != e->e_nname.bv_len ) {
779                 char *ptr = strchr( rdn.bv_val, ',' );
780                 rdn.bv_len = ptr - rdn.bv_val;
781         }
782         ber_dupbv( &ei.bei_rdn, &rdn );
783         if ( eip->bei_dkids ) eip->bei_dkids++;
784 #endif
785
786         rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
787         /* bdb_csn_commit can cause this when adding the database root entry */
788         if ( new->bei_e ) bdb_entry_return( new->bei_e );
789         new->bei_e = e;
790         e->e_private = new;
791         new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
792         eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
793         if (eip->bei_parent) {
794                 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
795         }
796
797         /* set lru mutex */
798         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
799         ++bdb->bi_cache.c_cursize;
800         /* lru_mutex is unlocked for us */
801         bdb_cache_lru_add( bdb, locker, new );
802
803         bdb_cache_entryinfo_unlock( eip );
804         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
805         return rc;
806 }
807
808 int
809 bdb_cache_modify(
810         Entry *e,
811         Attribute *newAttrs,
812         DB_ENV *env,
813         u_int32_t locker,
814         DB_LOCK *lock )
815 {
816         EntryInfo *ei = BEI(e);
817         int rc;
818         /* Get write lock on data */
819         rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
820
821         /* If we've done repeated mods on a cached entry, then e_attrs
822          * is no longer contiguous with the entry, and must be freed.
823          */
824         if ( ! rc ) {
825                 if ( (void *)e->e_attrs != (void *)(e+1) ) {
826                         attrs_free( e->e_attrs ); 
827                 }
828                 e->e_attrs = newAttrs;
829         }
830         return rc;
831 }
832
833 /*
834  * Change the rdn in the entryinfo. Also move to a new parent if needed.
835  */
836 int
837 bdb_cache_modrdn(
838         Entry *e,
839         struct berval *nrdn,
840         Entry *new,
841         EntryInfo *ein,
842         DB_ENV *env,
843         u_int32_t locker,
844         DB_LOCK *lock )
845 {
846         EntryInfo *ei = BEI(e), *pei;
847         struct berval rdn;
848         int rc;
849
850         /* Get write lock on data */
851         rc =  bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
852         if ( rc ) return rc;
853
854         /* If we've done repeated mods on a cached entry, then e_attrs
855          * is no longer contiguous with the entry, and must be freed.
856          */
857         if ( (void *)e->e_attrs != (void *)(e+1) ) {
858                 attrs_free( e->e_attrs );
859         }
860         e->e_attrs = new->e_attrs;
861         if( e->e_nname.bv_val < e->e_bv.bv_val ||
862                 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
863         {
864                 ch_free(e->e_name.bv_val);
865                 ch_free(e->e_nname.bv_val);
866         }
867         e->e_name = new->e_name;
868         e->e_nname = new->e_nname;
869
870         /* Lock the parent's kids AVL tree */
871         pei = ei->bei_parent;
872         bdb_cache_entryinfo_lock( pei );
873         avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
874         free( ei->bei_nrdn.bv_val );
875         ber_dupbv( &ei->bei_nrdn, nrdn );
876 #ifdef BDB_HIER
877         free( ei->bei_rdn.bv_val );
878
879         rdn = e->e_name;
880         if ( nrdn->bv_len != e->e_nname.bv_len ) {
881                 char *ptr = strchr(rdn.bv_val, ',');
882                 rdn.bv_len = ptr - rdn.bv_val;
883         }
884         ber_dupbv( &ei->bei_rdn, &rdn );
885 #endif
886
887         if (!ein) {
888                 ein = ei->bei_parent;
889         } else {
890                 ei->bei_parent = ein;
891                 bdb_cache_entryinfo_unlock( pei );
892                 bdb_cache_entryinfo_lock( ein );
893         }
894 #ifdef BDB_HIER
895         {
896                 int max = ei->bei_modrdns;
897                 /* Record the generation number of this change */
898                 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
899                         if ( pei->bei_modrdns > max ) max = pei->bei_modrdns;
900                 }
901                 ei->bei_modrdns = max + 1;
902         }
903 #endif
904         avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
905         bdb_cache_entryinfo_unlock( ein );
906         return rc;
907 }
908 /*
909  * cache_delete - delete the entry e from the cache. 
910  *
911  * returns:     0       e was deleted ok
912  *              1       e was not in the cache
913  *              -1      something bad happened
914  */
915 int
916 bdb_cache_delete(
917     Cache       *cache,
918     Entry               *e,
919     DB_ENV      *env,
920     u_int32_t   locker,
921     DB_LOCK     *lock )
922 {
923         EntryInfo *ei = BEI(e);
924         int     rc;
925
926         assert( e->e_private );
927
928         /* Set this early, warn off any queriers */
929         ei->bei_state |= CACHE_ENTRY_DELETED;
930
931         /* Lock the entry's info */
932         bdb_cache_entryinfo_lock( ei );
933
934         /* Get write lock on the data */
935         rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
936         if ( rc ) {
937                 /* couldn't lock, undo and give up */
938                 ei->bei_state ^= CACHE_ENTRY_DELETED;
939                 bdb_cache_entryinfo_unlock( ei );
940                 return rc;
941         }
942
943         /* set cache write lock */
944         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
945
946         /* Lock the parent's kids tree */
947         bdb_cache_entryinfo_lock( ei->bei_parent );
948
949 #ifdef NEW_LOGGING
950         LDAP_LOG( CACHE, ENTRY, 
951                 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
952 #else
953         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
954                 e->e_id, 0, 0 );
955 #endif
956
957         /* set lru mutex */
958         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
959         rc = bdb_cache_delete_internal( cache, e->e_private );
960         /* free lru mutex */
961         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
962
963         /* free cache write lock */
964         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
965         bdb_cache_entryinfo_unlock( ei->bei_parent );
966
967         /* Leave entry info locked */
968
969         return( rc );
970 }
971
972 void
973 bdb_cache_delete_cleanup(
974         Cache *cache,
975         Entry *e )
976 {
977         EntryInfo *ei = BEI(e);
978
979         ei->bei_e = NULL;
980         e->e_private = NULL;
981         bdb_entry_return( e );
982
983         free( ei->bei_nrdn.bv_val );
984         ei->bei_nrdn.bv_val = NULL;
985 #ifdef BDB_HIER
986         free( ei->bei_rdn.bv_val );
987         ei->bei_rdn.bv_val = NULL;
988         ei->bei_modrdns = 0;
989         ei->bei_ckids = 0;
990         ei->bei_dkids = 0;
991 #endif
992         ei->bei_parent = NULL;
993         ei->bei_kids = NULL;
994         ei->bei_lruprev = NULL;
995
996         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
997         ei->bei_lrunext = cache->c_eifree;
998         cache->c_eifree = ei;
999         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1000         bdb_cache_entryinfo_unlock( ei );
1001 }
1002
1003 static int
1004 bdb_cache_delete_internal(
1005     Cache       *cache,
1006     EntryInfo           *e )
1007 {
1008         int rc = 0;     /* return code */
1009
1010 #ifdef BDB_HIER
1011         e->bei_parent->bei_ckids--;
1012         if ( e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1013 #endif
1014         /* dn tree */
1015         if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1016                 == NULL )
1017         {
1018                 rc = -1;
1019         }
1020
1021         /* id tree */
1022         if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) {
1023                 rc = -1;
1024         }
1025
1026         if (rc != 0) {
1027                 return rc;
1028         }
1029
1030         /* lru */
1031         LRU_DELETE( cache, e );
1032         cache->c_cursize--;
1033
1034         /*
1035          * flag entry to be freed later by a call to cache_return_entry()
1036          */
1037         e->bei_state |= CACHE_ENTRY_DELETED;
1038
1039         return( 0 );
1040 }
1041
1042 static void
1043 bdb_entryinfo_release( void *data )
1044 {
1045         EntryInfo *ei = (EntryInfo *)data;
1046         if ( ei->bei_kids ) {
1047                 avl_free( ei->bei_kids, NULL );
1048         }
1049         if ( ei->bei_e ) {
1050                 ei->bei_e->e_private = NULL;
1051                 bdb_entry_return( ei->bei_e );
1052         }
1053         bdb_cache_entryinfo_destroy( ei );
1054 }
1055
1056 void
1057 bdb_cache_release_all( Cache *cache )
1058 {
1059         /* set cache write lock */
1060         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1061         /* set lru mutex */
1062         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1063
1064 #ifdef NEW_LOGGING
1065         LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
1066 #else
1067         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1068 #endif
1069
1070         avl_free( cache->c_dntree.bei_kids, NULL );
1071         avl_free( cache->c_idtree, bdb_entryinfo_release );
1072         cache->c_lruhead = NULL;
1073         cache->c_lrutail = NULL;
1074
1075         /* free lru mutex */
1076         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1077         /* free cache write lock */
1078         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1079 }
1080
1081 #ifdef LDAP_DEBUG
1082 static void
1083 bdb_lru_print( Cache *cache )
1084 {
1085         EntryInfo       *e;
1086
1087         fprintf( stderr, "LRU queue (head to tail):\n" );
1088         for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1089                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1090                         e->bei_nrdn.bv_val, e->bei_id );
1091         }
1092         fprintf( stderr, "LRU queue (tail to head):\n" );
1093         for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1094                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1095                         e->bei_nrdn.bv_val, e->bei_id );
1096         }
1097 }
1098 #endif
1099
1100 #ifdef BDB_REUSE_LOCKERS
1101 static void
1102 bdb_locker_id_free( void *key, void *data )
1103 {
1104         DB_ENV *env = key;
1105         int lockid = (int) data;
1106         int rc;
1107
1108         rc = XLOCK_ID_FREE( env, lockid );
1109         if ( rc == EINVAL ) {
1110                 DB_LOCKREQ lr;
1111 #ifdef NEW_LOGGING
1112                 LDAP_LOG( BACK_BDB, ERR,
1113                         "bdb_locker_id_free: %d err %s(%d)\n",
1114                         lockid, db_strerror(rc), rc );
1115 #else
1116                 Debug( LDAP_DEBUG_ANY,
1117                         "bdb_locker_id_free: %d err %s(%d)\n",
1118                         lockid, db_strerror(rc), rc );
1119 #endif
1120                 memset( &lr, 0, sizeof(lr) );
1121
1122                 /* release all locks held by this locker. */
1123                 lr.op = DB_LOCK_PUT_ALL;
1124                 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1125                 XLOCK_ID_FREE( env, lockid );
1126         }
1127 }
1128
1129 int
1130 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
1131 {
1132         int i, rc, lockid;
1133         void *data;
1134         void *ctx;
1135
1136         if ( !env || !locker ) return -1;
1137
1138         /* If no op was provided, try to find the ctx anyway... */
1139         if ( op ) {
1140                 ctx = op->o_threadctx;
1141         } else {
1142                 ctx = ldap_pvt_thread_pool_context();
1143         }
1144
1145         /* Shouldn't happen unless we're single-threaded */
1146         if ( !ctx ) {
1147                 *locker = 0;
1148                 return 0;
1149         }
1150
1151         if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1152                 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1153                         rc = XLOCK_ID( env, &lockid );
1154                         if (rc) ldap_pvt_thread_yield();
1155                 }
1156                 if ( rc != 0) {
1157                         return rc;
1158                 }
1159                 data = (void *)lockid;
1160                 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1161                         data, bdb_locker_id_free ) ) ) {
1162                         XLOCK_ID_FREE( env, lockid );
1163 #ifdef NEW_LOGGING
1164                         LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1165                                 db_strerror(rc), rc, 0 );
1166 #else
1167                         Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1168                                 db_strerror(rc), rc, 0 );
1169 #endif
1170
1171                         return rc;
1172                 }
1173         } else {
1174                 lockid = (int)data;
1175         }
1176         *locker = lockid;
1177         return 0;
1178 }
1179 #endif
1180
1181 void
1182 bdb_cache_delete_entry(
1183         struct bdb_info *bdb,
1184         EntryInfo *ei,
1185         u_int32_t locker,
1186         DB_LOCK *lock )
1187 {
1188         ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1189         if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, ei, 1, 1, lock ) == 0 )
1190         {
1191                 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1192                         LRU_DELETE( &bdb->bi_cache, ei );
1193                         ei->bei_e->e_private = NULL;
1194                         bdb_entry_return( ei->bei_e );
1195                         ei->bei_e = NULL;
1196                         --bdb->bi_cache.c_cursize;
1197                 }
1198                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1199         }
1200         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1201 }