]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/cache.c
ITS#4667 fix assert in connection_next() for PENDING connections
[openldap] / servers / slapd / back-monitor / 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 2001-2006 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include "ac/string.h"
26
27 #include "slap.h"
28
29 #include "back-monitor.h"
30
31 /*
32  * The cache maps DNs to Entries.
33  * Each entry, on turn, holds the list of its children in the e_private field.
34  * This is used by search operation to perform onelevel and subtree candidate
35  * selection.
36  */
37 typedef struct monitor_cache_t {
38         struct berval           mc_ndn;
39         Entry                   *mc_e;
40 } monitor_cache_t;
41
42 /*
43  * compares entries based on the dn
44  */
45 int
46 monitor_cache_cmp(
47         const void      *c1,
48         const void      *c2 )
49 {
50         monitor_cache_t         *cc1 = ( monitor_cache_t * )c1;
51         monitor_cache_t         *cc2 = ( monitor_cache_t * )c2;
52
53         /*
54          * case sensitive, because the dn MUST be normalized
55          */
56         return ber_bvcmp( &cc1->mc_ndn, &cc2->mc_ndn );
57 }
58
59 /*
60  * checks for duplicate entries
61  */
62 int
63 monitor_cache_dup(
64         void            *c1,
65         void            *c2 )
66 {
67         monitor_cache_t *cc1 = ( monitor_cache_t * )c1;
68         monitor_cache_t *cc2 = ( monitor_cache_t * )c2;
69
70         /*
71          * case sensitive, because the dn MUST be normalized
72          */
73         return ber_bvcmp( &cc1->mc_ndn, &cc2->mc_ndn ) == 0 ? -1 : 0;
74 }
75
76 /*
77  * adds an entry to the cache and inits the mutex
78  */
79 int
80 monitor_cache_add(
81         monitor_info_t  *mi,
82         Entry           *e )
83 {
84         monitor_cache_t *mc;
85         monitor_entry_t *mp;
86         int             rc;
87
88         assert( mi != NULL );
89         assert( e != NULL );
90
91         mp = ( monitor_entry_t *)e->e_private;
92
93         mc = ( monitor_cache_t * )ch_malloc( sizeof( monitor_cache_t ) );
94         mc->mc_ndn = e->e_nname;
95         mc->mc_e = e;
96         ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
97         rc = avl_insert( &mi->mi_cache, ( caddr_t )mc,
98                         monitor_cache_cmp, monitor_cache_dup );
99         ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
100
101         return rc;
102 }
103
104 /*
105  * locks the entry (no r/w)
106  */
107 int
108 monitor_cache_lock(
109         Entry           *e )
110 {
111         monitor_entry_t *mp;
112
113         assert( e != NULL );
114         assert( e->e_private != NULL );
115
116         mp = ( monitor_entry_t * )e->e_private;
117         ldap_pvt_thread_mutex_lock( &mp->mp_mutex );
118
119         return( 0 );
120 }
121
122 /*
123  * tries to lock the entry (no r/w)
124  */
125 int
126 monitor_cache_trylock(
127         Entry           *e )
128 {
129         monitor_entry_t *mp;
130
131         assert( e != NULL );
132         assert( e->e_private != NULL );
133
134         mp = ( monitor_entry_t * )e->e_private;
135         return ldap_pvt_thread_mutex_trylock( &mp->mp_mutex );
136 }
137
138 /*
139  * gets an entry from the cache based on the normalized dn 
140  * with mutex locked
141  */
142 int
143 monitor_cache_get(
144         monitor_info_t  *mi,
145         struct berval   *ndn,
146         Entry           **ep )
147 {
148         monitor_cache_t tmp_mc, *mc;
149
150         assert( mi != NULL );
151         assert( ndn != NULL );
152         assert( ep != NULL );
153
154         *ep = NULL;
155
156         tmp_mc.mc_ndn = *ndn;
157         ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
158         mc = ( monitor_cache_t * )avl_find( mi->mi_cache,
159                         ( caddr_t )&tmp_mc, monitor_cache_cmp );
160
161         if ( mc != NULL ) {
162                 /* entry is returned with mutex locked */
163                 monitor_cache_lock( mc->mc_e );
164                 *ep = mc->mc_e;
165         }
166
167         ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
168
169         return ( *ep == NULL ? -1 : 0 );
170 }
171
172 /*
173  * gets an entry from the cache based on the normalized dn 
174  * with mutex locked
175  */
176 int
177 monitor_cache_remove(
178         monitor_info_t  *mi,
179         struct berval   *ndn,
180         Entry           **ep )
181 {
182         monitor_cache_t tmp_mc, *mc;
183         struct berval   pndn;
184
185         assert( mi != NULL );
186         assert( ndn != NULL );
187         assert( ep != NULL );
188
189         *ep = NULL;
190
191         dnParent( ndn, &pndn );
192
193 retry:;
194         ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
195
196         tmp_mc.mc_ndn = *ndn;
197         mc = ( monitor_cache_t * )avl_find( mi->mi_cache,
198                         ( caddr_t )&tmp_mc, monitor_cache_cmp );
199
200         if ( mc != NULL ) {
201                 monitor_cache_t *pmc;
202
203                 if ( monitor_cache_trylock( mc->mc_e ) ) {
204                         ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
205                         goto retry;
206                 }
207
208                 tmp_mc.mc_ndn = pndn;
209                 pmc = ( monitor_cache_t * )avl_find( mi->mi_cache,
210                         ( caddr_t )&tmp_mc, monitor_cache_cmp );
211                 if ( pmc != NULL ) {
212                         monitor_entry_t *mp = (monitor_entry_t *)mc->mc_e->e_private,
213                                         *pmp = (monitor_entry_t *)pmc->mc_e->e_private;
214                         Entry           **entryp;
215
216                         if ( monitor_cache_trylock( pmc->mc_e ) ) {
217                                 monitor_cache_release( mi, mc->mc_e );
218                                 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
219                                 goto retry;
220                         }
221
222                         for ( entryp = &pmp->mp_children; *entryp != NULL;  ) {
223                                 monitor_entry_t *next = (monitor_entry_t *)(*entryp)->e_private;
224                                 if ( next == mp ) {
225                                         *entryp = next->mp_next;
226                                         entryp = NULL;
227                                         break;
228                                 }
229
230                                 entryp = &next->mp_next;
231                         }
232
233                         if ( entryp != NULL ) {
234                                 Debug( LDAP_DEBUG_ANY,
235                                         "monitor_cache_remove(\"%s\"): "
236                                         "not in parent's list\n",
237                                         ndn->bv_val, 0, 0 );
238                         }
239
240                         /* either succeeded, and the entry is no longer
241                          * in its parent's list, or failed, and the
242                          * entry is neither mucked with nor returned */
243                         monitor_cache_release( mi, pmc->mc_e );
244
245                         if ( entryp == NULL ) {
246                                 monitor_cache_t *tmpmc;
247
248                                 tmp_mc.mc_ndn = *ndn;
249                                 tmpmc = avl_delete( &mi->mi_cache,
250                                         ( caddr_t )&tmp_mc, monitor_cache_cmp );
251                                 assert( tmpmc == mc );
252
253                                 *ep = mc->mc_e;
254                                 ch_free( mc );
255                                 mc = NULL;
256
257                                 /* NOTE: we destroy the mutex, but otherwise
258                                  * leave the private data around; specifically,
259                                  * callbacks need be freed by someone else */
260
261                                 ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
262                                 mp->mp_next = NULL;
263                                 mp->mp_children = NULL;
264                         }
265
266                 }
267                 monitor_cache_release( mi, mc->mc_e );
268         }
269
270         ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
271
272         return ( *ep == NULL ? -1 : 0 );
273 }
274
275 /*
276  * If the entry exists in cache, it is returned in locked status;
277  * otherwise, if the parent exists, if it may generate volatile 
278  * descendants an attempt to generate the required entry is
279  * performed and, if successful, the entry is returned
280  */
281 int
282 monitor_cache_dn2entry(
283         Operation               *op,
284         SlapReply               *rs,
285         struct berval           *ndn,
286         Entry                   **ep,
287         Entry                   **matched )
288 {
289         monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
290         int                     rc;
291         struct berval           p_ndn = BER_BVNULL;
292         Entry                   *e_parent;
293         monitor_entry_t         *mp;
294                 
295         assert( mi != NULL );
296         assert( ndn != NULL );
297         assert( ep != NULL );
298         assert( matched != NULL );
299
300         *matched = NULL;
301
302         if ( !dnIsSuffix( ndn, &op->o_bd->be_nsuffix[ 0 ] ) ) {
303                 return( -1 );
304         }
305
306         rc = monitor_cache_get( mi, ndn, ep );
307         if ( !rc && *ep != NULL ) {
308                 return( 0 );
309         }
310
311         /* try with parent/ancestors */
312         if ( BER_BVISNULL( ndn ) ) {
313                 BER_BVSTR( &p_ndn, "" );
314
315         } else {
316                 dnParent( ndn, &p_ndn );
317         }
318
319         rc = monitor_cache_dn2entry( op, rs, &p_ndn, &e_parent, matched );
320         if ( rc || e_parent == NULL ) {
321                 return( -1 );
322         }
323
324         mp = ( monitor_entry_t * )e_parent->e_private;
325         rc = -1;
326         if ( mp->mp_flags & MONITOR_F_VOLATILE_CH ) {
327                 /* parent entry generates volatile children */
328                 rc = monitor_entry_create( op, rs, ndn, e_parent, ep );
329         }
330
331         if ( !rc ) {
332                 monitor_cache_lock( *ep );
333                 monitor_cache_release( mi, e_parent );
334
335         } else {
336                 *matched = e_parent;
337         }
338         
339         return( rc );
340 }
341
342 /*
343  * releases the lock of the entry; if it is marked as volatile, it is
344  * destroyed.
345  */
346 int
347 monitor_cache_release(
348         monitor_info_t  *mi,
349         Entry           *e )
350 {
351         monitor_entry_t *mp;
352
353         assert( mi != NULL );
354         assert( e != NULL );
355         assert( e->e_private != NULL );
356         
357         mp = ( monitor_entry_t * )e->e_private;
358
359         if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
360                 monitor_cache_t *mc, tmp_mc;
361
362                 /* volatile entries do not return to cache */
363                 ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
364                 tmp_mc.mc_ndn = e->e_nname;
365                 mc = avl_delete( &mi->mi_cache,
366                                 ( caddr_t )&tmp_mc, monitor_cache_cmp );
367                 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
368                 if ( mc != NULL ) {
369                         ch_free( mc );
370                 }
371                 
372                 ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
373                 ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
374                 ch_free( mp );
375                 e->e_private = NULL;
376                 entry_free( e );
377
378                 return( 0 );
379         }
380         
381         ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
382
383         return( 0 );
384 }
385
386 static void
387 monitor_entry_destroy( void *v_mc )
388 {
389         monitor_cache_t         *mc = (monitor_cache_t *)v_mc;
390
391         if ( mc->mc_e != NULL ) {
392                 monitor_entry_t *mp;
393
394                 assert( mc->mc_e->e_private != NULL );
395         
396                 mp = ( monitor_entry_t * )mc->mc_e->e_private;
397
398                 if ( mp->mp_cb ) {
399                         monitor_callback_t      *cb;
400
401                         for ( cb = mp->mp_cb; cb != NULL; ) {
402                                 monitor_callback_t      *next = cb->mc_next;
403
404                                 if ( cb->mc_free ) {
405                                         cb->mc_free( mc->mc_e, cb->mc_private );
406                                 }
407                                 ch_free( mp->mp_cb );
408
409                                 cb = next;
410                         }
411                 }
412
413                 ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
414
415                 ch_free( mp );
416                 mc->mc_e->e_private = NULL;
417                 entry_free( mc->mc_e );
418         }
419
420         ch_free( mc );
421 }
422
423 int
424 monitor_cache_destroy(
425         monitor_info_t  *mi )
426 {
427         if ( mi->mi_cache ) {
428                 avl_free( mi->mi_cache, monitor_entry_destroy );
429         }
430
431         return 0;
432 }
433