2001-07-15 01:34:24 +08:00
|
|
|
/* cache.c - routines to maintain an in-core cache of entries */
|
2003-11-27 10:35:20 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2005-01-02 04:49:32 +08:00
|
|
|
* Copyright 2001-2005 The OpenLDAP Foundation.
|
2003-12-09 01:41:40 +08:00
|
|
|
* Portions Copyright 2001-2003 Pierangelo Masarati.
|
2003-11-27 10:35:20 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was initially developed by Pierangelo Masarati for inclusion
|
|
|
|
* in OpenLDAP Software.
|
|
|
|
*/
|
2001-07-15 01:34:24 +08:00
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2004-09-13 06:12:58 +08:00
|
|
|
#include "ac/string.h"
|
2001-07-15 01:34:24 +08:00
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
|
|
|
|
#include "back-monitor.h"
|
|
|
|
|
2004-11-09 02:52:27 +08:00
|
|
|
/*
|
|
|
|
* The cache maps DNs to Entries.
|
|
|
|
* Each entry, on turn, holds the list of its children in the e_private field.
|
|
|
|
* This is used by search operation to perform onelevel and subtree candidate
|
|
|
|
* selection.
|
|
|
|
*/
|
|
|
|
typedef struct monitor_cache_t {
|
|
|
|
struct berval mc_ndn;
|
|
|
|
Entry *mc_e;
|
|
|
|
} monitor_cache_t;
|
|
|
|
|
2001-07-15 01:34:24 +08:00
|
|
|
/*
|
|
|
|
* compares entries based on the dn
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
monitor_cache_cmp(
|
2005-07-18 06:19:20 +08:00
|
|
|
const void *c1,
|
|
|
|
const void *c2 )
|
2001-07-15 01:34:24 +08:00
|
|
|
{
|
2004-11-09 02:52:27 +08:00
|
|
|
monitor_cache_t *cc1 = ( monitor_cache_t * )c1;
|
|
|
|
monitor_cache_t *cc2 = ( monitor_cache_t * )c2;
|
2001-12-27 23:16:12 +08:00
|
|
|
|
2001-07-15 01:34:24 +08:00
|
|
|
/*
|
|
|
|
* case sensitive, because the dn MUST be normalized
|
|
|
|
*/
|
2002-04-02 19:51:05 +08:00
|
|
|
return ber_bvcmp( &cc1->mc_ndn, &cc2->mc_ndn );
|
2001-07-15 01:34:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* checks for duplicate entries
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
monitor_cache_dup(
|
2005-07-18 06:19:20 +08:00
|
|
|
void *c1,
|
|
|
|
void *c2 )
|
2001-07-15 01:34:24 +08:00
|
|
|
{
|
2004-11-09 02:52:27 +08:00
|
|
|
monitor_cache_t *cc1 = ( monitor_cache_t * )c1;
|
|
|
|
monitor_cache_t *cc2 = ( monitor_cache_t * )c2;
|
2001-12-27 23:16:12 +08:00
|
|
|
|
2001-07-15 01:34:24 +08:00
|
|
|
/*
|
|
|
|
* case sensitive, because the dn MUST be normalized
|
|
|
|
*/
|
2002-04-02 19:51:05 +08:00
|
|
|
return ber_bvcmp( &cc1->mc_ndn, &cc2->mc_ndn ) == 0 ? -1 : 0;
|
2001-07-15 01:34:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* adds an entry to the cache and inits the mutex
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
monitor_cache_add(
|
2005-07-18 06:19:20 +08:00
|
|
|
monitor_info_t *mi,
|
|
|
|
Entry *e )
|
2001-07-15 01:34:24 +08:00
|
|
|
{
|
2004-11-09 02:52:27 +08:00
|
|
|
monitor_cache_t *mc;
|
|
|
|
monitor_entry_t *mp;
|
|
|
|
int rc;
|
2001-07-15 01:34:24 +08:00
|
|
|
|
|
|
|
assert( mi != NULL );
|
|
|
|
assert( e != NULL );
|
|
|
|
|
2004-11-09 02:52:27 +08:00
|
|
|
mp = ( monitor_entry_t *)e->e_private;
|
2001-07-15 01:34:24 +08:00
|
|
|
ldap_pvt_thread_mutex_init( &mp->mp_mutex );
|
|
|
|
|
2004-11-09 02:52:27 +08:00
|
|
|
mc = ( monitor_cache_t * )ch_malloc( sizeof( monitor_cache_t ) );
|
2002-04-02 19:51:05 +08:00
|
|
|
mc->mc_ndn = e->e_nname;
|
2001-07-15 01:34:24 +08:00
|
|
|
mc->mc_e = e;
|
|
|
|
ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
|
|
|
|
rc = avl_insert( &mi->mi_cache, ( caddr_t )mc,
|
|
|
|
monitor_cache_cmp, monitor_cache_dup );
|
|
|
|
ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* locks the entry (no r/w)
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
monitor_cache_lock(
|
2005-07-18 06:19:20 +08:00
|
|
|
Entry *e )
|
2001-07-15 01:34:24 +08:00
|
|
|
{
|
2005-07-18 06:19:20 +08:00
|
|
|
monitor_entry_t *mp;
|
2001-07-15 01:34:24 +08:00
|
|
|
|
2005-07-18 06:19:20 +08:00
|
|
|
assert( e != NULL );
|
|
|
|
assert( e->e_private != NULL );
|
2001-07-15 01:34:24 +08:00
|
|
|
|
2005-07-18 06:19:20 +08:00
|
|
|
mp = ( monitor_entry_t * )e->e_private;
|
|
|
|
ldap_pvt_thread_mutex_lock( &mp->mp_mutex );
|
2001-07-15 01:34:24 +08:00
|
|
|
|
2005-07-18 06:19:20 +08:00
|
|
|
return( 0 );
|
2001-07-15 01:34:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* gets an entry from the cache based on the normalized dn
|
|
|
|
* with mutex locked
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
monitor_cache_get(
|
2005-07-18 06:19:20 +08:00
|
|
|
monitor_info_t *mi,
|
|
|
|
struct berval *ndn,
|
|
|
|
Entry **ep )
|
2001-07-15 01:34:24 +08:00
|
|
|
{
|
2004-11-09 02:52:27 +08:00
|
|
|
monitor_cache_t tmp_mc, *mc;
|
2001-07-15 01:34:24 +08:00
|
|
|
|
|
|
|
assert( mi != NULL );
|
|
|
|
assert( ndn != NULL );
|
|
|
|
assert( ep != NULL );
|
|
|
|
|
2005-08-07 05:03:26 +08:00
|
|
|
*ep = NULL;
|
|
|
|
|
2002-04-02 19:51:05 +08:00
|
|
|
tmp_mc.mc_ndn = *ndn;
|
2001-07-15 01:34:24 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
|
2004-11-09 02:52:27 +08:00
|
|
|
mc = ( monitor_cache_t * )avl_find( mi->mi_cache,
|
2001-07-15 01:34:24 +08:00
|
|
|
( caddr_t )&tmp_mc, monitor_cache_cmp );
|
|
|
|
|
|
|
|
if ( mc != NULL ) {
|
|
|
|
/* entry is returned with mutex locked */
|
|
|
|
monitor_cache_lock( mc->mc_e );
|
|
|
|
*ep = mc->mc_e;
|
|
|
|
}
|
2005-08-07 05:03:26 +08:00
|
|
|
|
2001-07-15 01:34:24 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
|
|
|
|
|
2005-08-07 05:03:26 +08:00
|
|
|
return ( *ep == NULL ? -1 : 0 );
|
2001-07-15 01:34:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the entry exists in cache, it is returned in locked status;
|
|
|
|
* otherwise, if the parent exists, if it may generate volatile
|
|
|
|
* descendants an attempt to generate the required entry is
|
|
|
|
* performed and, if successful, the entry is returned
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
monitor_cache_dn2entry(
|
2005-07-18 06:19:20 +08:00
|
|
|
Operation *op,
|
|
|
|
SlapReply *rs,
|
|
|
|
struct berval *ndn,
|
|
|
|
Entry **ep,
|
|
|
|
Entry **matched )
|
2001-07-15 01:34:24 +08:00
|
|
|
{
|
2004-11-09 02:52:27 +08:00
|
|
|
monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
|
2003-04-12 09:17:05 +08:00
|
|
|
int rc;
|
2004-09-13 06:12:58 +08:00
|
|
|
struct berval p_ndn = BER_BVNULL;
|
2001-12-27 23:16:12 +08:00
|
|
|
Entry *e_parent;
|
2004-11-09 02:52:27 +08:00
|
|
|
monitor_entry_t *mp;
|
2001-07-15 01:34:24 +08:00
|
|
|
|
|
|
|
assert( mi != NULL );
|
|
|
|
assert( ndn != NULL );
|
|
|
|
assert( ep != NULL );
|
2001-12-10 17:50:06 +08:00
|
|
|
assert( matched != NULL );
|
|
|
|
|
|
|
|
*matched = NULL;
|
2001-07-15 01:34:24 +08:00
|
|
|
|
2004-11-12 20:51:53 +08:00
|
|
|
if ( !dnIsSuffix( ndn, &op->o_bd->be_nsuffix[ 0 ] ) ) {
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
2001-07-15 01:34:24 +08:00
|
|
|
rc = monitor_cache_get( mi, ndn, ep );
|
|
|
|
if ( !rc && *ep != NULL ) {
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try with parent/ancestors */
|
2004-09-13 06:12:58 +08:00
|
|
|
if ( BER_BVISNULL( ndn ) ) {
|
|
|
|
BER_BVSTR( &p_ndn, "" );
|
2001-12-28 23:58:51 +08:00
|
|
|
|
2001-12-27 23:16:12 +08:00
|
|
|
} else {
|
2004-09-13 06:12:58 +08:00
|
|
|
dnParent( ndn, &p_ndn );
|
2001-12-27 23:16:12 +08:00
|
|
|
}
|
2001-12-28 23:58:51 +08:00
|
|
|
|
2005-04-28 05:30:35 +08:00
|
|
|
rc = monitor_cache_dn2entry( op, rs, &p_ndn, &e_parent, matched );
|
2004-09-13 06:12:58 +08:00
|
|
|
if ( rc || e_parent == NULL ) {
|
2001-07-15 01:34:24 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
2004-11-09 02:52:27 +08:00
|
|
|
mp = ( monitor_entry_t * )e_parent->e_private;
|
2001-07-15 01:34:24 +08:00
|
|
|
rc = -1;
|
|
|
|
if ( mp->mp_flags & MONITOR_F_VOLATILE_CH ) {
|
|
|
|
/* parent entry generates volatile children */
|
2005-04-28 05:30:35 +08:00
|
|
|
rc = monitor_entry_create( op, rs, ndn, e_parent, ep );
|
2001-07-15 01:34:24 +08:00
|
|
|
}
|
2001-12-10 17:50:06 +08:00
|
|
|
|
|
|
|
if ( !rc ) {
|
2005-07-18 06:19:20 +08:00
|
|
|
monitor_cache_lock( *ep );
|
2001-12-10 17:50:06 +08:00
|
|
|
monitor_cache_release( mi, e_parent );
|
2005-07-18 06:19:20 +08:00
|
|
|
|
2001-12-10 17:50:06 +08:00
|
|
|
} else {
|
|
|
|
*matched = e_parent;
|
|
|
|
}
|
2001-07-15 01:34:24 +08:00
|
|
|
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* releases the lock of the entry; if it is marked as volatile, it is
|
|
|
|
* destroyed.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
monitor_cache_release(
|
2004-11-09 02:52:27 +08:00
|
|
|
monitor_info_t *mi,
|
2005-07-18 06:19:20 +08:00
|
|
|
Entry *e )
|
2001-07-15 01:34:24 +08:00
|
|
|
{
|
2004-11-09 02:52:27 +08:00
|
|
|
monitor_entry_t *mp;
|
2001-07-15 01:34:24 +08:00
|
|
|
|
|
|
|
assert( mi != NULL );
|
|
|
|
assert( e != NULL );
|
|
|
|
assert( e->e_private != NULL );
|
|
|
|
|
2004-11-09 02:52:27 +08:00
|
|
|
mp = ( monitor_entry_t * )e->e_private;
|
2001-07-15 01:34:24 +08:00
|
|
|
|
|
|
|
if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
|
2004-11-09 02:52:27 +08:00
|
|
|
monitor_cache_t *mc, tmp_mc;
|
2001-12-28 23:58:51 +08:00
|
|
|
|
2001-07-15 01:34:24 +08:00
|
|
|
/* volatile entries do not return to cache */
|
2001-12-28 23:58:51 +08:00
|
|
|
ldap_pvt_thread_mutex_lock( &mi->mi_cache_mutex );
|
2002-04-02 19:51:05 +08:00
|
|
|
tmp_mc.mc_ndn = e->e_nname;
|
2001-12-28 23:58:51 +08:00
|
|
|
mc = avl_delete( &mi->mi_cache,
|
|
|
|
( caddr_t )&tmp_mc, monitor_cache_cmp );
|
|
|
|
ldap_pvt_thread_mutex_unlock( &mi->mi_cache_mutex );
|
2005-07-18 06:19:20 +08:00
|
|
|
if ( mc != NULL ) {
|
|
|
|
ch_free( mc );
|
|
|
|
}
|
2001-12-28 23:58:51 +08:00
|
|
|
|
2002-04-02 20:27:06 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
|
2001-07-15 01:34:24 +08:00
|
|
|
ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
|
|
|
|
ch_free( mp );
|
|
|
|
e->e_private = NULL;
|
|
|
|
entry_free( e );
|
2001-12-28 23:58:51 +08:00
|
|
|
|
2001-07-15 01:34:24 +08:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
ldap_pvt_thread_mutex_unlock( &mp->mp_mutex );
|
2001-12-28 23:58:51 +08:00
|
|
|
|
2001-07-15 01:34:24 +08:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2005-07-20 22:59:56 +08:00
|
|
|
static void
|
|
|
|
monitor_entry_destroy( void *v_mc )
|
|
|
|
{
|
|
|
|
monitor_cache_t *mc = (monitor_cache_t *)v_mc;
|
|
|
|
|
|
|
|
if ( mc->mc_e != NULL ) {
|
|
|
|
monitor_entry_t *mp;
|
|
|
|
|
|
|
|
assert( mc->mc_e->e_private != NULL );
|
|
|
|
|
|
|
|
mp = ( monitor_entry_t * )mc->mc_e->e_private;
|
|
|
|
|
|
|
|
if ( mp->mp_cb ) {
|
2005-07-26 04:47:39 +08:00
|
|
|
if ( mp->mp_cb->mc_free ) {
|
|
|
|
mp->mp_cb->mc_free( mc->mc_e,
|
|
|
|
mp->mp_cb->mc_private );
|
|
|
|
}
|
|
|
|
ch_free( mp->mp_cb );
|
2005-07-20 22:59:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ldap_pvt_thread_mutex_destroy( &mp->mp_mutex );
|
|
|
|
|
|
|
|
ch_free( mp );
|
|
|
|
mc->mc_e->e_private = NULL;
|
|
|
|
entry_free( mc->mc_e );
|
|
|
|
}
|
|
|
|
|
|
|
|
ch_free( mc );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
monitor_cache_destroy(
|
|
|
|
monitor_info_t *mi )
|
|
|
|
{
|
|
|
|
if ( mi->mi_cache ) {
|
|
|
|
avl_free( mi->mi_cache, monitor_entry_destroy );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|