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