| 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-2024 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 |
Entry *parent ) |
| 84 |
{ |
| 85 |
monitor_cache_t tmp_mc, *mc, *pmc = NULL; |
| 86 |
Entry **ep = NULL, *prev = NULL; |
| 87 |
int rc = -1; |
| 88 |
|
| 89 |
assert( mi != NULL ); |
| 90 |
assert( e != NULL ); |
| 91 |
|
| 92 |
dnParent( &e->e_nname, &tmp_mc.mc_ndn ); |
| 93 |
|
| 94 |
mc = ( monitor_cache_t * )ch_malloc( sizeof( monitor_cache_t ) ); |
| 95 |
mc->mc_ndn = e->e_nname; |
| 96 |
mc->mc_e = e; |
| 97 |
|
| 98 |
if ( parent ) { |
| 99 |
/* Shortcut, but follow lock order as a fallback */ |
| 100 |
if ( ldap_pvt_thread_mutex_trylock( &mi->mi_cache_lock ) ) { |
| 101 |
monitor_cache_release( mi, parent ); |
| 102 |
ldap_pvt_thread_mutex_lock( &mi->mi_cache_lock ); |
| 103 |
monitor_cache_lock( parent ); |
| 104 |
} |
| 105 |
} else { |
| 106 |
ldap_pvt_thread_mutex_lock( &mi->mi_cache_lock ); |
| 107 |
} |
| 108 |
|
| 109 |
/* Allow database root be added */ |
| 110 |
if ( parent == NULL && mi->mi_cache != NULL ) { |
| 111 |
pmc = ldap_avl_find( mi->mi_cache, &tmp_mc, monitor_cache_cmp ); |
| 112 |
if ( pmc == NULL ) { |
| 113 |
goto done; |
| 114 |
} |
| 115 |
parent = pmc->mc_e; |
| 116 |
monitor_cache_lock( parent ); |
| 117 |
} |
| 118 |
|
| 119 |
rc = ldap_avl_insert( &mi->mi_cache, mc, |
| 120 |
monitor_cache_cmp, monitor_cache_dup ); |
| 121 |
if ( rc != LDAP_SUCCESS ) { |
| 122 |
goto done; |
| 123 |
} |
| 124 |
|
| 125 |
if ( parent != NULL ) { |
| 126 |
monitor_entry_t *mp = parent->e_private; |
| 127 |
|
| 128 |
if ( mp->mp_children ) { |
| 129 |
monitor_entry_t *tail; |
| 130 |
|
| 131 |
monitor_cache_lock( mp->mp_last ); |
| 132 |
tail = mp->mp_last->e_private; |
| 133 |
tail->mp_next = e; |
| 134 |
monitor_cache_release( mi, mp->mp_last ); |
| 135 |
mp->mp_last = e; |
| 136 |
} else { |
| 137 |
mp->mp_children = mp->mp_last = e; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
done: |
| 142 |
if ( pmc != NULL ) { |
| 143 |
monitor_cache_release( mi, parent ); |
| 144 |
} |
| 145 |
ldap_pvt_thread_mutex_unlock( &mi->mi_cache_lock ); |
| 146 |
|
| 147 |
if ( rc != LDAP_SUCCESS ) { |
| 148 |
ch_free( mc ); |
| 149 |
} |
| 150 |
return rc; |
| 151 |
} |
| 152 |
|
| 153 |
/* |
| 154 |
* locks the entry (no r/w) |
| 155 |
*/ |
| 156 |
int |
| 157 |
monitor_cache_lock( |
| 158 |
Entry *e ) |
| 159 |
{ |
| 160 |
monitor_entry_t *mp; |
| 161 |
|
| 162 |
assert( e != NULL ); |
| 163 |
assert( e->e_private != NULL ); |
| 164 |
|
| 165 |
mp = ( monitor_entry_t * )e->e_private; |
| 166 |
ldap_pvt_thread_mutex_lock( &mp->mp_mutex ); |
| 167 |
|
| 168 |
return( 0 ); |
| 169 |
} |
| 170 |
|
| 171 |
/* |
| 172 |
* tries to lock the entry (no r/w) |
| 173 |
*/ |
| 174 |
int |
| 175 |
monitor_cache_trylock( |
| 176 |
Entry *e ) |
| 177 |
{ |
| 178 |
monitor_entry_t *mp; |
| 179 |
|
| 180 |
assert( e != NULL ); |
| 181 |
assert( e->e_private != NULL ); |
| 182 |
|
| 183 |
mp = ( monitor_entry_t * )e->e_private; |
| 184 |
return ldap_pvt_thread_mutex_trylock( &mp->mp_mutex ); |
| 185 |
} |
| 186 |
|
| 187 |
/* |
| 188 |
* gets an entry from the cache based on the normalized dn |
| 189 |
* with mutex locked |
| 190 |
*/ |
| 191 |
int |
| 192 |
monitor_cache_get( |
| 193 |
monitor_info_t *mi, |
| 194 |
struct berval *ndn, |
| 195 |
Entry **ep ) |
| 196 |
{ |
| 197 |
monitor_cache_t tmp_mc, *mc; |
| 198 |
|
| 199 |
assert( mi != NULL ); |
| 200 |
assert( ndn != NULL ); |
| 201 |
assert( ep != NULL ); |
| 202 |
|
| 203 |
*ep = NULL; |
| 204 |
|
| 205 |
tmp_mc.mc_ndn = *ndn; |
| 206 |
|
| 207 |
ldap_pvt_thread_mutex_lock( &mi->mi_cache_lock ); |
| 208 |
mc = ( monitor_cache_t * )ldap_avl_find( mi->mi_cache, |
| 209 |
( caddr_t )&tmp_mc, monitor_cache_cmp ); |
| 210 |
|
| 211 |
if ( mc != NULL ) { |
| 212 |
/* entry is returned with mutex locked */ |
| 213 |
monitor_cache_lock( mc->mc_e ); |
| 214 |
*ep = mc->mc_e; |
| 215 |
} |
| 216 |
|
| 217 |
ldap_pvt_thread_mutex_unlock( &mi->mi_cache_lock ); |
| 218 |
|
| 219 |
return ( *ep == NULL ? -1 : 0 ); |
| 220 |
} |
| 221 |
|
| 222 |
/* |
| 223 |
* gets an entry from the cache based on the normalized dn |
| 224 |
* with mutex locked |
| 225 |
*/ |
| 226 |
int |
| 227 |
monitor_cache_remove( |
| 228 |
monitor_info_t *mi, |
| 229 |
struct berval *ndn, |
| 230 |
Entry **ep ) |
| 231 |
{ |
| 232 |
monitor_cache_t tmp_mc, *mc; |
| 233 |
struct berval pndn; |
| 234 |
|
| 235 |
assert( mi != NULL ); |
| 236 |
assert( ndn != NULL ); |
| 237 |
assert( ep != NULL ); |
| 238 |
|
| 239 |
*ep = NULL; |
| 240 |
|
| 241 |
dnParent( ndn, &pndn ); |
| 242 |
|
| 243 |
retry:; |
| 244 |
ldap_pvt_thread_mutex_lock( &mi->mi_cache_lock ); |
| 245 |
|
| 246 |
tmp_mc.mc_ndn = *ndn; |
| 247 |
mc = ( monitor_cache_t * )ldap_avl_find( mi->mi_cache, |
| 248 |
( caddr_t )&tmp_mc, monitor_cache_cmp ); |
| 249 |
|
| 250 |
if ( mc != NULL ) { |
| 251 |
monitor_cache_t *pmc; |
| 252 |
|
| 253 |
tmp_mc.mc_ndn = pndn; |
| 254 |
pmc = ( monitor_cache_t * )ldap_avl_find( mi->mi_cache, |
| 255 |
( caddr_t )&tmp_mc, monitor_cache_cmp ); |
| 256 |
if ( pmc != NULL ) { |
| 257 |
monitor_entry_t *mp = (monitor_entry_t *)mc->mc_e->e_private, |
| 258 |
*pmp = (monitor_entry_t *)pmc->mc_e->e_private; |
| 259 |
Entry **entryp, *prev = NULL; |
| 260 |
|
| 261 |
monitor_cache_lock( pmc->mc_e ); |
| 262 |
|
| 263 |
for ( entryp = &pmp->mp_children; *entryp != NULL; ) { |
| 264 |
monitor_entry_t *next = (monitor_entry_t *)(*entryp)->e_private; |
| 265 |
|
| 266 |
monitor_cache_lock( *entryp ); |
| 267 |
if ( next == mp ) { |
| 268 |
if ( mc->mc_e == pmp->mp_last ) { |
| 269 |
pmp->mp_last = prev; |
| 270 |
} |
| 271 |
*entryp = next->mp_next; |
| 272 |
entryp = NULL; |
| 273 |
break; |
| 274 |
} |
| 275 |
|
| 276 |
if ( prev != NULL ) { |
| 277 |
monitor_cache_release( mi, prev ); |
| 278 |
} |
| 279 |
prev = *entryp; |
| 280 |
entryp = &next->mp_next; |
| 281 |
} |
| 282 |
if ( prev ) { |
| 283 |
monitor_cache_release( mi, prev ); |
| 284 |
} |
| 285 |
|
| 286 |
if ( entryp != NULL ) { |
| 287 |
Debug( LDAP_DEBUG_ANY, |
| 288 |
"monitor_cache_remove(\"%s\"): " |
| 289 |
"not in parent's list\n", |
| 290 |
ndn->bv_val ); |
| 291 |
} |
| 292 |
|
| 293 |
/* either succeeded, and the entry is no longer |
| 294 |
* in its parent's list, or failed, and the |
| 295 |
* entry is neither mucked with nor returned */ |
| 296 |
monitor_cache_release( mi, pmc->mc_e ); |
| 297 |
|
| 298 |
if ( entryp == NULL ) { |
| 299 |
monitor_cache_t *tmpmc; |
| 300 |
|
| 301 |
tmp_mc.mc_ndn = *ndn; |
| 302 |
tmpmc = ldap_avl_delete( &mi->mi_cache, |
| 303 |
( caddr_t )&tmp_mc, monitor_cache_cmp ); |
| 304 |
assert( tmpmc == mc ); |
| 305 |
|
| 306 |
*ep = mc->mc_e; |
| 307 |
ch_free( mc ); |
| 308 |
mc = NULL; |
| 309 |
|
| 310 |
/* NOTE: we destroy the mutex, but otherwise |
| 311 |
* leave the private data around; specifically, |
| 312 |
* callbacks need be freed by someone else */ |
| 313 |
|
| 314 |
ldap_pvt_thread_mutex_destroy( &mp->mp_mutex ); |
| 315 |
mp->mp_next = NULL; |
| 316 |
mp->mp_children = NULL; |
| 317 |
mp->mp_last = NULL; |
| 318 |
} |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
if ( mc ) { |
| 323 |
monitor_cache_release( mi, mc->mc_e ); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
ldap_pvt_thread_mutex_unlock( &mi->mi_cache_lock ); |
| 328 |
|
| 329 |
return ( *ep == NULL ? -1 : 0 ); |
| 330 |
} |
| 331 |
|
| 332 |
/* |
| 333 |
* If the entry exists in cache, it is returned in locked status; |
| 334 |
* otherwise, if the parent exists, if it may generate volatile |
| 335 |
* descendants an attempt to generate the required entry is |
| 336 |
* performed and, if successful, the entry is returned |
| 337 |
*/ |
| 338 |
int |
| 339 |
monitor_cache_dn2entry( |
| 340 |
Operation *op, |
| 341 |
SlapReply *rs, |
| 342 |
struct berval *ndn, |
| 343 |
Entry **ep, |
| 344 |
Entry **matched ) |
| 345 |
{ |
| 346 |
monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private; |
| 347 |
int rc; |
| 348 |
struct berval p_ndn = BER_BVNULL; |
| 349 |
Entry *e_parent; |
| 350 |
monitor_entry_t *mp; |
| 351 |
|
| 352 |
assert( mi != NULL ); |
| 353 |
assert( ndn != NULL ); |
| 354 |
assert( ep != NULL ); |
| 355 |
assert( matched != NULL ); |
| 356 |
|
| 357 |
*matched = NULL; |
| 358 |
|
| 359 |
if ( !dnIsSuffix( ndn, &op->o_bd->be_nsuffix[ 0 ] ) ) { |
| 360 |
return( -1 ); |
| 361 |
} |
| 362 |
|
| 363 |
rc = monitor_cache_get( mi, ndn, ep ); |
| 364 |
if ( !rc && *ep != NULL ) { |
| 365 |
return( 0 ); |
| 366 |
} |
| 367 |
|
| 368 |
/* try with parent/ancestors */ |
| 369 |
if ( BER_BVISNULL( ndn ) ) { |
| 370 |
BER_BVSTR( &p_ndn, "" ); |
| 371 |
|
| 372 |
} else { |
| 373 |
dnParent( ndn, &p_ndn ); |
| 374 |
} |
| 375 |
|
| 376 |
rc = monitor_cache_dn2entry( op, rs, &p_ndn, &e_parent, matched ); |
| 377 |
if ( rc || e_parent == NULL ) { |
| 378 |
return( -1 ); |
| 379 |
} |
| 380 |
|
| 381 |
mp = ( monitor_entry_t * )e_parent->e_private; |
| 382 |
rc = -1; |
| 383 |
if ( mp->mp_flags & MONITOR_F_VOLATILE_CH ) { |
| 384 |
/* parent entry generates volatile children */ |
| 385 |
rc = monitor_entry_create( op, rs, ndn, e_parent, ep ); |
| 386 |
} |
| 387 |
|
| 388 |
if ( !rc ) { |
| 389 |
monitor_cache_lock( *ep ); |
| 390 |
monitor_cache_release( mi, e_parent ); |
| 391 |
|
| 392 |
} else { |
| 393 |
*matched = e_parent; |
| 394 |
} |
| 395 |
|
| 396 |
return( rc ); |
| 397 |
} |
| 398 |
|
| 399 |
/* |
| 400 |
* releases the lock of the entry; if it is marked as volatile, it is |
| 401 |
* destroyed. |
| 402 |
*/ |
| 403 |
int |
| 404 |
monitor_cache_release( |
| 405 |
monitor_info_t *mi, |
| 406 |
Entry *e ) |
| 407 |
{ |
| 408 |
monitor_entry_t *mp; |
| 409 |
|
| 410 |
assert( mi != NULL ); |
| 411 |
assert( e != NULL ); |
| 412 |
assert( e->e_private != NULL ); |
| 413 |
|
| 414 |
mp = ( monitor_entry_t * )e->e_private; |
| 415 |
|
| 416 |
if ( mp->mp_flags & MONITOR_F_VOLATILE ) { |
| 417 |
ldap_pvt_thread_mutex_unlock( &mp->mp_mutex ); |
| 418 |
ldap_pvt_thread_mutex_destroy( &mp->mp_mutex ); |
| 419 |
ch_free( mp ); |
| 420 |
e->e_private = NULL; |
| 421 |
entry_free( e ); |
| 422 |
|
| 423 |
return( 0 ); |
| 424 |
} |
| 425 |
|
| 426 |
ldap_pvt_thread_mutex_unlock( &mp->mp_mutex ); |
| 427 |
|
| 428 |
return( 0 ); |
| 429 |
} |
| 430 |
|
| 431 |
static void |
| 432 |
monitor_entry_destroy( void *v_mc ) |
| 433 |
{ |
| 434 |
monitor_cache_t *mc = (monitor_cache_t *)v_mc; |
| 435 |
|
| 436 |
if ( mc->mc_e != NULL ) { |
| 437 |
monitor_entry_t *mp; |
| 438 |
|
| 439 |
assert( mc->mc_e->e_private != NULL ); |
| 440 |
|
| 441 |
mp = ( monitor_entry_t * )mc->mc_e->e_private; |
| 442 |
|
| 443 |
if ( mp->mp_cb ) { |
| 444 |
monitor_callback_t *cb; |
| 445 |
|
| 446 |
for ( cb = mp->mp_cb; cb != NULL; ) { |
| 447 |
monitor_callback_t *next = cb->mc_next; |
| 448 |
|
| 449 |
if ( cb->mc_free ) { |
| 450 |
(void)cb->mc_free( mc->mc_e, &cb->mc_private ); |
| 451 |
} |
| 452 |
ch_free( mp->mp_cb ); |
| 453 |
|
| 454 |
cb = next; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
ldap_pvt_thread_mutex_destroy( &mp->mp_mutex ); |
| 459 |
|
| 460 |
ch_free( mp ); |
| 461 |
mc->mc_e->e_private = NULL; |
| 462 |
entry_free( mc->mc_e ); |
| 463 |
} |
| 464 |
|
| 465 |
ch_free( mc ); |
| 466 |
} |
| 467 |
|
| 468 |
int |
| 469 |
monitor_cache_destroy( |
| 470 |
monitor_info_t *mi ) |
| 471 |
{ |
| 472 |
if ( mi->mi_cache ) { |
| 473 |
ldap_avl_free( mi->mi_cache, monitor_entry_destroy ); |
| 474 |
} |
| 475 |
|
| 476 |
return 0; |
| 477 |
} |
| 478 |
|
| 479 |
int monitor_back_release( |
| 480 |
Operation *op, |
| 481 |
Entry *e, |
| 482 |
int rw ) |
| 483 |
{ |
| 484 |
monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private; |
| 485 |
return monitor_cache_release( mi, e ); |
| 486 |
} |