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