]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/cache.c
Declare bdb_cache_entry_db_unlock().
[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 /*
535  * cache_find_id - find an entry in the cache, given id.
536  * The entry is locked for Read upon return. Call with islocked TRUE if
537  * the supplied *eip was already locked.
538  */
539
540 int
541 bdb_cache_find_id(
542         Operation *op,
543         DB_TXN  *tid,
544         ID                              id,
545         EntryInfo       **eip,
546         int             islocked,
547         u_int32_t       locker,
548         DB_LOCK         *lock
549 )
550 {
551         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
552         Entry   *ep = NULL;
553         int     rc = 0;
554         EntryInfo ei;
555         int lru_del = 0;
556
557         ei.bei_id = id;
558
559         /* If we weren't given any info, see if we have it already cached */
560         if ( !*eip ) {
561 again:          ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
562                 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
563                                         (caddr_t) &ei, bdb_id_cmp );
564                 if ( *eip ) {
565                         /* If the lock attempt fails, the info is in use */
566                         if ( ldap_pvt_thread_mutex_trylock(
567                                         &(*eip)->bei_kids_mutex )) {
568                                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
569                                 /* If this node is being deleted, treat
570                                  * as if the delete has already finished
571                                  */
572                                 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
573                                         return DB_NOTFOUND;
574                                 }
575                                 /* otherwise, wait for the info to free up */
576                                 ldap_pvt_thread_yield();
577                                 goto again;
578                         }
579                         /* If this info isn't hooked up to its parent yet,
580                          * unlock and wait for it to be fully initialized
581                          */
582                         if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
583                                 bdb_cache_entryinfo_unlock( *eip );
584                                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
585                                 ldap_pvt_thread_yield();
586                                 goto again;
587                         }
588                         islocked = 1;
589                 }
590                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
591         }
592
593         /* See if the ID exists in the database; add it to the cache if so */
594         if ( !*eip ) {
595 #ifndef BDB_HIER
596                 rc = bdb_id2entry( op->o_bd, tid, id, &ep );
597                 if ( rc == 0 ) {
598                         rc = bdb_cache_find_ndn( op, tid,
599                                 &ep->e_nname, eip );
600                         if ( *eip )
601                                 islocked = 1;
602                         if ( rc ) {
603                                 bdb_entry_return( ep );
604                                 ep = NULL;
605                         }
606                 }
607 #else
608                 rc = hdb_cache_find_parent(op, tid, id, eip );
609                 if ( rc == 0 && *eip )
610                         islocked = 1;
611 #endif
612         }
613
614         /* Ok, we found the info, do we have the entry? */
615         if ( *eip && rc == 0 ) {
616                 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
617                         rc = DB_NOTFOUND;
618                 } else {
619                         bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
620                                         *eip, 0, 0, lock );
621                         if ( !(*eip)->bei_e ) {
622                                 if (!ep) {
623                                         rc = bdb_id2entry( op->o_bd, tid, id, &ep );
624                                 }
625                                 if ( rc == 0 ) {
626                                         bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
627                                                 *eip, 1, 0, lock );
628                                         /* Make sure no other modifier beat us to it */
629                                         if ( (*eip)->bei_e ) {
630                                                 bdb_entry_return( ep );
631                                                 ep = NULL;
632                                         } else {
633                                                 ep->e_private = *eip;
634 #ifdef BDB_HIER
635                                                 bdb_fix_dn( ep, 0 );
636 #endif
637                                                 (*eip)->bei_e = ep;
638                                         }
639                                         bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
640                                                 *eip, 0, 0, lock );
641                                 }
642                         } else {
643                                 /* If we had the entry already, this item
644                                  * is on the LRU list.
645                                  */
646                                 lru_del = 1;
647 #ifdef BDB_HIER
648                                 rc = bdb_fix_dn( (*eip)->bei_e, 1 );
649                                 if ( rc ) {
650                                         bdb_cache_entry_db_relock( bdb->bi_dbenv,
651                                                 locker, *eip, 1, 0, lock );
652                                         /* check again in case other modifier did it already */
653                                         if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
654                                                 rc = bdb_fix_dn( (*eip)->bei_e, 2 );
655                                         bdb_cache_entry_db_relock( bdb->bi_dbenv,
656                                                 locker, *eip, 0, 0, lock );
657                                 }
658 #endif
659                         }
660                 }
661         }
662         if ( rc == 0 ) {
663                 /* set lru mutex */
664                 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
665                 /* if entry is old, remove from old spot on LRU list */
666                 if ( lru_del ) {
667                         LRU_DELETE( &bdb->bi_cache, *eip );
668                 } else {
669                 /* if entry is new, bump cache size */
670                         bdb->bi_cache.c_cursize++;
671                 }
672                 /* lru_mutex is unlocked for us */
673                 bdb_cache_lru_add( bdb, locker, *eip );
674         }
675
676         if ( islocked ) {
677                 bdb_cache_entryinfo_unlock( *eip );
678         }
679         return rc;
680 }
681
682 int
683 bdb_cache_children(
684         Operation *op,
685         DB_TXN *txn,
686         Entry *e
687 )
688 {
689         int rc;
690
691         if ( BEI(e)->bei_kids ) {
692                 return 0;
693         }
694         if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
695                 return DB_NOTFOUND;
696         }
697         rc = bdb_dn2id_children( op, txn, e );
698         if ( rc == DB_NOTFOUND ) {
699                 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS;
700         }
701         return rc;
702 }
703
704 /* Update the cache after a successful database Add. */
705 int
706 bdb_cache_add(
707         struct bdb_info *bdb,
708         EntryInfo *eip,
709         Entry *e,
710         struct berval *nrdn,
711         u_int32_t locker
712 )
713 {
714         EntryInfo *new, ei;
715         struct berval rdn = e->e_name;
716         int rc;
717
718         ei.bei_id = e->e_id;
719         ei.bei_parent = eip;
720         ei.bei_nrdn = *nrdn;
721 #ifdef BDB_HIER
722         if ( nrdn->bv_len != e->e_nname.bv_len ) {
723                 char *ptr = strchr( rdn.bv_val, ',' );
724                 rdn.bv_len = ptr - rdn.bv_val;
725         }
726         ber_dupbv( &ei.bei_rdn, &rdn );
727         if ( eip->bei_dkids ) eip->bei_dkids++;
728 #endif
729         rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
730         new->bei_e = e;
731         e->e_private = new;
732         new->bei_state = CACHE_ENTRY_NO_KIDS;
733         eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
734
735         /* set lru mutex */
736         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
737         ++bdb->bi_cache.c_cursize;
738         /* lru_mutex is unlocked for us */
739         bdb_cache_lru_add( bdb, locker, new );
740
741         bdb_cache_entryinfo_unlock( eip );
742         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
743         return rc;
744 }
745
746 int
747 bdb_cache_modify(
748         Entry *e,
749         Attribute *newAttrs,
750         DB_ENV *env,
751         u_int32_t locker,
752         DB_LOCK *lock
753 )
754 {
755         EntryInfo *ei = BEI(e);
756         
757         /* Get write lock on data */
758         bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
759
760         /* If we've done repeated mods on a cached entry, then e_attrs
761          * is no longer contiguous with the entry, and must be freed.
762          */
763         if ( (void *)e->e_attrs != (void *)(e+1) ) {
764                 attrs_free( e->e_attrs );
765         }
766         e->e_attrs = newAttrs;
767
768         return 0;
769 }
770
771 /*
772  * Change the rdn in the entryinfo. Also move to a new parent if needed.
773  */
774 int
775 bdb_cache_modrdn(
776         Entry *e,
777         struct berval *nrdn,
778         Entry *new,
779         EntryInfo *ein,
780         DB_ENV *env,
781         u_int32_t locker,
782         DB_LOCK *lock
783 )
784 {
785         EntryInfo *ei = BEI(e), *pei;
786         struct berval rdn;
787         int rc = 0;
788
789         /* Get write lock on data */
790         bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
791
792         /* If we've done repeated mods on a cached entry, then e_attrs
793          * is no longer contiguous with the entry, and must be freed.
794          */
795         if ( (void *)e->e_attrs != (void *)(e+1) ) {
796                 attrs_free( e->e_attrs );
797         }
798         e->e_attrs = new->e_attrs;
799         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
800                 e->e_bv.bv_val + e->e_bv.bv_len ) {
801                 ch_free(e->e_name.bv_val);
802                 ch_free(e->e_nname.bv_val);
803         }
804         e->e_name = new->e_name;
805         e->e_nname = new->e_nname;
806
807         /* Lock the parent's kids AVL tree */
808         pei = ei->bei_parent;
809         bdb_cache_entryinfo_lock( pei );
810         avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
811         free( ei->bei_nrdn.bv_val );
812         ber_dupbv( &ei->bei_nrdn, nrdn );
813 #ifdef BDB_HIER
814         free( ei->bei_rdn.bv_val );
815
816         rdn = e->e_name;
817         if ( nrdn->bv_len != e->e_nname.bv_len ) {
818                 char *ptr = strchr(rdn.bv_val, ',');
819                 rdn.bv_len = ptr - rdn.bv_val;
820         }
821         ber_dupbv( &ei->bei_rdn, &rdn );
822 #endif
823
824         if (!ein) {
825                 ein = ei->bei_parent;
826         } else {
827                 ei->bei_parent = ein;
828                 bdb_cache_entryinfo_unlock( pei );
829                 bdb_cache_entryinfo_lock( ein );
830         }
831 #ifdef BDB_HIER
832         { int max = ei->bei_modrdns;
833         /* Record the generation number of this change */
834                 for ( pei = ein; pei->bei_parent; pei = pei->bei_parent ) {
835                         if ( pei->bei_modrdns > max )
836                                 max = pei->bei_modrdns;
837                 }
838                 ei->bei_modrdns = max + 1;
839         }
840 #endif
841         avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
842         bdb_cache_entryinfo_unlock( ein );
843         return rc;
844 }
845 /*
846  * cache_delete - delete the entry e from the cache. 
847  *
848  * returns:     0       e was deleted ok
849  *              1       e was not in the cache
850  *              -1      something bad happened
851  */
852 int
853 bdb_cache_delete(
854     Cache       *cache,
855     Entry               *e,
856     DB_ENV      *env,
857     u_int32_t   locker,
858     DB_LOCK     *lock
859 )
860 {
861         EntryInfo *ei = BEI(e);
862         int     rc;
863
864         assert( e->e_private );
865
866         /* Set this early, warn off any queriers */
867         ei->bei_state |= CACHE_ENTRY_DELETED;
868
869         /* Lock the entry's info */
870         bdb_cache_entryinfo_lock( ei );
871
872         /* Get write lock on the data */
873         bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
874
875         /* set cache write lock */
876         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
877
878         /* Lock the parent's kids tree */
879         bdb_cache_entryinfo_lock( ei->bei_parent );
880
881 #ifdef NEW_LOGGING
882         LDAP_LOG( CACHE, ENTRY, 
883                 "bdb_cache_delete: delete %ld.\n", e->e_id, 0, 0 );
884 #else
885         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
886                 e->e_id, 0, 0 );
887 #endif
888
889         /* set lru mutex */
890         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
891         rc = bdb_cache_delete_internal( cache, e->e_private );
892         /* free lru mutex */
893         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
894
895         /* free cache write lock */
896         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
897         bdb_cache_entryinfo_unlock( ei->bei_parent );
898
899         /* Leave entry info locked */
900
901         return( rc );
902 }
903
904 void
905 bdb_cache_delete_cleanup(
906         Cache *cache,
907         Entry *e
908 )
909 {
910         EntryInfo *ei = BEI(e);
911
912         ei->bei_e = NULL;
913         e->e_private = NULL;
914         bdb_entry_return( e );
915
916         free( ei->bei_nrdn.bv_val );
917         ei->bei_nrdn.bv_val = NULL;
918 #ifdef BDB_HIER
919         free( ei->bei_rdn.bv_val );
920         ei->bei_rdn.bv_val = NULL;
921         ei->bei_modrdns = 0;
922         ei->bei_ckids = 0;
923         ei->bei_dkids = 0;
924 #endif
925         ei->bei_parent = NULL;
926         ei->bei_kids = NULL;
927         ei->bei_lruprev = NULL;
928
929         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
930         ei->bei_lrunext = cache->c_eifree;
931         cache->c_eifree = ei;
932         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
933         bdb_cache_entryinfo_unlock( ei );
934 }
935
936 static int
937 bdb_cache_delete_internal(
938     Cache       *cache,
939     EntryInfo           *e
940 )
941 {
942         int rc = 0;     /* return code */
943
944 #ifdef BDB_HIER
945         e->bei_parent->bei_ckids--;
946         if ( e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
947 #endif
948         /* dn tree */
949         if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
950         {
951                 rc = -1;
952         }
953
954         /* id tree */
955         if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
956         {
957                 rc = -1;
958         }
959
960         if (rc != 0) {
961                 return rc;
962         }
963
964         /* lru */
965         LRU_DELETE( cache, e );
966         cache->c_cursize--;
967
968         /*
969          * flag entry to be freed later by a call to cache_return_entry()
970          */
971         e->bei_state |= CACHE_ENTRY_DELETED;
972
973         return( 0 );
974 }
975
976 static void
977 bdb_entryinfo_release( void *data )
978 {
979         EntryInfo *ei = (EntryInfo *)data;
980         if ( ei->bei_kids ) {
981                 avl_free( ei->bei_kids, NULL );
982         }
983         if ( ei->bei_e ) {
984                 ei->bei_e->e_private = NULL;
985                 bdb_entry_return( ei->bei_e );
986         }
987         bdb_cache_entryinfo_destroy( ei );
988 }
989
990 void
991 bdb_cache_release_all( Cache *cache )
992 {
993         /* set cache write lock */
994         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
995         /* set lru mutex */
996         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
997
998 #ifdef NEW_LOGGING
999         LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
1000 #else
1001         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1002 #endif
1003
1004         avl_free( cache->c_dntree.bei_kids, NULL );
1005         avl_free( cache->c_idtree, bdb_entryinfo_release );
1006         cache->c_lruhead = NULL;
1007         cache->c_lrutail = NULL;
1008
1009         /* free lru mutex */
1010         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
1011         /* free cache write lock */
1012         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1013 }
1014
1015 #ifdef LDAP_DEBUG
1016 static void
1017 bdb_lru_print( Cache *cache )
1018 {
1019         EntryInfo       *e;
1020
1021         fprintf( stderr, "LRU queue (head to tail):\n" );
1022         for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
1023                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1024                         e->bei_nrdn.bv_val, e->bei_id );
1025         }
1026         fprintf( stderr, "LRU queue (tail to head):\n" );
1027         for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
1028                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
1029                         e->bei_nrdn.bv_val, e->bei_id );
1030         }
1031 }
1032 #endif
1033
1034 #ifdef BDB_REUSE_LOCKERS
1035 static void
1036 bdb_locker_id_free( void *key, void *data )
1037 {
1038         DB_ENV *env = key;
1039         int lockid = (int) data;
1040         int rc;
1041
1042
1043         rc = XLOCK_ID_FREE( env, lockid );
1044         if ( rc == EINVAL ) {
1045                 DB_LOCKREQ lr;
1046 #ifdef NEW_LOGGING
1047                 LDAP_LOG( BACK_BDB, ERR,
1048                         "bdb_locker_id_free: %d err %s(%d)\n",
1049                         lockid, db_strerror(rc), rc );
1050 #else
1051                 Debug( LDAP_DEBUG_ANY,
1052                         "bdb_locker_id_free: %d err %s(%d)\n",
1053                         lockid, db_strerror(rc), rc );
1054 #endif
1055                 memset( &lr, 0, sizeof(lr) );
1056
1057                 /* release all locks held by this locker. */
1058                 lr.op = DB_LOCK_PUT_ALL;
1059                 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1060                 XLOCK_ID_FREE( env, lockid );
1061         }
1062 }
1063
1064 int
1065 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
1066 {
1067         int i, rc, lockid;
1068         void *data;
1069         void *ctx;
1070
1071         if ( !env || !locker ) return -1;
1072
1073         /* If no op was provided, try to find the ctx anyway... */
1074         if ( op ) {
1075                 ctx = op->o_threadctx;
1076         } else {
1077                 ctx = ldap_pvt_thread_pool_context();
1078         }
1079
1080         /* Shouldn't happen unless we're single-threaded */
1081         if ( !ctx ) {
1082                 *locker = 0;
1083                 return 0;
1084         }
1085
1086         if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1087                 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1088                         rc = XLOCK_ID( env, &lockid );
1089                         if (rc) ldap_pvt_thread_yield();
1090                 }
1091                 if ( rc != 0) {
1092                         return rc;
1093                 }
1094                 data = (void *)lockid;
1095                 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1096                         data, bdb_locker_id_free ) ) ) {
1097                         XLOCK_ID_FREE( env, lockid );
1098 #ifdef NEW_LOGGING
1099                         LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
1100                                 db_strerror(rc), rc, 0 );
1101 #else
1102                         Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1103                                 db_strerror(rc), rc, 0 );
1104 #endif
1105
1106                         return rc;
1107                 }
1108         } else {
1109                 lockid = (int)data;
1110         }
1111         *locker = lockid;
1112         return 0;
1113 }
1114 #endif
1115
1116 void
1117 bdb_cache_delete_entry(
1118         struct bdb_info *bdb,
1119         EntryInfo *ei,
1120         u_int32_t locker,
1121         DB_LOCK *lock )
1122 {
1123         ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
1124         if ( bdb_cache_entry_db_lock( bdb->bi_dbenv, locker, ei, 1, 1, lock ) == 0 ) {
1125                 if ( ei->bei_e && !(ei->bei_state & CACHE_ENTRY_NOT_LINKED )) {
1126                         LRU_DELETE( &bdb->bi_cache, ei );
1127                         ei->bei_e->e_private = NULL;
1128                         bdb_entry_return( ei->bei_e );
1129                         ei->bei_e = NULL;
1130                         --bdb->bi_cache.c_cursize;
1131                 }
1132                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, lock );
1133         }
1134         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1135 }