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