1 /* cache.c - routines to maintain an in-core cache of entries */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2006 The OpenLDAP Foundation.
6 * Portions Copyright 2001-2003 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Pierangelo Masarati for inclusion
19 * in OpenLDAP Software.
25 #include "ac/string.h"
29 #include "back-monitor.h"
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
37 typedef struct monitor_cache_t {
43 * compares entries based on the dn
50 monitor_cache_t *cc1 = ( monitor_cache_t * )c1;
51 monitor_cache_t *cc2 = ( monitor_cache_t * )c2;
54 * case sensitive, because the dn MUST be normalized
56 return ber_bvcmp( &cc1->mc_ndn, &cc2->mc_ndn );
60 * checks for duplicate entries
67 monitor_cache_t *cc1 = ( monitor_cache_t * )c1;
68 monitor_cache_t *cc2 = ( monitor_cache_t * )c2;
71 * case sensitive, because the dn MUST be normalized
73 return ber_bvcmp( &cc1->mc_ndn, &cc2->mc_ndn ) == 0 ? -1 : 0;
77 * adds an entry to the cache and inits the mutex
91 mp = ( monitor_entry_t *)e->e_private;
93 mc = ( monitor_cache_t * )ch_malloc( sizeof( monitor_cache_t ) );
94 mc->mc_ndn = e->e_nname;
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 );
105 * locks the entry (no r/w)
114 assert( e->e_private != NULL );
116 mp = ( monitor_entry_t * )e->e_private;
117 ldap_pvt_thread_mutex_lock( &mp->mp_mutex );
123 * tries to lock the entry (no r/w)
126 monitor_cache_trylock(
132 assert( e->e_private != NULL );
134 mp = ( monitor_entry_t * )e->e_private;
135 return ldap_pvt_thread_mutex_trylock( &mp->mp_mutex );
139 * gets an entry from the cache based on the normalized dn
148 monitor_cache_t tmp_mc, *mc;
150 assert( mi != NULL );
151 assert( ndn != NULL );
152 assert( ep != NULL );
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 );
162 /* entry is returned with mutex locked */
163 monitor_cache_lock( mc->mc_e );
167 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
169 return ( *ep == NULL ? -1 : 0 );
173 * gets an entry from the cache based on the normalized dn
177 monitor_cache_remove(
182 monitor_cache_t tmp_mc, *mc;
185 assert( mi != NULL );
186 assert( ndn != NULL );
187 assert( ep != NULL );
191 dnParent( ndn, &pndn );
194 ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
196 tmp_mc.mc_ndn = *ndn;
197 mc = ( monitor_cache_t * )avl_find( mi->mi_cache,
198 ( caddr_t )&tmp_mc, monitor_cache_cmp );
201 monitor_cache_t *pmc;
203 if ( monitor_cache_trylock( mc->mc_e ) ) {
204 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
208 tmp_mc.mc_ndn = pndn;
209 pmc = ( monitor_cache_t * )avl_find( mi->mi_cache,
210 ( caddr_t )&tmp_mc, monitor_cache_cmp );
212 monitor_entry_t *mp = (monitor_entry_t *)mc->mc_e->e_private,
213 *pmp = (monitor_entry_t *)pmc->mc_e->e_private;
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 );
222 for ( entryp = &pmp->mp_children; *entryp != NULL; ) {
223 monitor_entry_t *next = (monitor_entry_t *)(*entryp)->e_private;
225 *entryp = next->mp_next;
230 entryp = &next->mp_next;
233 if ( entryp != NULL ) {
234 Debug( LDAP_DEBUG_ANY,
235 "monitor_cache_remove(\"%s\"): "
236 "not in parent's list\n",
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 );
245 if ( entryp == NULL ) {
246 monitor_cache_t *tmpmc;
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 );
257 /* NOTE: we destroy the mutex, but otherwise
258 * leave the private data around; specifically,
259 * callbacks need be freed by someone else */
261 ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
263 mp->mp_children = NULL;
269 monitor_cache_release( mi, mc->mc_e );
273 ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
275 return ( *ep == NULL ? -1 : 0 );
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
285 monitor_cache_dn2entry(
292 monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
294 struct berval p_ndn = BER_BVNULL;
298 assert( mi != NULL );
299 assert( ndn != NULL );
300 assert( ep != NULL );
301 assert( matched != NULL );
305 if ( !dnIsSuffix( ndn, &op->o_bd->be_nsuffix[ 0 ] ) ) {
309 rc = monitor_cache_get( mi, ndn, ep );
310 if ( !rc && *ep != NULL ) {
314 /* try with parent/ancestors */
315 if ( BER_BVISNULL( ndn ) ) {
316 BER_BVSTR( &p_ndn, "" );
319 dnParent( ndn, &p_ndn );
322 rc = monitor_cache_dn2entry( op, rs, &p_ndn, &e_parent, matched );
323 if ( rc || e_parent == NULL ) {
327 mp = ( monitor_entry_t * )e_parent->e_private;
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 );
335 monitor_cache_lock( *ep );
336 monitor_cache_release( mi, e_parent );
346 * releases the lock of the entry; if it is marked as volatile, it is
350 monitor_cache_release(
356 assert( mi != NULL );
358 assert( e->e_private != NULL );
360 mp = ( monitor_entry_t * )e->e_private;
362 if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
363 monitor_cache_t *mc, tmp_mc;
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 );
375 ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
376 ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
384 ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
390 monitor_entry_destroy( void *v_mc )
392 monitor_cache_t *mc = (monitor_cache_t *)v_mc;
394 if ( mc->mc_e != NULL ) {
397 assert( mc->mc_e->e_private != NULL );
399 mp = ( monitor_entry_t * )mc->mc_e->e_private;
402 monitor_callback_t *cb;
404 for ( cb = mp->mp_cb; cb != NULL; ) {
405 monitor_callback_t *next = cb->mc_next;
408 (void)cb->mc_free( mc->mc_e, &cb->mc_private );
410 ch_free( mp->mp_cb );
416 ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
419 mc->mc_e->e_private = NULL;
420 entry_free( mc->mc_e );
427 monitor_cache_destroy(
430 if ( mi->mi_cache ) {
431 avl_free( mi->mi_cache, monitor_entry_destroy );