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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
31 #include "isns_server.h"
32 #include "isns_cache.h"
33 #include "isns_msgq.h"
35 #include "isns_htab.h"
40 extern msg_queue_t
*sys_q
;
43 extern int verbose_lock
;
61 * ****************************************************************************
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 * ****************************************************************************
76 * allocate global cache memory.
78 imc
= (cache_t
*)calloc(sizeof (cache_t
), 1);
80 obj_tab_init(imc
) != 0 ||
81 dd_matrix_init(imc
) != 0) {
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
;
101 imc
->clone
= assoc_clone
;
102 imc
->ddd
= update_ddd
;
104 imc
->dump
= obj_dump
;
111 * ****************************************************************************
113 * destroy the cache data.
115 * ****************************************************************************
125 * ****************************************************************************
127 * grab the lock on the cache data.
129 * mode - the read/write mode of the lock.
130 * return - error code.
132 * ****************************************************************************
143 ret
= rw_wrlock(&imc
->l
);
146 printf("cache locked for writing.\n");
151 ret
= rw_rdlock(&imc
->l
);
154 printf("cache locked for reading.\n");
159 ret
= rw_tryrdlock(&imc
->l
);
163 printf("cache locked for reading.\n");
165 printf("cache locked for reading failed.\n");
178 * ****************************************************************************
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 * ****************************************************************************
196 if (mode
!= CACHE_NO_ACTION
) {
197 /* sync between cache and data store */
198 if (mode
== CACHE_WRITE
) {
203 /* rest the cache update flag */
204 RESET_CACHE_UPDATED();
207 ASSERT(!IS_CACHE_UPDATED());
210 (void) rw_unlock(&imc
->l
);
213 printf("cache unlocked.\n");
222 * ****************************************************************************
224 * grab the read lock on the cache.
226 * return - error code.
228 * ****************************************************************************
234 return (cache_lock(CACHE_READ
));
238 * ****************************************************************************
240 * grab the write lock on the cache.
242 * return - error code.
244 * ****************************************************************************
250 return (cache_lock(CACHE_WRITE
));
254 * ****************************************************************************
256 * synchronize the cache with persistent data store and
259 * ec - 0: commit the cache update; otherwise retreat it.
260 * return - error code.
262 * ****************************************************************************
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
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
281 * return - error code.
283 * ****************************************************************************
289 return (cache_unlock(CACHE_READ
, 0));
293 * ****************************************************************************
295 * get the hash table for individual type of object.
297 * type - the object type.
298 * return - the hash table.
300 * ****************************************************************************
307 if (type
> 0 && type
< MAX_OBJ_TYPE
) {
308 return (imc
->t
[type
]);
315 * ****************************************************************************
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 * ****************************************************************************
347 * ****************************************************************************
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 * ****************************************************************************
363 int (*callback
)(void *, void *)
366 return (htab_lookup(imc
->t
[lcp
->type
],
368 (lcp
->op
[0] == OP_INTEGER
) ? lcp
->data
[0].ui
: 0,
375 * ****************************************************************************
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 * ****************************************************************************
391 int (*callback
)(void *, void *)
394 return (htab_lookup(imc
->t
[lcp
->type
],
396 (lcp
->op
[0] == OP_INTEGER
) ? lcp
->data
[0].ui
: 0,
403 * ****************************************************************************
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)
413 * return - error code.
415 * ****************************************************************************
425 return (htab_add(imc
->t
[obj
->type
], obj
, flag
, uid_p
, update_p
));
429 * ****************************************************************************
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 * ****************************************************************************
446 return (htab_remove(imc
->t
[lcp
->type
],
448 (lcp
->op
[0] == OP_INTEGER
) ? lcp
->data
[0].ui
: 0,
453 * ****************************************************************************
455 * dump the hash table for debugging purpose.
457 * type - the object type.
459 * ****************************************************************************
467 (void) htab_dump(imc
->t
[type
]);