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