dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / isns / isnsd / cache.c
blob28510db3231fc0711f37422e626cdc8970340754
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "isns_server.h"
32 #include "isns_cache.h"
33 #include "isns_msgq.h"
34 #include "isns_obj.h"
35 #include "isns_htab.h"
38 * external variables
40 extern msg_queue_t *sys_q;
42 #ifdef DEBUG
43 extern int verbose_lock;
44 #endif
47 * global data
49 int cache_flag = 0;
52 * local variables
54 static cache_t *imc;
57 * local functions.
61 * ****************************************************************************
62 * cache_init:
63 * create the cache data initially, including to invoke individual
64 * functions for creating the hash tables for object storage and
65 * discovery domain membership matrix.
67 * return - 0: no error; 1: otherwise.
69 * ****************************************************************************
71 int
72 cache_init(
76 * allocate global cache memory.
78 imc = (cache_t *)calloc(sizeof (cache_t), 1);
79 if (imc == NULL ||
80 obj_tab_init(imc) != 0 ||
81 dd_matrix_init(imc) != 0) {
82 cache_destroy();
83 return (1); /* no memory */
87 * initialize global cache rwlock.
89 (void) rwlock_init(&imc->l, 0, NULL);
92 * inintialize global cache functions.
94 imc->get_hval = obj_hval;
95 imc->get_uid = get_obj_uid;
96 imc->set_uid = set_obj_uid;
97 imc->timestamp = get_timestamp;
98 imc->add_hook = add_object;
99 imc->replace_hook = replace_object;
100 imc->cmp = obj_cmp;
101 imc->clone = assoc_clone;
102 imc->ddd = update_ddd;
103 #ifdef DEBUG
104 imc->dump = obj_dump;
105 #endif
107 return (0);
111 * ****************************************************************************
112 * cache_destroy:
113 * destroy the cache data.
115 * ****************************************************************************
117 void
118 cache_destroy(
121 /* do nothing */
125 * ****************************************************************************
126 * cache_lock:
127 * grab the lock on the cache data.
129 * mode - the read/write mode of the lock.
130 * return - error code.
132 * ****************************************************************************
135 cache_lock(
136 int mode
139 int ret = 0;
141 switch (mode) {
142 case CACHE_WRITE:
143 ret = rw_wrlock(&imc->l);
144 #ifdef DEBUG
145 if (verbose_lock) {
146 printf("cache locked for writing.\n");
148 #endif
149 break;
150 case CACHE_READ:
151 ret = rw_rdlock(&imc->l);
152 #ifdef DEBUG
153 if (verbose_lock) {
154 printf("cache locked for reading.\n");
156 #endif
157 break;
158 case CACHE_TRY_READ:
159 ret = rw_tryrdlock(&imc->l);
160 #ifdef DEBUG
161 if (verbose_lock) {
162 if (ret == 0) {
163 printf("cache locked for reading.\n");
164 } else {
165 printf("cache locked for reading failed.\n");
168 #endif
169 break;
170 default:
171 break;
174 return (ret);
178 * ****************************************************************************
179 * cache_unlock:
180 * release the lock on the cache data.
181 * if the cache was locked for writing, a synchronization between
182 * the cache and persistent data store needs to be performed.
184 * mode - the read/write mode which the cache data was locked for.
185 * ec - 0: commit the cache update; otherwise retreat it.
186 * return - error code.
188 * ****************************************************************************
191 cache_unlock(
192 int mode,
193 int ec
196 if (mode != CACHE_NO_ACTION) {
197 /* sync between cache and data store */
198 if (mode == CACHE_WRITE) {
199 if (sys_q) {
200 ec = data_sync(ec);
203 /* rest the cache update flag */
204 RESET_CACHE_UPDATED();
207 ASSERT(!IS_CACHE_UPDATED());
209 /* unlock it */
210 (void) rw_unlock(&imc->l);
211 #ifdef DEBUG
212 if (verbose_lock) {
213 printf("cache unlocked.\n");
215 #endif
218 return (ec);
222 * ****************************************************************************
223 * cache_lock_read:
224 * grab the read lock on the cache.
226 * return - error code.
228 * ****************************************************************************
231 cache_lock_read(
234 return (cache_lock(CACHE_READ));
238 * ****************************************************************************
239 * cache_lock_write:
240 * grab the write lock on the cache.
242 * return - error code.
244 * ****************************************************************************
247 cache_lock_write(
250 return (cache_lock(CACHE_WRITE));
254 * ****************************************************************************
255 * cache_unlock_sync:
256 * synchronize the cache with persistent data store and
257 * release the lock.
259 * ec - 0: commit the cache update; otherwise retreat it.
260 * return - error code.
262 * ****************************************************************************
265 cache_unlock_sync(
266 int ec
269 return (cache_unlock(CACHE_WRITE, ec));
273 * ****************************************************************************
274 * cache_unlock_nosync:
275 * release the lock, no need to sync the data between cache and
276 * data store.
277 * if the cache has been updated, do not call this function, call
278 * cache_unlock_sync() with non-zero error code to indicate the
279 * sync action.
281 * return - error code.
283 * ****************************************************************************
286 cache_unlock_nosync(
289 return (cache_unlock(CACHE_READ, 0));
293 * ****************************************************************************
294 * cache_get_htab:
295 * get the hash table for individual type of object.
297 * type - the object type.
298 * return - the hash table.
300 * ****************************************************************************
302 htab_t *
303 cache_get_htab(
304 isns_type_t type
307 if (type > 0 && type < MAX_OBJ_TYPE) {
308 return (imc->t[type]);
311 return (NULL);
315 * ****************************************************************************
316 * cache_get_matrix:
317 * get the membership matrix for a discovery domain or a
318 * discovery domain set.
320 * type - the discovery domain or discovery domain set object type.
321 * return - the matrix.
323 * ****************************************************************************
325 matrix_t *
326 cache_get_matrix(
327 isns_type_t type
330 matrix_t *x = NULL;
332 switch (type) {
333 case OBJ_DD:
334 x = imc->x[0];
335 break;
336 case OBJ_DDS:
337 x = imc->x[1];
338 break;
339 default:
340 break;
343 return (x);
347 * ****************************************************************************
348 * cache_lookup:
349 * invoke the hash table lookup for looking up a specific object and
350 * perform the callback function on the object.
352 * lcp - the object lookup control data.
353 * uid_p - the pointer of object UID for returning.
354 * callback - the callback function for the object.
355 * return - error code.
357 * ****************************************************************************
360 cache_lookup(
361 lookup_ctrl_t *lcp,
362 uint32_t *uid_p,
363 int (*callback)(void *, void *)
366 return (htab_lookup(imc->t[lcp->type],
367 lcp,
368 (lcp->op[0] == OP_INTEGER) ? lcp->data[0].ui : 0,
369 uid_p,
370 callback,
371 0));
375 * ****************************************************************************
376 * cache_lookup:
377 * invoke the hash table lookup for looking up a specific object,
378 * the callback function is going to change the key of the object.
380 * lcp - the object lookup control data.
381 * uid_p - the pointer of object UID for returning.
382 * callback - the callback function for the object.
383 * return - error code.
385 * ****************************************************************************
388 cache_rekey(
389 lookup_ctrl_t *lcp,
390 uint32_t *uid_p,
391 int (*callback)(void *, void *)
394 return (htab_lookup(imc->t[lcp->type],
395 lcp,
396 (lcp->op[0] == OP_INTEGER) ? lcp->data[0].ui : 0,
397 uid_p,
398 callback,
399 1));
403 * ****************************************************************************
404 * cache_add:
405 * invoke hash table add to add an object.
407 * obj - the object being added.
408 * flag - 0: a real object;
409 * otherwise an association object for discovery domain membership.
410 * uid_p - the pointer of object UID for returning.
411 * update_p - the pointer of flag (update object or newly register)
412 * for returning.
413 * return - error code.
415 * ****************************************************************************
418 cache_add(
419 isns_obj_t *obj,
420 int flag,
421 uint32_t *uid_p,
422 int *update_p
425 return (htab_add(imc->t[obj->type], obj, flag, uid_p, update_p));
429 * ****************************************************************************
430 * cache_remove:
431 * invoke hash table remove to remove an object.
433 * lcp - the lookup control data for the object being removed.
434 * flag - 0: a real object;
435 * otherwise an association object for discovery domain membership.
436 * return - the removed object.
438 * ****************************************************************************
440 isns_obj_t *
441 cache_remove(
442 lookup_ctrl_t *lcp,
443 int flag
446 return (htab_remove(imc->t[lcp->type],
447 lcp,
448 (lcp->op[0] == OP_INTEGER) ? lcp->data[0].ui : 0,
449 flag));
453 * ****************************************************************************
454 * cache_dump_htab:
455 * dump the hash table for debugging purpose.
457 * type - the object type.
459 * ****************************************************************************
461 #ifdef DEBUG
462 void
463 cache_dump_htab(
464 isns_type_t type
467 (void) htab_dump(imc->t[type]);
469 #endif