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