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