]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/cache.c
Warning cleanup (avoid cast double return val -> integer, gcc gets suspicious)
[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-2007 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
268                 if ( mc ) {
269                         monitor_cache_release( mi, mc->mc_e );
270                 }
271         }
272
273         ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
274
275         return ( *ep == NULL ? -1 : 0 );
276 }
277
278 /*
279  * If the entry exists in cache, it is returned in locked status;
280  * otherwise, if the parent exists, if it may generate volatile 
281  * descendants an attempt to generate the required entry is
282  * performed and, if successful, the entry is returned
283  */
284 int
285 monitor_cache_dn2entry(
286         Operation               *op,
287         SlapReply               *rs,
288         struct berval           *ndn,
289         Entry                   **ep,
290         Entry                   **matched )
291 {
292         monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
293         int                     rc;
294         struct berval           p_ndn = BER_BVNULL;
295         Entry                   *e_parent;
296         monitor_entry_t         *mp;
297                 
298         assert( mi != NULL );
299         assert( ndn != NULL );
300         assert( ep != NULL );
301         assert( matched != NULL );
302
303         *matched = NULL;
304
305         if ( !dnIsSuffix( ndn, &op->o_bd->be_nsuffix[ 0 ] ) ) {
306                 return( -1 );
307         }
308
309         rc = monitor_cache_get( mi, ndn, ep );
310         if ( !rc && *ep != NULL ) {
311                 return( 0 );
312         }
313
314         /* try with parent/ancestors */
315         if ( BER_BVISNULL( ndn ) ) {
316                 BER_BVSTR( &p_ndn, "" );
317
318         } else {
319                 dnParent( ndn, &p_ndn );
320         }
321
322         rc = monitor_cache_dn2entry( op, rs, &p_ndn, &e_parent, matched );
323         if ( rc || e_parent == NULL ) {
324                 return( -1 );
325         }
326
327         mp = ( monitor_entry_t * )e_parent->e_private;
328         rc = -1;
329         if ( mp->mp_flags & MONITOR_F_VOLATILE_CH ) {
330                 /* parent entry generates volatile children */
331                 rc = monitor_entry_create( op, rs, ndn, e_parent, ep );
332         }
333
334         if ( !rc ) {
335                 monitor_cache_lock( *ep );
336                 monitor_cache_release( mi, e_parent );
337
338         } else {
339                 *matched = e_parent;
340         }
341         
342         return( rc );
343 }
344
345 /*
346  * releases the lock of the entry; if it is marked as volatile, it is
347  * destroyed.
348  */
349 int
350 monitor_cache_release(
351         monitor_info_t  *mi,
352         Entry           *e )
353 {
354         monitor_entry_t *mp;
355
356         assert( mi != NULL );
357         assert( e != NULL );
358         assert( e->e_private != NULL );
359         
360         mp = ( monitor_entry_t * )e->e_private;
361
362         if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
363                 monitor_cache_t *mc, tmp_mc;
364
365                 /* volatile entries do not return to cache */
366                 ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
367                 tmp_mc.mc_ndn = e->e_nname;
368                 mc = avl_delete( &mi->mi_cache,
369                                 ( caddr_t )&tmp_mc, monitor_cache_cmp );
370                 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
371                 if ( mc != NULL ) {
372                         ch_free( mc );
373                 }
374                 
375                 ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
376                 ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
377                 ch_free( mp );
378                 e->e_private = NULL;
379                 entry_free( e );
380
381                 return( 0 );
382         }
383         
384         ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
385
386         return( 0 );
387 }
388
389 static void
390 monitor_entry_destroy( void *v_mc )
391 {
392         monitor_cache_t         *mc = (monitor_cache_t *)v_mc;
393
394         if ( mc->mc_e != NULL ) {
395                 monitor_entry_t *mp;
396
397                 assert( mc->mc_e->e_private != NULL );
398         
399                 mp = ( monitor_entry_t * )mc->mc_e->e_private;
400
401                 if ( mp->mp_cb ) {
402                         monitor_callback_t      *cb;
403
404                         for ( cb = mp->mp_cb; cb != NULL; ) {
405                                 monitor_callback_t      *next = cb->mc_next;
406
407                                 if ( cb->mc_free ) {
408                                         (void)cb->mc_free( mc->mc_e, &cb->mc_private );
409                                 }
410                                 ch_free( mp->mp_cb );
411
412                                 cb = next;
413                         }
414                 }
415
416                 ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
417
418                 ch_free( mp );
419                 mc->mc_e->e_private = NULL;
420                 entry_free( mc->mc_e );
421         }
422
423         ch_free( mc );
424 }
425
426 int
427 monitor_cache_destroy(
428         monitor_info_t  *mi )
429 {
430         if ( mi->mi_cache ) {
431                 avl_free( mi->mi_cache, monitor_entry_destroy );
432         }
433
434         return 0;
435 }
436