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