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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "nscd_db.h"
#include "nscd_log.h"
/*
* Access control structure for a piece of nscd data. This structure
* is always tagged before the nscd data. nscd_alloc, which should
* be used to allocate memory that requires access control or usage
* count control, will initialize this access control structure at the
* start of the memory returned to the caller.
*/
struct nscd_access_s {
void *data; /* addr of real data */
void (*free_func)(nscd_acc_data_t *data); /* destructor */
mutex_t mutex; /* protect this structure */
mutex_t *data_mutex;
rwlock_t *data_rwlock;
cond_t *data_cond;
int nUse; /* usage count */
int type;
int delete; /* no longer available */
nscd_seq_num_t seq_num; /* sequence number */
};
/* size should be in multiple of 8 */
static int sizeof_access = roundup(sizeof (nscd_access_t));
#define ABORT_DUE_TO_NO_VALID_NSCD_ACCESS_DATA 0
#define ASSERT_ACCESS_DATA \
if (access->data != data) \
assert(ABORT_DUE_TO_NO_VALID_NSCD_ACCESS_DATA)
#define SET_ACCESS_PTR \
access = (nscd_access_t *) \
((void *)((char *)data - sizeof_access))
static void _nscd_free(nscd_acc_data_t *data);
/*
* FUNCTION: _nscd_release
*
* Decrements the usage count maintained in the access data
* tagged before 'data'. Delete the nscd data item if the delete
* flag is set and the usage count reaches 0.
*/
void
_nscd_release(
nscd_acc_data_t *data)
{
nscd_access_t *access;
char *me = "_nscd_release";
if (data == NULL)
return;
SET_ACCESS_PTR;
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "data = %p, access->data = %p, "
"seq = %lld, nUse = %d\n",
data, access->data, access->seq_num, access->nUse);
ASSERT_ACCESS_DATA;
(void) mutex_lock(&access->mutex);
access->nUse--;
if (access->nUse < 0) {
#define ACCESS_NUSE_LESS_THAN_ZERO 0
assert(ACCESS_NUSE_LESS_THAN_ZERO);
}
if (access->nUse <= 0 &&
access->delete == 1) {
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "deleting data %p\n", access->data);
(access->free_func)(access->data);
/*
* if we get here, no other thread could be
* holding the access->mutex lock, It is safe
* to free the memory containing the mutex
* structure. No mutex_unlock is necessary.
*/
_nscd_free(data);
} else
(void) mutex_unlock(&access->mutex);
}
/*
* FUNCTION: _nscd_destroy
*
* Marks the nscd data item as to-be-deleted and then releases
* (If the usage count happens to be zero, then _nscd_release()
* will destroy the data.)
*
* Note that _nscd_destroy should only be called if the
* caller has created the nscd data with _nscd_alloc
* (with the exception of _nscd_set). That nscd data
* item should be private to the caller.
*/
static void
_nscd_destroy(
nscd_acc_data_t *data)
{
nscd_access_t *access;
char *me = "_nscd_destroy";
if (data == NULL)
return;
SET_ACCESS_PTR;
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "data = %p, access->data = %p\n", data, access->data);
ASSERT_ACCESS_DATA;
(void) mutex_lock(&access->mutex);
access->delete = 1;
(void) mutex_unlock(&access->mutex);
_nscd_release(data);
}
/*
* FUNCTION: _nscd_get
*
* Increment the usage count by one if 'data' can
* be found in the internal address database.
*/
nscd_acc_data_t *
_nscd_get(
nscd_acc_data_t *data)
{
nscd_access_t *access;
void *ret = data;
rwlock_t *addr_rwlock;
char *me = "_nscd_get";
if (data == NULL)
return (NULL);
SET_ACCESS_PTR;
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "data = %p, access->data = %p, seq#= %lld, nUse = %d\n",
data, access->data, access->seq_num, access->nUse);
ASSERT_ACCESS_DATA;
/*
* see if this addr is still valid,
* if so, _nscd_is_int_addr will
* do a read lock on the returned
* multiple readers/single writer lock
* to prevent the access data from being
* deleted while it is being accessed.
*/
if ((addr_rwlock = _nscd_is_int_addr(data,
access->seq_num)) == NULL) {
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "internal address %p not found\n", data);
assert(addr_rwlock != NULL);
return (NULL);
}
(void) mutex_lock(&access->mutex);
if (access->delete == 1)
ret = NULL;
else
access->nUse++;
(void) mutex_unlock(&access->mutex);
/*
* done with the multiple readers/single writer lock
*/
(void) rw_unlock(addr_rwlock);
return (ret);
}
/*
* FUNCTION: _nscd_set
*
* _nscd_set sets the address of a nscd data item
* to 'new' and delete the old nscd data (old).
* The pointer 'new' is returned.
*/
nscd_acc_data_t *
_nscd_set(
nscd_acc_data_t *old,
nscd_acc_data_t *new)
{
nscd_acc_data_t *old_data, *new_data;
char *me = "_nscd_set";
if (new == old)
return (old);
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "new = %p, old = %p\n", new, old);
old_data = _nscd_get(old);
new_data = _nscd_get(new);
if (old_data != new_data) {
_nscd_destroy(old_data);
_nscd_release(new_data);
return (new_data);
}
/* if old_data == new_data, both must be NULL */
return (NULL);
}
/*
* FUNCTION: _nscd_rdlock
*
* Lock (rw_rdlock) a nscd data item for reading. The caller
* needs to call _nscd_rw_unlock() to unlock the data item
* when done using the data.
*/
nscd_acc_data_t *
_nscd_rdlock(
nscd_acc_data_t *data)
{
nscd_access_t *access;
void *ret;
char *me = "_nscd_rdlock";
ret = _nscd_get(data);
if (ret == NULL)
return (NULL);
SET_ACCESS_PTR;
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "data = %p, access->data = %p\n", data, access->data);
ASSERT_ACCESS_DATA;
assert(access->data_rwlock != NULL);
(void) rw_rdlock(access->data_rwlock);
return (ret);
}
/*
* FUNCTION: _nscd_wrlock
*
* Lock (rw_wrlock) a nscd data item for writing. The caller
* needs to call _nscd_rw_unlock() to unlock the data item
* when done using the data.
*/
nscd_acc_data_t *
_nscd_wrlock(
nscd_acc_data_t *data)
{
nscd_access_t *access;
void *ret;
char *me = "_nscd_wrlock";
ret = _nscd_get(data);
if (ret == NULL)
return (NULL);
SET_ACCESS_PTR;
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "data = %p, access->data = %p\n", data, access->data);
ASSERT_ACCESS_DATA;
assert(access->data_rwlock != NULL);
(void) rw_wrlock(access->data_rwlock);
return (ret);
}
/*
* FUNCTION: _nscd_rw_unlock
*
* Unlock (rw_unlock) a locked nscd data item.
*/
void
_nscd_rw_unlock(
nscd_acc_data_t *data)
{
nscd_access_t *access;
char *me = "_nscd_rw_unlock";
if (data == NULL)
return;
SET_ACCESS_PTR;
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "data = %p, access->data = %p\n",
data, access->data);
ASSERT_ACCESS_DATA;
assert(access->data_rwlock != NULL);
(void) rw_unlock(access->data_rwlock);
_nscd_release(data);
}
/*
* FUNCTION: _nscd_rw_unlock_no_release
*
* Unlock (rw_unlock) a locked nscd data item but without release
* it, i.e., without decrement the usage count to indicate that
* the data item is still being referenced.
*/
void
_nscd_rw_unlock_no_release(
nscd_acc_data_t *data)
{
nscd_access_t *access;
if (data == NULL)
return;
SET_ACCESS_PTR;
ASSERT_ACCESS_DATA;
assert(access->data_rwlock != NULL);
(void) rw_unlock(access->data_rwlock);
}
/*
* FUNCTION: _nscd_mutex_lock
*
* Lock (mutex_lock) a nscd data item. The caller needs
* to call _nscd_mutex_unlock() to unlock the data item
* when done using the data.
*/
nscd_acc_data_t *
_nscd_mutex_lock(
nscd_acc_data_t *data)
{
nscd_access_t *access;
void *ret;
char *me = "_nscd_mutex_lock";
ret = _nscd_get(data);
if (ret == NULL)
return (NULL);
SET_ACCESS_PTR;
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "data = %p, access->data = %p\n", data, access->data);
ASSERT_ACCESS_DATA;
assert(access->data_mutex != NULL);
(void) mutex_lock(access->data_mutex);
return (ret);
}
/*
* FUNCTION: _nscd_mutex_unlock
*
* Unlock a locked nscd data item (that were locked by _nscd_mutex_lock)..
*/
void
_nscd_mutex_unlock(
nscd_acc_data_t *data)
{
nscd_access_t *access;
char *me = "_nscd_mutex_unlock";
if (data == NULL)
return;
SET_ACCESS_PTR;
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "data = %p, access->data = %p\n", data, access->data);
ASSERT_ACCESS_DATA;
assert(access->data_mutex != NULL);
(void) mutex_unlock(access->data_mutex);
_nscd_release(data);
}
/*
* FUNCTION: _nscd_cond_wait
*
* Perform a condition wait with the cond_t and mutex_t associated
* with data.
*/
void
_nscd_cond_wait(
nscd_acc_data_t *data, cond_t *cond)
{
nscd_access_t *access;
char *me = "_nscd_cond_wait";
if (data == NULL)
return;
SET_ACCESS_PTR;
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "data = %p, access->data = %p\n", data, access->data);
ASSERT_ACCESS_DATA;
assert(access->data_cond != NULL && access->data_mutex != NULL);
if (cond == NULL)
(void) cond_wait(access->data_cond, access->data_mutex);
else
(void) cond_wait(cond, access->data_mutex);
}
/*
* FUNCTION: _nscd_cond_signal
*
* Perform a condition signal with the cond_t associated with 'data'.
*/
void
_nscd_cond_signal(
nscd_acc_data_t *data)
{
nscd_access_t *access;
char *me = "_nscd_cond_signal";
if (data == NULL)
return;
SET_ACCESS_PTR;
_NSCD_LOG(NSCD_LOG_ACCESS_INFO, NSCD_LOG_LEVEL_DEBUG)
(me, "data = %p, access->data = %p\n", data, access->data);
ASSERT_ACCESS_DATA;
assert(access->data_cond != NULL);
(void) cond_signal(access->data_cond);
}
/*
* FUNCTION: _nscd_alloc
*
* Allocate a piece of nscd memory. 'data_free'
* is the function to invoke to free the data
* stored in this memory, i.e., the desctrctor.
* 'option' indicate whether a mutex or a
* readers/writer (or both, or none) should also
* be allocated.
*/
nscd_acc_data_t *
_nscd_alloc(
int type,
size_t size,
void (*data_free)(nscd_acc_data_t *data),
int option)
{
nscd_access_t *access;
nscd_acc_data_t *ptr;
nscd_seq_num_t seq_num;
rwlock_t *rwlock = NULL;
mutex_t *mutex = NULL;
cond_t *cond = NULL;
if ((ptr = (nscd_acc_data_t *)calloc(1,
size + sizeof_access)) == NULL)
return (NULL);
if (option & NSCD_ALLOC_MUTEX) {
if ((mutex = (mutex_t *)calloc(1, sizeof (mutex_t))) ==
NULL) {
free(ptr);
return (NULL);
} else
(void) mutex_init(mutex, USYNC_THREAD, NULL);
}
if (option & NSCD_ALLOC_RWLOCK) {
if ((rwlock = (rwlock_t *)calloc(1, sizeof (rwlock_t))) ==
NULL) {
free(ptr);
free(mutex);
return (NULL);
} else
(void) rwlock_init(rwlock, USYNC_THREAD, NULL);
}
if (option & NSCD_ALLOC_COND) {
if ((cond = (cond_t *)calloc(1, sizeof (cond_t))) ==
NULL) {
free(ptr);
free(mutex);
free(rwlock);
return (NULL);
} else
(void) cond_init(cond, USYNC_THREAD, NULL);
}
/* get current sequence number */
seq_num = _nscd_get_seq_num();
access = (nscd_access_t *)ptr;
access->data = (char *)ptr + sizeof_access;
access->data_mutex = mutex;
access->data_rwlock = rwlock;
access->data_cond = cond;
access->nUse = 0;
access->delete = 0;
access->type = type;
access->free_func = data_free;
access->seq_num = seq_num;
/* add the address to the internal address database */
if (_nscd_add_int_addr(access->data, type,
seq_num) != NSCD_SUCCESS) {
free(ptr);
return (NULL);
}
return (access->data);
}
/*
* FUNCTION: _nscd_free
*
* Free a piece of nscd memory.
*/
static void
_nscd_free(
nscd_acc_data_t *data)
{
nscd_access_t *access;
if (data == NULL)
return;
SET_ACCESS_PTR;
ASSERT_ACCESS_DATA;
/* remove the address from the internal address database */
_nscd_del_int_addr(access->data, access->seq_num);
if (access->data_mutex)
free(access->data_mutex);
if (access->data_rwlock)
free(access->data_rwlock);
if (access->data_cond)
free(access->data_cond);
(void) memset(access, 0, sizeof (*access));
free(access);
}
|