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