]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/cache.c
Fix SWAP macro
[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 static int
388 hdb_cache_find_parent(
389         Operation *op,
390         DB_TXN *txn,
391         ID id,
392         EntryInfo **res )
393 {
394         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
395         EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
396         int rc;
397         int addlru = 0;
398
399         ei.bei_id = id;
400         ei.bei_kids = NULL;
401         ei.bei_ckids = 0;
402
403         for (;;) {
404                 rc = hdb_dn2id_parent( op, txn, &ei, &eip.bei_id );
405                 if ( rc ) break;
406
407                 /* Save the previous node, if any */
408                 ei2 = ein;
409
410                 /* Create a new node for the current ID */
411                 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
412                 ein->bei_id = ei.bei_id;
413                 ein->bei_kids = ei.bei_kids;
414                 ein->bei_nrdn = ei.bei_nrdn;
415                 ein->bei_rdn = ei.bei_rdn;
416                 ein->bei_ckids = ei.bei_ckids;
417 #ifdef SLAP_ZONE_ALLOC
418                 ein->bei_bdb = bdb;
419 #endif
420                 ei.bei_ckids = 0;
421                 
422                 /* This node is not fully connected yet */
423                 ein->bei_state = CACHE_ENTRY_NOT_LINKED;
424
425                 /* Insert this node into the ID tree */
426                 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
427                 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
428                         bdb_id_cmp, avl_dup_error ) ) {
429
430                         /* Someone else created this node just before us.
431                          * Free our new copy and use the existing one.
432                          */
433                         bdb_cache_entryinfo_destroy( ein );
434                         ein = (EntryInfo *)avl_find( bdb->bi_cache.c_idtree,
435                                 (caddr_t) &ei, bdb_id_cmp );
436                         
437                         /* Link in any kids we've already processed */
438                         if ( ei2 ) {
439                                 bdb_cache_entryinfo_lock( ein );
440                                 avl_insert( &ein->bei_kids, (caddr_t)ei2,
441                                         bdb_rdn_cmp, avl_dup_error );
442                                 ein->bei_ckids++;
443                                 bdb_cache_entryinfo_unlock( ein );
444                         }
445                         addlru = 0;
446
447                 }
448                 if ( addlru ) {
449                         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
450                         LRU_ADD( &bdb->bi_cache, ein );
451                         ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
452                 }
453                 addlru = 1;
454
455                 /* If this is the first time, save this node
456                  * to be returned later.
457                  */
458                 if ( eir == NULL ) eir = ein;
459
460                 /* If there was a previous node, link it to this one */
461                 if ( ei2 ) ei2->bei_parent = ein;
462
463                 /* Look for this node's parent */
464                 if ( eip.bei_id ) {
465                         ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
466                                         (caddr_t) &eip, bdb_id_cmp );
467                 } else {
468                         ei2 = &bdb->bi_cache.c_dntree;
469                 }
470                 bdb->bi_cache.c_eiused++;
471                 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
472                                 bdb->bi_cache.c_leaves++;
473                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
474
475                 /* Got the parent, link in and we're done. */
476                 if ( ei2 ) {
477                         bdb_cache_entryinfo_lock( ei2 );
478                         ein->bei_parent = ei2;
479                         avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
480                                 avl_dup_error);
481                         ei2->bei_ckids++;
482                         bdb_cache_entryinfo_unlock( ei2 );
483                         bdb_cache_entryinfo_lock( eir );
484
485                         /* Reset all the state info */
486                         for (ein = eir; ein != ei2; ein=ein->bei_parent)
487                                 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
488                         *res = eir;
489                         break;
490                 }
491                 ei.bei_kids = NULL;
492                 ei.bei_id = eip.bei_id;
493                 ei.bei_ckids = 1;
494                 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
495                         avl_dup_error );
496         }
497         return rc;
498 }
499
500 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
501  * of a given node
502  */
503 int hdb_cache_load(
504         struct bdb_info *bdb,
505         EntryInfo *ei,
506         EntryInfo **res )
507 {
508         EntryInfo *ei2;
509         int rc;
510
511         /* See if we already have this one */
512         bdb_cache_entryinfo_lock( ei->bei_parent );
513         ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
514         bdb_cache_entryinfo_unlock( ei->bei_parent );
515
516         if ( !ei2 ) {
517                 /* Not found, add it */
518                 struct berval bv;
519
520                 /* bei_rdn was not malloc'd before, do it now */
521                 ber_dupbv( &bv, &ei->bei_rdn );
522                 ei->bei_rdn = bv;
523
524                 rc = bdb_entryinfo_add_internal( bdb, ei, res );
525                 bdb_cache_entryinfo_unlock( ei->bei_parent );
526                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
527         } else {
528                 /* Found, return it */
529                 *res = ei2;
530                 return 0;
531         }
532         return rc;
533 }
534 #endif
535
536 /* caller must have lru_mutex locked. mutex
537  * will be unlocked on return.
538  */
539 static void
540 bdb_cache_lru_add(
541         struct bdb_info *bdb,
542         u_int32_t       locker,
543         EntryInfo *ei )
544 {
545         DB_LOCK         lock, *lockp;
546
547         if ( locker ) {
548                 lockp = &lock;
549         } else {
550                 lockp = NULL;
551         }
552
553         /* See if we're above the cache size limit */
554         if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize ) {
555                 EntryInfo *elru, *elprev;
556                 int i = 0;
557
558                 /* Look for an unused entry to remove */
559                 for (elru = bdb->bi_cache.c_lrutail; elru; elru = elprev, i++ ) {
560                         elprev = elru->bei_lruprev;
561
562                         /* Too many probes, not enough idle, give up */
563                         if (i > 10)
564                                 break;
565
566                         /* If we can successfully writelock it, then
567                          * the object is idle.
568                          */
569                         if ( bdb_cache_entry_db_lock( bdb->bi_dbenv,
570                                         bdb->bi_cache.c_locker, elru, 1, 1, lockp ) == 0 ) {
571
572                                 int stop = 0, decr = 0;
573
574                                 /* If this node is in the process of linking into the cache,
575                                  * or this node is being deleted, skip it.
576                                  */
577                                 if ( elru->bei_state &
578                                         ( CACHE_ENTRY_NOT_LINKED | CACHE_ENTRY_DELETED )) {
579                                         bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
580                                         continue;
581                                 }
582                                 /* Free entry for this node if it's present */
583                                 if ( elru->bei_e ) {
584                                         elru->bei_e->e_private = NULL;
585 #ifdef SLAP_ZONE_ALLOC
586                                         bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
587 #else
588                                         bdb_entry_return( elru->bei_e );
589 #endif
590                                         elru->bei_e = NULL;
591                                         decr = 1;
592                                 }
593                                 /* ITS#4010 if we're in slapcat, and this node is a leaf
594                                  * node, free it.
595                                  *
596                                  * FIXME: we need to do this for slapd as well, (which is
597                                  * why we compute bi_cache.c_leaves now) but at the moment
598                                  * we can't because it causes unresolvable deadlocks. 
599                                  */
600                                 if ( slapMode & SLAP_TOOL_READONLY ) {
601                                         if ( !elru->bei_kids ) {
602                                                 /* This does LRU_DELETE for us */
603                                                 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
604                                                 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
605                                         }
606                                         /* Leave node on LRU list for a future pass */
607                                 } else {
608                                         LRU_DELETE( &bdb->bi_cache, elru );
609                                 }
610                                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lockp );
611
612                                 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
613                                 if ( decr )
614                                         --bdb->bi_cache.c_cursize;
615                                 if (bdb->bi_cache.c_cursize <= bdb->bi_cache.c_maxsize)
616                                         stop = 1;
617                                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
618                                 if (stop) break;
619                         }
620                 }
621         }
622         LRU_ADD( &bdb->bi_cache, ei );
623         ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
624 }
625
626 EntryInfo *
627 bdb_cache_find_info(
628         struct bdb_info *bdb,
629         ID id )
630 {
631         EntryInfo       ei = { 0 },
632                         *ei2;
633
634         ei.bei_id = id;
635
636         ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
637         ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
638                                         (caddr_t) &ei, bdb_id_cmp );
639         ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
640         return ei2;
641 }
642
643 /*
644  * cache_find_id - find an entry in the cache, given id.
645  * The entry is locked for Read upon return. Call with islocked TRUE if
646  * the supplied *eip was already locked.
647  */
648
649 int
650 bdb_cache_find_id(
651         Operation *op,
652         DB_TXN  *tid,
653         ID                              id,
654         EntryInfo       **eip,
655         int             islocked,
656         u_int32_t       locker,
657         DB_LOCK         *lock )
658 {
659         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
660         Entry   *ep = NULL;
661         int     rc = 0, load = 0;
662         EntryInfo ei = { 0 };
663
664         ei.bei_id = id;
665
666 #ifdef SLAP_ZONE_ALLOC
667         slap_zh_rlock(bdb->bi_cache.c_zctx);
668 #endif
669         /* If we weren't given any info, see if we have it already cached */
670         if ( !*eip ) {
671 again:  ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
672                 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
673                         (caddr_t) &ei, bdb_id_cmp );
674                 if ( *eip ) {
675                         /* If the lock attempt fails, the info is in use */
676                         if ( ldap_pvt_thread_mutex_trylock(
677                                         &(*eip)->bei_kids_mutex )) {
678                                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
679                                 /* If this node is being deleted, treat
680                                  * as if the delete has already finished
681                                  */
682                                 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
683                                         return DB_NOTFOUND;
684                                 }
685                                 /* otherwise, wait for the info to free up */
686                                 ldap_pvt_thread_yield();
687                                 goto again;
688                         }
689                         /* If this info isn't hooked up to its parent yet,
690                          * unlock and wait for it to be fully initialized
691                          */
692                         if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
693                                 bdb_cache_entryinfo_unlock( *eip );
694                                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
695                                 ldap_pvt_thread_yield();
696                                 goto again;
697                         }
698                         islocked = 1;
699                 }
700                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
701         }
702
703         /* See if the ID exists in the database; add it to the cache if so */
704         if ( !*eip ) {
705 #ifndef BDB_HIER
706                 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
707                 if ( rc == 0 ) {
708                         rc = bdb_cache_find_ndn( op, tid,
709                                 &ep->e_nname, eip );
710                         if ( *eip ) islocked = 1;
711                         if ( rc ) {
712 #ifdef SLAP_ZONE_ALLOC
713                                 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
714 #else
715                                 bdb_entry_return( ep );
716 #endif
717                                 ep = NULL;
718                         }
719                 }
720 #else
721                 rc = hdb_cache_find_parent(op, tid, id, eip );
722                 if ( rc == 0 && *eip ) islocked = 1;
723 #endif
724         }
725
726         /* Ok, we found the info, do we have the entry? */
727         if ( *eip && rc == 0 ) {
728                 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
729                         rc = DB_NOTFOUND;
730                 } else {
731                         /* Make sure only one thread tries to load the entry */
732 load1:
733 #ifdef SLAP_ZONE_ALLOC
734                         if ((*eip)->bei_e && !slap_zn_validate(
735                                         bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
736                                 (*eip)->bei_e = NULL;
737                                 (*eip)->bei_zseq = 0;
738                         }
739 #endif
740                         if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
741                                 load = 1;
742                                 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
743                         }
744                         if ( islocked ) {
745                                 bdb_cache_entryinfo_unlock( *eip );
746                                 islocked = 0;
747                         }
748                         rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, *eip, 0, 0, lock );
749                         if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
750                                 rc = DB_NOTFOUND;
751                                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
752                         } else if ( rc == 0 ) {
753                                 if ( load ) {
754                                         DB_TXN *ltid;
755                                         u_int32_t locker2 = locker;
756
757                                         /* We don't wrap entire read operations in txn's, but
758                                          * we need our cache entry lock and any DB page locks
759                                          * to be associated, in order for deadlock detection
760                                          * to work properly. So if we need to read from the DB,
761                                          * we use a long-lived per-thread txn for this step.
762                                          */
763                                         if ( !ep && !tid ) {
764                                                 rc = bdb_txn_get( op, bdb->bi_dbenv, &ltid, 0 );
765                                                 if ( ltid )
766                                                         locker2 = TXN_ID( ltid );
767                                         } else {
768                                                 ltid = tid;
769                                         }
770                                         /* Give up original read lock, obtain write lock with
771                                          * (possibly) new locker ID.
772                                          */
773                                     if ( rc == 0 ) {
774                                                 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker2,
775                                                         *eip, 1, 0, lock );
776                                         }
777                                         if ( rc == 0 && !ep) {
778                                                 rc = bdb_id2entry( op->o_bd, ltid, id, &ep );
779                                         }
780                                         if ( rc == 0 ) {
781                                                 ep->e_private = *eip;
782 #ifdef BDB_HIER
783                                                 bdb_fix_dn( ep, 0 );
784 #endif
785                                                 (*eip)->bei_e = ep;
786 #ifdef SLAP_ZONE_ALLOC
787                                                 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
788 #endif
789                                                 ep = NULL;
790                                         }
791                                         (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
792                                         if ( rc == 0 ) {
793                                                 /* If we succeeded, downgrade back to a readlock. */
794                                                 rc = bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
795                                                         *eip, 0, 0, lock );
796                                         } else {
797                                                 /* Otherwise, release the lock. */
798                                                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
799                                         }
800                                         if ( locker2 != locker ) {
801                                                 /* If we're using the per-thread txn, release all
802                                                  * of its page locks now.
803                                                  */
804                                                 DB_LOCKREQ list;
805                                                 list.op = DB_LOCK_PUT_ALL;
806                                                 list.obj = NULL;
807                                                 bdb->bi_dbenv->lock_vec( bdb->bi_dbenv, locker2,
808                                                         0, &list, 1, NULL );
809                                                 /* If this txn was deadlocked, we must abort it
810                                                  * and invalidate this per-thread txn.
811                                                  */
812                                                 if ( rc == DB_LOCK_DEADLOCK ) {
813                                                         bdb_txn_get( op, bdb->bi_dbenv, &ltid, 1 );
814                                                 }
815                                         }
816                                 } else if ( !(*eip)->bei_e ) {
817                                         /* Some other thread is trying to load the entry,
818                                          * give it a chance to finish.
819                                          */
820                                         bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
821                                         ldap_pvt_thread_yield();
822                                         bdb_cache_entryinfo_lock( *eip );
823                                         islocked = 1;
824                                         goto load1;
825 #ifdef BDB_HIER
826                                 } else {
827                                         /* Check for subtree renames
828                                          */
829                                         rc = bdb_fix_dn( (*eip)->bei_e, 1 );
830                                         if ( rc ) {
831                                                 bdb_cache_entry_db_relock( bdb->bi_dbenv,
832                                                         locker, *eip, 1, 0, lock );
833                                                 /* check again in case other modifier did it already */
834                                                 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
835                                                         rc = bdb_fix_dn( (*eip)->bei_e, 2 );
836                                                 bdb_cache_entry_db_relock( bdb->bi_dbenv,
837                                                         locker, *eip, 0, 0, lock );
838                                         }
839 #endif
840                                 }
841
842                         }
843                 }
844         }
845         if ( islocked ) {
846                 bdb_cache_entryinfo_unlock( *eip );
847         }
848         if ( ep ) {
849 #ifdef SLAP_ZONE_ALLOC
850                 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
851 #else
852                 bdb_entry_return( ep );
853 #endif
854         }
855         if ( rc == 0 ) {
856
857                 if ( load ) {
858                         ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
859                         bdb->bi_cache.c_cursize++;
860                         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
861                 }
862
863                 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
864
865                 /* If the LRU list has only one entry and this is it, it
866                  * doesn't need to be added again.
867                  */
868                 if ( bdb->bi_cache.c_lruhead == bdb->bi_cache.c_lrutail &&
869                         bdb->bi_cache.c_lruhead == *eip ) {
870                         ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
871                 } else {
872
873                         /* if entry is on LRU list, remove from old spot */
874                         if ( (*eip)->bei_lrunext || (*eip)->bei_lruprev ) {
875                                 LRU_DELETE( &bdb->bi_cache, *eip );
876                         }
877                         /* lru_mutex is unlocked for us */
878                         bdb_cache_lru_add( bdb, locker, *eip );
879                 }
880         }
881
882 #ifdef SLAP_ZONE_ALLOC
883         if (rc == 0 && (*eip)->bei_e) {
884                 slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
885         }
886         slap_zh_runlock(bdb->bi_cache.c_zctx);
887 #endif
888         return rc;
889 }
890
891 int
892 bdb_cache_children(
893         Operation *op,
894         DB_TXN *txn,
895         Entry *e )
896 {
897         int rc;
898
899         if ( BEI(e)->bei_kids ) {
900                 return 0;
901         }
902         if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
903                 return DB_NOTFOUND;
904         }
905         rc = bdb_dn2id_children( op, txn, e );
906         if ( rc == DB_NOTFOUND ) {
907                 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
908         }
909         return rc;
910 }
911
912 /* Update the cache after a successful database Add. */
913 int
914 bdb_cache_add(
915         struct bdb_info *bdb,
916         EntryInfo *eip,
917         Entry *e,
918         struct berval *nrdn,
919         u_int32_t locker )
920 {
921         EntryInfo *new, ei;
922         DB_LOCK lock;
923         int rc;
924 #ifdef BDB_HIER
925         struct berval rdn = e->e_name;
926 #endif
927
928         ei.bei_id = e->e_id;
929         ei.bei_parent = eip;
930         ei.bei_nrdn = *nrdn;
931         ei.bei_lockpad = 0;
932
933         /* Lock this entry so that bdb_add can run to completion.
934          * It can only fail if BDB has run out of lock resources.
935          */
936         rc = bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, &ei, 1, 0, &lock );
937         if ( rc ) {
938                 bdb_cache_entryinfo_unlock( eip );
939                 return rc;
940         }
941
942 #ifdef BDB_HIER
943         if ( nrdn->bv_len != e->e_nname.bv_len ) {
944                 char *ptr = strchr( rdn.bv_val, ',' );
945                 rdn.bv_len = ptr - rdn.bv_val;
946         }
947         ber_dupbv( &ei.bei_rdn, &rdn );
948         if ( eip->bei_dkids ) eip->bei_dkids++;
949 #endif
950
951         rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
952         /* bdb_csn_commit can cause this when adding the database root entry */
953         if ( new->bei_e ) {
954                 new->bei_e->e_private = NULL;
955 #ifdef SLAP_ZONE_ALLOC
956                 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
957 #else
958                 bdb_entry_return( new->bei_e );
959 #endif
960         }
961         new->bei_e = e;
962         e->e_private = new;
963         new->bei_state = CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
964         eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
965         if (eip->bei_parent) {
966                 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
967         }
968         bdb_cache_entryinfo_unlock( eip );
969
970         ++bdb->bi_cache.c_cursize;
971         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
972
973         /* set lru mutex */
974         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
975
976         /* lru_mutex is unlocked for us */
977         bdb_cache_lru_add( bdb, locker, new );
978
979         return rc;
980 }
981
982 int
983 bdb_cache_modify(
984         Entry *e,
985         Attribute *newAttrs,
986         DB_ENV *env,
987         u_int32_t locker,
988         DB_LOCK *lock )
989 {
990         EntryInfo *ei = BEI(e);
991         int rc;
992         /* Get write lock on data */
993         rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
994
995         /* If we've done repeated mods on a cached entry, then e_attrs
996          * is no longer contiguous with the entry, and must be freed.
997          */
998         if ( ! rc ) {
999                 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1000                         attrs_free( e->e_attrs ); 
1001                 }
1002                 e->e_attrs = newAttrs;
1003         }
1004         return rc;
1005 }
1006
1007 /*
1008  * Change the rdn in the entryinfo. Also move to a new parent if needed.
1009  */
1010 int
1011 bdb_cache_modrdn(
1012         struct bdb_info *bdb,
1013         Entry *e,
1014         struct berval *nrdn,
1015         Entry *new,
1016         EntryInfo *ein,
1017         u_int32_t locker,
1018         DB_LOCK *lock )
1019 {
1020         EntryInfo *ei = BEI(e), *pei;
1021         int rc;
1022 #ifdef BDB_HIER
1023         struct berval rdn;
1024 #endif
1025
1026         /* Get write lock on data */
1027         rc =  bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ei, 1, 0, lock );
1028         if ( rc ) return rc;
1029
1030         /* If we've done repeated mods on a cached entry, then e_attrs
1031          * is no longer contiguous with the entry, and must be freed.
1032          */
1033         if ( (void *)e->e_attrs != (void *)(e+1) ) {
1034                 attrs_free( e->e_attrs );
1035         }
1036         e->e_attrs = new->e_attrs;
1037         if( e->e_nname.bv_val < e->e_bv.bv_val ||
1038                 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1039         {
1040                 ch_free(e->e_name.bv_val);
1041                 ch_free(e->e_nname.bv_val);
1042         }
1043         e->e_name = new->e_name;
1044         e->e_nname = new->e_nname;
1045
1046         /* Lock the parent's kids AVL tree */
1047         pei = ei->bei_parent;
1048         bdb_cache_entryinfo_lock( pei );
1049         avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1050         free( ei->bei_nrdn.bv_val );
1051         ber_dupbv( &ei->bei_nrdn, nrdn );
1052 #ifdef BDB_HIER
1053         free( ei->bei_rdn.bv_val );
1054
1055         rdn = e->e_name;
1056         if ( nrdn->bv_len != e->e_nname.bv_len ) {
1057                 char *ptr = strchr(rdn.bv_val, ',');
1058                 rdn.bv_len = ptr - rdn.bv_val;
1059         }
1060         ber_dupbv( &ei->bei_rdn, &rdn );
1061 #endif
1062
1063         if (!ein) {
1064                 ein = ei->bei_parent;
1065         } else {
1066                 ei->bei_parent = ein;
1067                 bdb_cache_entryinfo_unlock( pei );
1068                 bdb_cache_entryinfo_lock( ein );
1069         }
1070 #ifdef BDB_HIER
1071         {
1072                 /* Record the generation number of this change */
1073                 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1074                 bdb->bi_modrdns++;
1075                 ei->bei_modrdns = bdb->bi_modrdns;
1076                 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1077         }
1078 #endif
1079         avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1080         bdb_cache_entryinfo_unlock( ein );
1081         return rc;
1082 }
1083 /*
1084  * cache_delete - delete the entry e from the cache. 
1085  *
1086  * returns:     0       e was deleted ok
1087  *              1       e was not in the cache
1088  *              -1      something bad happened
1089  */
1090 int
1091 bdb_cache_delete(
1092     Cache       *cache,
1093     Entry               *e,
1094     DB_ENV      *env,
1095     u_int32_t   locker,
1096     DB_LOCK     *lock )
1097 {
1098         EntryInfo *ei = BEI(e);
1099         int     rc;
1100
1101         assert( e->e_private != NULL );
1102
1103         /* Set this early, warn off any queriers */
1104         ei->bei_state |= CACHE_ENTRY_DELETED;
1105
1106         /* Lock the entry's info */
1107         bdb_cache_entryinfo_lock( ei );
1108
1109         /* Get write lock on the data */
1110         rc = bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
1111         if ( rc ) {
1112                 /* couldn't lock, undo and give up */
1113                 ei->bei_state ^= CACHE_ENTRY_DELETED;
1114                 bdb_cache_entryinfo_unlock( ei );
1115                 return rc;
1116         }
1117
1118         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1119                 e->e_id, 0, 0 );
1120
1121         /* set lru mutex */
1122         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1123
1124         /* set cache write lock */
1125         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1126
1127         rc = bdb_cache_delete_internal( cache, e->e_private, 1 );
1128
1129         /* free cache write lock */
1130         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1131
1132         /* free lru mutex */
1133         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1134
1135         /* Leave entry info locked */
1136
1137         return( rc );
1138 }
1139
1140 void
1141 bdb_cache_delete_cleanup(
1142         Cache *cache,
1143         EntryInfo *ei )
1144 {
1145         if ( ei->bei_e ) {
1146                 ei->bei_e->e_private = NULL;
1147 #ifdef SLAP_ZONE_ALLOC
1148                 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1149 #else
1150                 bdb_entry_return( ei->bei_e );
1151 #endif
1152                 ei->bei_e = NULL;
1153         }
1154
1155         free( ei->bei_nrdn.bv_val );
1156         ei->bei_nrdn.bv_val = NULL;
1157 #ifdef BDB_HIER
1158         free( ei->bei_rdn.bv_val );
1159         ei->bei_rdn.bv_val = NULL;
1160         ei->bei_modrdns = 0;
1161         ei->bei_ckids = 0;
1162         ei->bei_dkids = 0;
1163 #endif
1164         ei->bei_parent = NULL;
1165         ei->bei_kids = NULL;
1166         ei->bei_lruprev = NULL;
1167
1168         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1169         ei->bei_lrunext = cache->c_eifree;
1170         cache->c_eifree = ei;
1171         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1172         bdb_cache_entryinfo_unlock( ei );
1173 }
1174
1175 static int
1176 bdb_cache_delete_internal(
1177     Cache       *cache,
1178     EntryInfo           *e,
1179     int         decr )
1180 {
1181         int rc = 0;     /* return code */
1182
1183         /* Lock the parent's kids tree */
1184         bdb_cache_entryinfo_lock( e->bei_parent );
1185
1186 #ifdef BDB_HIER
1187         e->bei_parent->bei_ckids--;
1188         if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1189 #endif
1190         /* dn tree */
1191         if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1192                 == NULL )
1193         {
1194                 rc = -1;
1195         }
1196         if ( e->bei_parent->bei_kids )
1197                 cache->c_leaves--;
1198
1199         /* id tree */
1200         if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL ) {
1201                 rc = -1;
1202         }
1203
1204         if ( rc == 0 ){
1205                 cache->c_eiused--;
1206
1207                 /* lru */
1208                 LRU_DELETE( cache, e );
1209                 if ( e->bei_e ) cache->c_cursize--;
1210         }
1211
1212         bdb_cache_entryinfo_unlock( e->bei_parent );
1213
1214         return( rc );
1215 }
1216
1217 static void
1218 bdb_entryinfo_release( void *data )
1219 {
1220         EntryInfo *ei = (EntryInfo *)data;
1221         if ( ei->bei_kids ) {
1222                 avl_free( ei->bei_kids, NULL );
1223         }
1224         if ( ei->bei_e ) {
1225                 ei->bei_e->e_private = NULL;
1226 #ifdef SLAP_ZONE_ALLOC
1227                 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1228 #else
1229                 bdb_entry_return( ei->bei_e );
1230 #endif
1231         }
1232         bdb_cache_entryinfo_destroy( ei );
1233 }
1234
1235 void
1236 bdb_cache_release_all( Cache *cache )
1237 {
1238         /* set cache write lock */
1239         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1240         /* set lru mutex */
1241         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
1242
1243         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1244
1245         avl_free( cache->c_dntree.bei_kids, NULL );
1246         avl_free( cache->c_idtree, bdb_entryinfo_release );
1247         for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1248                 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1249                 bdb_cache_entryinfo_destroy(cache->c_eifree);
1250         }
1251         cache->c_cursize = 0;
1252         cache->c_eiused = 0;
1253         cache->c_leaves = 0;
1254         cache->c_idtree = NULL;
1255         cache->c_lruhead = NULL;
1256         cache->c_lrutail = NULL;
1257         cache->c_dntree.bei_kids = NULL;
1258
1259         /* free lru mutex */
1260         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1261         /* free cache write lock */
1262         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1263 }
1264
1265 #ifdef LDAP_DEBUG
1266 static void
1267 bdb_lru_print( Cache *cache )
1268 {
1269         EntryInfo       *e;
1270
1271         fprintf( stderr, "LRU queue (head to tail):\n" );
1272         for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1273                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1274                         e->bei_nrdn.bv_val, e->bei_id );
1275         }
1276         fprintf( stderr, "LRU queue (tail to head):\n" );
1277         for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1278                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1279                         e->bei_nrdn.bv_val, e->bei_id );
1280         }
1281 }
1282 #endif
1283
1284 static void
1285 bdb_txn_free( void *key, void *data )
1286 {
1287         DB_TXN *txn = data;
1288         TXN_ABORT( txn );
1289 }
1290
1291 /* Obtain a long-lived transaction for the current thread.
1292  * If reset == 1, remove the current transaction. */
1293 static int
1294 bdb_txn_get( Operation *op, DB_ENV *env, DB_TXN **txn, int reset )
1295 {
1296         int i, rc;
1297         void *ctx, *data = NULL;
1298
1299         if ( slapMode & SLAP_TOOL_MODE ) {
1300                 *txn = NULL;
1301                 return 0;
1302         }
1303
1304         /* If no op was provided, try to find the ctx anyway... */
1305         if ( op ) {
1306                 ctx = op->o_threadctx;
1307         } else {
1308                 ctx = ldap_pvt_thread_pool_context();
1309         }
1310
1311         /* Shouldn't happen unless we're single-threaded */
1312         if ( !ctx ) {
1313                 *txn = NULL;
1314                 return 0;
1315         }
1316
1317         if ( reset ) {
1318                 TXN_ABORT( *txn );
1319                 return ldap_pvt_thread_pool_setkey( ctx, ((char *)env)+1, NULL, NULL );
1320         }
1321
1322         if ( ldap_pvt_thread_pool_getkey( ctx, ((char *)env)+1, &data, NULL ) ||
1323                 data == NULL ) {
1324                 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1325                         rc = TXN_BEGIN( env, NULL, txn, READ_TXN_FLAG );
1326 #if DB_VERSION_FULL == 0x04020034
1327                         if ( rc == EINVAL && READ_TXN_FLAG ) {
1328                                 READ_TXN_FLAG = 0;
1329                                 Debug( LDAP_DEBUG_ANY,
1330                                         "bdb_txn_get: BerkeleyDB 4.2.52 library needs TXN patch!\n",
1331                                         0, 0, 0 );
1332                                 i--;
1333                                 continue;
1334                         }
1335 #endif
1336                         if (rc) ldap_pvt_thread_yield();
1337                 }
1338                 if ( rc != 0) {
1339                         return rc;
1340                 }
1341                 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, ((char *)env)+1,
1342                         *txn, bdb_txn_free ) ) ) {
1343                         TXN_ABORT( *txn );
1344                         Debug( LDAP_DEBUG_ANY, "bdb_txn_get: err %s(%d)\n",
1345                                 db_strerror(rc), rc, 0 );
1346
1347                         return rc;
1348                 }
1349         } else {
1350                 *txn = data;
1351         }
1352         return 0;
1353 }
1354
1355 #ifdef BDB_REUSE_LOCKERS
1356 static void
1357 bdb_locker_id_free( void *key, void *data )
1358 {
1359         DB_ENV *env = key;
1360         u_int32_t lockid = (long)data;
1361         int rc;
1362
1363         rc = XLOCK_ID_FREE( env, lockid );
1364         if ( rc == EINVAL ) {
1365                 DB_LOCKREQ lr;
1366                 Debug( LDAP_DEBUG_ANY,
1367                         "bdb_locker_id_free: %lu err %s(%d)\n",
1368                         (unsigned long) lockid, db_strerror(rc), rc );
1369                 /* release all locks held by this locker. */
1370                 lr.op = DB_LOCK_PUT_ALL;
1371                 lr.obj = NULL;
1372                 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1373                 XLOCK_ID_FREE( env, lockid );
1374         }
1375 }
1376
1377 int
1378 bdb_locker_id( Operation *op, DB_ENV *env, u_int32_t *locker )
1379 {
1380         int i, rc;
1381         u_int32_t lockid;
1382         void *data;
1383         void *ctx;
1384
1385         if ( !env || !locker ) return -1;
1386
1387         /* If no op was provided, try to find the ctx anyway... */
1388         if ( op ) {
1389                 ctx = op->o_threadctx;
1390         } else {
1391                 ctx = ldap_pvt_thread_pool_context();
1392         }
1393
1394         /* Shouldn't happen unless we're single-threaded */
1395         if ( !ctx ) {
1396                 *locker = 0;
1397                 return 0;
1398         }
1399
1400         if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1401                 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1402                         rc = XLOCK_ID( env, &lockid );
1403                         if (rc) ldap_pvt_thread_yield();
1404                 }
1405                 if ( rc != 0) {
1406                         return rc;
1407                 }
1408                 data = (void *)((long)lockid);
1409                 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1410                         data, bdb_locker_id_free ) ) ) {
1411                         XLOCK_ID_FREE( env, lockid );
1412                         Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1413                                 db_strerror(rc), rc, 0 );
1414
1415                         return rc;
1416                 }
1417         } else {
1418                 lockid = (long)data;
1419         }
1420         *locker = lockid;
1421         return 0;
1422 }
1423 #endif /* BDB_REUSE_LOCKERS */
1424
1425 void
1426 bdb_cache_delete_entry(
1427         struct bdb_info *bdb,
1428         EntryInfo *ei,
1429         u_int32_t locker,
1430         DB_LOCK *lock )
1431 {
1432         ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1433         if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, bdb->bi_cache.c_locker, ei, 1, 1, lock ) == 0 )
1434         {
1435                 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1436                         LRU_DELETE( &bdb->bi_cache, ei );
1437                         ei->bei_e->e_private = NULL;
1438 #ifdef SLAP_ZONE_ALLOC
1439                         bdb_entry_return( bdb, ei->bei_e, ei->bei_zseq );
1440 #else
1441                         bdb_entry_return( ei->bei_e );
1442 #endif
1443                         ei->bei_e = NULL;
1444                         --bdb->bi_cache.c_cursize;
1445                 }
1446                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1447         }
1448         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1449 }