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
|
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "isns_server.h"
#include "isns_cache.h"
#include "isns_msgq.h"
#include "isns_obj.h"
#include "isns_htab.h"
/*
* external variables
*/
extern msg_queue_t *sys_q;
#ifdef DEBUG
extern int verbose_lock;
#endif
/*
* global data
*/
int cache_flag = 0;
/*
* local variables
*/
static cache_t *imc;
/*
* local functions.
*/
/*
* ****************************************************************************
* cache_init:
* create the cache data initially, including to invoke individual
* functions for creating the hash tables for object storage and
* discovery domain membership matrix.
*
* return - 0: no error; 1: otherwise.
*
* ****************************************************************************
*/
int
cache_init(void)
{
/*
* allocate global cache memory.
*/
imc = (cache_t *)calloc(sizeof (cache_t), 1);
if (imc == NULL ||
obj_tab_init(imc) != 0 ||
dd_matrix_init(imc) != 0) {
cache_destroy();
return (1); /* no memory */
}
/*
* initialize global cache rwlock.
*/
(void) rwlock_init(&imc->l, USYNC_PROCESS, NULL);
/*
* inintialize global cache functions.
*/
imc->get_hval = obj_hval;
imc->get_uid = get_obj_uid;
imc->set_uid = set_obj_uid;
imc->timestamp = get_timestamp;
imc->add_hook = add_object;
imc->replace_hook = replace_object;
imc->cmp = obj_cmp;
imc->clone = assoc_clone;
imc->ddd = update_ddd;
#ifdef DEBUG
imc->dump = obj_dump;
#endif
return (0);
}
/*
* ****************************************************************************
* cache_destroy:
* destroy the cache data.
*
* ****************************************************************************
*/
void
cache_destroy(void)
{
/* do nothing */
}
/*
* ****************************************************************************
* cache_lock:
* grab the lock on the cache data.
*
* mode - the read/write mode of the lock.
* return - error code.
*
* ****************************************************************************
*/
int
cache_lock(int mode)
{
int ret = 0;
switch (mode) {
case CACHE_WRITE:
ret = rw_wrlock(&imc->l);
#ifdef DEBUG
if (verbose_lock) {
printf("cache locked for writing.\n");
}
#endif
break;
case CACHE_READ:
ret = rw_rdlock(&imc->l);
#ifdef DEBUG
if (verbose_lock) {
printf("cache locked for reading.\n");
}
#endif
break;
case CACHE_TRY_READ:
ret = rw_tryrdlock(&imc->l);
#ifdef DEBUG
if (verbose_lock) {
if (ret == 0) {
printf("cache locked for reading.\n");
} else {
printf("cache locked for reading failed.\n");
}
}
#endif
break;
default:
break;
}
return (ret);
}
/*
* ****************************************************************************
* cache_unlock:
* release the lock on the cache data.
* if the cache was locked for writing, a synchronization between
* the cache and persistent data store needs to be performed.
*
* mode - the read/write mode which the cache data was locked for.
* ec - 0: commit the cache update; otherwise retreat it.
* return - error code.
*
* ****************************************************************************
*/
int
cache_unlock(int mode, int ec)
{
if (mode != CACHE_NO_ACTION) {
/* sync between cache and data store */
if (mode == CACHE_WRITE) {
if (sys_q) {
ec = data_sync(ec);
}
/* rest the cache update flag */
RESET_CACHE_UPDATED();
}
ASSERT(!IS_CACHE_UPDATED());
/* unlock it */
(void) rw_unlock(&imc->l);
#ifdef DEBUG
if (verbose_lock) {
printf("cache unlocked.\n");
}
#endif
}
return (ec);
}
/*
* ****************************************************************************
* cache_lock_read:
* grab the read lock on the cache.
*
* return - error code.
*
* ****************************************************************************
*/
int
cache_lock_read(void)
{
return (cache_lock(CACHE_READ));
}
/*
* ****************************************************************************
* cache_lock_write:
* grab the write lock on the cache.
*
* return - error code.
*
* ****************************************************************************
*/
int
cache_lock_write(void)
{
return (cache_lock(CACHE_WRITE));
}
/*
* ****************************************************************************
* cache_unlock_sync:
* synchronize the cache with persistent data store and
* release the lock.
*
* ec - 0: commit the cache update; otherwise retreat it.
* return - error code.
*
* ****************************************************************************
*/
int
cache_unlock_sync(int ec)
{
return (cache_unlock(CACHE_WRITE, ec));
}
/*
* ****************************************************************************
* cache_unlock_nosync:
* release the lock, no need to sync the data between cache and
* data store.
* if the cache has been updated, do not call this function, call
* cache_unlock_sync() with non-zero error code to indicate the
* sync action.
*
* return - error code.
*
* ****************************************************************************
*/
int
cache_unlock_nosync(void)
{
return (cache_unlock(CACHE_READ, 0));
}
/*
* ****************************************************************************
* cache_get_htab:
* get the hash table for individual type of object.
*
* type - the object type.
* return - the hash table.
*
* ****************************************************************************
*/
htab_t *
cache_get_htab(isns_type_t type)
{
if (type > 0 && type < MAX_OBJ_TYPE) {
return (imc->t[type]);
}
return (NULL);
}
/*
* ****************************************************************************
* cache_get_matrix:
* get the membership matrix for a discovery domain or a
* discovery domain set.
*
* type - the discovery domain or discovery domain set object type.
* return - the matrix.
*
* ****************************************************************************
*/
matrix_t *
cache_get_matrix(isns_type_t type)
{
matrix_t *x = NULL;
switch (type) {
case OBJ_DD:
x = imc->x[0];
break;
case OBJ_DDS:
x = imc->x[1];
break;
default:
break;
}
return (x);
}
/*
* ****************************************************************************
* cache_lookup:
* invoke the hash table lookup for looking up a specific object and
* perform the callback function on the object.
*
* lcp - the object lookup control data.
* uid_p - the pointer of object UID for returning.
* callback - the callback function for the object.
* return - error code.
*
* ****************************************************************************
*/
int
cache_lookup(lookup_ctrl_t *lcp, uint32_t *uid_p,
int (*callback)(void *, void *))
{
return (htab_lookup(imc->t[lcp->type],
lcp,
(lcp->op[0] == OP_INTEGER) ? lcp->data[0].ui : 0,
uid_p,
callback,
0));
}
/*
* ****************************************************************************
* cache_lookup:
* invoke the hash table lookup for looking up a specific object,
* the callback function is going to change the key of the object.
*
* lcp - the object lookup control data.
* uid_p - the pointer of object UID for returning.
* callback - the callback function for the object.
* return - error code.
*
* ****************************************************************************
*/
int
cache_rekey(lookup_ctrl_t *lcp, uint32_t *uid_p,
int (*callback)(void *, void *))
{
return (htab_lookup(imc->t[lcp->type],
lcp,
(lcp->op[0] == OP_INTEGER) ? lcp->data[0].ui : 0,
uid_p,
callback,
1));
}
/*
* ****************************************************************************
* cache_add:
* invoke hash table add to add an object.
*
* obj - the object being added.
* flag - 0: a real object;
* otherwise an association object for discovery domain membership.
* uid_p - the pointer of object UID for returning.
* update_p - the pointer of flag (update object or newly register)
* for returning.
* return - error code.
*
* ****************************************************************************
*/
int
cache_add(isns_obj_t *obj, int flag, uint32_t *uid_p, int *update_p)
{
return (htab_add(imc->t[obj->type], obj, flag, uid_p, update_p));
}
/*
* ****************************************************************************
* cache_remove:
* invoke hash table remove to remove an object.
*
* lcp - the lookup control data for the object being removed.
* flag - 0: a real object;
* otherwise an association object for discovery domain membership.
* return - the removed object.
*
* ****************************************************************************
*/
isns_obj_t *
cache_remove(lookup_ctrl_t *lcp, int flag)
{
return (htab_remove(imc->t[lcp->type],
lcp,
(lcp->op[0] == OP_INTEGER) ? lcp->data[0].ui : 0,
flag));
}
/*
* ****************************************************************************
* cache_dump_htab:
* dump the hash table for debugging purpose.
*
* type - the object type.
*
* ****************************************************************************
*/
#ifdef DEBUG
void
cache_dump_htab(isns_type_t type)
{
(void) htab_dump(imc->t[type]);
}
#endif
|