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