xref: /illumos-gate/usr/src/cmd/isns/isnsd/obj.c (revision a2b0e4f1)
1fcf3ce44SJohn Forte /*
2fcf3ce44SJohn Forte  * CDDL HEADER START
3fcf3ce44SJohn Forte  *
4fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7fcf3ce44SJohn Forte  *
8fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11fcf3ce44SJohn Forte  * and limitations under the License.
12fcf3ce44SJohn Forte  *
13fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18fcf3ce44SJohn Forte  *
19fcf3ce44SJohn Forte  * CDDL HEADER END
20fcf3ce44SJohn Forte  */
21fcf3ce44SJohn Forte 
22fcf3ce44SJohn Forte /*
23530e2b59Swl  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24fcf3ce44SJohn Forte  * Use is subject to license terms.
25fcf3ce44SJohn Forte  */
26fcf3ce44SJohn Forte 
27fcf3ce44SJohn Forte #include <stdio.h>
28fcf3ce44SJohn Forte #include <stdlib.h>
29fcf3ce44SJohn Forte #include <string.h>
30fcf3ce44SJohn Forte #include <pthread.h>
31fcf3ce44SJohn Forte #include <sys/types.h>
32fcf3ce44SJohn Forte #include <sys/socket.h>
33fcf3ce44SJohn Forte #include <netinet/in.h>
34fcf3ce44SJohn Forte #include <arpa/inet.h>
35fcf3ce44SJohn Forte 
36fcf3ce44SJohn Forte #include "isns_server.h"
37fcf3ce44SJohn Forte #include "isns_msgq.h"
38fcf3ce44SJohn Forte #include "isns_htab.h"
39fcf3ce44SJohn Forte #include "isns_cache.h"
40fcf3ce44SJohn Forte #include "isns_pdu.h"
41fcf3ce44SJohn Forte #include "isns_obj.h"
42fcf3ce44SJohn Forte #include "isns_dd.h"
43fcf3ce44SJohn Forte #include "isns_func.h"
44fcf3ce44SJohn Forte #include "isns_dseng.h"
45fcf3ce44SJohn Forte #include "isns_log.h"
46fcf3ce44SJohn Forte #include "isns_scn.h"
47fcf3ce44SJohn Forte #include "isns_utils.h"
48fcf3ce44SJohn Forte #include "isns_esi.h"
49fcf3ce44SJohn Forte 
50fcf3ce44SJohn Forte /*
51fcf3ce44SJohn Forte  * external variables
52fcf3ce44SJohn Forte  */
53fcf3ce44SJohn Forte #ifdef DEBUG
54fcf3ce44SJohn Forte extern int verbose_mc;
55fcf3ce44SJohn Forte extern void print_object(char *, isns_obj_t *);
56fcf3ce44SJohn Forte #endif
57fcf3ce44SJohn Forte 
58fcf3ce44SJohn Forte extern msg_queue_t *sys_q;
59fcf3ce44SJohn Forte extern msg_queue_t *scn_q;
60fcf3ce44SJohn Forte 
61fcf3ce44SJohn Forte extern pthread_mutex_t el_mtx;
62fcf3ce44SJohn Forte 
63fcf3ce44SJohn Forte extern int cache_flag;
64fcf3ce44SJohn Forte 
65fcf3ce44SJohn Forte /*
66fcf3ce44SJohn Forte  * global data
67fcf3ce44SJohn Forte  */
68fcf3ce44SJohn Forte 
69fcf3ce44SJohn Forte /*
70fcf3ce44SJohn Forte  * local variables
71fcf3ce44SJohn Forte  */
72fcf3ce44SJohn Forte /* type of parent object */
73fcf3ce44SJohn Forte const int TYPE_OF_PARENT[MAX_OBJ_TYPE_FOR_SIZE] = {
74fcf3ce44SJohn Forte 	0,
75fcf3ce44SJohn Forte 	0,
76fcf3ce44SJohn Forte 	ISCSI_PARENT_TYPE,
77fcf3ce44SJohn Forte 	PORTAL_PARENT_TYPE,
78fcf3ce44SJohn Forte 	PG_PARENT_TYPE,
79fcf3ce44SJohn Forte 	0,	/* OBJ_DD */
80fcf3ce44SJohn Forte 	0,	/* OBJ_DDS */
81fcf3ce44SJohn Forte 	0,	/* MAX_OBJ_TYPE */
82fcf3ce44SJohn Forte 	0,	/* OBJ_DUMMY1 */
83fcf3ce44SJohn Forte 	0,	/* OBJ_DUMMY2 */
84fcf3ce44SJohn Forte 	0,	/* OBJ_DUMMY3 */
85fcf3ce44SJohn Forte 	0,	/* OBJ_DUMMY4 */
86fcf3ce44SJohn Forte 	ASSOC_ISCSI_PARENT_TYPE,
87fcf3ce44SJohn Forte 	ASSOC_DD_PARENT_TYPE
88fcf3ce44SJohn Forte };
89fcf3ce44SJohn Forte 
90fcf3ce44SJohn Forte /* number of children object type */
91fcf3ce44SJohn Forte const int NUM_OF_CHILD[MAX_OBJ_TYPE] = {
92fcf3ce44SJohn Forte 	0,
93fcf3ce44SJohn Forte 	MAX_ENTITY_CHILD,
94fcf3ce44SJohn Forte 	MAX_ISCSI_CHILD,
95fcf3ce44SJohn Forte 	MAX_PORTAL_CHILD,
96fcf3ce44SJohn Forte 	MAX_PG_CHILD,
97fcf3ce44SJohn Forte 	0,
98fcf3ce44SJohn Forte 	0
99fcf3ce44SJohn Forte };
100fcf3ce44SJohn Forte 
101fcf3ce44SJohn Forte /* type of a child object */
102fcf3ce44SJohn Forte const int TYPE_OF_CHILD[MAX_OBJ_TYPE][MAX_CHILD_TYPE] = {
103fcf3ce44SJohn Forte 	{ 0, 0 },
104fcf3ce44SJohn Forte 	{ OBJ_ISCSI, OBJ_PORTAL },
105fcf3ce44SJohn Forte 	{ 0, 0 },
106fcf3ce44SJohn Forte 	{ 0, 0 },
107fcf3ce44SJohn Forte 	{ 0, 0 },
108fcf3ce44SJohn Forte 	{ 0, 0 },
109fcf3ce44SJohn Forte 	{ 0, 0 }
110fcf3ce44SJohn Forte };
111fcf3ce44SJohn Forte 
112fcf3ce44SJohn Forte /* number of attributes of certain type of object */
113fcf3ce44SJohn Forte const int NUM_OF_ATTRS[MAX_OBJ_TYPE_FOR_SIZE] = {
114fcf3ce44SJohn Forte 	0,
115fcf3ce44SJohn Forte 	NUM_OF_ENTITY_ATTRS,
116fcf3ce44SJohn Forte 	NUM_OF_ISCSI_ATTRS,
117fcf3ce44SJohn Forte 	NUM_OF_PORTAL_ATTRS,
118fcf3ce44SJohn Forte 	NUM_OF_PG_ATTRS,
119fcf3ce44SJohn Forte 	NUM_OF_DD_ATTRS,
120fcf3ce44SJohn Forte 	NUM_OF_DDS_ATTRS,
121fcf3ce44SJohn Forte 	0,			/* MAX_OBJ_TYPE */
122fcf3ce44SJohn Forte 	0,			/* OBJ_DUMMY1 */
123fcf3ce44SJohn Forte 	0,			/* OBJ_DUMMY2 */
124fcf3ce44SJohn Forte 	0,			/* OBJ_DUMMY3 */
125fcf3ce44SJohn Forte 	0,			/* OBJ_DUMMY4 */
126fcf3ce44SJohn Forte 	NUM_OF_ASSOC_ISCSI_ATTRS,
127fcf3ce44SJohn Forte 	NUM_OF_ASSOC_DD_ATTRS
128fcf3ce44SJohn Forte };
129fcf3ce44SJohn Forte 
130fcf3ce44SJohn Forte /* the tag of UID of each type of object */
131fcf3ce44SJohn Forte static const int UID_TAG[MAX_OBJ_TYPE_FOR_SIZE] = {
132fcf3ce44SJohn Forte 	0,
133fcf3ce44SJohn Forte 	ISNS_ENTITY_INDEX_ATTR_ID,
134fcf3ce44SJohn Forte 	ISNS_ISCSI_NODE_INDEX_ATTR_ID,
135fcf3ce44SJohn Forte 	ISNS_PORTAL_INDEX_ATTR_ID,
136fcf3ce44SJohn Forte 	ISNS_PG_INDEX_ATTR_ID,
137fcf3ce44SJohn Forte 	ISNS_DD_ID_ATTR_ID,
138fcf3ce44SJohn Forte 	ISNS_DD_SET_ID_ATTR_ID,
139fcf3ce44SJohn Forte 	0,			/* MAX_OBJ_TYPE */
140fcf3ce44SJohn Forte 	0,			/* OBJ_DUMMY1 */
141fcf3ce44SJohn Forte 	0,			/* OBJ_DUMMY2 */
142fcf3ce44SJohn Forte 	0,			/* OBJ_DUMMY3 */
143fcf3ce44SJohn Forte 	0,			/* OBJ_DUMMY4 */
144fcf3ce44SJohn Forte 	ISNS_DD_ISCSI_INDEX_ATTR_ID,
145fcf3ce44SJohn Forte 	ISNS_DD_ID_ATTR_ID
146fcf3ce44SJohn Forte };
147fcf3ce44SJohn Forte 
148fcf3ce44SJohn Forte /* the index of UID of each type of object */
149fcf3ce44SJohn Forte const int UID_ATTR_INDEX[MAX_OBJ_TYPE_FOR_SIZE] = {
150fcf3ce44SJohn Forte 	0,
151fcf3ce44SJohn Forte 	ATTR_INDEX_ENTITY(ISNS_ENTITY_INDEX_ATTR_ID),
152fcf3ce44SJohn Forte 	ATTR_INDEX_ISCSI(ISNS_ISCSI_NODE_INDEX_ATTR_ID),
153fcf3ce44SJohn Forte 	ATTR_INDEX_PORTAL(ISNS_PORTAL_INDEX_ATTR_ID),
154fcf3ce44SJohn Forte 	ATTR_INDEX_PG(ISNS_PG_INDEX_ATTR_ID),
155fcf3ce44SJohn Forte 	ATTR_INDEX_DD(ISNS_DD_ID_ATTR_ID),
156fcf3ce44SJohn Forte 	ATTR_INDEX_DDS(ISNS_DD_SET_ID_ATTR_ID),
157fcf3ce44SJohn Forte 	0,			/* MAX_OBJ_TYPE */
158fcf3ce44SJohn Forte 	0,			/* OBJ_DUMMY1 */
159fcf3ce44SJohn Forte 	0,			/* OBJ_DUMMY2 */
160fcf3ce44SJohn Forte 	0,			/* OBJ_DUMMY3 */
161fcf3ce44SJohn Forte 	0,			/* OBJ_DUMMY4 */
162fcf3ce44SJohn Forte 	ATTR_INDEX_ASSOC_ISCSI(ISNS_DD_ISCSI_INDEX_ATTR_ID),
163fcf3ce44SJohn Forte 	ATTR_INDEX_ASSOC_DD(ISNS_DD_ID_ATTR_ID)
164fcf3ce44SJohn Forte };
165fcf3ce44SJohn Forte 
166fcf3ce44SJohn Forte /* the index of the key attributes of each type of object */
167fcf3ce44SJohn Forte static const int KEY_ATTR_INDEX[MAX_OBJ_TYPE][MAX_KEY_ATTRS] = {
168fcf3ce44SJohn Forte 	{ 0 },
169fcf3ce44SJohn Forte 	{ ATTR_INDEX_ENTITY(ISNS_EID_ATTR_ID), 0 },
170fcf3ce44SJohn Forte 	{ ATTR_INDEX_ISCSI(ISNS_ISCSI_NAME_ATTR_ID),
171fcf3ce44SJohn Forte 		0 },
172fcf3ce44SJohn Forte 	{ ATTR_INDEX_PORTAL(ISNS_PORTAL_IP_ADDR_ATTR_ID),
173fcf3ce44SJohn Forte 		ATTR_INDEX_PORTAL(ISNS_PORTAL_PORT_ATTR_ID),
174fcf3ce44SJohn Forte 		0 },
175fcf3ce44SJohn Forte 	{ ATTR_INDEX_PG(ISNS_PG_ISCSI_NAME_ATTR_ID),
176fcf3ce44SJohn Forte 		ATTR_INDEX_PG(ISNS_PG_PORTAL_IP_ADDR_ATTR_ID),
177fcf3ce44SJohn Forte 		ATTR_INDEX_PG(ISNS_PG_PORTAL_PORT_ATTR_ID) },
178fcf3ce44SJohn Forte 	{ ATTR_INDEX_DD(ISNS_DD_NAME_ATTR_ID), 0 },
179fcf3ce44SJohn Forte 	{ ATTR_INDEX_DDS(ISNS_DD_SET_NAME_ATTR_ID), 0 }
180fcf3ce44SJohn Forte };
181fcf3ce44SJohn Forte 
182fcf3ce44SJohn Forte /* the operating methods for key attributes of each type of object */
183fcf3ce44SJohn Forte static const int KEY_ATTR_OP[MAX_OBJ_TYPE][MAX_KEY_ATTRS] = {
184fcf3ce44SJohn Forte 	{ 0 },
185fcf3ce44SJohn Forte 	{ OP_STRING, 0 },
186fcf3ce44SJohn Forte 	{ OP_STRING, 0 },
187fcf3ce44SJohn Forte 	{ OP_MEMORY_IP6, OP_INTEGER, 0 },
188fcf3ce44SJohn Forte 	{ OP_STRING, OP_MEMORY_IP6, OP_INTEGER },
189fcf3ce44SJohn Forte 	{ OP_STRING, 0 },
190fcf3ce44SJohn Forte 	{ OP_STRING, 0 }
191fcf3ce44SJohn Forte };
192fcf3ce44SJohn Forte 
193fcf3ce44SJohn Forte /* the size of each type of object */
194fcf3ce44SJohn Forte static const int SIZEOF_OBJ[MAX_OBJ_TYPE_FOR_SIZE] = {
195fcf3ce44SJohn Forte 	0,
196fcf3ce44SJohn Forte 	sizeof (isns_entity_t),
197fcf3ce44SJohn Forte 	sizeof (isns_iscsi_t),
198fcf3ce44SJohn Forte 	sizeof (isns_portal_t),
199fcf3ce44SJohn Forte 	sizeof (isns_pg_t),
200fcf3ce44SJohn Forte 	sizeof (isns_dd_t),
201fcf3ce44SJohn Forte 	sizeof (isns_dds_t),
202fcf3ce44SJohn Forte 	0,
203fcf3ce44SJohn Forte 	0,
204fcf3ce44SJohn Forte 	0,
205fcf3ce44SJohn Forte 	0,
206fcf3ce44SJohn Forte 	0,
207fcf3ce44SJohn Forte 	sizeof (isns_assoc_iscsi_t),
208fcf3ce44SJohn Forte 	sizeof (isns_assoc_dd_t)
209fcf3ce44SJohn Forte };
210fcf3ce44SJohn Forte 
211fcf3ce44SJohn Forte #ifdef DEBUG
212fcf3ce44SJohn Forte const int NUM_OF_REF[MAX_OBJ_TYPE_FOR_SIZE] = {
213fcf3ce44SJohn Forte #else
214fcf3ce44SJohn Forte static const int NUM_OF_REF[MAX_OBJ_TYPE_FOR_SIZE] = {
215fcf3ce44SJohn Forte #endif
216fcf3ce44SJohn Forte 	0,
217fcf3ce44SJohn Forte 	0,
218fcf3ce44SJohn Forte 	0,
219fcf3ce44SJohn Forte 	0,
220fcf3ce44SJohn Forte 	PG_REF_COUNT,
221fcf3ce44SJohn Forte 	0,
222fcf3ce44SJohn Forte 	0,
223fcf3ce44SJohn Forte 	0,
224fcf3ce44SJohn Forte 	0,
225fcf3ce44SJohn Forte 	0,
226fcf3ce44SJohn Forte 	0,
227fcf3ce44SJohn Forte 	0,
228fcf3ce44SJohn Forte 	0,
229fcf3ce44SJohn Forte 	0
230fcf3ce44SJohn Forte };
231fcf3ce44SJohn Forte 
232fcf3ce44SJohn Forte /* the type of the reference object */
233fcf3ce44SJohn Forte static const int TYPE_OF_REF[MAX_OBJ_TYPE][MAX_REF_COUNT + 1] = {
234fcf3ce44SJohn Forte 	{ 0 },
235fcf3ce44SJohn Forte 	{ 0 },
236fcf3ce44SJohn Forte 	{ OBJ_PG, OBJ_PORTAL, 0 },
237fcf3ce44SJohn Forte 	{ OBJ_PG, OBJ_ISCSI, 0 },
238fcf3ce44SJohn Forte 	{ 0, OBJ_ISCSI, OBJ_PORTAL },
239fcf3ce44SJohn Forte 	{ 0 },
240fcf3ce44SJohn Forte 	{ 0 }
241fcf3ce44SJohn Forte };
242fcf3ce44SJohn Forte 
243fcf3ce44SJohn Forte /* the operating method for match operation of the reference object */
244fcf3ce44SJohn Forte #define	MAX_REF_MATCH	(2)
245fcf3ce44SJohn Forte static const int REF_MATCH_OPS[MAX_OBJ_TYPE][MAX_REF_MATCH] = {
246fcf3ce44SJohn Forte 	{ 0, 0 },
247fcf3ce44SJohn Forte 	{ 0, 0 },
248fcf3ce44SJohn Forte 	{ OP_STRING, 0 },
249fcf3ce44SJohn Forte 	{ OP_MEMORY_IP6, OP_INTEGER },
250fcf3ce44SJohn Forte 	{ 0, 0 },
251fcf3ce44SJohn Forte 	{ 0, 0 },
252fcf3ce44SJohn Forte 	{ 0, 0 }
253fcf3ce44SJohn Forte };
254fcf3ce44SJohn Forte 
255fcf3ce44SJohn Forte /* the index of the attribute of being matched object */
256fcf3ce44SJohn Forte static const int REF_MATCH_ID1[MAX_OBJ_TYPE][MAX_REF_MATCH] = {
257fcf3ce44SJohn Forte 	{ 0, 0 },
258fcf3ce44SJohn Forte 	{ 0, 0 },
259fcf3ce44SJohn Forte 	{ ATTR_INDEX_ISCSI(ISNS_ISCSI_NAME_ATTR_ID), 0 },
260fcf3ce44SJohn Forte 	{ ATTR_INDEX_PORTAL(ISNS_PORTAL_IP_ADDR_ATTR_ID),
261fcf3ce44SJohn Forte 		ATTR_INDEX_PORTAL(ISNS_PORTAL_PORT_ATTR_ID) },
262fcf3ce44SJohn Forte 	{ 0, 0 },
263fcf3ce44SJohn Forte 	{ 0, 0 },
264fcf3ce44SJohn Forte 	{ 0, 0 }
265fcf3ce44SJohn Forte };
266fcf3ce44SJohn Forte 
267fcf3ce44SJohn Forte /* the index of the attribute of matching object */
268fcf3ce44SJohn Forte static const int REF_MATCH_ID2[MAX_OBJ_TYPE][MAX_REF_MATCH] = {
269fcf3ce44SJohn Forte 	{ 0, 0 },
270fcf3ce44SJohn Forte 	{ 0, 0 },
271fcf3ce44SJohn Forte 	{ ATTR_INDEX_PG(ISNS_PG_ISCSI_NAME_ATTR_ID), 0 },
272fcf3ce44SJohn Forte 	{ ATTR_INDEX_PG(ISNS_PG_PORTAL_IP_ADDR_ATTR_ID),
273fcf3ce44SJohn Forte 		ATTR_INDEX_PG(ISNS_PG_PORTAL_PORT_ATTR_ID) },
274fcf3ce44SJohn Forte 	{ 0, 0 },
275fcf3ce44SJohn Forte 	{ 0, 0 },
276fcf3ce44SJohn Forte 	{ 0, 0 }
277fcf3ce44SJohn Forte };
278fcf3ce44SJohn Forte 
279fcf3ce44SJohn Forte /*
280fcf3ce44SJohn Forte  * local functions.
281fcf3ce44SJohn Forte  */
282fcf3ce44SJohn Forte static uint32_t get_reg_period();
283fcf3ce44SJohn Forte static char *make_unique_name(int *, uint32_t);
284fcf3ce44SJohn Forte static lookup_ctrl_t *set_lookup_ctrl(lookup_ctrl_t *, isns_obj_t *);
285fcf3ce44SJohn Forte static int setup_ref_lcp(lookup_ctrl_t *,
286fcf3ce44SJohn Forte 	const isns_obj_t *, const isns_obj_t *);
287fcf3ce44SJohn Forte static int setup_deref_lcp(lookup_ctrl_t *,
288fcf3ce44SJohn Forte 	const isns_obj_t *, isns_type_t);
289fcf3ce44SJohn Forte static int cb_get_parent(void *, void *);
290fcf3ce44SJohn Forte static int cb_node_child(void *, void *);
291fcf3ce44SJohn Forte static int cb_set_ref(void *, void *);
292fcf3ce44SJohn Forte static int cb_clear_ref(void *, void *);
293fcf3ce44SJohn Forte static int cb_add_child(void *, void *);
294fcf3ce44SJohn Forte static int cb_remove_child(void *, void *);
295fcf3ce44SJohn Forte static int cb_verify_ref(void *, void *);
296fcf3ce44SJohn Forte static int cb_ref_new2old(void *, void *);
297fcf3ce44SJohn Forte static int cb_new_ref(void *, void *);
298fcf3ce44SJohn Forte static int ref_new2old(
299fcf3ce44SJohn Forte 	lookup_ctrl_t *, isns_type_t, uint32_t, const isns_obj_t *);
300fcf3ce44SJohn Forte static int ref_new2new(
301fcf3ce44SJohn Forte 	lookup_ctrl_t *, const isns_obj_t *, const isns_obj_t *);
302fcf3ce44SJohn Forte static int new_ref(const isns_obj_t *, const isns_obj_t *);
303fcf3ce44SJohn Forte static uint32_t setup_parent_lcp(lookup_ctrl_t *, isns_obj_t *);
304fcf3ce44SJohn Forte static int set_obj_offline(isns_obj_t *);
305fcf3ce44SJohn Forte static int copy_attrs(isns_obj_t *, const isns_obj_t *);
306fcf3ce44SJohn Forte 
307fcf3ce44SJohn Forte static isns_obj_t *make_default_pg(const isns_obj_t *, const isns_obj_t *);
308fcf3ce44SJohn Forte static isns_obj_t *(*const make_ref[MAX_OBJ_TYPE])
309fcf3ce44SJohn Forte 	(const isns_obj_t *, const isns_obj_t *) = {
310fcf3ce44SJohn Forte 		NULL,
311fcf3ce44SJohn Forte 		NULL,
312fcf3ce44SJohn Forte 		&make_default_pg,
313fcf3ce44SJohn Forte 		&make_default_pg,
314fcf3ce44SJohn Forte 		NULL,
315fcf3ce44SJohn Forte 		NULL,
316fcf3ce44SJohn Forte 		NULL
317fcf3ce44SJohn Forte };
318fcf3ce44SJohn Forte 
319fcf3ce44SJohn Forte static uint32_t entity_hval(void *, uint16_t, uint32_t *);
320fcf3ce44SJohn Forte static uint32_t iscsi_hval(void *, uint16_t, uint32_t *);
321fcf3ce44SJohn Forte static uint32_t portal_hval(void *, uint16_t, uint32_t *);
322fcf3ce44SJohn Forte static uint32_t pg_hval(void *, uint16_t, uint32_t *);
323fcf3ce44SJohn Forte static uint32_t dd_hval(void *, uint16_t, uint32_t *);
324fcf3ce44SJohn Forte static uint32_t dds_hval(void *, uint16_t, uint32_t *);
325fcf3ce44SJohn Forte static uint32_t (*const hval_func[MAX_OBJ_TYPE])
326fcf3ce44SJohn Forte 	(void *, uint16_t, uint32_t *) = {
327fcf3ce44SJohn Forte 		NULL,
328fcf3ce44SJohn Forte 		&entity_hval,
329fcf3ce44SJohn Forte 		&iscsi_hval,
330fcf3ce44SJohn Forte 		&portal_hval,
331fcf3ce44SJohn Forte 		&pg_hval,
332fcf3ce44SJohn Forte 		&dd_hval,
333fcf3ce44SJohn Forte 		&dds_hval
334fcf3ce44SJohn Forte };
335fcf3ce44SJohn Forte 
336fcf3ce44SJohn Forte /*
337fcf3ce44SJohn Forte  * ****************************************************************************
338fcf3ce44SJohn Forte  *
339fcf3ce44SJohn Forte  * entity_hval:
340fcf3ce44SJohn Forte  *	caculate the hash value of a network entity object.
341fcf3ce44SJohn Forte  *
342fcf3ce44SJohn Forte  * p	- the pointer pointers to network entity object or
343fcf3ce44SJohn Forte  *	  the lookup control data, both have the key attribute
344fcf3ce44SJohn Forte  *	  of a network entity object.
345fcf3ce44SJohn Forte  * chunk- which chunk of the hash table.
346fcf3ce44SJohn Forte  * flags- pointer to flags.
347fcf3ce44SJohn Forte  * return - the hash value.
348fcf3ce44SJohn Forte  *
349fcf3ce44SJohn Forte  * ****************************************************************************
350fcf3ce44SJohn Forte  */
351fcf3ce44SJohn Forte static uint32_t
entity_hval(void * p,uint16_t chunk,uint32_t * flags)352fcf3ce44SJohn Forte entity_hval(
353fcf3ce44SJohn Forte 	void *p,
354fcf3ce44SJohn Forte 	/* LINTED E_FUNC_ARG_UNUSED */
355fcf3ce44SJohn Forte 	uint16_t chunk,
356fcf3ce44SJohn Forte 	uint32_t *flags
357fcf3ce44SJohn Forte )
358fcf3ce44SJohn Forte {
359fcf3ce44SJohn Forte 	uchar_t *key;
360fcf3ce44SJohn Forte 	isns_obj_t *obj;
361fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp;
362fcf3ce44SJohn Forte 
363fcf3ce44SJohn Forte 	if ((*flags & FLAGS_CTRL_MASK) == 0) {
364fcf3ce44SJohn Forte 		/* p pointers to a network entity object */
365fcf3ce44SJohn Forte 		obj = (isns_obj_t *)p;
366fcf3ce44SJohn Forte 		key = obj->attrs[ATTR_INDEX_ENTITY(ISNS_EID_ATTR_ID)].
367fcf3ce44SJohn Forte 		    value.ptr;
368fcf3ce44SJohn Forte 	} else {
369fcf3ce44SJohn Forte 		/* p is lookup control data */
370fcf3ce44SJohn Forte 		lcp = (lookup_ctrl_t *)p;
371fcf3ce44SJohn Forte 		key = lcp->data[0].ptr;
372fcf3ce44SJohn Forte 	}
373fcf3ce44SJohn Forte 
374fcf3ce44SJohn Forte 	return (htab_compute_hval(key));
375fcf3ce44SJohn Forte }
376fcf3ce44SJohn Forte 
377fcf3ce44SJohn Forte /*
378fcf3ce44SJohn Forte  * ****************************************************************************
379fcf3ce44SJohn Forte  *
380fcf3ce44SJohn Forte  * iscsi_hval:
381fcf3ce44SJohn Forte  *	caculate the hash value of an iscsi storage node object.
382fcf3ce44SJohn Forte  *
383fcf3ce44SJohn Forte  * p	- the pointer pointers to iscsi storage node object or
384fcf3ce44SJohn Forte  *	  the lookup control data, both have the key attribute
385fcf3ce44SJohn Forte  *	  of an iscsi storage node object.
386fcf3ce44SJohn Forte  * chunk- which chunk of the hash table.
387fcf3ce44SJohn Forte  * flags- pointer to flags.
388fcf3ce44SJohn Forte  * return - the hash value.
389fcf3ce44SJohn Forte  *
390fcf3ce44SJohn Forte  * ****************************************************************************
391fcf3ce44SJohn Forte  */
392fcf3ce44SJohn Forte static uint32_t
iscsi_hval(void * p,uint16_t chunk,uint32_t * flags)393fcf3ce44SJohn Forte iscsi_hval(
394fcf3ce44SJohn Forte 	void *p,
395fcf3ce44SJohn Forte 	/* LINTED E_FUNC_ARG_UNUSED */
396fcf3ce44SJohn Forte 	uint16_t chunk,
397fcf3ce44SJohn Forte 	uint32_t *flags
398fcf3ce44SJohn Forte )
399fcf3ce44SJohn Forte {
400fcf3ce44SJohn Forte 	uchar_t *key;
401fcf3ce44SJohn Forte 	isns_obj_t *obj;
402fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp;
403fcf3ce44SJohn Forte 
404fcf3ce44SJohn Forte 	if ((*flags & FLAGS_CTRL_MASK) == 0) {
405fcf3ce44SJohn Forte 		/* p pointers to an iscsi storage node object */
406fcf3ce44SJohn Forte 		obj = (isns_obj_t *)p;
407fcf3ce44SJohn Forte 		key = obj->attrs[ATTR_INDEX_ISCSI(ISNS_ISCSI_NAME_ATTR_ID)].
408fcf3ce44SJohn Forte 		    value.ptr;
409fcf3ce44SJohn Forte 	} else {
410fcf3ce44SJohn Forte 		/* p is lookup control data */
411fcf3ce44SJohn Forte 		lcp = (lookup_ctrl_t *)p;
412fcf3ce44SJohn Forte 		key = lcp->data[0].ptr;
413fcf3ce44SJohn Forte 	}
414fcf3ce44SJohn Forte 
415fcf3ce44SJohn Forte 	return (htab_compute_hval(key));
416fcf3ce44SJohn Forte }
417fcf3ce44SJohn Forte 
418fcf3ce44SJohn Forte /*
419fcf3ce44SJohn Forte  * ****************************************************************************
420fcf3ce44SJohn Forte  *
421fcf3ce44SJohn Forte  * portal_hval:
422fcf3ce44SJohn Forte  *	caculate the hash value of a portal object.
423fcf3ce44SJohn Forte  *
424fcf3ce44SJohn Forte  * p	- the pointer pointers to a portal object or the lookup control
425fcf3ce44SJohn Forte  *	  data, both have the key attributes of a portal object.
426fcf3ce44SJohn Forte  * chunk- which chunk of the hash table.
427fcf3ce44SJohn Forte  * flags- pointer to flags.
428fcf3ce44SJohn Forte  * return - the hash value.
429fcf3ce44SJohn Forte  *
430fcf3ce44SJohn Forte  * ****************************************************************************
431fcf3ce44SJohn Forte  */
432fcf3ce44SJohn Forte static uint32_t
portal_hval(void * p,uint16_t chunk,uint32_t * flags)433fcf3ce44SJohn Forte portal_hval(
434fcf3ce44SJohn Forte 	void *p,
435fcf3ce44SJohn Forte 	/* LINTED E_FUNC_ARG_UNUSED */
436fcf3ce44SJohn Forte 	uint16_t chunk,
437fcf3ce44SJohn Forte 	uint32_t *flags
438fcf3ce44SJohn Forte )
439fcf3ce44SJohn Forte {
440fcf3ce44SJohn Forte 	char buff[INET6_ADDRSTRLEN + 8] = { 0 };
441fcf3ce44SJohn Forte 	char buff2[8] = { 0 };
442fcf3ce44SJohn Forte 	uchar_t *key;
443fcf3ce44SJohn Forte 	isns_obj_t *obj;
444fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp;
445fcf3ce44SJohn Forte 
446fcf3ce44SJohn Forte 	in6_addr_t *ip;
447fcf3ce44SJohn Forte 	uint32_t port;
448fcf3ce44SJohn Forte 
449fcf3ce44SJohn Forte 	if ((*flags & FLAGS_CTRL_MASK) == 0) {
450fcf3ce44SJohn Forte 		/* p pointers to a portal object */
451fcf3ce44SJohn Forte 		obj = (isns_obj_t *)p;
452fcf3ce44SJohn Forte 		ip = obj->attrs[ATTR_INDEX_PORTAL
453fcf3ce44SJohn Forte 		    (ISNS_PORTAL_IP_ADDR_ATTR_ID)].value.ip;
454fcf3ce44SJohn Forte 		port = obj->attrs[ATTR_INDEX_PORTAL
455fcf3ce44SJohn Forte 		    (ISNS_PORTAL_PORT_ATTR_ID)].value.ui;
456fcf3ce44SJohn Forte 	} else {
457fcf3ce44SJohn Forte 		/* p is lookup control data */
458fcf3ce44SJohn Forte 		lcp = (lookup_ctrl_t *)p;
459fcf3ce44SJohn Forte 		ip = lcp->data[0].ip;
460fcf3ce44SJohn Forte 		port = lcp->data[1].ui;
461fcf3ce44SJohn Forte 	}
462fcf3ce44SJohn Forte 
463fcf3ce44SJohn Forte 	key = (uchar_t *)inet_ntop(AF_INET6, (void *)ip,
464fcf3ce44SJohn Forte 	    buff, sizeof (buff));
465fcf3ce44SJohn Forte 	(void) snprintf(buff2, sizeof (buff2), "%d", port);
466fcf3ce44SJohn Forte 	(void) strcat((char *)key, buff2);
467fcf3ce44SJohn Forte 
468fcf3ce44SJohn Forte 	return (htab_compute_hval(key));
469fcf3ce44SJohn Forte }
470fcf3ce44SJohn Forte 
471fcf3ce44SJohn Forte /*
472fcf3ce44SJohn Forte  * ****************************************************************************
473fcf3ce44SJohn Forte  *
474fcf3ce44SJohn Forte  * pg_hval:
475fcf3ce44SJohn Forte  *	caculate the hash value of a portal group object.
476fcf3ce44SJohn Forte  *
477fcf3ce44SJohn Forte  * p	- the pointer pointers to a portal group object or the lookup
478fcf3ce44SJohn Forte  *	  control data, both have the key attributes of a portal object.
479fcf3ce44SJohn Forte  * chunk- which chunk of the hash table.
480fcf3ce44SJohn Forte  * flags- pointer to flags.
481fcf3ce44SJohn Forte  * return - the hash value.
482fcf3ce44SJohn Forte  *
483fcf3ce44SJohn Forte  * ****************************************************************************
484fcf3ce44SJohn Forte  */
485fcf3ce44SJohn Forte static uint32_t
pg_hval(void * p,uint16_t chunk,uint32_t * flags)486fcf3ce44SJohn Forte pg_hval(
487fcf3ce44SJohn Forte 	void *p,
488fcf3ce44SJohn Forte 	uint16_t chunk,
489fcf3ce44SJohn Forte 	uint32_t *flags
490fcf3ce44SJohn Forte )
491fcf3ce44SJohn Forte {
492fcf3ce44SJohn Forte 	char buff[INET6_ADDRSTRLEN + 8] = { 0 };
493fcf3ce44SJohn Forte 	char buff2[8] = { 0 };
494fcf3ce44SJohn Forte 	uchar_t *key = NULL;
495fcf3ce44SJohn Forte 	isns_obj_t *obj;
496fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp;
497fcf3ce44SJohn Forte 
498fcf3ce44SJohn Forte 	in6_addr_t *ip = NULL;
499fcf3ce44SJohn Forte 	uint32_t port;
500fcf3ce44SJohn Forte 
501fcf3ce44SJohn Forte 	if ((*flags & FLAGS_CTRL_MASK) == 0) {
502fcf3ce44SJohn Forte 		/* p is a portal group object */
503fcf3ce44SJohn Forte 		obj = (isns_obj_t *)p;
504fcf3ce44SJohn Forte 		if (chunk == 0) {
505fcf3ce44SJohn Forte 			/* the first chunk */
506fcf3ce44SJohn Forte 			key = obj->attrs[ATTR_INDEX_PG
507fcf3ce44SJohn Forte 			    (ISNS_PG_ISCSI_NAME_ATTR_ID)].value.ptr;
508fcf3ce44SJohn Forte 		} else {
509fcf3ce44SJohn Forte 			/* another chunk */
510fcf3ce44SJohn Forte 			ip = obj->attrs[ATTR_INDEX_PG
511fcf3ce44SJohn Forte 			    (ISNS_PG_PORTAL_IP_ADDR_ATTR_ID)].value.ip;
512fcf3ce44SJohn Forte 			port = obj->attrs[ATTR_INDEX_PG
513fcf3ce44SJohn Forte 			    (ISNS_PG_PORTAL_PORT_ATTR_ID)].value.ui;
514fcf3ce44SJohn Forte 		}
515fcf3ce44SJohn Forte 	} else {
516fcf3ce44SJohn Forte 		/* p is a lookup control data */
517fcf3ce44SJohn Forte 		lcp = (lookup_ctrl_t *)p;
518fcf3ce44SJohn Forte 		/* clear the chunk flags */
519fcf3ce44SJohn Forte 		*flags &= ~FLAGS_CHUNK_MASK;
520fcf3ce44SJohn Forte 		if (lcp->op[0] == OP_STRING) {
521fcf3ce44SJohn Forte 			/* the first chunk */
522fcf3ce44SJohn Forte 			key = lcp->data[0].ptr;
523fcf3ce44SJohn Forte 		} else {
524fcf3ce44SJohn Forte 			/* another chunk */
525fcf3ce44SJohn Forte 			ip = lcp->data[0].ip;
526fcf3ce44SJohn Forte 			port = lcp->data[1].ui;
527fcf3ce44SJohn Forte 			*flags |= 1;
528fcf3ce44SJohn Forte 		}
529fcf3ce44SJohn Forte 	}
530fcf3ce44SJohn Forte 
531fcf3ce44SJohn Forte 	if (key == NULL) {
532fcf3ce44SJohn Forte 		key = (uchar_t *)inet_ntop(AF_INET6, (void *)ip,
533fcf3ce44SJohn Forte 		    buff, sizeof (buff));
534fcf3ce44SJohn Forte 		(void) snprintf(buff2, sizeof (buff2), "%d", port);
535fcf3ce44SJohn Forte 		(void) strcat((char *)key, buff2);
536fcf3ce44SJohn Forte 	}
537fcf3ce44SJohn Forte 
538fcf3ce44SJohn Forte 	return (htab_compute_hval(key));
539fcf3ce44SJohn Forte }
540fcf3ce44SJohn Forte 
541fcf3ce44SJohn Forte /*
542fcf3ce44SJohn Forte  * ****************************************************************************
543fcf3ce44SJohn Forte  *
544fcf3ce44SJohn Forte  * dd_hval:
545fcf3ce44SJohn Forte  *	caculate the hash value of a DD object.
546fcf3ce44SJohn Forte  *
547fcf3ce44SJohn Forte  * p	- the pointer pointers to a DD object or the lookup control data,
548fcf3ce44SJohn Forte  *	  both have the key attributes of a DD object.
549fcf3ce44SJohn Forte  * chunk- which chunk of the hash table.
550fcf3ce44SJohn Forte  * flags- pointer to flags.
551fcf3ce44SJohn Forte  * return - the hash value.
552fcf3ce44SJohn Forte  *
553fcf3ce44SJohn Forte  * ****************************************************************************
554fcf3ce44SJohn Forte  */
555fcf3ce44SJohn Forte static uint32_t
dd_hval(void * p,uint16_t chunk,uint32_t * flags)556fcf3ce44SJohn Forte dd_hval(
557fcf3ce44SJohn Forte 	void *p,
558fcf3ce44SJohn Forte 	/* LINTED E_FUNC_ARG_UNUSED */
559fcf3ce44SJohn Forte 	uint16_t chunk,
560fcf3ce44SJohn Forte 	uint32_t *flags
561fcf3ce44SJohn Forte )
562fcf3ce44SJohn Forte {
563fcf3ce44SJohn Forte 	uchar_t *key;
564fcf3ce44SJohn Forte 	isns_obj_t *obj;
565fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp;
566fcf3ce44SJohn Forte 
567fcf3ce44SJohn Forte 	if ((*flags & FLAGS_CTRL_MASK) == 0) {
568fcf3ce44SJohn Forte 		/* p is a DD object */
569fcf3ce44SJohn Forte 		obj = (isns_obj_t *)p;
570fcf3ce44SJohn Forte 		key = obj->attrs[ATTR_INDEX_DD(ISNS_DD_NAME_ATTR_ID)].
571fcf3ce44SJohn Forte 		    value.ptr;
572fcf3ce44SJohn Forte 	} else {
573fcf3ce44SJohn Forte 		/* p is a lookup control data */
574fcf3ce44SJohn Forte 		lcp = (lookup_ctrl_t *)p;
575fcf3ce44SJohn Forte 		key = lcp->data[0].ptr;
576fcf3ce44SJohn Forte 	}
577fcf3ce44SJohn Forte 
578fcf3ce44SJohn Forte 	return (htab_compute_hval(key));
579fcf3ce44SJohn Forte }
580fcf3ce44SJohn Forte 
581fcf3ce44SJohn Forte /*
582fcf3ce44SJohn Forte  * ****************************************************************************
583fcf3ce44SJohn Forte  *
584fcf3ce44SJohn Forte  * dds_hval:
585fcf3ce44SJohn Forte  *	caculate the hash value of a DD-set object.
586fcf3ce44SJohn Forte  *
587fcf3ce44SJohn Forte  * p	- the pointer pointers to a DD-set object or the lookup control data,
588fcf3ce44SJohn Forte  *	  both have the key attributes of a DD-set object.
589fcf3ce44SJohn Forte  * chunk- which chunk of the hash table.
590fcf3ce44SJohn Forte  * flags- pointer to flags.
591fcf3ce44SJohn Forte  * return - the hash value.
592fcf3ce44SJohn Forte  *
593fcf3ce44SJohn Forte  * ****************************************************************************
594fcf3ce44SJohn Forte  */
595fcf3ce44SJohn Forte static uint32_t
dds_hval(void * p,uint16_t chunk,uint32_t * flags)596fcf3ce44SJohn Forte dds_hval(
597fcf3ce44SJohn Forte 	void *p,
598fcf3ce44SJohn Forte 	/* LINTED E_FUNC_ARG_UNUSED */
599fcf3ce44SJohn Forte 	uint16_t chunk,
600fcf3ce44SJohn Forte 	uint32_t *flags
601fcf3ce44SJohn Forte )
602fcf3ce44SJohn Forte {
603fcf3ce44SJohn Forte 	uchar_t *key;
604fcf3ce44SJohn Forte 	isns_obj_t *obj;
605fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp;
606fcf3ce44SJohn Forte 
607fcf3ce44SJohn Forte 	if ((*flags & FLAGS_CTRL_MASK) == 0) {
608fcf3ce44SJohn Forte 		/* p is a DD-set object */
609fcf3ce44SJohn Forte 		obj = (isns_obj_t *)p;
610fcf3ce44SJohn Forte 		key = obj->attrs[ATTR_INDEX_DDS(ISNS_DD_SET_NAME_ATTR_ID)].
611fcf3ce44SJohn Forte 		    value.ptr;
612fcf3ce44SJohn Forte 	} else {
613fcf3ce44SJohn Forte 		/* p is lookup control data */
614fcf3ce44SJohn Forte 		lcp = (lookup_ctrl_t *)p;
615fcf3ce44SJohn Forte 		key = lcp->data[0].ptr;
616fcf3ce44SJohn Forte 	}
617fcf3ce44SJohn Forte 
618fcf3ce44SJohn Forte 	return (htab_compute_hval(key));
619fcf3ce44SJohn Forte }
620fcf3ce44SJohn Forte 
621fcf3ce44SJohn Forte /*
622fcf3ce44SJohn Forte  * ****************************************************************************
623fcf3ce44SJohn Forte  *
624fcf3ce44SJohn Forte  * obj_hval:
625fcf3ce44SJohn Forte  *	caculate the hash value of an object.
626fcf3ce44SJohn Forte  *
627fcf3ce44SJohn Forte  * p	- the pointer pointers to an object or lookup control data,
628fcf3ce44SJohn Forte  *	  both has the object type and the key attributes of an object.
629fcf3ce44SJohn Forte  * chunk- which chunk of the hash table.
630fcf3ce44SJohn Forte  * flags- pointer to flags.
631fcf3ce44SJohn Forte  * return - the hash value.
632fcf3ce44SJohn Forte  *
633fcf3ce44SJohn Forte  * ****************************************************************************
634fcf3ce44SJohn Forte  */
635fcf3ce44SJohn Forte uint32_t
obj_hval(void * p,uint16_t chunk,uint32_t * flags)636fcf3ce44SJohn Forte obj_hval(
637fcf3ce44SJohn Forte 	void *p,
638fcf3ce44SJohn Forte 	uint16_t chunk,
639fcf3ce44SJohn Forte 	uint32_t *flags
640fcf3ce44SJohn Forte )
641fcf3ce44SJohn Forte {
642fcf3ce44SJohn Forte 	isns_type_t type = ((isns_obj_t *)p)->type;
643fcf3ce44SJohn Forte 
644fcf3ce44SJohn Forte 	return (hval_func[type](p, chunk, flags));
645fcf3ce44SJohn Forte }
646fcf3ce44SJohn Forte 
647fcf3ce44SJohn Forte /*
648fcf3ce44SJohn Forte  * ****************************************************************************
649fcf3ce44SJohn Forte  *
650fcf3ce44SJohn Forte  * get_obj_uid:
651fcf3ce44SJohn Forte  *	get the UID of an object.
652fcf3ce44SJohn Forte  *
653fcf3ce44SJohn Forte  * p	- the pointer pointers to an object.
654fcf3ce44SJohn Forte  * return - the UID.
655fcf3ce44SJohn Forte  *
656fcf3ce44SJohn Forte  * ****************************************************************************
657fcf3ce44SJohn Forte  */
658fcf3ce44SJohn Forte uint32_t
get_obj_uid(const void * p)659fcf3ce44SJohn Forte get_obj_uid(
660fcf3ce44SJohn Forte 	const void *p
661fcf3ce44SJohn Forte )
662fcf3ce44SJohn Forte {
663fcf3ce44SJohn Forte 	isns_obj_t *obj = (isns_obj_t *)p;
664fcf3ce44SJohn Forte 	isns_attr_t *attr = &obj->attrs[UID_ATTR_INDEX[obj->type]];
665fcf3ce44SJohn Forte 	uint32_t uid = attr->value.ui;
666fcf3ce44SJohn Forte 	return (uid);
667fcf3ce44SJohn Forte }
668fcf3ce44SJohn Forte 
669fcf3ce44SJohn Forte /*
670fcf3ce44SJohn Forte  * ****************************************************************************
671fcf3ce44SJohn Forte  *
672fcf3ce44SJohn Forte  * set_obj_uid:
673fcf3ce44SJohn Forte  *	set the UID of an object.
674fcf3ce44SJohn Forte  *
675fcf3ce44SJohn Forte  * p	- the pointer pointers to an object.
676fcf3ce44SJohn Forte  * uid	- the UID.
677fcf3ce44SJohn Forte  * return - the UID.
678fcf3ce44SJohn Forte  *
679fcf3ce44SJohn Forte  * ****************************************************************************
680fcf3ce44SJohn Forte  */
681fcf3ce44SJohn Forte uint32_t
set_obj_uid(void * p,uint32_t uid)682fcf3ce44SJohn Forte set_obj_uid(
683fcf3ce44SJohn Forte 	void *p,
684fcf3ce44SJohn Forte 	uint32_t uid
685fcf3ce44SJohn Forte )
686fcf3ce44SJohn Forte {
687fcf3ce44SJohn Forte 	isns_obj_t *obj = (isns_obj_t *)p;
688fcf3ce44SJohn Forte 	isns_attr_t *attr = &obj->attrs[UID_ATTR_INDEX[obj->type]];
689fcf3ce44SJohn Forte 
690fcf3ce44SJohn Forte 	/* set the tag, len and value */
691fcf3ce44SJohn Forte 	attr->tag = UID_TAG[obj->type];
692fcf3ce44SJohn Forte 	attr->len = 4;
693fcf3ce44SJohn Forte 	attr->value.ui = uid;
694fcf3ce44SJohn Forte 
695fcf3ce44SJohn Forte 	return (uid);
696fcf3ce44SJohn Forte }
697fcf3ce44SJohn Forte 
698fcf3ce44SJohn Forte /*
699fcf3ce44SJohn Forte  * ****************************************************************************
700fcf3ce44SJohn Forte  *
701fcf3ce44SJohn Forte  * obj_cmp:
702fcf3ce44SJohn Forte  *	compare between two objects or an object with a lookup control data.
703fcf3ce44SJohn Forte  *
704fcf3ce44SJohn Forte  * p1	- the pointer points to an object.
705fcf3ce44SJohn Forte  * p2	- the pointer points to an object or a lookup control data.
706fcf3ce44SJohn Forte  * flags- 0: p2 is an object; otherwise p2 is a lookup control data.
707fcf3ce44SJohn Forte  * return - the comparsion result.
708fcf3ce44SJohn Forte  *
709fcf3ce44SJohn Forte  * ****************************************************************************
710fcf3ce44SJohn Forte  */
711fcf3ce44SJohn Forte int
obj_cmp(void * p1,void * p2,int flags)712fcf3ce44SJohn Forte obj_cmp(
713fcf3ce44SJohn Forte 	void *p1,
714fcf3ce44SJohn Forte 	void *p2,
715fcf3ce44SJohn Forte 	int flags
716fcf3ce44SJohn Forte )
717fcf3ce44SJohn Forte {
718fcf3ce44SJohn Forte 	isns_obj_t *obj = (isns_obj_t *)p1;
719fcf3ce44SJohn Forte 	lookup_ctrl_t buff = { 0 };
720fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp;
721fcf3ce44SJohn Forte 	uint32_t uid;
722fcf3ce44SJohn Forte 
723fcf3ce44SJohn Forte 	if (flags == 0) {
724fcf3ce44SJohn Forte 		lcp = set_lookup_ctrl(&buff, (isns_obj_t *)p2);
725fcf3ce44SJohn Forte 	} else {
726fcf3ce44SJohn Forte 		lcp = (lookup_ctrl_t *)p2;
727fcf3ce44SJohn Forte 		uid = get_obj_uid(obj);
728fcf3ce44SJohn Forte 		/* the object are linked with decending order by */
729fcf3ce44SJohn Forte 		/* the object UID, if the object UID is greater than */
730fcf3ce44SJohn Forte 		/* or equal to the current UID, it needs to compare */
731fcf3ce44SJohn Forte 		/* for the next one. */
732fcf3ce44SJohn Forte 		if (lcp->curr_uid != 0 && uid >= lcp->curr_uid) {
733fcf3ce44SJohn Forte 			return (-1);
734fcf3ce44SJohn Forte 		}
735fcf3ce44SJohn Forte 	}
736fcf3ce44SJohn Forte 
737fcf3ce44SJohn Forte 	return (key_cmp(lcp, obj));
738fcf3ce44SJohn Forte }
739fcf3ce44SJohn Forte 
740fcf3ce44SJohn Forte /*
741fcf3ce44SJohn Forte  * ****************************************************************************
742fcf3ce44SJohn Forte  *
743fcf3ce44SJohn Forte  * replace_object:
744fcf3ce44SJohn Forte  *	replace an existing object with the new one.
745fcf3ce44SJohn Forte  *
746fcf3ce44SJohn Forte  * p1	- the pointer points to an object being replaced.
747fcf3ce44SJohn Forte  * p2	- the pointer points to a new object.
748fcf3ce44SJohn Forte  * uid_p- points to uid for returning.
749fcf3ce44SJohn Forte  * flag	- 0: do not free the source object, otherwise free it.
750fcf3ce44SJohn Forte  * return - error code.
751fcf3ce44SJohn Forte  *
752fcf3ce44SJohn Forte  * ****************************************************************************
753fcf3ce44SJohn Forte  */
754fcf3ce44SJohn Forte int
replace_object(void * p1,void * p2,uint32_t * uid_p,int flag)755fcf3ce44SJohn Forte replace_object(
756fcf3ce44SJohn Forte 	void *p1,
757fcf3ce44SJohn Forte 	void *p2,
758fcf3ce44SJohn Forte 	uint32_t *uid_p,
759fcf3ce44SJohn Forte 	int flag
760fcf3ce44SJohn Forte )
761fcf3ce44SJohn Forte {
762fcf3ce44SJohn Forte 	int ec = 0;
763fcf3ce44SJohn Forte 
764fcf3ce44SJohn Forte #ifndef SKIP_SRC_AUTH
765fcf3ce44SJohn Forte 	uint32_t *pp_dst, *pp_src, swap;
766fcf3ce44SJohn Forte #endif
767fcf3ce44SJohn Forte 	int online;
768fcf3ce44SJohn Forte 
769fcf3ce44SJohn Forte 	isns_obj_t *dst = (isns_obj_t *)p1;
770fcf3ce44SJohn Forte 	isns_obj_t *src = (isns_obj_t *)p2;
771fcf3ce44SJohn Forte 
772fcf3ce44SJohn Forte 	if (src->type == OBJ_DD || src->type == OBJ_DDS) {
773fcf3ce44SJohn Forte 		/* replace not allowed */
774fcf3ce44SJohn Forte 		return (ERR_NAME_IN_USE);
775fcf3ce44SJohn Forte 	}
776fcf3ce44SJohn Forte 
777fcf3ce44SJohn Forte 	online = is_obj_online(dst);
778fcf3ce44SJohn Forte 
779fcf3ce44SJohn Forte 	/* set cache update flag */
780fcf3ce44SJohn Forte 	SET_CACHE_UPDATED();
781fcf3ce44SJohn Forte 
782fcf3ce44SJohn Forte 	/* update parent uid */
783fcf3ce44SJohn Forte #ifndef SKIP_SRC_AUTH
784fcf3ce44SJohn Forte 	pp_dst = get_parent_p(dst);
785fcf3ce44SJohn Forte 	if (pp_dst != NULL) {
786fcf3ce44SJohn Forte 		pp_src = get_parent_p(src);
787fcf3ce44SJohn Forte 		swap = *pp_dst;
788fcf3ce44SJohn Forte 		*pp_dst = *pp_src;
789fcf3ce44SJohn Forte 		if (swap != 0) {
790fcf3ce44SJohn Forte 			*pp_src = swap;
791fcf3ce44SJohn Forte 		}
792fcf3ce44SJohn Forte 	}
793fcf3ce44SJohn Forte #endif
794fcf3ce44SJohn Forte 
795fcf3ce44SJohn Forte 	/* update all of attributes */
796fcf3ce44SJohn Forte 	if (copy_attrs(dst, src) != 0) {
797fcf3ce44SJohn Forte 		return (ISNS_RSP_INTERNAL_ERROR);
798fcf3ce44SJohn Forte 	}
799fcf3ce44SJohn Forte 
800fcf3ce44SJohn Forte 	/* free up the src object */
801fcf3ce44SJohn Forte 	if (flag != 0) {
802fcf3ce44SJohn Forte 		(void) free_object(src);
803fcf3ce44SJohn Forte 	} else if (online == 0) {
804fcf3ce44SJohn Forte 		(void) set_obj_uid(src, get_obj_uid(dst));
805fcf3ce44SJohn Forte 		(void) set_obj_offline(src);
806fcf3ce44SJohn Forte 	}
807fcf3ce44SJohn Forte 
808fcf3ce44SJohn Forte 	/* update data store */
809fcf3ce44SJohn Forte 	if (sys_q != NULL) {
810fcf3ce44SJohn Forte 		ec = write_data(DATA_UPDATE, dst);
811fcf3ce44SJohn Forte 	} else {
812fcf3ce44SJohn Forte 		/* we should never have duplicated entry in data store */
813fcf3ce44SJohn Forte 		ec = ISNS_RSP_INTERNAL_ERROR;
814fcf3ce44SJohn Forte 	}
815fcf3ce44SJohn Forte 
816fcf3ce44SJohn Forte 	/* trigger a scn */
817fcf3ce44SJohn Forte 	if (ec == 0) {
818fcf3ce44SJohn Forte 		if (scn_q != NULL) {
819fcf3ce44SJohn Forte 			(void) make_scn((online == 0) ?
820fcf3ce44SJohn Forte 			    ISNS_OBJECT_ADDED :
821fcf3ce44SJohn Forte 			    ISNS_OBJECT_UPDATED,
822fcf3ce44SJohn Forte 			    dst);
823fcf3ce44SJohn Forte 		}
824fcf3ce44SJohn Forte 		if (uid_p != NULL) {
825fcf3ce44SJohn Forte 			*uid_p = get_obj_uid(dst);
826fcf3ce44SJohn Forte 		}
827fcf3ce44SJohn Forte 	}
828fcf3ce44SJohn Forte 
829fcf3ce44SJohn Forte 	return (ec);
830fcf3ce44SJohn Forte }
831fcf3ce44SJohn Forte 
832fcf3ce44SJohn Forte /*
833fcf3ce44SJohn Forte  * ****************************************************************************
834fcf3ce44SJohn Forte  *
835fcf3ce44SJohn Forte  * add_object:
836fcf3ce44SJohn Forte  *	post function after adding a new object.
837fcf3ce44SJohn Forte  *
838fcf3ce44SJohn Forte  * p	- object which has been added.
839fcf3ce44SJohn Forte  * return - error code.
840fcf3ce44SJohn Forte  *
841fcf3ce44SJohn Forte  * ****************************************************************************
842fcf3ce44SJohn Forte  */
843fcf3ce44SJohn Forte int
add_object(void * p)844fcf3ce44SJohn Forte add_object(
845fcf3ce44SJohn Forte 	void *p
846fcf3ce44SJohn Forte )
847fcf3ce44SJohn Forte {
848fcf3ce44SJohn Forte 	int ec = 0;
849fcf3ce44SJohn Forte 
850fcf3ce44SJohn Forte 	isns_obj_t *obj = (isns_obj_t *)p;
851fcf3ce44SJohn Forte 
852fcf3ce44SJohn Forte 	/* add the new object to data store */
853fcf3ce44SJohn Forte 	if (sys_q != NULL) {
854fcf3ce44SJohn Forte 		ec = write_data(DATA_ADD, obj);
855fcf3ce44SJohn Forte 	}
856fcf3ce44SJohn Forte 
857fcf3ce44SJohn Forte 	/* trigger a scn */
858fcf3ce44SJohn Forte 	if (ec == 0 && scn_q != NULL) {
859fcf3ce44SJohn Forte 		(void) make_scn(ISNS_OBJECT_ADDED, obj);
860fcf3ce44SJohn Forte 	}
861fcf3ce44SJohn Forte 
862fcf3ce44SJohn Forte 	return (ec);
863fcf3ce44SJohn Forte }
864fcf3ce44SJohn Forte 
865fcf3ce44SJohn Forte /*
866fcf3ce44SJohn Forte  * ****************************************************************************
867fcf3ce44SJohn Forte  *
868fcf3ce44SJohn Forte  * obj_tab_init:
869fcf3ce44SJohn Forte  *	initialize the object hash tables.
870fcf3ce44SJohn Forte  *
871fcf3ce44SJohn Forte  * c	- points to the cache.
872fcf3ce44SJohn Forte  * return - error code.
873fcf3ce44SJohn Forte  *
874fcf3ce44SJohn Forte  * ****************************************************************************
875fcf3ce44SJohn Forte  */
876fcf3ce44SJohn Forte int
obj_tab_init(struct cache * c)877fcf3ce44SJohn Forte obj_tab_init(
878fcf3ce44SJohn Forte 	struct cache *c
879fcf3ce44SJohn Forte )
880fcf3ce44SJohn Forte {
881fcf3ce44SJohn Forte 	htab_t *t;
882fcf3ce44SJohn Forte 
883fcf3ce44SJohn Forte 	htab_init();
884fcf3ce44SJohn Forte 
885fcf3ce44SJohn Forte 	/*
886fcf3ce44SJohn Forte 	 * allocate an array of pointer for the object hash tables.
887fcf3ce44SJohn Forte 	 */
888fcf3ce44SJohn Forte 	c->t = (struct htab **)calloc(sizeof (struct htab *), MAX_OBJ_TYPE);
889fcf3ce44SJohn Forte 	if (c->t == NULL) {
890fcf3ce44SJohn Forte 		return (1);
891fcf3ce44SJohn Forte 	}
892fcf3ce44SJohn Forte 
893fcf3ce44SJohn Forte 	/*
894fcf3ce44SJohn Forte 	 * hash table for network entity objects.
895fcf3ce44SJohn Forte 	 */
896fcf3ce44SJohn Forte 	t = htab_create(UID_FLAGS_SEQ, 8, 1);
897fcf3ce44SJohn Forte 	if (t != NULL) {
898fcf3ce44SJohn Forte 		t->c = c;
899fcf3ce44SJohn Forte 		c->t[OBJ_ENTITY] = t;
900fcf3ce44SJohn Forte 	} else {
901fcf3ce44SJohn Forte 		return (1);
902fcf3ce44SJohn Forte 	}
903fcf3ce44SJohn Forte 
904fcf3ce44SJohn Forte 	/*
905fcf3ce44SJohn Forte 	 * hash table for iscsi storage node objects.
906fcf3ce44SJohn Forte 	 */
907fcf3ce44SJohn Forte 	t = htab_create(UID_FLAGS_SEQ, 8, 1);
908fcf3ce44SJohn Forte 	if (t != NULL) {
909fcf3ce44SJohn Forte 		t->c = c;
910fcf3ce44SJohn Forte 		c->t[OBJ_ISCSI] = t;
911fcf3ce44SJohn Forte 	} else {
912fcf3ce44SJohn Forte 		return (1);
913fcf3ce44SJohn Forte 	}
914fcf3ce44SJohn Forte 
915fcf3ce44SJohn Forte 	/*
916fcf3ce44SJohn Forte 	 * hash table for portal objects.
917fcf3ce44SJohn Forte 	 */
918fcf3ce44SJohn Forte 	t = htab_create(UID_FLAGS_SEQ, 8, 1);
919fcf3ce44SJohn Forte 	if (t != NULL) {
920fcf3ce44SJohn Forte 		t->c = c;
921fcf3ce44SJohn Forte 		c->t[OBJ_PORTAL] = t;
922fcf3ce44SJohn Forte 	} else {
923fcf3ce44SJohn Forte 		return (1);
924fcf3ce44SJohn Forte 	}
925fcf3ce44SJohn Forte 
926fcf3ce44SJohn Forte 	/*
927fcf3ce44SJohn Forte 	 * hash table for portal group objects.
928fcf3ce44SJohn Forte 	 */
929fcf3ce44SJohn Forte 	t = htab_create(UID_FLAGS_SEQ, 8, 2);
930fcf3ce44SJohn Forte 	if (t != NULL) {
931fcf3ce44SJohn Forte 		t->c = c;
932fcf3ce44SJohn Forte 		c->t[OBJ_PG] = t;
933fcf3ce44SJohn Forte 	} else {
934fcf3ce44SJohn Forte 		return (1);
935fcf3ce44SJohn Forte 	}
936fcf3ce44SJohn Forte 
937fcf3ce44SJohn Forte 	/*
938fcf3ce44SJohn Forte 	 * hash table for discovery domain objects.
939fcf3ce44SJohn Forte 	 */
940fcf3ce44SJohn Forte 	t = htab_create(0, 6, 1);
941fcf3ce44SJohn Forte 	if (t != NULL) {
942fcf3ce44SJohn Forte 		t->c = c;
943fcf3ce44SJohn Forte 		c->t[OBJ_DD] = t;
944fcf3ce44SJohn Forte 	} else {
945fcf3ce44SJohn Forte 		return (1);
946fcf3ce44SJohn Forte 	}
947fcf3ce44SJohn Forte 
948fcf3ce44SJohn Forte 	/*
949fcf3ce44SJohn Forte 	 * hash table for discovery domain set objects.
950fcf3ce44SJohn Forte 	 */
951fcf3ce44SJohn Forte 	t = htab_create(0, 4, 1);
952fcf3ce44SJohn Forte 	if (t != NULL) {
953fcf3ce44SJohn Forte 		t->c = c;
954fcf3ce44SJohn Forte 		c->t[OBJ_DDS] = t;
955fcf3ce44SJohn Forte 	} else {
956fcf3ce44SJohn Forte 		return (1);
957fcf3ce44SJohn Forte 	}
958fcf3ce44SJohn Forte 
959fcf3ce44SJohn Forte 	return (0);
960fcf3ce44SJohn Forte }
961fcf3ce44SJohn Forte 
962fcf3ce44SJohn Forte /*
963fcf3ce44SJohn Forte  * ****************************************************************************
964fcf3ce44SJohn Forte  *
965fcf3ce44SJohn Forte  * get_ref_np:
966fcf3ce44SJohn Forte  *	get the ref pointer of the portal group object.
967fcf3ce44SJohn Forte  *
968fcf3ce44SJohn Forte  * obj	- portal group object.
969fcf3ce44SJohn Forte  * return - ref pointer.
970fcf3ce44SJohn Forte  *
971fcf3ce44SJohn Forte  * ****************************************************************************
972fcf3ce44SJohn Forte  */
973fcf3ce44SJohn Forte static uint32_t *
get_ref_np(isns_obj_t * obj,int n)974fcf3ce44SJohn Forte get_ref_np(
975fcf3ce44SJohn Forte 	isns_obj_t *obj,
976fcf3ce44SJohn Forte 	int n
977fcf3ce44SJohn Forte )
978fcf3ce44SJohn Forte {
979fcf3ce44SJohn Forte 	uint32_t *refp =
980fcf3ce44SJohn Forte 	    obj->type == OBJ_PG ? &((isns_pg_t *)obj)->ref[n] : NULL;
981fcf3ce44SJohn Forte 
982fcf3ce44SJohn Forte 	return (refp);
983fcf3ce44SJohn Forte }
984fcf3ce44SJohn Forte 
985fcf3ce44SJohn Forte #ifdef DEBUG
986fcf3ce44SJohn Forte uint32_t
987fcf3ce44SJohn Forte #else
988fcf3ce44SJohn Forte static uint32_t
989fcf3ce44SJohn Forte #endif
get_ref_n(isns_obj_t * obj,int n)990fcf3ce44SJohn Forte get_ref_n(
991fcf3ce44SJohn Forte 	isns_obj_t *obj,
992fcf3ce44SJohn Forte 	int n
993fcf3ce44SJohn Forte )
994fcf3ce44SJohn Forte {
995fcf3ce44SJohn Forte 	return (*get_ref_np(obj, n));
996fcf3ce44SJohn Forte }
997fcf3ce44SJohn Forte 
998fcf3ce44SJohn Forte static uint32_t *
get_ref_p(isns_obj_t * obj,isns_type_t rt)999fcf3ce44SJohn Forte get_ref_p(
1000fcf3ce44SJohn Forte 	isns_obj_t *obj,
1001fcf3ce44SJohn Forte 	isns_type_t rt
1002fcf3ce44SJohn Forte )
1003fcf3ce44SJohn Forte {
1004fcf3ce44SJohn Forte 	isns_type_t t = obj->type;
1005fcf3ce44SJohn Forte 
1006fcf3ce44SJohn Forte 	int i = 0;
1007fcf3ce44SJohn Forte 	while (i < NUM_OF_REF[t]) {
1008fcf3ce44SJohn Forte 		if (rt == TYPE_OF_REF[t][i + 1]) {
1009fcf3ce44SJohn Forte 			return (get_ref_np(obj, i));
1010fcf3ce44SJohn Forte 		}
1011fcf3ce44SJohn Forte 		i ++;
1012fcf3ce44SJohn Forte 	}
1013fcf3ce44SJohn Forte 
1014fcf3ce44SJohn Forte 	return (NULL);
1015fcf3ce44SJohn Forte }
1016fcf3ce44SJohn Forte 
1017fcf3ce44SJohn Forte uint32_t
get_ref_t(isns_obj_t * obj,isns_type_t type)1018fcf3ce44SJohn Forte get_ref_t(
1019fcf3ce44SJohn Forte 	isns_obj_t *obj,
1020fcf3ce44SJohn Forte 	isns_type_t type
1021fcf3ce44SJohn Forte )
1022fcf3ce44SJohn Forte {
1023fcf3ce44SJohn Forte 	uint32_t *refp = get_ref_p(obj, type);
1024fcf3ce44SJohn Forte 
1025fcf3ce44SJohn Forte 	if (refp != NULL) {
1026fcf3ce44SJohn Forte 		return (*refp);
1027fcf3ce44SJohn Forte 	/* LINTED E_NOP_ELSE_STMT */
1028fcf3ce44SJohn Forte 	} else {
1029fcf3ce44SJohn Forte 		ASSERT(0);
1030fcf3ce44SJohn Forte 	}
1031fcf3ce44SJohn Forte 
1032fcf3ce44SJohn Forte 	return (0);
1033fcf3ce44SJohn Forte }
1034fcf3ce44SJohn Forte 
1035fcf3ce44SJohn Forte /*
1036fcf3ce44SJohn Forte  * ****************************************************************************
1037fcf3ce44SJohn Forte  *
1038fcf3ce44SJohn Forte  * get_parent_p:
1039fcf3ce44SJohn Forte  *	get the pointer of the parent object.
1040fcf3ce44SJohn Forte  *
1041fcf3ce44SJohn Forte  * obj	- an object.
1042fcf3ce44SJohn Forte  * return - parent object pointer.
1043fcf3ce44SJohn Forte  *
1044fcf3ce44SJohn Forte  * ****************************************************************************
1045fcf3ce44SJohn Forte  */
1046fcf3ce44SJohn Forte uint32_t *const
get_parent_p(const isns_obj_t * obj)1047fcf3ce44SJohn Forte get_parent_p(
1048fcf3ce44SJohn Forte 	const isns_obj_t *obj
1049fcf3ce44SJohn Forte )
1050fcf3ce44SJohn Forte {
1051fcf3ce44SJohn Forte 	uint32_t *pp;
1052fcf3ce44SJohn Forte 	switch (obj->type) {
1053fcf3ce44SJohn Forte 	case OBJ_ISCSI:
1054fcf3ce44SJohn Forte 		pp = &((isns_iscsi_t *)obj)->puid;
1055fcf3ce44SJohn Forte 		break;
1056fcf3ce44SJohn Forte 	case OBJ_PORTAL:
1057fcf3ce44SJohn Forte 		pp = &((isns_portal_t *)obj)->puid;
1058fcf3ce44SJohn Forte 		break;
1059fcf3ce44SJohn Forte 	case OBJ_PG:
1060fcf3ce44SJohn Forte 		pp = &((isns_pg_t *)obj)->puid;
1061fcf3ce44SJohn Forte 		break;
1062fcf3ce44SJohn Forte 	case OBJ_ASSOC_ISCSI:
1063fcf3ce44SJohn Forte 		pp = &((isns_assoc_iscsi_t *)obj)->puid;
1064fcf3ce44SJohn Forte 		break;
1065fcf3ce44SJohn Forte 	case OBJ_ASSOC_DD:
1066fcf3ce44SJohn Forte 		pp = &((isns_assoc_dd_t *)obj)->puid;
1067fcf3ce44SJohn Forte 		break;
1068fcf3ce44SJohn Forte 	default:
1069fcf3ce44SJohn Forte 		pp = NULL;
1070fcf3ce44SJohn Forte 		break;
1071fcf3ce44SJohn Forte 	}
1072fcf3ce44SJohn Forte 
1073fcf3ce44SJohn Forte 	return (pp);
1074fcf3ce44SJohn Forte }
1075fcf3ce44SJohn Forte 
1076fcf3ce44SJohn Forte uint32_t
get_parent_uid(const isns_obj_t * obj)1077fcf3ce44SJohn Forte get_parent_uid(
1078fcf3ce44SJohn Forte 	const isns_obj_t *obj
1079fcf3ce44SJohn Forte )
1080fcf3ce44SJohn Forte {
1081fcf3ce44SJohn Forte 	uint32_t *pp = get_parent_p(obj);
1082fcf3ce44SJohn Forte 	if (pp != NULL) {
1083fcf3ce44SJohn Forte 		return (*pp);
1084fcf3ce44SJohn Forte 	}
1085fcf3ce44SJohn Forte 
1086fcf3ce44SJohn Forte 	return (0);
1087fcf3ce44SJohn Forte }
1088fcf3ce44SJohn Forte 
1089fcf3ce44SJohn Forte /*
1090fcf3ce44SJohn Forte  * ****************************************************************************
1091fcf3ce44SJohn Forte  *
1092fcf3ce44SJohn Forte  * get_child_np:
1093fcf3ce44SJohn Forte  *	get the pointer of the UID array of the n'th child of an object.
1094fcf3ce44SJohn Forte  *
1095fcf3ce44SJohn Forte  * obj	- an object.
1096fcf3ce44SJohn Forte  * n	- the child index.
1097fcf3ce44SJohn Forte  * return - the pointer of the UID array.
1098fcf3ce44SJohn Forte  *
1099fcf3ce44SJohn Forte  * ****************************************************************************
1100fcf3ce44SJohn Forte  */
1101fcf3ce44SJohn Forte static uint32_t **
get_child_np(isns_obj_t * obj,int n)1102fcf3ce44SJohn Forte get_child_np(
1103fcf3ce44SJohn Forte 	isns_obj_t *obj,
1104fcf3ce44SJohn Forte 	int n
1105fcf3ce44SJohn Forte )
1106fcf3ce44SJohn Forte {
1107fcf3ce44SJohn Forte 	uint32_t **pp =
1108fcf3ce44SJohn Forte 	    obj->type == OBJ_ENTITY ? &((isns_entity_t *)obj)->cuid[n] : NULL;
1109fcf3ce44SJohn Forte 
1110fcf3ce44SJohn Forte 	return (pp);
1111fcf3ce44SJohn Forte }
1112fcf3ce44SJohn Forte 
1113fcf3ce44SJohn Forte /*
1114fcf3ce44SJohn Forte  * ****************************************************************************
1115fcf3ce44SJohn Forte  *
1116fcf3ce44SJohn Forte  * get_child_n:
1117fcf3ce44SJohn Forte  *	get the UID array of the n'th child of an object.
1118fcf3ce44SJohn Forte  *
1119fcf3ce44SJohn Forte  * obj	- an object.
1120fcf3ce44SJohn Forte  * n	- the child index.
1121fcf3ce44SJohn Forte  * return - the UID array.
1122fcf3ce44SJohn Forte  *
1123fcf3ce44SJohn Forte  * ****************************************************************************
1124fcf3ce44SJohn Forte  */
1125fcf3ce44SJohn Forte #ifdef DEBUG
1126fcf3ce44SJohn Forte uint32_t *
1127fcf3ce44SJohn Forte #else
1128fcf3ce44SJohn Forte static uint32_t *
1129fcf3ce44SJohn Forte #endif
get_child_n(isns_obj_t * obj,int n)1130fcf3ce44SJohn Forte get_child_n(
1131fcf3ce44SJohn Forte 	isns_obj_t *obj,
1132fcf3ce44SJohn Forte 	int n
1133fcf3ce44SJohn Forte )
1134fcf3ce44SJohn Forte {
1135fcf3ce44SJohn Forte 	uint32_t **pp = get_child_np(obj, n);
1136fcf3ce44SJohn Forte 
1137fcf3ce44SJohn Forte 	if (pp != NULL) {
1138fcf3ce44SJohn Forte 		return (*pp);
1139fcf3ce44SJohn Forte 	}
1140fcf3ce44SJohn Forte 
1141fcf3ce44SJohn Forte 	ASSERT(0);
1142fcf3ce44SJohn Forte 	return (NULL);
1143fcf3ce44SJohn Forte }
1144fcf3ce44SJohn Forte 
1145fcf3ce44SJohn Forte /*
1146fcf3ce44SJohn Forte  * ****************************************************************************
1147fcf3ce44SJohn Forte  *
1148fcf3ce44SJohn Forte  * get_child_p:
1149fcf3ce44SJohn Forte  *	get the pointer of the UID array of the child matching the type.
1150fcf3ce44SJohn Forte  *
1151fcf3ce44SJohn Forte  * base	- an object.
1152fcf3ce44SJohn Forte  * child_type	- the child object type.
1153fcf3ce44SJohn Forte  * return - the pointer of the UID array.
1154fcf3ce44SJohn Forte  *
1155fcf3ce44SJohn Forte  * ****************************************************************************
1156fcf3ce44SJohn Forte  */
1157fcf3ce44SJohn Forte static uint32_t **
get_child_p(isns_obj_t * base,int child_type)1158fcf3ce44SJohn Forte get_child_p(
1159fcf3ce44SJohn Forte 	isns_obj_t *base,
1160fcf3ce44SJohn Forte 	int child_type
1161fcf3ce44SJohn Forte )
1162fcf3ce44SJohn Forte {
1163fcf3ce44SJohn Forte 	uint32_t **pp = NULL;
1164fcf3ce44SJohn Forte 	int i = 0;
1165fcf3ce44SJohn Forte 	while (i < NUM_OF_CHILD[base->type]) {
1166fcf3ce44SJohn Forte 		if (child_type == TYPE_OF_CHILD[base->type][i]) {
1167fcf3ce44SJohn Forte 			pp = get_child_np(base, i);
1168fcf3ce44SJohn Forte 			break;
1169fcf3ce44SJohn Forte 		}
1170fcf3ce44SJohn Forte 		i ++;
1171fcf3ce44SJohn Forte 	}
1172fcf3ce44SJohn Forte 
1173fcf3ce44SJohn Forte 	return (pp);
1174fcf3ce44SJohn Forte }
1175fcf3ce44SJohn Forte 
1176fcf3ce44SJohn Forte /*
1177fcf3ce44SJohn Forte  * ****************************************************************************
1178fcf3ce44SJohn Forte  *
1179fcf3ce44SJohn Forte  * get_child_t:
1180fcf3ce44SJohn Forte  *	get the UID array of the child object matching the type.
1181fcf3ce44SJohn Forte  *
1182fcf3ce44SJohn Forte  * base	- an object.
1183fcf3ce44SJohn Forte  * child_type	- the child object type.
1184fcf3ce44SJohn Forte  * return - the UID array.
1185fcf3ce44SJohn Forte  *
1186fcf3ce44SJohn Forte  * ****************************************************************************
1187fcf3ce44SJohn Forte  */
1188fcf3ce44SJohn Forte uint32_t *
get_child_t(isns_obj_t * base,int child_type)1189fcf3ce44SJohn Forte get_child_t(
1190fcf3ce44SJohn Forte 	isns_obj_t *base,
1191fcf3ce44SJohn Forte 	int child_type
1192fcf3ce44SJohn Forte )
1193fcf3ce44SJohn Forte {
1194fcf3ce44SJohn Forte 	uint32_t **pp = get_child_p(base, child_type);
1195fcf3ce44SJohn Forte 
1196fcf3ce44SJohn Forte 	if (pp != NULL) {
1197fcf3ce44SJohn Forte 		return (*pp);
1198fcf3ce44SJohn Forte 	} else {
1199fcf3ce44SJohn Forte 		return (NULL);
1200fcf3ce44SJohn Forte 	}
1201fcf3ce44SJohn Forte }
1202fcf3ce44SJohn Forte 
1203fcf3ce44SJohn Forte /*
1204fcf3ce44SJohn Forte  * ****************************************************************************
1205fcf3ce44SJohn Forte  *
1206fcf3ce44SJohn Forte  * key_cmp:
1207fcf3ce44SJohn Forte  *	compare the object against the lookup control data.
1208fcf3ce44SJohn Forte  *
1209fcf3ce44SJohn Forte  * lcp	- the lookup control data.
1210fcf3ce44SJohn Forte  * obj	- an object.
1211fcf3ce44SJohn Forte  * return - comparison result.
1212fcf3ce44SJohn Forte  *
1213fcf3ce44SJohn Forte  * ****************************************************************************
1214fcf3ce44SJohn Forte  */
1215fcf3ce44SJohn Forte int
key_cmp(lookup_ctrl_t * lcp,isns_obj_t * obj)1216fcf3ce44SJohn Forte key_cmp(
1217fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp,
1218fcf3ce44SJohn Forte 	isns_obj_t *obj
1219fcf3ce44SJohn Forte )
1220fcf3ce44SJohn Forte {
1221fcf3ce44SJohn Forte 	int i = 0;
1222fcf3ce44SJohn Forte 	int match = 1;
1223fcf3ce44SJohn Forte 	while (i < MAX_LOOKUP_CTRL && lcp->op[i] > 0 && match) {
1224fcf3ce44SJohn Forte 		isns_attr_t *attr = &obj->attrs[lcp->id[i]];
1225fcf3ce44SJohn Forte 		switch (lcp->op[i]) {
1226fcf3ce44SJohn Forte 			case OP_STRING:
1227fcf3ce44SJohn Forte 				match = (strcmp((const char *)lcp->data[i].ptr,
1228fcf3ce44SJohn Forte 				    (const char *)attr->value.ptr) == 0);
1229fcf3ce44SJohn Forte 				break;
1230fcf3ce44SJohn Forte 			case OP_INTEGER:
1231fcf3ce44SJohn Forte 				match = (lcp->data[i].ui == attr->value.ui);
1232fcf3ce44SJohn Forte 				break;
1233fcf3ce44SJohn Forte 			case OP_MEMORY_IP6:
1234fcf3ce44SJohn Forte 				match = !memcmp((void *)lcp->data[i].ip,
1235fcf3ce44SJohn Forte 				    (void *)attr->value.ip,
1236fcf3ce44SJohn Forte 				    sizeof (in6_addr_t));
1237fcf3ce44SJohn Forte 				break;
1238fcf3ce44SJohn Forte 			default:
1239fcf3ce44SJohn Forte 				ASSERT(0);
1240fcf3ce44SJohn Forte 				match = 0;
1241fcf3ce44SJohn Forte 				break;
1242fcf3ce44SJohn Forte 		}
1243fcf3ce44SJohn Forte 		i ++;
1244fcf3ce44SJohn Forte 	}
1245fcf3ce44SJohn Forte 
1246fcf3ce44SJohn Forte 	if (i && match) {
1247fcf3ce44SJohn Forte 		return (0);
1248fcf3ce44SJohn Forte 	} else {
1249fcf3ce44SJohn Forte 		return (1);
1250fcf3ce44SJohn Forte 	}
1251fcf3ce44SJohn Forte }
1252fcf3ce44SJohn Forte 
1253fcf3ce44SJohn Forte /*
1254fcf3ce44SJohn Forte  * ****************************************************************************
1255fcf3ce44SJohn Forte  *
1256fcf3ce44SJohn Forte  * set_lookup_ctrl:
1257fcf3ce44SJohn Forte  *	fill in the lookup control data for an object.
1258fcf3ce44SJohn Forte  *
1259fcf3ce44SJohn Forte  * lcp	- the lookup control data.
1260fcf3ce44SJohn Forte  * obj	- an object.
1261fcf3ce44SJohn Forte  * return - the lookup control data.
1262fcf3ce44SJohn Forte  *
1263fcf3ce44SJohn Forte  * ****************************************************************************
1264fcf3ce44SJohn Forte  */
1265fcf3ce44SJohn Forte static lookup_ctrl_t *
set_lookup_ctrl(lookup_ctrl_t * lcp,isns_obj_t * obj)1266fcf3ce44SJohn Forte set_lookup_ctrl(
1267fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp,
1268fcf3ce44SJohn Forte 	isns_obj_t *obj
1269fcf3ce44SJohn Forte )
1270fcf3ce44SJohn Forte {
1271fcf3ce44SJohn Forte 	isns_type_t type = obj->type;
1272fcf3ce44SJohn Forte 	uint32_t id, op;
1273fcf3ce44SJohn Forte 	int i = 0;
1274fcf3ce44SJohn Forte 
1275fcf3ce44SJohn Forte 	lcp->type = type;
1276fcf3ce44SJohn Forte 	while (i < MAX_KEY_ATTRS) {
1277fcf3ce44SJohn Forte 		op = KEY_ATTR_OP[type][i];
1278fcf3ce44SJohn Forte 		if (op != 0) {
1279fcf3ce44SJohn Forte 			id = KEY_ATTR_INDEX[type][i];
1280fcf3ce44SJohn Forte 			lcp->id[i] = id;
1281fcf3ce44SJohn Forte 			lcp->op[i] = op;
1282fcf3ce44SJohn Forte 			lcp->data[i].ui = obj->attrs[id].value.ui;
1283fcf3ce44SJohn Forte 		} else {
1284fcf3ce44SJohn Forte 			break;
1285fcf3ce44SJohn Forte 		}
1286fcf3ce44SJohn Forte 		i ++;
1287fcf3ce44SJohn Forte 	}
1288fcf3ce44SJohn Forte 
1289fcf3ce44SJohn Forte 	return (lcp);
1290fcf3ce44SJohn Forte }
1291fcf3ce44SJohn Forte 
1292fcf3ce44SJohn Forte /*
1293fcf3ce44SJohn Forte  * ****************************************************************************
1294fcf3ce44SJohn Forte  *
1295fcf3ce44SJohn Forte  * assign_attr:
1296fcf3ce44SJohn Forte  *	assign an attribute.
1297fcf3ce44SJohn Forte  *
1298fcf3ce44SJohn Forte  * attr	- the attribute being assigned.
1299fcf3ce44SJohn Forte  * tmp	- the attribute.
1300fcf3ce44SJohn Forte  * return - error code.
1301fcf3ce44SJohn Forte  *
1302fcf3ce44SJohn Forte  * ****************************************************************************
1303fcf3ce44SJohn Forte  */
1304fcf3ce44SJohn Forte int
assign_attr(isns_attr_t * attr,const isns_attr_t * tmp)1305fcf3ce44SJohn Forte assign_attr(
1306fcf3ce44SJohn Forte 	isns_attr_t *attr,
1307fcf3ce44SJohn Forte 	const isns_attr_t *tmp
1308fcf3ce44SJohn Forte )
1309fcf3ce44SJohn Forte {
1310fcf3ce44SJohn Forte 	uint32_t t;
1311fcf3ce44SJohn Forte 
1312fcf3ce44SJohn Forte 	switch (tmp->tag) {
1313fcf3ce44SJohn Forte 	case ISNS_EID_ATTR_ID:
1314fcf3ce44SJohn Forte 	case ISNS_DD_SET_NAME_ATTR_ID:
1315fcf3ce44SJohn Forte 	case ISNS_DD_NAME_ATTR_ID:
1316fcf3ce44SJohn Forte 		if (tmp->len == 0 && attr->len == 0) {
1317fcf3ce44SJohn Forte 			int len;
1318fcf3ce44SJohn Forte 			char *name = make_unique_name(&len, tmp->tag);
1319fcf3ce44SJohn Forte 			if (name != NULL) {
1320fcf3ce44SJohn Forte 				attr->value.ptr = (uchar_t *)name;
1321fcf3ce44SJohn Forte 				attr->tag = tmp->tag;
1322fcf3ce44SJohn Forte 				attr->len = len;
1323fcf3ce44SJohn Forte 			} else {
1324fcf3ce44SJohn Forte 				/* memory exhausted */
1325fcf3ce44SJohn Forte 				return (1);
1326fcf3ce44SJohn Forte 			}
1327fcf3ce44SJohn Forte 		}
1328*a2b0e4f1SToomas Soome 		/* FALLTHROUGH */
1329fcf3ce44SJohn Forte 	case ISNS_PORTAL_NAME_ATTR_ID:
1330fcf3ce44SJohn Forte 	case ISNS_ISCSI_NAME_ATTR_ID:
1331fcf3ce44SJohn Forte 	case ISNS_ISCSI_ALIAS_ATTR_ID:
1332fcf3ce44SJohn Forte 	case ISNS_ISCSI_AUTH_METHOD_ATTR_ID:
1333fcf3ce44SJohn Forte 	case ISNS_PG_ISCSI_NAME_ATTR_ID:
1334fcf3ce44SJohn Forte 	case ISNS_DD_ISCSI_NAME_ATTR_ID:
1335fcf3ce44SJohn Forte 		if (tmp->len == 0) {
1336fcf3ce44SJohn Forte 			return (0);
1337530e2b59Swl 		} else if (tmp->len >= attr->len) {
1338530e2b59Swl 			attr->value.ptr = realloc(
1339530e2b59Swl 			    attr->value.ptr, tmp->len + 1);
1340fcf3ce44SJohn Forte 		}
1341fcf3ce44SJohn Forte 		if (attr->value.ptr != NULL) {
1342530e2b59Swl 			(void) strncpy((char *)attr->value.ptr,
1343530e2b59Swl 			    (char *)tmp->value.ptr, tmp->len);
1344530e2b59Swl 			attr->value.ptr[tmp->len] = 0;
1345fcf3ce44SJohn Forte 			attr->tag = tmp->tag;
1346fcf3ce44SJohn Forte 			attr->len = tmp->len;
1347fcf3ce44SJohn Forte 		} else {
1348fcf3ce44SJohn Forte 			/* memory exhausted */
1349fcf3ce44SJohn Forte 			return (1);
1350fcf3ce44SJohn Forte 		}
1351fcf3ce44SJohn Forte 		break;
1352fcf3ce44SJohn Forte 	case ISNS_MGMT_IP_ADDR_ATTR_ID:
1353fcf3ce44SJohn Forte 	case ISNS_PORTAL_IP_ADDR_ATTR_ID:
1354fcf3ce44SJohn Forte 	case ISNS_PG_PORTAL_IP_ADDR_ATTR_ID:
1355fcf3ce44SJohn Forte 		if (attr->value.ip == NULL) {
1356fcf3ce44SJohn Forte 			attr->value.ip = (in6_addr_t *)calloc(1, tmp->len);
1357fcf3ce44SJohn Forte 		}
1358fcf3ce44SJohn Forte 		if (attr->value.ip != NULL) {
1359fcf3ce44SJohn Forte 			(void) memcpy((void *)attr->value.ip,
1360fcf3ce44SJohn Forte 			    (void *)tmp->value.ip, tmp->len);
1361fcf3ce44SJohn Forte 			attr->tag = tmp->tag;
1362fcf3ce44SJohn Forte 			attr->len = tmp->len;
1363fcf3ce44SJohn Forte 		} else {
1364fcf3ce44SJohn Forte 			/* memory exhausted */
1365fcf3ce44SJohn Forte 			return (1);
1366fcf3ce44SJohn Forte 		}
1367fcf3ce44SJohn Forte 		break;
1368fcf3ce44SJohn Forte 	case ISNS_ENTITY_INDEX_ATTR_ID:
1369fcf3ce44SJohn Forte 	case ISNS_PORTAL_INDEX_ATTR_ID:
1370fcf3ce44SJohn Forte 	case ISNS_ISCSI_NODE_INDEX_ATTR_ID:
1371fcf3ce44SJohn Forte 	case ISNS_PG_INDEX_ATTR_ID:
1372fcf3ce44SJohn Forte 	case ISNS_DD_SET_ID_ATTR_ID:
1373fcf3ce44SJohn Forte 	case ISNS_DD_ID_ATTR_ID:
1374fcf3ce44SJohn Forte 		if (attr->value.ui != 0) {
1375fcf3ce44SJohn Forte 			break;
1376fcf3ce44SJohn Forte 		}
1377*a2b0e4f1SToomas Soome 		/* FALLTHROUGH */
1378fcf3ce44SJohn Forte 	case ISNS_ENTITY_PROTOCOL_ATTR_ID:
1379fcf3ce44SJohn Forte 	case ISNS_VERSION_RANGE_ATTR_ID:
1380fcf3ce44SJohn Forte 
1381fcf3ce44SJohn Forte 	case ISNS_PORTAL_PORT_ATTR_ID:
1382fcf3ce44SJohn Forte 	case ISNS_ESI_PORT_ATTR_ID:
1383fcf3ce44SJohn Forte 	case ISNS_SCN_PORT_ATTR_ID:
1384fcf3ce44SJohn Forte 
1385fcf3ce44SJohn Forte 	case ISNS_ISCSI_NODE_TYPE_ATTR_ID:
1386fcf3ce44SJohn Forte 	case ISNS_ISCSI_SCN_BITMAP_ATTR_ID:
1387fcf3ce44SJohn Forte 
1388fcf3ce44SJohn Forte 	case ISNS_PG_PORTAL_PORT_ATTR_ID:
1389fcf3ce44SJohn Forte 	case ISNS_PG_TAG_ATTR_ID:
1390fcf3ce44SJohn Forte 
1391fcf3ce44SJohn Forte 	case ISNS_DD_SET_STATUS_ATTR_ID:
1392fcf3ce44SJohn Forte 	case ISNS_DD_ISCSI_INDEX_ATTR_ID:
1393fcf3ce44SJohn Forte 		attr->tag = tmp->tag;
1394fcf3ce44SJohn Forte 		attr->len = tmp->len;
1395fcf3ce44SJohn Forte 		attr->value.ui = tmp->value.ui;
1396fcf3ce44SJohn Forte 		break;
1397fcf3ce44SJohn Forte 	case ISNS_ENTITY_REG_PERIOD_ATTR_ID:
1398fcf3ce44SJohn Forte 		attr->tag = tmp->tag;
1399fcf3ce44SJohn Forte 		attr->len = tmp->len;
1400fcf3ce44SJohn Forte 		attr->value.ui = tmp->value.ui;
1401fcf3ce44SJohn Forte 		t = get_reg_period();
1402fcf3ce44SJohn Forte 		if (attr->value.ui > t) {
1403fcf3ce44SJohn Forte 			attr->value.ui = t;
1404fcf3ce44SJohn Forte 		} else if (attr->value.ui < ONE_DAY) {
1405fcf3ce44SJohn Forte 			attr->value.ui = ONE_DAY;
1406fcf3ce44SJohn Forte 		}
1407fcf3ce44SJohn Forte 		break;
1408fcf3ce44SJohn Forte 	case ISNS_ESI_INTERVAL_ATTR_ID:
1409fcf3ce44SJohn Forte 		attr->tag = tmp->tag;
1410fcf3ce44SJohn Forte 		attr->len = tmp->len;
1411fcf3ce44SJohn Forte 		attr->value.ui = tmp->value.ui;
1412fcf3ce44SJohn Forte 		if (attr->value.ui > ONE_DAY) {
1413fcf3ce44SJohn Forte 			attr->value.ui = ONE_DAY;
1414fcf3ce44SJohn Forte 		} else if (attr->value.ui < MIN_ESI_INTVAL) {
1415fcf3ce44SJohn Forte 			attr->value.ui = MIN_ESI_INTVAL; /* 20 seconds */
1416fcf3ce44SJohn Forte 		}
1417fcf3ce44SJohn Forte 		break;
1418fcf3ce44SJohn Forte 	default:
1419fcf3ce44SJohn Forte 		ASSERT(0);
1420fcf3ce44SJohn Forte 		/* don't assign the attribute */
1421fcf3ce44SJohn Forte 		break;
1422fcf3ce44SJohn Forte 	}
1423fcf3ce44SJohn Forte 	return (0);
1424fcf3ce44SJohn Forte }
1425fcf3ce44SJohn Forte 
1426fcf3ce44SJohn Forte /*
1427fcf3ce44SJohn Forte  * ****************************************************************************
1428fcf3ce44SJohn Forte  *
1429fcf3ce44SJohn Forte  * copy_attrs:
1430fcf3ce44SJohn Forte  *	copy all of attributes from one object to another.
1431fcf3ce44SJohn Forte  *
1432fcf3ce44SJohn Forte  * dst	- the destination object.
1433fcf3ce44SJohn Forte  * tmp	- the source object.
1434fcf3ce44SJohn Forte  * return - error code.
1435fcf3ce44SJohn Forte  *
1436fcf3ce44SJohn Forte  * ****************************************************************************
1437fcf3ce44SJohn Forte  */
1438fcf3ce44SJohn Forte static int
copy_attrs(isns_obj_t * dst,const isns_obj_t * src)1439fcf3ce44SJohn Forte copy_attrs(
1440fcf3ce44SJohn Forte 	isns_obj_t *dst,
1441fcf3ce44SJohn Forte 	const isns_obj_t *src
1442fcf3ce44SJohn Forte )
1443fcf3ce44SJohn Forte {
1444fcf3ce44SJohn Forte 	int i = 0;
1445fcf3ce44SJohn Forte 	int n = NUM_OF_ATTRS[dst->type];
1446fcf3ce44SJohn Forte 
1447fcf3ce44SJohn Forte 	isns_attr_t *dst_attr;
1448fcf3ce44SJohn Forte 	const isns_attr_t *src_attr;
1449fcf3ce44SJohn Forte 
1450fcf3ce44SJohn Forte 	while (i < n) {
1451fcf3ce44SJohn Forte 		src_attr = &(src->attrs[i]);
1452fcf3ce44SJohn Forte 		if (src_attr->tag != 0) {
1453fcf3ce44SJohn Forte 			dst_attr = &(dst->attrs[i]);
1454fcf3ce44SJohn Forte 			if (assign_attr(dst_attr, src_attr) != 0) {
1455fcf3ce44SJohn Forte 				return (1);
1456fcf3ce44SJohn Forte 			}
1457fcf3ce44SJohn Forte 		}
1458fcf3ce44SJohn Forte 		i ++;
1459fcf3ce44SJohn Forte 	}
1460fcf3ce44SJohn Forte 
1461fcf3ce44SJohn Forte 	return (0);
1462fcf3ce44SJohn Forte }
1463fcf3ce44SJohn Forte 
1464fcf3ce44SJohn Forte /*
1465fcf3ce44SJohn Forte  * ****************************************************************************
1466fcf3ce44SJohn Forte  *
1467fcf3ce44SJohn Forte  * extract_attr:
1468fcf3ce44SJohn Forte  *	extract an attribute from a TLV format data.
1469fcf3ce44SJohn Forte  *
1470fcf3ce44SJohn Forte  * attr	- the attribute.
1471fcf3ce44SJohn Forte  * tlv	- the TLV format data.
1472fcf3ce44SJohn Forte  * return - error code.
1473fcf3ce44SJohn Forte  *
1474fcf3ce44SJohn Forte  * ****************************************************************************
1475fcf3ce44SJohn Forte  */
1476fcf3ce44SJohn Forte int
extract_attr(isns_attr_t * attr,const isns_tlv_t * tlv,int flag)1477fcf3ce44SJohn Forte extract_attr(
1478fcf3ce44SJohn Forte 	isns_attr_t *attr,
1479fcf3ce44SJohn Forte 	const isns_tlv_t *tlv,
1480fcf3ce44SJohn Forte 	int flag
1481fcf3ce44SJohn Forte )
1482fcf3ce44SJohn Forte {
1483fcf3ce44SJohn Forte 	int ec = 0;
1484fcf3ce44SJohn Forte 
1485fcf3ce44SJohn Forte 	uint32_t min_len = 4, max_len = 224;
1486fcf3ce44SJohn Forte 
1487fcf3ce44SJohn Forte 	switch (tlv->attr_id) {
1488fcf3ce44SJohn Forte 	case ISNS_EID_ATTR_ID:
1489fcf3ce44SJohn Forte 		min_len = 0;
1490*a2b0e4f1SToomas Soome 		/* FALLTHROUGH */
1491fcf3ce44SJohn Forte 	case ISNS_PORTAL_NAME_ATTR_ID:
1492fcf3ce44SJohn Forte 	case ISNS_ISCSI_ALIAS_ATTR_ID:
1493fcf3ce44SJohn Forte 	case ISNS_DD_SET_NAME_ATTR_ID:
1494fcf3ce44SJohn Forte 	case ISNS_DD_NAME_ATTR_ID:
1495fcf3ce44SJohn Forte 		max_len = 256;
1496*a2b0e4f1SToomas Soome 		/* FALLTHROUGH */
1497fcf3ce44SJohn Forte 	case ISNS_ISCSI_NAME_ATTR_ID:
1498fcf3ce44SJohn Forte 	case ISNS_PG_ISCSI_NAME_ATTR_ID:
1499fcf3ce44SJohn Forte 		if (tlv->attr_len < min_len || tlv->attr_len > max_len) {
1500fcf3ce44SJohn Forte 			ec = ISNS_RSP_MSG_FORMAT_ERROR;
1501fcf3ce44SJohn Forte 		} else {
1502fcf3ce44SJohn Forte 			attr->tag = tlv->attr_id;
1503fcf3ce44SJohn Forte 			attr->len = tlv->attr_len;
1504fcf3ce44SJohn Forte 			attr->value.ptr = (uchar_t *)&(tlv->attr_value[0]);
1505fcf3ce44SJohn Forte 		}
1506fcf3ce44SJohn Forte 		break;
1507fcf3ce44SJohn Forte 	case ISNS_ISCSI_AUTH_METHOD_ATTR_ID:
1508fcf3ce44SJohn Forte 		attr->tag = tlv->attr_id;
1509fcf3ce44SJohn Forte 		attr->len = tlv->attr_len;
1510fcf3ce44SJohn Forte 		attr->value.ptr = (uchar_t *)&(tlv->attr_value[0]);
1511fcf3ce44SJohn Forte 		break;
1512fcf3ce44SJohn Forte 	case ISNS_MGMT_IP_ADDR_ATTR_ID:
1513fcf3ce44SJohn Forte 	case ISNS_PORTAL_IP_ADDR_ATTR_ID:
1514fcf3ce44SJohn Forte 	case ISNS_PG_PORTAL_IP_ADDR_ATTR_ID:
1515fcf3ce44SJohn Forte 		if (tlv->attr_len != 16) {
1516fcf3ce44SJohn Forte 			ec = ISNS_RSP_MSG_FORMAT_ERROR;
1517fcf3ce44SJohn Forte 		} else {
1518fcf3ce44SJohn Forte 			attr->tag = tlv->attr_id;
1519fcf3ce44SJohn Forte 			attr->len = tlv->attr_len;
1520fcf3ce44SJohn Forte 			attr->value.ip = (void *)&(tlv->attr_value[0]);
1521fcf3ce44SJohn Forte 		}
1522fcf3ce44SJohn Forte 		break;
1523fcf3ce44SJohn Forte 	case ISNS_ENTITY_PROTOCOL_ATTR_ID:
1524fcf3ce44SJohn Forte 	case ISNS_VERSION_RANGE_ATTR_ID:
1525fcf3ce44SJohn Forte 	case ISNS_ENTITY_REG_PERIOD_ATTR_ID:
1526fcf3ce44SJohn Forte 		/* fall throught */
1527fcf3ce44SJohn Forte 	case ISNS_PORTAL_PORT_ATTR_ID:
1528fcf3ce44SJohn Forte 	case ISNS_ESI_INTERVAL_ATTR_ID:
1529fcf3ce44SJohn Forte 	case ISNS_ESI_PORT_ATTR_ID:
1530fcf3ce44SJohn Forte 	case ISNS_SCN_PORT_ATTR_ID:
1531fcf3ce44SJohn Forte 		/* fall throught */
1532fcf3ce44SJohn Forte 	case ISNS_ISCSI_NODE_TYPE_ATTR_ID:
1533fcf3ce44SJohn Forte 		/* fall throught */
1534fcf3ce44SJohn Forte 	case ISNS_PG_PORTAL_PORT_ATTR_ID:
1535fcf3ce44SJohn Forte 		/* fall throught */
1536fcf3ce44SJohn Forte 	case ISNS_DD_SET_ID_ATTR_ID:
1537fcf3ce44SJohn Forte 	case ISNS_DD_SET_STATUS_ATTR_ID:
1538fcf3ce44SJohn Forte 		/* fall throught */
1539fcf3ce44SJohn Forte 	case ISNS_DD_ID_ATTR_ID:
1540fcf3ce44SJohn Forte 		if (tlv->attr_len != 4) {
1541fcf3ce44SJohn Forte 			ec = ISNS_RSP_MSG_FORMAT_ERROR;
1542fcf3ce44SJohn Forte 			break;
1543fcf3ce44SJohn Forte 		}
1544*a2b0e4f1SToomas Soome 		/* FALLTHROUGH */
1545fcf3ce44SJohn Forte 	case ISNS_PG_TAG_ATTR_ID:
1546fcf3ce44SJohn Forte 		attr->tag = tlv->attr_id;
1547fcf3ce44SJohn Forte 		attr->len = tlv->attr_len;
1548fcf3ce44SJohn Forte 		if (tlv->attr_len == 4) {
1549fcf3ce44SJohn Forte 			attr->value.ui = ntohl(*(uint32_t *)
1550fcf3ce44SJohn Forte 			    &(tlv->attr_value[0]));
1551fcf3ce44SJohn Forte 		} else {
1552fcf3ce44SJohn Forte 			attr->value.ui = 0;
1553fcf3ce44SJohn Forte 		}
1554fcf3ce44SJohn Forte 		break;
1555fcf3ce44SJohn Forte 	case ISNS_ISCSI_SCN_BITMAP_ATTR_ID:
1556fcf3ce44SJohn Forte 		/* ignore scn bitmap attribute during object registration, */
1557fcf3ce44SJohn Forte 		/* it is registered by scn_reg message. */
1558fcf3ce44SJohn Forte 	case ISNS_ENTITY_ISAKMP_P1_ATTR_ID:
1559fcf3ce44SJohn Forte 	case ISNS_ENTITY_CERT_ATTR_ID:
1560fcf3ce44SJohn Forte 	case ISNS_PORTAL_SEC_BMP_ATTR_ID:
1561fcf3ce44SJohn Forte 	case ISNS_PORTAL_ISAKMP_P1_ATTR_ID:
1562fcf3ce44SJohn Forte 	case ISNS_PORTAL_ISAKMP_P2_ATTR_ID:
1563fcf3ce44SJohn Forte 	case ISNS_PORTAL_CERT_ATTR_ID:
1564fcf3ce44SJohn Forte 		break;
1565fcf3ce44SJohn Forte 	case ISNS_PORTAL_INDEX_ATTR_ID:
1566fcf3ce44SJohn Forte 	case ISNS_ISCSI_NODE_INDEX_ATTR_ID:
1567fcf3ce44SJohn Forte 	case ISNS_PG_INDEX_ATTR_ID:
1568fcf3ce44SJohn Forte 		if (flag == 0) {
1569fcf3ce44SJohn Forte 			if (tlv->attr_len != 4) {
1570fcf3ce44SJohn Forte 				ec = ISNS_RSP_MSG_FORMAT_ERROR;
1571fcf3ce44SJohn Forte 			} else {
1572fcf3ce44SJohn Forte 				attr->tag = tlv->attr_id;
1573fcf3ce44SJohn Forte 				attr->len = tlv->attr_len;
1574fcf3ce44SJohn Forte 				attr->value.ui = ntohl(*(uint32_t *)
1575fcf3ce44SJohn Forte 				    &(tlv->attr_value[0]));
1576fcf3ce44SJohn Forte 			}
1577fcf3ce44SJohn Forte 			break;
1578fcf3ce44SJohn Forte 		}
1579*a2b0e4f1SToomas Soome 		/* FALLTHROUGH */
1580fcf3ce44SJohn Forte 	case ISNS_ENTITY_INDEX_ATTR_ID:
1581fcf3ce44SJohn Forte 	case ISNS_TIMESTAMP_ATTR_ID:
1582fcf3ce44SJohn Forte 	default:
1583fcf3ce44SJohn Forte 		if (flag == 0) {
1584fcf3ce44SJohn Forte 			ec = ISNS_RSP_INVALID_QRY;
1585fcf3ce44SJohn Forte 		} else {
1586fcf3ce44SJohn Forte 			ec = ISNS_RSP_INVALID_REGIS;
1587fcf3ce44SJohn Forte 		}
1588fcf3ce44SJohn Forte 		break;
1589fcf3ce44SJohn Forte 	}
1590fcf3ce44SJohn Forte 
1591fcf3ce44SJohn Forte 	return (ec);
1592fcf3ce44SJohn Forte }
1593fcf3ce44SJohn Forte 
1594fcf3ce44SJohn Forte /*
1595fcf3ce44SJohn Forte  * ****************************************************************************
1596fcf3ce44SJohn Forte  *
1597fcf3ce44SJohn Forte  * copy_attr:
1598fcf3ce44SJohn Forte  *	copy an attribute from a TLV format data.
1599fcf3ce44SJohn Forte  *
1600fcf3ce44SJohn Forte  * attr	- the attribute.
1601fcf3ce44SJohn Forte  * tlv	- the TLV format data.
1602fcf3ce44SJohn Forte  * return - error code.
1603fcf3ce44SJohn Forte  *
1604fcf3ce44SJohn Forte  * ****************************************************************************
1605fcf3ce44SJohn Forte  */
1606fcf3ce44SJohn Forte static int
copy_attr(isns_attr_t * attr,const isns_tlv_t * tlv)1607fcf3ce44SJohn Forte copy_attr(
1608fcf3ce44SJohn Forte 	isns_attr_t *attr,
1609fcf3ce44SJohn Forte 	const isns_tlv_t *tlv
1610fcf3ce44SJohn Forte )
1611fcf3ce44SJohn Forte {
1612fcf3ce44SJohn Forte 	int ec = 0;
1613fcf3ce44SJohn Forte 
1614fcf3ce44SJohn Forte 	isns_attr_t tmp = { 0 };
1615fcf3ce44SJohn Forte 
1616fcf3ce44SJohn Forte 	/* extract the attribute first */
1617fcf3ce44SJohn Forte 	ec = extract_attr(&tmp, tlv, 1);
1618fcf3ce44SJohn Forte 
1619fcf3ce44SJohn Forte 	/* assign the attribute */
1620fcf3ce44SJohn Forte 	if (ec == 0 && tmp.tag != 0) {
1621fcf3ce44SJohn Forte 		if (assign_attr(attr, &tmp) != 0) {
1622fcf3ce44SJohn Forte 			ec = ISNS_RSP_INTERNAL_ERROR;
1623fcf3ce44SJohn Forte 		}
1624fcf3ce44SJohn Forte 	}
1625fcf3ce44SJohn Forte 
1626fcf3ce44SJohn Forte 	return (ec);
1627fcf3ce44SJohn Forte }
1628fcf3ce44SJohn Forte 
1629fcf3ce44SJohn Forte /*
1630fcf3ce44SJohn Forte  * ****************************************************************************
1631fcf3ce44SJohn Forte  *
1632fcf3ce44SJohn Forte  * get_timestamp:
1633fcf3ce44SJohn Forte  *	get current timestamp.
1634fcf3ce44SJohn Forte  *
1635fcf3ce44SJohn Forte  * return - current timestamp.
1636fcf3ce44SJohn Forte  *
1637fcf3ce44SJohn Forte  * ****************************************************************************
1638fcf3ce44SJohn Forte  */
1639fcf3ce44SJohn Forte uint32_t
get_timestamp()1640fcf3ce44SJohn Forte get_timestamp(
1641fcf3ce44SJohn Forte )
1642fcf3ce44SJohn Forte {
1643fcf3ce44SJohn Forte 	uint32_t t;
1644fcf3ce44SJohn Forte 	int flag;
1645fcf3ce44SJohn Forte 
1646fcf3ce44SJohn Forte 	/* block the scheduler */
1647fcf3ce44SJohn Forte 	(void) pthread_mutex_lock(&el_mtx);
1648fcf3ce44SJohn Forte 
1649fcf3ce44SJohn Forte 	/* get most current time */
1650fcf3ce44SJohn Forte 	if (sys_q != NULL) {
1651fcf3ce44SJohn Forte 		/* need to wakeup idle */
1652fcf3ce44SJohn Forte 		flag = 1;
1653fcf3ce44SJohn Forte 	} else {
1654fcf3ce44SJohn Forte 		flag = 0;
1655fcf3ce44SJohn Forte 	}
1656fcf3ce44SJohn Forte 	t = get_stopwatch(flag);
1657fcf3ce44SJohn Forte 
1658fcf3ce44SJohn Forte 	/* unblock it */
1659fcf3ce44SJohn Forte 	(void) pthread_mutex_unlock(&el_mtx);
1660fcf3ce44SJohn Forte 
1661fcf3ce44SJohn Forte 	return (t);
1662fcf3ce44SJohn Forte }
1663fcf3ce44SJohn Forte 
1664fcf3ce44SJohn Forte /*
1665fcf3ce44SJohn Forte  * ****************************************************************************
1666fcf3ce44SJohn Forte  *
1667fcf3ce44SJohn Forte  * get_reg_period:
1668fcf3ce44SJohn Forte  *	get the longest registration period.
1669fcf3ce44SJohn Forte  *
1670fcf3ce44SJohn Forte  * return - the longest registration period.
1671fcf3ce44SJohn Forte  *
1672fcf3ce44SJohn Forte  * ****************************************************************************
1673fcf3ce44SJohn Forte  */
1674fcf3ce44SJohn Forte static uint32_t
get_reg_period()1675fcf3ce44SJohn Forte get_reg_period(
1676fcf3ce44SJohn Forte )
1677fcf3ce44SJohn Forte {
1678fcf3ce44SJohn Forte 	uint32_t t;
1679fcf3ce44SJohn Forte 	uint32_t period;
1680fcf3ce44SJohn Forte 
1681fcf3ce44SJohn Forte 	/* get most current time */
1682fcf3ce44SJohn Forte 	t = get_timestamp();
1683fcf3ce44SJohn Forte 
1684fcf3ce44SJohn Forte 	/* just one second before the end of the world */
1685fcf3ce44SJohn Forte 	period = INFINITY - t - 1;
1686fcf3ce44SJohn Forte 
1687fcf3ce44SJohn Forte 	return (period);
1688fcf3ce44SJohn Forte }
1689fcf3ce44SJohn Forte 
1690fcf3ce44SJohn Forte /*
1691fcf3ce44SJohn Forte  * ****************************************************************************
1692fcf3ce44SJohn Forte  *
1693fcf3ce44SJohn Forte  * obj_calloc:
1694fcf3ce44SJohn Forte  *	allocate memory space for an object.
1695fcf3ce44SJohn Forte  *
1696fcf3ce44SJohn Forte  * type	- the object type.
1697fcf3ce44SJohn Forte  * return - pointer of the object being allocated.
1698fcf3ce44SJohn Forte  *
1699fcf3ce44SJohn Forte  * ****************************************************************************
1700fcf3ce44SJohn Forte  */
1701fcf3ce44SJohn Forte isns_obj_t *
obj_calloc(int type)1702fcf3ce44SJohn Forte obj_calloc(
1703fcf3ce44SJohn Forte 	int type
1704fcf3ce44SJohn Forte )
1705fcf3ce44SJohn Forte {
1706fcf3ce44SJohn Forte 	isns_obj_t *obj = NULL;
1707fcf3ce44SJohn Forte 
1708fcf3ce44SJohn Forte 	obj = (isns_obj_t *)calloc(1, SIZEOF_OBJ[type]);
1709fcf3ce44SJohn Forte 	if (obj != NULL) {
1710fcf3ce44SJohn Forte 		obj->type = type;
1711fcf3ce44SJohn Forte #ifdef DEBUG
1712fcf3ce44SJohn Forte 	if (verbose_mc) {
1713fcf3ce44SJohn Forte 		printf("object(%d) allocated\n", type);
1714fcf3ce44SJohn Forte 	}
1715fcf3ce44SJohn Forte #endif
1716fcf3ce44SJohn Forte 	}
1717fcf3ce44SJohn Forte 
1718fcf3ce44SJohn Forte 	return (obj);
1719fcf3ce44SJohn Forte }
1720fcf3ce44SJohn Forte 
1721fcf3ce44SJohn Forte /*
1722fcf3ce44SJohn Forte  * ****************************************************************************
1723fcf3ce44SJohn Forte  *
1724fcf3ce44SJohn Forte  * make_default_entity:
1725fcf3ce44SJohn Forte  *	generate a default network entity object.
1726fcf3ce44SJohn Forte  *
1727fcf3ce44SJohn Forte  * return - pointer of the default network entity object.
1728fcf3ce44SJohn Forte  *
1729fcf3ce44SJohn Forte  * ****************************************************************************
1730fcf3ce44SJohn Forte  */
1731fcf3ce44SJohn Forte isns_obj_t *
make_default_entity()1732fcf3ce44SJohn Forte make_default_entity(
1733fcf3ce44SJohn Forte )
1734fcf3ce44SJohn Forte {
1735fcf3ce44SJohn Forte 	uint32_t t;
1736fcf3ce44SJohn Forte 
1737fcf3ce44SJohn Forte 	isns_obj_t *obj = obj_calloc(OBJ_ENTITY);
1738fcf3ce44SJohn Forte 	isns_attr_t *attr;
1739fcf3ce44SJohn Forte 	if (obj != NULL) {
1740fcf3ce44SJohn Forte 		int len;
1741fcf3ce44SJohn Forte 		char *eid = make_unique_name(&len, ISNS_EID_ATTR_ID);
1742fcf3ce44SJohn Forte 		if (!eid) {
1743fcf3ce44SJohn Forte 			free(obj);
1744fcf3ce44SJohn Forte 			return (NULL);
1745fcf3ce44SJohn Forte 		}
1746fcf3ce44SJohn Forte 		attr = &obj->attrs[ATTR_INDEX_ENTITY(ISNS_EID_ATTR_ID)];
1747fcf3ce44SJohn Forte 
1748fcf3ce44SJohn Forte 		/* set default entity name */
1749fcf3ce44SJohn Forte 		attr->tag = ISNS_EID_ATTR_ID;
1750fcf3ce44SJohn Forte 		attr->len = len;
1751fcf3ce44SJohn Forte 		attr->value.ptr = (uchar_t *)eid;
1752fcf3ce44SJohn Forte 
1753fcf3ce44SJohn Forte 		/* set default registration period */
1754fcf3ce44SJohn Forte 		attr = &obj->attrs[
1755fcf3ce44SJohn Forte 		    ATTR_INDEX_ENTITY(ISNS_ENTITY_REG_PERIOD_ATTR_ID)];
1756fcf3ce44SJohn Forte 		if (attr->tag == 0) {
1757fcf3ce44SJohn Forte 			attr->tag = ISNS_ENTITY_REG_PERIOD_ATTR_ID;
1758fcf3ce44SJohn Forte 			attr->len = 4;
1759fcf3ce44SJohn Forte 			t = get_reg_period();
1760fcf3ce44SJohn Forte 			attr->value.ui = t;
1761fcf3ce44SJohn Forte 		}
1762fcf3ce44SJohn Forte 	}
1763fcf3ce44SJohn Forte 
1764fcf3ce44SJohn Forte 	return (obj);
1765fcf3ce44SJohn Forte }
1766fcf3ce44SJohn Forte 
1767fcf3ce44SJohn Forte /*
1768fcf3ce44SJohn Forte  * ****************************************************************************
1769fcf3ce44SJohn Forte  *
1770fcf3ce44SJohn Forte  * make_default_pg:
1771fcf3ce44SJohn Forte  *	generate a default portal group object.
1772fcf3ce44SJohn Forte  *
1773fcf3ce44SJohn Forte  * iscsi  - the iscsi storage node object.
1774fcf3ce44SJohn Forte  * portal - the portal object.
1775fcf3ce44SJohn Forte  * return - pointer of the default portal group object.
1776fcf3ce44SJohn Forte  *
1777fcf3ce44SJohn Forte  * ****************************************************************************
1778fcf3ce44SJohn Forte  */
1779fcf3ce44SJohn Forte static isns_obj_t *
make_default_pg(const isns_obj_t * p1,const isns_obj_t * p2)1780fcf3ce44SJohn Forte make_default_pg(
1781fcf3ce44SJohn Forte 	const isns_obj_t *p1,
1782fcf3ce44SJohn Forte 	const isns_obj_t *p2
1783fcf3ce44SJohn Forte )
1784fcf3ce44SJohn Forte {
1785fcf3ce44SJohn Forte 	const isns_obj_t *iscsi, *portal;
1786fcf3ce44SJohn Forte 	const isns_attr_t *name, *addr, *port;
1787fcf3ce44SJohn Forte 	isns_obj_t *pg;
1788fcf3ce44SJohn Forte 
1789fcf3ce44SJohn Forte 	uchar_t *pg_name;
1790fcf3ce44SJohn Forte 	in6_addr_t *pg_addr;
1791fcf3ce44SJohn Forte 
1792fcf3ce44SJohn Forte 	isns_attr_t *attr;
1793fcf3ce44SJohn Forte 
1794fcf3ce44SJohn Forte 	uint32_t *refp;
1795fcf3ce44SJohn Forte 
1796fcf3ce44SJohn Forte 	if (p1->type == OBJ_ISCSI) {
1797fcf3ce44SJohn Forte 		iscsi = p1;
1798fcf3ce44SJohn Forte 		portal = p2;
1799fcf3ce44SJohn Forte 	} else {
1800fcf3ce44SJohn Forte 		iscsi = p2;
1801fcf3ce44SJohn Forte 		portal = p1;
1802fcf3ce44SJohn Forte 	}
1803fcf3ce44SJohn Forte 	name = &iscsi->attrs[ATTR_INDEX_ISCSI(ISNS_ISCSI_NAME_ATTR_ID)];
1804fcf3ce44SJohn Forte 	addr = &portal->attrs[ATTR_INDEX_PORTAL(ISNS_PORTAL_IP_ADDR_ATTR_ID)];
1805fcf3ce44SJohn Forte 	port = &portal->attrs[ATTR_INDEX_PORTAL(ISNS_PORTAL_PORT_ATTR_ID)];
1806fcf3ce44SJohn Forte 
1807fcf3ce44SJohn Forte 	pg = obj_calloc(OBJ_PG);
1808fcf3ce44SJohn Forte 	pg_name = (uchar_t *)malloc(name->len);
1809fcf3ce44SJohn Forte 	pg_addr = (in6_addr_t *)malloc(addr->len);
1810fcf3ce44SJohn Forte 	if (pg != NULL && pg_name != NULL && pg_addr != NULL) {
1811fcf3ce44SJohn Forte 		(void) strcpy((char *)pg_name, (char *)name->value.ptr);
1812fcf3ce44SJohn Forte 		attr = &pg->attrs[ATTR_INDEX_PG(ISNS_PG_ISCSI_NAME_ATTR_ID)];
1813fcf3ce44SJohn Forte 		attr->tag = ISNS_PG_ISCSI_NAME_ATTR_ID;
1814fcf3ce44SJohn Forte 		attr->len = name->len;
1815fcf3ce44SJohn Forte 		attr->value.ptr = pg_name;
1816fcf3ce44SJohn Forte 
1817fcf3ce44SJohn Forte 		(void) memcpy((void *)pg_addr,
1818fcf3ce44SJohn Forte 		    (void *)addr->value.ip, addr->len);
1819fcf3ce44SJohn Forte 		attr = &pg->attrs[ATTR_INDEX_PG(
1820fcf3ce44SJohn Forte 		    ISNS_PG_PORTAL_IP_ADDR_ATTR_ID)];
1821fcf3ce44SJohn Forte 		attr->tag = ISNS_PG_PORTAL_IP_ADDR_ATTR_ID;
1822fcf3ce44SJohn Forte 		attr->len = addr->len;
1823fcf3ce44SJohn Forte 		attr->value.ip = pg_addr;
1824fcf3ce44SJohn Forte 
1825fcf3ce44SJohn Forte 		attr = &pg->attrs[ATTR_INDEX_PG(
1826fcf3ce44SJohn Forte 		    ISNS_PG_PORTAL_PORT_ATTR_ID)];
1827fcf3ce44SJohn Forte 		attr->tag = ISNS_PG_PORTAL_PORT_ATTR_ID;
1828fcf3ce44SJohn Forte 		attr->len = port->len;
1829fcf3ce44SJohn Forte 		attr->value.ui = port->value.ui;
1830fcf3ce44SJohn Forte 
1831fcf3ce44SJohn Forte 		attr = &pg->attrs[ATTR_INDEX_PG(
1832fcf3ce44SJohn Forte 		    ISNS_PG_TAG_ATTR_ID)];
1833fcf3ce44SJohn Forte 		attr->tag = ISNS_PG_TAG_ATTR_ID;
1834fcf3ce44SJohn Forte 		attr->len = 4;
1835fcf3ce44SJohn Forte 		attr->value.ui = ISNS_DEFAULT_PGT;
1836fcf3ce44SJohn Forte 
1837fcf3ce44SJohn Forte 		refp = get_ref_p(pg, OBJ_ISCSI);
1838fcf3ce44SJohn Forte 		*refp = get_obj_uid(iscsi);
1839fcf3ce44SJohn Forte 
1840fcf3ce44SJohn Forte 		refp = get_ref_p(pg, OBJ_PORTAL);
1841fcf3ce44SJohn Forte 		*refp = get_obj_uid(portal);
1842fcf3ce44SJohn Forte 
1843fcf3ce44SJohn Forte 		(void) set_parent_obj(pg, get_parent_uid(iscsi));
1844fcf3ce44SJohn Forte 	} else {
1845fcf3ce44SJohn Forte 		free(pg);
1846fcf3ce44SJohn Forte 		free(pg_name);
1847fcf3ce44SJohn Forte 		free(pg_addr);
1848fcf3ce44SJohn Forte 		pg = NULL;
1849fcf3ce44SJohn Forte 	}
1850fcf3ce44SJohn Forte 
1851fcf3ce44SJohn Forte 	return (pg);
1852fcf3ce44SJohn Forte }
1853fcf3ce44SJohn Forte 
1854fcf3ce44SJohn Forte /*
1855fcf3ce44SJohn Forte  * ****************************************************************************
1856fcf3ce44SJohn Forte  *
1857fcf3ce44SJohn Forte  * reg_get_entity:
1858fcf3ce44SJohn Forte  *	parse the Operating Attributes of the DevAttrReg message and
1859fcf3ce44SJohn Forte  *	create the Network Entity object if it has one.
1860fcf3ce44SJohn Forte  *
1861fcf3ce44SJohn Forte  * p	- the pointer of the object for returning.
1862fcf3ce44SJohn Forte  * op	- the operating attributes.
1863fcf3ce44SJohn Forte  * op_len - the length of the operating attributes.
1864fcf3ce44SJohn Forte  * return - error code.
1865fcf3ce44SJohn Forte  *
1866fcf3ce44SJohn Forte  * ****************************************************************************
1867fcf3ce44SJohn Forte  */
1868fcf3ce44SJohn Forte int
reg_get_entity(isns_obj_t ** p,isns_tlv_t ** op,uint16_t * op_len)1869fcf3ce44SJohn Forte reg_get_entity(
1870fcf3ce44SJohn Forte 	isns_obj_t **p,
1871fcf3ce44SJohn Forte 	isns_tlv_t **op,
1872fcf3ce44SJohn Forte 	uint16_t *op_len
1873fcf3ce44SJohn Forte )
1874fcf3ce44SJohn Forte {
1875fcf3ce44SJohn Forte 	int ec = 0;
1876fcf3ce44SJohn Forte 
1877fcf3ce44SJohn Forte 	isns_tlv_t *tmp;
1878fcf3ce44SJohn Forte 	uint16_t tmp_len;
1879fcf3ce44SJohn Forte 	isns_attr_t *attr;
1880fcf3ce44SJohn Forte 
1881fcf3ce44SJohn Forte 	isns_obj_t *entity = NULL;
1882fcf3ce44SJohn Forte 
1883fcf3ce44SJohn Forte 	tmp = *op;
1884fcf3ce44SJohn Forte 	tmp_len = *op_len;
1885fcf3ce44SJohn Forte 
1886fcf3ce44SJohn Forte 	/* parse the entity object */
1887fcf3ce44SJohn Forte 	if (tmp_len >= 8 && IS_ENTITY_KEY(tmp->attr_id)) {
1888fcf3ce44SJohn Forte 		entity = obj_calloc(OBJ_ENTITY);
1889fcf3ce44SJohn Forte 		if (entity != NULL) {
1890fcf3ce44SJohn Forte 			do {
1891fcf3ce44SJohn Forte 				attr = &entity->attrs[
1892fcf3ce44SJohn Forte 				    ATTR_INDEX_ENTITY(tmp->attr_id)];
1893fcf3ce44SJohn Forte 				ec = copy_attr(attr, tmp);
1894fcf3ce44SJohn Forte 				NEXT_TLV(tmp, tmp_len);
1895fcf3ce44SJohn Forte 			} while (ec == 0 &&
1896fcf3ce44SJohn Forte 			    tmp_len >= 8 &&
1897fcf3ce44SJohn Forte 			    IS_ENTITY_ATTR(tmp->attr_id));
1898fcf3ce44SJohn Forte 		} else {
1899fcf3ce44SJohn Forte 			ec = ISNS_RSP_INTERNAL_ERROR;
1900fcf3ce44SJohn Forte 		}
1901fcf3ce44SJohn Forte 
1902fcf3ce44SJohn Forte 		if (ec == 0) {
1903fcf3ce44SJohn Forte 			/* set default registration period */
1904fcf3ce44SJohn Forte 			attr = &entity->attrs[
1905fcf3ce44SJohn Forte 			    ATTR_INDEX_ENTITY(ISNS_ENTITY_REG_PERIOD_ATTR_ID)];
1906fcf3ce44SJohn Forte 			if (attr->tag == 0) {
1907fcf3ce44SJohn Forte 				attr->tag = ISNS_ENTITY_REG_PERIOD_ATTR_ID;
1908fcf3ce44SJohn Forte 				attr->len = 4;
1909fcf3ce44SJohn Forte 				attr->value.ui = get_reg_period();
1910fcf3ce44SJohn Forte 			}
1911fcf3ce44SJohn Forte 		} else if (entity != NULL) {
1912fcf3ce44SJohn Forte 			free(entity);
1913fcf3ce44SJohn Forte 			entity = NULL;
1914fcf3ce44SJohn Forte 		}
1915fcf3ce44SJohn Forte 	}
1916fcf3ce44SJohn Forte 
1917fcf3ce44SJohn Forte 	*p = entity;
1918fcf3ce44SJohn Forte 	*op = tmp;
1919fcf3ce44SJohn Forte 	*op_len = tmp_len;
1920fcf3ce44SJohn Forte 
1921fcf3ce44SJohn Forte 	return (ec);
1922fcf3ce44SJohn Forte }
1923fcf3ce44SJohn Forte 
1924fcf3ce44SJohn Forte /*
1925fcf3ce44SJohn Forte  * ****************************************************************************
1926fcf3ce44SJohn Forte  *
1927fcf3ce44SJohn Forte  * reg_get_iscsi:
1928fcf3ce44SJohn Forte  *	parse the Operating Attributes of the DevAttrReg message and
1929fcf3ce44SJohn Forte  *	create an iSCSI Storage Node object.
1930fcf3ce44SJohn Forte  *
1931fcf3ce44SJohn Forte  * p	- the pointer of the object for returning.
1932fcf3ce44SJohn Forte  * pg_key1 - the pointer of iscsi storage node name for returning.
1933fcf3ce44SJohn Forte  * op	- the operating attributes.
1934fcf3ce44SJohn Forte  * op_len - the length of the operating attributes.
1935fcf3ce44SJohn Forte  * return - error code.
1936fcf3ce44SJohn Forte  *
1937fcf3ce44SJohn Forte  * ****************************************************************************
1938fcf3ce44SJohn Forte  */
1939fcf3ce44SJohn Forte static int
reg_get_iscsi(isns_obj_t ** p,isns_attr_t * pg_key1,isns_tlv_t ** op,uint16_t * op_len)1940fcf3ce44SJohn Forte reg_get_iscsi(
1941fcf3ce44SJohn Forte 	isns_obj_t **p,
1942fcf3ce44SJohn Forte 	isns_attr_t *pg_key1,
1943fcf3ce44SJohn Forte 	isns_tlv_t **op,
1944fcf3ce44SJohn Forte 	uint16_t *op_len
1945fcf3ce44SJohn Forte )
1946fcf3ce44SJohn Forte {
1947fcf3ce44SJohn Forte 	int ec = 0;
1948fcf3ce44SJohn Forte 
1949fcf3ce44SJohn Forte 	isns_tlv_t *tmp;
1950fcf3ce44SJohn Forte 	uint16_t tmp_len;
1951fcf3ce44SJohn Forte 	isns_attr_t *attr;
1952fcf3ce44SJohn Forte 
1953fcf3ce44SJohn Forte 	isns_obj_t *obj = NULL;
1954fcf3ce44SJohn Forte 
1955fcf3ce44SJohn Forte 	tmp = *op;
1956fcf3ce44SJohn Forte 	tmp_len = *op_len;
1957fcf3ce44SJohn Forte 
1958fcf3ce44SJohn Forte 	/* keep the iscsi storage node name for */
1959fcf3ce44SJohn Forte 	/* parsing a pg object which is immediately */
1960fcf3ce44SJohn Forte 	/* followed with a PGT by the iscsi storage node */
1961fcf3ce44SJohn Forte 	pg_key1->tag = PG_KEY1;
1962fcf3ce44SJohn Forte 	pg_key1->len = tmp->attr_len;
1963fcf3ce44SJohn Forte 	pg_key1->value.ptr = (uchar_t *)&tmp->attr_value[0];
1964fcf3ce44SJohn Forte 
1965fcf3ce44SJohn Forte 	/* parse one iscsi storage node object */
1966fcf3ce44SJohn Forte 	obj = obj_calloc(OBJ_ISCSI);
1967fcf3ce44SJohn Forte 	if (obj != NULL) {
1968fcf3ce44SJohn Forte 		/* parse key & non-key attributes */
1969fcf3ce44SJohn Forte 		do {
1970fcf3ce44SJohn Forte 			attr = &obj->attrs[
1971fcf3ce44SJohn Forte 			    ATTR_INDEX_ISCSI(tmp->attr_id)];
1972fcf3ce44SJohn Forte 			ec = copy_attr(attr, tmp);
1973fcf3ce44SJohn Forte 			NEXT_TLV(tmp, tmp_len);
1974fcf3ce44SJohn Forte 		} while (ec == 0 &&
1975fcf3ce44SJohn Forte 		    tmp_len >= 8 &&
1976fcf3ce44SJohn Forte 		    IS_ISCSI_ATTR(tmp->attr_id));
1977fcf3ce44SJohn Forte 	} else {
1978fcf3ce44SJohn Forte 		/* no memory */
1979fcf3ce44SJohn Forte 		ec = ISNS_RSP_INTERNAL_ERROR;
1980fcf3ce44SJohn Forte 	}
1981fcf3ce44SJohn Forte 
1982fcf3ce44SJohn Forte 	*p = obj;
1983fcf3ce44SJohn Forte 	*op = tmp;
1984fcf3ce44SJohn Forte 	*op_len = tmp_len;
1985fcf3ce44SJohn Forte 
1986fcf3ce44SJohn Forte 	return (ec);
1987fcf3ce44SJohn Forte }
1988fcf3ce44SJohn Forte 
1989fcf3ce44SJohn Forte /*
1990fcf3ce44SJohn Forte  * ****************************************************************************
1991fcf3ce44SJohn Forte  *
1992fcf3ce44SJohn Forte  * reg_get_portal:
1993fcf3ce44SJohn Forte  *	parse the Operating Attributes of the DevAttrReg message and
1994fcf3ce44SJohn Forte  *	create a Portal object.
1995fcf3ce44SJohn Forte  *
1996fcf3ce44SJohn Forte  * p	- the pointer of the object for returning.
1997fcf3ce44SJohn Forte  * pg_key1 - the pointer of portal ip addr for returning.
1998fcf3ce44SJohn Forte  * pg_key2 - the pointer of portal port for returning.
1999fcf3ce44SJohn Forte  * op	- the operating attributes.
2000fcf3ce44SJohn Forte  * op_len - the length of the operating attributes.
2001fcf3ce44SJohn Forte  * return - error code.
2002fcf3ce44SJohn Forte  *
2003fcf3ce44SJohn Forte  * ****************************************************************************
2004fcf3ce44SJohn Forte  */
2005fcf3ce44SJohn Forte static int
reg_get_portal(isns_obj_t ** p,isns_attr_t * pg_key1,isns_attr_t * pg_key2,isns_tlv_t ** op,uint16_t * op_len)2006fcf3ce44SJohn Forte reg_get_portal(
2007fcf3ce44SJohn Forte 	isns_obj_t **p,
2008fcf3ce44SJohn Forte 	isns_attr_t *pg_key1,
2009fcf3ce44SJohn Forte 	isns_attr_t *pg_key2,
2010fcf3ce44SJohn Forte 	isns_tlv_t **op,
2011fcf3ce44SJohn Forte 	uint16_t *op_len
2012fcf3ce44SJohn Forte )
2013fcf3ce44SJohn Forte {
2014fcf3ce44SJohn Forte 	int ec = 0;
2015fcf3ce44SJohn Forte 
2016fcf3ce44SJohn Forte 	isns_tlv_t *tmp;
2017fcf3ce44SJohn Forte 	uint16_t tmp_len;
2018fcf3ce44SJohn Forte 	isns_attr_t *attr;
2019fcf3ce44SJohn Forte 
2020fcf3ce44SJohn Forte 	isns_obj_t *obj = NULL;
2021fcf3ce44SJohn Forte 
2022fcf3ce44SJohn Forte 	isns_tlv_t *ip;
2023fcf3ce44SJohn Forte 
2024fcf3ce44SJohn Forte 	tmp = *op;
2025fcf3ce44SJohn Forte 	tmp_len = *op_len;
2026fcf3ce44SJohn Forte 
2027fcf3ce44SJohn Forte 	/* keep the portal ip addr */
2028fcf3ce44SJohn Forte 	pg_key1->tag = PG_KEY2;
2029fcf3ce44SJohn Forte 	pg_key1->len = tmp->attr_len;
2030fcf3ce44SJohn Forte 	pg_key1->value.ip = (void *)&tmp->attr_value[0];
2031fcf3ce44SJohn Forte 	ip = tmp;
2032fcf3ce44SJohn Forte 
2033fcf3ce44SJohn Forte 	NEXT_TLV(tmp, tmp_len);
2034fcf3ce44SJohn Forte 	if (tmp_len > 8 &&
2035fcf3ce44SJohn Forte 	    tmp->attr_id == PORTAL_KEY2 &&
2036fcf3ce44SJohn Forte 	    tmp->attr_len == 4) {
2037fcf3ce44SJohn Forte 		/* keep the portal port */
2038fcf3ce44SJohn Forte 		pg_key2->tag = PG_KEY3;
2039fcf3ce44SJohn Forte 		pg_key2->len = tmp->attr_len;
2040fcf3ce44SJohn Forte 		pg_key2->value.ui = ntohl(*(uint32_t *)&tmp->attr_value[0]);
2041fcf3ce44SJohn Forte 
2042fcf3ce44SJohn Forte 		/* parse one portal object */
2043fcf3ce44SJohn Forte 		obj = obj_calloc(OBJ_PORTAL);
2044fcf3ce44SJohn Forte 		if (obj != NULL) {
2045fcf3ce44SJohn Forte 			/* copy ip addr attribute */
2046fcf3ce44SJohn Forte 			attr = &obj->attrs[
2047fcf3ce44SJohn Forte 			    ATTR_INDEX_PORTAL(ip->attr_id)];
2048fcf3ce44SJohn Forte 			ec = copy_attr(attr, ip);
2049fcf3ce44SJohn Forte 			/* copy port attribute */
2050fcf3ce44SJohn Forte 			if (ec == 0) {
2051fcf3ce44SJohn Forte 				attr = &obj->attrs[
2052fcf3ce44SJohn Forte 				    ATTR_INDEX_PORTAL(tmp->attr_id)];
2053fcf3ce44SJohn Forte 				ec = copy_attr(attr, tmp);
2054fcf3ce44SJohn Forte 			}
2055fcf3ce44SJohn Forte 			/* parse non-key attributes */
2056fcf3ce44SJohn Forte 			NEXT_TLV(tmp, tmp_len);
2057fcf3ce44SJohn Forte 			while (ec == 0 &&
2058fcf3ce44SJohn Forte 			    tmp_len >= 8 &&
2059fcf3ce44SJohn Forte 			    IS_PORTAL_ATTR(tmp->attr_id)) {
2060fcf3ce44SJohn Forte 				attr = &obj->attrs[
2061fcf3ce44SJohn Forte 				    ATTR_INDEX_PORTAL(
2062fcf3ce44SJohn Forte 				    tmp->attr_id)];
2063fcf3ce44SJohn Forte 				ec = copy_attr(attr, tmp);
2064fcf3ce44SJohn Forte 				NEXT_TLV(tmp, tmp_len);
2065fcf3ce44SJohn Forte 			}
2066fcf3ce44SJohn Forte 		} else {
2067fcf3ce44SJohn Forte 			/* no memory */
2068fcf3ce44SJohn Forte 			ec = ISNS_RSP_INTERNAL_ERROR;
2069fcf3ce44SJohn Forte 		}
2070fcf3ce44SJohn Forte 	} else {
2071fcf3ce44SJohn Forte 		/* ip address is not followed by port */
2072fcf3ce44SJohn Forte 		ec = ISNS_RSP_MSG_FORMAT_ERROR;
2073fcf3ce44SJohn Forte 	}
2074fcf3ce44SJohn Forte 
2075fcf3ce44SJohn Forte 	*p = obj;
2076fcf3ce44SJohn Forte 	*op = tmp;
2077fcf3ce44SJohn Forte 	*op_len = tmp_len;
2078fcf3ce44SJohn Forte 
2079fcf3ce44SJohn Forte 	return (ec);
2080fcf3ce44SJohn Forte }
2081fcf3ce44SJohn Forte 
2082fcf3ce44SJohn Forte /*
2083fcf3ce44SJohn Forte  * ****************************************************************************
2084fcf3ce44SJohn Forte  *
2085fcf3ce44SJohn Forte  * reg_get_pg:
2086fcf3ce44SJohn Forte  *	parse the Operating Attributes of the DevAttrReg message and
2087fcf3ce44SJohn Forte  *	create a Portal Group object.
2088fcf3ce44SJohn Forte  *
2089fcf3ce44SJohn Forte  * p	- the pointer of the object for returning.
2090fcf3ce44SJohn Forte  * op	- the operating attributes.
2091fcf3ce44SJohn Forte  * op_len - the length of the operating attributes.
2092fcf3ce44SJohn Forte  * return - error code.
2093fcf3ce44SJohn Forte  *
2094fcf3ce44SJohn Forte  * ****************************************************************************
2095fcf3ce44SJohn Forte  */
2096fcf3ce44SJohn Forte static int
reg_get_pg(isns_obj_t ** p,isns_tlv_t ** op,uint16_t * op_len)2097fcf3ce44SJohn Forte reg_get_pg(
2098fcf3ce44SJohn Forte 	isns_obj_t **p,
2099fcf3ce44SJohn Forte 	isns_tlv_t **op,
2100fcf3ce44SJohn Forte 	uint16_t *op_len
2101fcf3ce44SJohn Forte )
2102fcf3ce44SJohn Forte {
2103fcf3ce44SJohn Forte 	int ec = 0;
2104fcf3ce44SJohn Forte 
2105fcf3ce44SJohn Forte 	isns_tlv_t *tmp;
2106fcf3ce44SJohn Forte 	uint16_t tmp_len;
2107fcf3ce44SJohn Forte 	isns_attr_t *attr;
2108fcf3ce44SJohn Forte 
2109fcf3ce44SJohn Forte 	isns_obj_t *obj = NULL;
2110fcf3ce44SJohn Forte 
2111fcf3ce44SJohn Forte 	tmp = *op;
2112fcf3ce44SJohn Forte 	tmp_len = *op_len;
2113fcf3ce44SJohn Forte 
2114fcf3ce44SJohn Forte 	/* parse a complete pg object */
2115fcf3ce44SJohn Forte 	obj = obj_calloc(OBJ_PG);
2116fcf3ce44SJohn Forte 	if (obj != NULL) {
2117fcf3ce44SJohn Forte 		/* parse attributes */
2118fcf3ce44SJohn Forte 		do {
2119fcf3ce44SJohn Forte 			attr = &obj->attrs[
2120fcf3ce44SJohn Forte 			    ATTR_INDEX_PG(tmp->attr_id)];
2121fcf3ce44SJohn Forte 			ec = copy_attr(attr, tmp);
2122fcf3ce44SJohn Forte 			NEXT_TLV(tmp, tmp_len);
2123fcf3ce44SJohn Forte 		} while (ec == 0 &&
2124fcf3ce44SJohn Forte 		    tmp_len >= 8 &&
2125fcf3ce44SJohn Forte 		    IS_PG_ATTR(tmp->attr_id));
2126fcf3ce44SJohn Forte 	} else {
2127fcf3ce44SJohn Forte 		ec = ISNS_RSP_INTERNAL_ERROR;
2128fcf3ce44SJohn Forte 	}
2129fcf3ce44SJohn Forte 
2130fcf3ce44SJohn Forte 	*p = obj;
2131fcf3ce44SJohn Forte 	*op = tmp;
2132fcf3ce44SJohn Forte 	*op_len = tmp_len;
2133fcf3ce44SJohn Forte 
2134fcf3ce44SJohn Forte 	return (ec);
2135fcf3ce44SJohn Forte }
2136fcf3ce44SJohn Forte 
2137fcf3ce44SJohn Forte /*
2138fcf3ce44SJohn Forte  * ****************************************************************************
2139fcf3ce44SJohn Forte  *
2140fcf3ce44SJohn Forte  * reg_get_pg1:
2141fcf3ce44SJohn Forte  *	parse the Operating Attributes of the DevAttrReg message and
2142fcf3ce44SJohn Forte  *	create a Portal Group object which is followed to a Portal object.
2143fcf3ce44SJohn Forte  *
2144fcf3ce44SJohn Forte  * p	- the pointer of the object for returning.
2145fcf3ce44SJohn Forte  * pgt	- the size-3 array of pointers which have the pg portal ip addr, port
2146fcf3ce44SJohn Forte  *	  and the pg tag attributes.
2147fcf3ce44SJohn Forte  * op	- the operating attributes.
2148fcf3ce44SJohn Forte  * op_len - the length of the operating attributes.
2149fcf3ce44SJohn Forte  * return - error code.
2150fcf3ce44SJohn Forte  *
2151fcf3ce44SJohn Forte  * ****************************************************************************
2152fcf3ce44SJohn Forte  */
2153fcf3ce44SJohn Forte static int
reg_get_pg1(isns_obj_t ** p,isns_attr_t const * pgt,isns_tlv_t ** op,uint16_t * op_len)2154fcf3ce44SJohn Forte reg_get_pg1(
2155fcf3ce44SJohn Forte 	isns_obj_t **p,
2156fcf3ce44SJohn Forte 	isns_attr_t const *pgt,
2157fcf3ce44SJohn Forte 	isns_tlv_t **op,
2158fcf3ce44SJohn Forte 	uint16_t *op_len
2159fcf3ce44SJohn Forte )
2160fcf3ce44SJohn Forte {
2161fcf3ce44SJohn Forte 	int ec = 0;
2162fcf3ce44SJohn Forte 
2163fcf3ce44SJohn Forte 	isns_tlv_t *tmp;
2164fcf3ce44SJohn Forte 	uint16_t tmp_len;
2165fcf3ce44SJohn Forte 	isns_attr_t *attr;
2166fcf3ce44SJohn Forte 
2167fcf3ce44SJohn Forte 	isns_obj_t *obj = NULL;
2168fcf3ce44SJohn Forte 	int i = 0;
2169fcf3ce44SJohn Forte 
2170fcf3ce44SJohn Forte 	tmp = *op;
2171fcf3ce44SJohn Forte 	tmp_len = *op_len;
2172fcf3ce44SJohn Forte 
2173fcf3ce44SJohn Forte 	if (pgt[0].tag == PG_KEY2 &&
2174fcf3ce44SJohn Forte 	    pgt[1].tag == PG_KEY3) {
2175fcf3ce44SJohn Forte 		/* the pg iscsi storage node name is */
2176fcf3ce44SJohn Forte 		/* followed to a portal group tag */
2177fcf3ce44SJohn Forte 		obj = obj_calloc(OBJ_PG);
2178fcf3ce44SJohn Forte 		if (obj != NULL) {
2179fcf3ce44SJohn Forte 			/* copy pg iscsi storage node name */
2180fcf3ce44SJohn Forte 			attr = &obj->attrs[
2181fcf3ce44SJohn Forte 			    ATTR_INDEX_PG(tmp->attr_id)];
2182fcf3ce44SJohn Forte 			ec = copy_attr(attr, tmp);
2183fcf3ce44SJohn Forte 			/* copy pg ip addr, pg port & pgt */
2184fcf3ce44SJohn Forte 			while (ec == 0 && i < 3) {
2185fcf3ce44SJohn Forte 				attr = &obj->attrs[
2186fcf3ce44SJohn Forte 				    ATTR_INDEX_PG(pgt[i].tag)];
2187fcf3ce44SJohn Forte 				ec = assign_attr(attr, &pgt[i]);
2188fcf3ce44SJohn Forte 				i ++;
2189fcf3ce44SJohn Forte 			}
2190fcf3ce44SJohn Forte 			NEXT_TLV(tmp, tmp_len);
2191fcf3ce44SJohn Forte 		} else {
2192fcf3ce44SJohn Forte 			/* no memory */
2193fcf3ce44SJohn Forte 			ec = ISNS_RSP_INTERNAL_ERROR;
2194fcf3ce44SJohn Forte 		}
2195fcf3ce44SJohn Forte 	} else {
2196fcf3ce44SJohn Forte 		ec = ISNS_RSP_MSG_FORMAT_ERROR;
2197fcf3ce44SJohn Forte 	}
2198fcf3ce44SJohn Forte 
2199fcf3ce44SJohn Forte 	*p = obj;
2200fcf3ce44SJohn Forte 	*op = tmp;
2201fcf3ce44SJohn Forte 	*op_len = tmp_len;
2202fcf3ce44SJohn Forte 
2203fcf3ce44SJohn Forte 	return (ec);
2204fcf3ce44SJohn Forte }
2205fcf3ce44SJohn Forte 
2206fcf3ce44SJohn Forte /*
2207fcf3ce44SJohn Forte  * ****************************************************************************
2208fcf3ce44SJohn Forte  *
2209fcf3ce44SJohn Forte  * reg_get_pg2:
2210fcf3ce44SJohn Forte  *	parse the Operating Attributes of the DevAttrReg message and
2211fcf3ce44SJohn Forte  *	create a Portal Group object which is followed to a iSCSI
2212fcf3ce44SJohn Forte  *	Storage Node object.
2213fcf3ce44SJohn Forte  *
2214fcf3ce44SJohn Forte  * p	- the pointer of the object for returning.
2215fcf3ce44SJohn Forte  * pgt	- the size-3 array of pointers which have the pg iscsi storage
2216fcf3ce44SJohn Forte  *	  node name and the pg tag attributes.
2217fcf3ce44SJohn Forte  * op	- the operating attributes.
2218fcf3ce44SJohn Forte  * op_len - the length of the operating attributes.
2219fcf3ce44SJohn Forte  * return - error code.
2220fcf3ce44SJohn Forte  *
2221fcf3ce44SJohn Forte  * ****************************************************************************
2222fcf3ce44SJohn Forte  */
2223fcf3ce44SJohn Forte static int
reg_get_pg2(isns_obj_t ** p,isns_attr_t const * pgt,isns_tlv_t ** op,uint16_t * op_len)2224fcf3ce44SJohn Forte reg_get_pg2(
2225fcf3ce44SJohn Forte 	isns_obj_t **p,
2226fcf3ce44SJohn Forte 	isns_attr_t const *pgt,
2227fcf3ce44SJohn Forte 	isns_tlv_t **op,
2228fcf3ce44SJohn Forte 	uint16_t *op_len
2229fcf3ce44SJohn Forte )
2230fcf3ce44SJohn Forte {
2231fcf3ce44SJohn Forte 	int ec = 0;
2232fcf3ce44SJohn Forte 
2233fcf3ce44SJohn Forte 	isns_tlv_t *tmp;
2234fcf3ce44SJohn Forte 	uint16_t tmp_len;
2235fcf3ce44SJohn Forte 	isns_attr_t *attr;
2236fcf3ce44SJohn Forte 
2237fcf3ce44SJohn Forte 	isns_obj_t *obj = NULL;
2238fcf3ce44SJohn Forte 	int i = 0;
2239fcf3ce44SJohn Forte 
2240fcf3ce44SJohn Forte 	isns_tlv_t *ip;
2241fcf3ce44SJohn Forte 
2242fcf3ce44SJohn Forte 	tmp = *op;
2243fcf3ce44SJohn Forte 	tmp_len = *op_len;
2244fcf3ce44SJohn Forte 
2245fcf3ce44SJohn Forte 	/* keep ip address */
2246fcf3ce44SJohn Forte 	ip = tmp;
2247fcf3ce44SJohn Forte 	NEXT_TLV(tmp, tmp_len);
2248fcf3ce44SJohn Forte 
2249fcf3ce44SJohn Forte 	if (tmp_len > 8 &&
2250fcf3ce44SJohn Forte 	    /* expect pg portal port */
2251fcf3ce44SJohn Forte 	    tmp->attr_id == PG_KEY3 &&
2252fcf3ce44SJohn Forte 	    tmp->attr_len == 4 &&
2253fcf3ce44SJohn Forte 	    /* expect pg tag */
2254fcf3ce44SJohn Forte 	    pgt[2].tag == PG_PGT &&
2255fcf3ce44SJohn Forte 	    /* expect pg iscsi storage node name only */
2256fcf3ce44SJohn Forte 	    pgt[1].tag == 0 &&
2257fcf3ce44SJohn Forte 	    pgt[0].tag == PG_KEY1) {
2258fcf3ce44SJohn Forte 		/* the pg portal ip addr & port is followed */
2259fcf3ce44SJohn Forte 		/* to a pg tag and we have the iscsi storage */
2260fcf3ce44SJohn Forte 		/* node parsed previously */
2261fcf3ce44SJohn Forte 		obj = obj_calloc(OBJ_PG);
2262fcf3ce44SJohn Forte 		if (obj != NULL) {
2263fcf3ce44SJohn Forte 			/* copy the pg ip addr */
2264fcf3ce44SJohn Forte 			attr = &obj->attrs[
2265fcf3ce44SJohn Forte 			    ATTR_INDEX_PG(ip->attr_id)];
2266fcf3ce44SJohn Forte 			ec = copy_attr(attr, ip);
2267fcf3ce44SJohn Forte 			/* copy the pg port */
2268fcf3ce44SJohn Forte 			if (ec == 0) {
2269fcf3ce44SJohn Forte 				attr = &obj->attrs[
2270fcf3ce44SJohn Forte 				    ATTR_INDEX_PG(tmp->attr_id)];
2271fcf3ce44SJohn Forte 				ec = copy_attr(attr, tmp);
2272fcf3ce44SJohn Forte 			}
2273fcf3ce44SJohn Forte 			/* copy pg iscsi storage node name & pgt */
2274fcf3ce44SJohn Forte 			while (ec == 0 && i < 3) {
2275fcf3ce44SJohn Forte 				attr = &obj->attrs[
2276fcf3ce44SJohn Forte 				    ATTR_INDEX_PG(pgt[i].tag)];
2277fcf3ce44SJohn Forte 				ec = assign_attr(attr, &pgt[i]);
2278fcf3ce44SJohn Forte 				i += 2;
2279fcf3ce44SJohn Forte 			}
2280fcf3ce44SJohn Forte 			NEXT_TLV(tmp, tmp_len);
2281fcf3ce44SJohn Forte 		} else {
2282fcf3ce44SJohn Forte 			ec = ISNS_RSP_INTERNAL_ERROR;
2283fcf3ce44SJohn Forte 		}
2284fcf3ce44SJohn Forte 	} else {
2285fcf3ce44SJohn Forte 		ec = ISNS_RSP_MSG_FORMAT_ERROR;
2286fcf3ce44SJohn Forte 	}
2287fcf3ce44SJohn Forte 
2288fcf3ce44SJohn Forte 	*p = obj;
2289fcf3ce44SJohn Forte 	*op = tmp;
2290fcf3ce44SJohn Forte 	*op_len = tmp_len;
2291fcf3ce44SJohn Forte 
2292fcf3ce44SJohn Forte 	return (ec);
2293fcf3ce44SJohn Forte }
2294fcf3ce44SJohn Forte 
2295fcf3ce44SJohn Forte /*
2296fcf3ce44SJohn Forte  * ****************************************************************************
2297fcf3ce44SJohn Forte  *
2298fcf3ce44SJohn Forte  * reg_get_obj:
2299fcf3ce44SJohn Forte  *	parse and create one object from the rest of Operating Attributes
2300fcf3ce44SJohn Forte  *	of the DevAttrReg message, the object can be iSCSI Storage Node,
2301fcf3ce44SJohn Forte  *	Portal or Portal Group.
2302fcf3ce44SJohn Forte  *
2303fcf3ce44SJohn Forte  * p	- the pointer of the object for returning.
2304fcf3ce44SJohn Forte  * pgt	- an attribute array with size 3, the elements are:
2305fcf3ce44SJohn Forte  *	  0: the first pg key attribute, it is either the name of an
2306fcf3ce44SJohn Forte  *	     iscsi storage node object or the ip addr of a portal object.
2307fcf3ce44SJohn Forte  *	  1: the second pg key attribute, i.e. the portal port.
2308fcf3ce44SJohn Forte  *	  2: the portal group tag attribute.
2309fcf3ce44SJohn Forte  * op	- the operating attributes.
2310fcf3ce44SJohn Forte  * op_len - the length of the operating attributes.
2311fcf3ce44SJohn Forte  * return - error code.
2312fcf3ce44SJohn Forte  *
2313fcf3ce44SJohn Forte  * ****************************************************************************
2314fcf3ce44SJohn Forte  */
2315fcf3ce44SJohn Forte int
reg_get_obj(isns_obj_t ** p,isns_attr_t * pgt,isns_tlv_t ** op,uint16_t * op_len)2316fcf3ce44SJohn Forte reg_get_obj(
2317fcf3ce44SJohn Forte 	isns_obj_t **p,
2318fcf3ce44SJohn Forte 	isns_attr_t *pgt,
2319fcf3ce44SJohn Forte 	isns_tlv_t **op,
2320fcf3ce44SJohn Forte 	uint16_t *op_len
2321fcf3ce44SJohn Forte )
2322fcf3ce44SJohn Forte {
2323fcf3ce44SJohn Forte 	int ec = 0;
2324fcf3ce44SJohn Forte 
2325fcf3ce44SJohn Forte 	int derefd = 0;
2326fcf3ce44SJohn Forte 
2327fcf3ce44SJohn Forte 	uint32_t pg_tag;
2328fcf3ce44SJohn Forte 
2329fcf3ce44SJohn Forte 	if (*op_len == 0) {
2330fcf3ce44SJohn Forte 		*p = NULL;
2331fcf3ce44SJohn Forte 		return (0);
2332fcf3ce44SJohn Forte 	}
2333fcf3ce44SJohn Forte 
2334fcf3ce44SJohn Forte 	switch ((*op)->attr_id) {
2335fcf3ce44SJohn Forte 	case ISCSI_KEY:
2336fcf3ce44SJohn Forte 		ec = reg_get_iscsi(p, &pgt[0], op, op_len);
2337fcf3ce44SJohn Forte 		pgt[1].tag = 0;
2338fcf3ce44SJohn Forte 		pgt[2].tag = 0;
2339fcf3ce44SJohn Forte 		break;
2340fcf3ce44SJohn Forte 	case PORTAL_KEY1:
2341fcf3ce44SJohn Forte 		ec = reg_get_portal(p, &pgt[0], &pgt[1], op, op_len);
2342fcf3ce44SJohn Forte 		pgt[2].tag = 0;
2343fcf3ce44SJohn Forte 		break;
2344fcf3ce44SJohn Forte 	case PG_KEY1:
2345fcf3ce44SJohn Forte 		if (pgt[2].tag == PG_PGT) {
2346fcf3ce44SJohn Forte 			/* pg iscsi storage node name is */
2347fcf3ce44SJohn Forte 			/* followed to a pgt */
2348fcf3ce44SJohn Forte 			ec = reg_get_pg1(p, pgt, op, op_len);
2349fcf3ce44SJohn Forte 		} else {
2350fcf3ce44SJohn Forte 			/* a complete pg object */
2351fcf3ce44SJohn Forte 			ec = reg_get_pg(p, op, op_len);
2352fcf3ce44SJohn Forte 			pgt[0].tag = 0;
2353fcf3ce44SJohn Forte 			pgt[1].tag = 0;
2354fcf3ce44SJohn Forte 			pgt[2].tag = 0;
2355fcf3ce44SJohn Forte 		}
2356fcf3ce44SJohn Forte 		break;
2357fcf3ce44SJohn Forte 	case PG_KEY2:
2358fcf3ce44SJohn Forte 		/* pg portal ip addr is followed to a pgt */
2359fcf3ce44SJohn Forte 		ec = reg_get_pg2(p, pgt, op, op_len);
2360fcf3ce44SJohn Forte 		break;
2361fcf3ce44SJohn Forte 	case PG_PGT:
2362fcf3ce44SJohn Forte 		switch (pgt[0].tag) {
2363fcf3ce44SJohn Forte 		case 0:
2364fcf3ce44SJohn Forte 			/* portal group tag does not follow */
2365fcf3ce44SJohn Forte 			/* iscsi storage node or portal object */
2366fcf3ce44SJohn Forte 			*p = NULL;
2367fcf3ce44SJohn Forte 			ec = ISNS_RSP_MSG_FORMAT_ERROR;
2368fcf3ce44SJohn Forte 			break;
2369fcf3ce44SJohn Forte 		case PG_KEY1:
2370fcf3ce44SJohn Forte 		case PG_KEY2:
2371fcf3ce44SJohn Forte 			pgt[2].tag = PG_PGT;
2372fcf3ce44SJohn Forte 			pgt[2].len = (*op)->attr_len;
2373fcf3ce44SJohn Forte 			pg_tag = 0;
2374fcf3ce44SJohn Forte 			switch ((*op)->attr_len) {
2375fcf3ce44SJohn Forte 			case 4:
2376fcf3ce44SJohn Forte 				pg_tag = ntohl(*(uint32_t *)
2377fcf3ce44SJohn Forte 				    &(*op)->attr_value[0]);
2378*a2b0e4f1SToomas Soome 				/* FALLTHROUGH */
2379fcf3ce44SJohn Forte 			case 0:
2380fcf3ce44SJohn Forte 				pgt[2].value.ui = pg_tag;
2381fcf3ce44SJohn Forte 				break;
2382fcf3ce44SJohn Forte 			default:
2383fcf3ce44SJohn Forte 				*p = NULL;
2384fcf3ce44SJohn Forte 				ec = ISNS_RSP_MSG_FORMAT_ERROR;
2385fcf3ce44SJohn Forte 				break;
2386fcf3ce44SJohn Forte 			}
2387fcf3ce44SJohn Forte 			if (ec == 0) {
2388fcf3ce44SJohn Forte 				derefd = 1;
2389fcf3ce44SJohn Forte 				NEXT_TLV(*op, *op_len);
2390fcf3ce44SJohn Forte 				ec = reg_get_obj(p, pgt, op, op_len);
2391fcf3ce44SJohn Forte 			}
2392fcf3ce44SJohn Forte 			break;
2393fcf3ce44SJohn Forte 		default:
2394fcf3ce44SJohn Forte 			/* should never happen */
2395fcf3ce44SJohn Forte 			ASSERT(0);
2396fcf3ce44SJohn Forte 			*p = NULL;
2397fcf3ce44SJohn Forte 			ec = ISNS_RSP_INTERNAL_ERROR;
2398fcf3ce44SJohn Forte 			break;
2399fcf3ce44SJohn Forte 		}
2400fcf3ce44SJohn Forte 		break;
2401fcf3ce44SJohn Forte 	default:
2402fcf3ce44SJohn Forte 		*p = NULL;
2403fcf3ce44SJohn Forte 		ec = ISNS_RSP_MSG_FORMAT_ERROR;
2404fcf3ce44SJohn Forte 		break;
2405fcf3ce44SJohn Forte 	}
2406fcf3ce44SJohn Forte 
2407fcf3ce44SJohn Forte 	if (ec == 0 && derefd == 0) {
2408fcf3ce44SJohn Forte 		ec = update_deref_obj(*p);
2409fcf3ce44SJohn Forte 	}
2410fcf3ce44SJohn Forte 
2411fcf3ce44SJohn Forte 	if (ec != 0 && *p != NULL) {
2412fcf3ce44SJohn Forte 		free_one_object(*p);
2413fcf3ce44SJohn Forte 		*p = NULL;
2414fcf3ce44SJohn Forte 	}
2415fcf3ce44SJohn Forte 
2416fcf3ce44SJohn Forte 	return (ec);
2417fcf3ce44SJohn Forte }
2418fcf3ce44SJohn Forte 
2419fcf3ce44SJohn Forte /*
2420fcf3ce44SJohn Forte  * ****************************************************************************
2421fcf3ce44SJohn Forte  *
2422fcf3ce44SJohn Forte  * reg_auth_src:
2423fcf3ce44SJohn Forte  *	Authorize the source attribute the DevAttrReg message.
2424fcf3ce44SJohn Forte  *	The update can only performed by the node who has the owenership.
2425fcf3ce44SJohn Forte  *
2426fcf3ce44SJohn Forte  * p	- the pointer of the object for returning.
2427fcf3ce44SJohn Forte  * pgt	- an attribute array with size 3, the elements are:
2428fcf3ce44SJohn Forte  *	  0: the first pg key attribute, it is either the name of an
2429fcf3ce44SJohn Forte  *	     iscsi storage node object or the ip addr of a portal object.
2430fcf3ce44SJohn Forte  *	  1: the second pg key attribute, i.e. the portal port.
2431fcf3ce44SJohn Forte  *	  2: the portal group tag attribute.
2432fcf3ce44SJohn Forte  * op	- the operating attributes.
2433fcf3ce44SJohn Forte  * op_len - the length of the operating attributes.
2434fcf3ce44SJohn Forte  * return - error code.
2435fcf3ce44SJohn Forte  *
2436fcf3ce44SJohn Forte  * ****************************************************************************
2437fcf3ce44SJohn Forte  */
2438fcf3ce44SJohn Forte int
reg_auth_src(isns_type_t type,uint32_t uid,uchar_t * src)2439fcf3ce44SJohn Forte reg_auth_src(
2440fcf3ce44SJohn Forte 	isns_type_t type,
2441fcf3ce44SJohn Forte 	uint32_t uid,
2442fcf3ce44SJohn Forte 	uchar_t *src
2443fcf3ce44SJohn Forte )
2444fcf3ce44SJohn Forte {
2445fcf3ce44SJohn Forte 	lookup_ctrl_t lc;
2446fcf3ce44SJohn Forte 	uint32_t puid;
2447fcf3ce44SJohn Forte 
2448fcf3ce44SJohn Forte 	puid = is_parent_there(src);
2449fcf3ce44SJohn Forte 
2450fcf3ce44SJohn Forte 	if (TYPE_OF_PARENT[type] != 0) {
2451fcf3ce44SJohn Forte 		SET_UID_LCP(&lc, type, uid);
2452fcf3ce44SJohn Forte 		uid = cache_lookup(&lc, NULL, cb_get_parent);
2453fcf3ce44SJohn Forte 		type = TYPE_OF_PARENT[type];
2454fcf3ce44SJohn Forte 	}
2455fcf3ce44SJohn Forte 
2456fcf3ce44SJohn Forte 	if (uid != 0 && puid == 0) {
2457fcf3ce44SJohn Forte 		SET_UID_LCP(&lc, type, uid);
2458fcf3ce44SJohn Forte 		uid = cache_lookup(&lc, NULL, cb_node_child);
2459fcf3ce44SJohn Forte 	}
2460fcf3ce44SJohn Forte 
2461fcf3ce44SJohn Forte 	if (puid != uid) {
2462fcf3ce44SJohn Forte 		return (0);
2463fcf3ce44SJohn Forte 	}
2464fcf3ce44SJohn Forte 
2465fcf3ce44SJohn Forte 	return (1);
2466fcf3ce44SJohn Forte }
2467fcf3ce44SJohn Forte 
2468fcf3ce44SJohn Forte /*
2469fcf3ce44SJohn Forte  * ****************************************************************************
2470fcf3ce44SJohn Forte  *
2471fcf3ce44SJohn Forte  * is_obj_online:
2472fcf3ce44SJohn Forte  *	determine if the object is currently registered with the server.
2473fcf3ce44SJohn Forte  *
2474fcf3ce44SJohn Forte  * obj - the object being checked.
2475fcf3ce44SJohn Forte  * return - 0: not registered, otherwise registered.
2476fcf3ce44SJohn Forte  *
2477fcf3ce44SJohn Forte  * ****************************************************************************
2478fcf3ce44SJohn Forte  */
2479fcf3ce44SJohn Forte int
is_obj_online(const isns_obj_t * obj)2480fcf3ce44SJohn Forte is_obj_online(
2481fcf3ce44SJohn Forte 	const isns_obj_t *obj
2482fcf3ce44SJohn Forte )
2483fcf3ce44SJohn Forte {
2484fcf3ce44SJohn Forte 	int online = 1;
2485fcf3ce44SJohn Forte 
2486fcf3ce44SJohn Forte 	switch (obj->type) {
2487fcf3ce44SJohn Forte 	case OBJ_ISCSI:
2488fcf3ce44SJohn Forte 		online = obj->attrs[ATTR_INDEX_ISCSI(
2489fcf3ce44SJohn Forte 		    ISNS_ISCSI_NODE_TYPE_ATTR_ID)].value.ui == 0 ? 0 : 1;
2490fcf3ce44SJohn Forte 		break;
2491fcf3ce44SJohn Forte 	default:
2492fcf3ce44SJohn Forte 		break;
2493fcf3ce44SJohn Forte 	}
2494fcf3ce44SJohn Forte 
2495fcf3ce44SJohn Forte 	return (online);
2496fcf3ce44SJohn Forte }
2497fcf3ce44SJohn Forte 
2498fcf3ce44SJohn Forte static int
set_obj_offline(isns_obj_t * obj)2499fcf3ce44SJohn Forte set_obj_offline(
2500fcf3ce44SJohn Forte 	isns_obj_t *obj
2501fcf3ce44SJohn Forte )
2502fcf3ce44SJohn Forte {
2503fcf3ce44SJohn Forte 	switch (obj->type) {
2504fcf3ce44SJohn Forte 	case OBJ_ISCSI:
2505fcf3ce44SJohn Forte 		obj->attrs[ATTR_INDEX_ISCSI(
2506fcf3ce44SJohn Forte 		    ISNS_ISCSI_NODE_TYPE_ATTR_ID)].value.ui = 0;
2507fcf3ce44SJohn Forte 		break;
2508fcf3ce44SJohn Forte 	default:
2509fcf3ce44SJohn Forte 		break;
2510fcf3ce44SJohn Forte 	}
2511fcf3ce44SJohn Forte 
2512fcf3ce44SJohn Forte 	return (0);
2513fcf3ce44SJohn Forte }
2514fcf3ce44SJohn Forte 
2515fcf3ce44SJohn Forte /*
2516fcf3ce44SJohn Forte  * ****************************************************************************
2517fcf3ce44SJohn Forte  *
2518fcf3ce44SJohn Forte  * assoc_clone:
2519fcf3ce44SJohn Forte  *	clone the association object.
2520fcf3ce44SJohn Forte  *
2521fcf3ce44SJohn Forte  * p - the object being cloned.
2522fcf3ce44SJohn Forte  * clone_flag - 0: the object is being removed;
2523fcf3ce44SJohn Forte  *		1: only the association is being removed.
2524fcf3ce44SJohn Forte  * return - the clone object.
2525fcf3ce44SJohn Forte  *
2526fcf3ce44SJohn Forte  * ****************************************************************************
2527fcf3ce44SJohn Forte  */
2528fcf3ce44SJohn Forte void *
assoc_clone(void * p,int clone_flag)2529fcf3ce44SJohn Forte assoc_clone(
2530fcf3ce44SJohn Forte 	void *p,
2531fcf3ce44SJohn Forte 	int clone_flag
2532fcf3ce44SJohn Forte )
2533fcf3ce44SJohn Forte {
2534fcf3ce44SJohn Forte 	isns_type_t type;
2535fcf3ce44SJohn Forte 	isns_obj_t *clone;
2536fcf3ce44SJohn Forte 	const isns_attr_t *src_attr;
2537fcf3ce44SJohn Forte 	isns_attr_t *dst_attr;
2538fcf3ce44SJohn Forte 	uint32_t id, op;
2539fcf3ce44SJohn Forte 	int i = 0;
2540fcf3ce44SJohn Forte 
2541fcf3ce44SJohn Forte 	const isns_obj_t *obj;
2542fcf3ce44SJohn Forte 	uint32_t dd_flag;
2543fcf3ce44SJohn Forte 	int online;
2544fcf3ce44SJohn Forte 
2545fcf3ce44SJohn Forte 	int state;
2546fcf3ce44SJohn Forte 
2547fcf3ce44SJohn Forte 	obj = (isns_obj_t *)p;
2548fcf3ce44SJohn Forte 
2549fcf3ce44SJohn Forte 	if (obj->type != OBJ_ISCSI) {
2550fcf3ce44SJohn Forte 		return (NULL);
2551fcf3ce44SJohn Forte 	}
2552fcf3ce44SJohn Forte 
2553fcf3ce44SJohn Forte 	dd_flag = (get_dd_id(get_obj_uid(obj), ISNS_DEFAULT_DD_ID) == 0) ?
2554fcf3ce44SJohn Forte 	    0 : 1;
2555fcf3ce44SJohn Forte 	online = is_obj_online(obj);
2556fcf3ce44SJohn Forte 
2557fcf3ce44SJohn Forte 	state = (clone_flag << 2) | (dd_flag  << 1) | online;
2558fcf3ce44SJohn Forte 
2559fcf3ce44SJohn Forte 	/* clone_flag	dd_flag	online	action		*/
2560fcf3ce44SJohn Forte 	/* 0		0	0	ASSERT(0)	*/
2561fcf3ce44SJohn Forte 	/* 0		0	1	NULL		*/
2562fcf3ce44SJohn Forte 	/* 0		1	0	itself		*/
2563fcf3ce44SJohn Forte 	/* 0		1	1	clone it	*/
2564fcf3ce44SJohn Forte 	/* 1		0	0	NULL		*/
2565fcf3ce44SJohn Forte 	/* 1		0	1	itself		*/
2566fcf3ce44SJohn Forte 	/* 1		1	0	itself		*/
2567fcf3ce44SJohn Forte 	/* 1		1	1	itself		*/
2568fcf3ce44SJohn Forte 
2569fcf3ce44SJohn Forte 	switch (state) {
2570fcf3ce44SJohn Forte 	case 0:
2571fcf3ce44SJohn Forte 		ASSERT(0);
2572fcf3ce44SJohn Forte 	case 1:
2573fcf3ce44SJohn Forte 	case 4:
2574fcf3ce44SJohn Forte 		return (NULL);
2575fcf3ce44SJohn Forte 	case 2:
2576fcf3ce44SJohn Forte 	case 5:
2577fcf3ce44SJohn Forte 	case 6:
2578fcf3ce44SJohn Forte 	case 7:
2579fcf3ce44SJohn Forte 		return (p);
2580fcf3ce44SJohn Forte 	case 3:
2581fcf3ce44SJohn Forte 	default:
2582fcf3ce44SJohn Forte 		break;
2583fcf3ce44SJohn Forte 	}
2584fcf3ce44SJohn Forte 
2585fcf3ce44SJohn Forte 	type = obj->type;
2586fcf3ce44SJohn Forte 	clone = obj_calloc(type);
2587fcf3ce44SJohn Forte 
2588fcf3ce44SJohn Forte 	if (clone != NULL) {
2589fcf3ce44SJohn Forte 		id = UID_ATTR_INDEX[type];
2590fcf3ce44SJohn Forte 		src_attr = &(obj->attrs[id]);
2591fcf3ce44SJohn Forte 		dst_attr = &(clone->attrs[id]);
2592fcf3ce44SJohn Forte 		if (assign_attr(dst_attr, src_attr) != 0) {
2593fcf3ce44SJohn Forte 			free_one_object(clone);
2594fcf3ce44SJohn Forte 			return (NULL);
2595fcf3ce44SJohn Forte 		}
2596fcf3ce44SJohn Forte 
2597fcf3ce44SJohn Forte 		while (i < MAX_KEY_ATTRS) {
2598fcf3ce44SJohn Forte 			op = KEY_ATTR_OP[type][i];
2599fcf3ce44SJohn Forte 			if (op != 0) {
2600fcf3ce44SJohn Forte 				id = KEY_ATTR_INDEX[type][i];
2601fcf3ce44SJohn Forte 				src_attr = &(obj->attrs[id]);
2602fcf3ce44SJohn Forte 				dst_attr = &(clone->attrs[id]);
2603fcf3ce44SJohn Forte 				if (assign_attr(dst_attr, src_attr) != 0) {
2604fcf3ce44SJohn Forte 					free_one_object(clone);
2605fcf3ce44SJohn Forte 					return (NULL);
2606fcf3ce44SJohn Forte 				}
2607fcf3ce44SJohn Forte 			} else {
2608fcf3ce44SJohn Forte 				break;
2609fcf3ce44SJohn Forte 			}
2610fcf3ce44SJohn Forte 			i ++;
2611fcf3ce44SJohn Forte 		}
2612fcf3ce44SJohn Forte 	}
2613fcf3ce44SJohn Forte 
2614fcf3ce44SJohn Forte 	return ((void *)clone);
2615fcf3ce44SJohn Forte }
2616fcf3ce44SJohn Forte 
2617fcf3ce44SJohn Forte /*
2618fcf3ce44SJohn Forte  * ****************************************************************************
2619fcf3ce44SJohn Forte  *
2620fcf3ce44SJohn Forte  * free_one_object:
2621fcf3ce44SJohn Forte  *	free up one object.
2622fcf3ce44SJohn Forte  *
2623fcf3ce44SJohn Forte  * obj - the object being freed.
2624fcf3ce44SJohn Forte  *
2625fcf3ce44SJohn Forte  * ****************************************************************************
2626fcf3ce44SJohn Forte  */
2627fcf3ce44SJohn Forte void
free_one_object(isns_obj_t * obj)2628fcf3ce44SJohn Forte free_one_object(
2629fcf3ce44SJohn Forte 	isns_obj_t *obj
2630fcf3ce44SJohn Forte )
2631fcf3ce44SJohn Forte {
2632fcf3ce44SJohn Forte 	int i;
2633fcf3ce44SJohn Forte 	uint32_t *cuid;
2634fcf3ce44SJohn Forte 	if (obj == NULL) {
2635fcf3ce44SJohn Forte 		return;
2636fcf3ce44SJohn Forte 	}
2637fcf3ce44SJohn Forte 	for (i = 0; i < NUM_OF_ATTRS[obj->type]; i++) {
2638fcf3ce44SJohn Forte 		isns_attr_t *attr = &obj->attrs[i];
2639fcf3ce44SJohn Forte 		switch (attr->tag) {
2640fcf3ce44SJohn Forte 			case ISNS_EID_ATTR_ID:
2641fcf3ce44SJohn Forte 			case ISNS_ISCSI_NAME_ATTR_ID:
2642fcf3ce44SJohn Forte 			case ISNS_ISCSI_ALIAS_ATTR_ID:
2643fcf3ce44SJohn Forte 			case ISNS_ISCSI_AUTH_METHOD_ATTR_ID:
2644fcf3ce44SJohn Forte 			case ISNS_PG_ISCSI_NAME_ATTR_ID:
2645fcf3ce44SJohn Forte 			case ISNS_PORTAL_IP_ADDR_ATTR_ID:
2646fcf3ce44SJohn Forte 			case ISNS_PORTAL_NAME_ATTR_ID:
2647fcf3ce44SJohn Forte 			case ISNS_PG_PORTAL_IP_ADDR_ATTR_ID:
2648fcf3ce44SJohn Forte 			case ISNS_DD_SET_NAME_ATTR_ID:
2649fcf3ce44SJohn Forte 			case ISNS_DD_NAME_ATTR_ID:
2650fcf3ce44SJohn Forte 			case ISNS_DD_ISCSI_NAME_ATTR_ID:
2651fcf3ce44SJohn Forte 			case ISNS_DD_FC_PORT_NAME_ATTR_ID:
2652fcf3ce44SJohn Forte 			case ISNS_DD_PORTAL_IP_ADDR_ATTR_ID:
2653fcf3ce44SJohn Forte #ifdef DEBUG
2654fcf3ce44SJohn Forte 				if (verbose_mc) {
2655fcf3ce44SJohn Forte 					printf("memory(%d) deallocated\n",
2656fcf3ce44SJohn Forte 					    attr->len);
2657fcf3ce44SJohn Forte 				}
2658fcf3ce44SJohn Forte #endif
2659fcf3ce44SJohn Forte 				free(attr->value.ptr);
2660fcf3ce44SJohn Forte 				attr->value.ptr = NULL;
2661fcf3ce44SJohn Forte 				break;
2662fcf3ce44SJohn Forte 			default:
2663fcf3ce44SJohn Forte 				break;
2664fcf3ce44SJohn Forte 		}
2665fcf3ce44SJohn Forte 	}
2666fcf3ce44SJohn Forte 
2667fcf3ce44SJohn Forte 	/* free child uids */
2668fcf3ce44SJohn Forte 	i = 0;
2669fcf3ce44SJohn Forte 	while (i < NUM_OF_CHILD[obj->type]) {
2670fcf3ce44SJohn Forte 		cuid = get_child_n(obj, i);
2671fcf3ce44SJohn Forte 		free(cuid);
2672fcf3ce44SJohn Forte 		i ++;
2673fcf3ce44SJohn Forte 	}
2674fcf3ce44SJohn Forte 
2675fcf3ce44SJohn Forte 	/* at last, free the object itself */
2676fcf3ce44SJohn Forte #ifdef DEBUG
2677fcf3ce44SJohn Forte 	if (verbose_mc) {
2678fcf3ce44SJohn Forte 		printf("object(%d) deallocated\n", obj->type);
2679fcf3ce44SJohn Forte 	}
2680fcf3ce44SJohn Forte #endif
2681fcf3ce44SJohn Forte 	free(obj);
2682fcf3ce44SJohn Forte }
2683fcf3ce44SJohn Forte 
2684fcf3ce44SJohn Forte /*
2685fcf3ce44SJohn Forte  * ****************************************************************************
2686fcf3ce44SJohn Forte  *
2687fcf3ce44SJohn Forte  * free_object:
2688fcf3ce44SJohn Forte  *	free up one object.
2689fcf3ce44SJohn Forte  *
2690fcf3ce44SJohn Forte  * obj - the object being freed.
2691fcf3ce44SJohn Forte  *
2692fcf3ce44SJohn Forte  * ****************************************************************************
2693fcf3ce44SJohn Forte  */
2694fcf3ce44SJohn Forte void
free_object(isns_obj_t * obj)2695fcf3ce44SJohn Forte free_object(
2696fcf3ce44SJohn Forte 	isns_obj_t *obj
2697fcf3ce44SJohn Forte )
2698fcf3ce44SJohn Forte {
2699fcf3ce44SJohn Forte 	free_one_object(obj);
2700fcf3ce44SJohn Forte }
2701fcf3ce44SJohn Forte 
2702fcf3ce44SJohn Forte /*
2703fcf3ce44SJohn Forte  * ****************************************************************************
2704fcf3ce44SJohn Forte  *
2705fcf3ce44SJohn Forte  * set_parent_obj:
2706fcf3ce44SJohn Forte  *	set the parent object UID.
2707fcf3ce44SJohn Forte  *
2708fcf3ce44SJohn Forte  * obj - the child object.
2709fcf3ce44SJohn Forte  * puid- the parent object UID.
2710fcf3ce44SJohn Forte  * return - error code.
2711fcf3ce44SJohn Forte  *
2712fcf3ce44SJohn Forte  * ****************************************************************************
2713fcf3ce44SJohn Forte  */
2714fcf3ce44SJohn Forte int
set_parent_obj(isns_obj_t * obj,uint32_t puid)2715fcf3ce44SJohn Forte set_parent_obj(
2716fcf3ce44SJohn Forte 	isns_obj_t *obj,
2717fcf3ce44SJohn Forte 	uint32_t puid
2718fcf3ce44SJohn Forte )
2719fcf3ce44SJohn Forte {
2720fcf3ce44SJohn Forte 	uint32_t *const p = get_parent_p(obj);
2721fcf3ce44SJohn Forte 	if (p != NULL) {
2722fcf3ce44SJohn Forte 		*p = puid;
2723fcf3ce44SJohn Forte 	}
2724fcf3ce44SJohn Forte 
2725fcf3ce44SJohn Forte 	return (0);
2726fcf3ce44SJohn Forte }
2727fcf3ce44SJohn Forte 
2728fcf3ce44SJohn Forte /*
2729fcf3ce44SJohn Forte  * ****************************************************************************
2730fcf3ce44SJohn Forte  *
2731fcf3ce44SJohn Forte  * buff_child_obj:
2732fcf3ce44SJohn Forte  *	add a child object UID to the child object array.
2733fcf3ce44SJohn Forte  *
2734fcf3ce44SJohn Forte  * obj - the parent object.
2735fcf3ce44SJohn Forte  * child_type - the type of the child object.
2736fcf3ce44SJohn Forte  * number  - the number of the child object.
2737fcf3ce44SJohn Forte  * return - the length of the child object UID array.
2738fcf3ce44SJohn Forte  *
2739fcf3ce44SJohn Forte  * ****************************************************************************
2740fcf3ce44SJohn Forte  */
2741fcf3ce44SJohn Forte int
buff_child_obj(const isns_type_t ptype,const isns_type_t ctype,const void * c,void const *** child)2742fcf3ce44SJohn Forte buff_child_obj(
2743fcf3ce44SJohn Forte 	const isns_type_t ptype,
2744fcf3ce44SJohn Forte 	const isns_type_t ctype,
2745fcf3ce44SJohn Forte 	const void *c,
2746fcf3ce44SJohn Forte 	void const ***child
2747fcf3ce44SJohn Forte )
2748fcf3ce44SJohn Forte {
2749fcf3ce44SJohn Forte 	int ec = 0;
2750fcf3ce44SJohn Forte 
2751fcf3ce44SJohn Forte 	int i = 0;
2752fcf3ce44SJohn Forte 	void const ***pp, **p;
2753fcf3ce44SJohn Forte 	uint32_t num, new_num;
2754fcf3ce44SJohn Forte 
2755fcf3ce44SJohn Forte 	pp = NULL;
2756fcf3ce44SJohn Forte 	/* get the pointer of the array which the child belongs to */
2757fcf3ce44SJohn Forte 	while (i < NUM_OF_CHILD[ptype]) {
2758fcf3ce44SJohn Forte 		if (TYPE_OF_CHILD[ptype][i] == ctype) {
2759fcf3ce44SJohn Forte 			pp = &child[i];
2760fcf3ce44SJohn Forte 			break;
2761fcf3ce44SJohn Forte 		}
2762fcf3ce44SJohn Forte 		i ++;
2763fcf3ce44SJohn Forte 	}
2764fcf3ce44SJohn Forte 
2765fcf3ce44SJohn Forte 	/* the child type is not applicable */
2766fcf3ce44SJohn Forte 	if (pp == NULL) {
2767fcf3ce44SJohn Forte 		return (ec);
2768fcf3ce44SJohn Forte 	}
2769fcf3ce44SJohn Forte 
2770fcf3ce44SJohn Forte 	p = *pp;
2771fcf3ce44SJohn Forte 	/* get an empty slot from the uid array for this child */
2772fcf3ce44SJohn Forte 	if (p != NULL) {
2773fcf3ce44SJohn Forte 		num = (uint32_t)*p;
2774fcf3ce44SJohn Forte 		i = 0;
2775fcf3ce44SJohn Forte 		while (i < num) {
2776fcf3ce44SJohn Forte 			if (p[++i] == NULL) {
2777fcf3ce44SJohn Forte 				/* found it */
2778fcf3ce44SJohn Forte 				p[i] = c;
2779fcf3ce44SJohn Forte 				return (ec);
2780fcf3ce44SJohn Forte 			}
2781fcf3ce44SJohn Forte 		}
2782fcf3ce44SJohn Forte 		p = *pp;
2783fcf3ce44SJohn Forte 		new_num = num + 1;
2784fcf3ce44SJohn Forte 	} else {
2785fcf3ce44SJohn Forte 		num = 0;
2786fcf3ce44SJohn Forte 		new_num = 1;
2787fcf3ce44SJohn Forte 	}
2788fcf3ce44SJohn Forte 
2789fcf3ce44SJohn Forte 	/* the array is full, enlarge the child uid array */
2790fcf3ce44SJohn Forte 	p = (void const **)realloc(p, (new_num + 1) * sizeof (void *));
2791fcf3ce44SJohn Forte 	if (p != NULL) {
2792fcf3ce44SJohn Forte 		*pp = p;
2793fcf3ce44SJohn Forte 		*p = (void *)new_num;
2794fcf3ce44SJohn Forte 		p[new_num] = c;
2795fcf3ce44SJohn Forte 	} else {
2796fcf3ce44SJohn Forte 		ec = ISNS_RSP_INTERNAL_ERROR;
2797fcf3ce44SJohn Forte 	}
2798fcf3ce44SJohn Forte 
2799fcf3ce44SJohn Forte 	return (ec);
2800fcf3ce44SJohn Forte }
2801fcf3ce44SJohn Forte 
2802fcf3ce44SJohn Forte /*
2803fcf3ce44SJohn Forte  * ****************************************************************************
2804fcf3ce44SJohn Forte  *
2805fcf3ce44SJohn Forte  * update_child_object:
2806fcf3ce44SJohn Forte  *	update the child object of a network entity object.
2807fcf3ce44SJohn Forte  *
2808fcf3ce44SJohn Forte  * puid - the UID of the parent object, i.e. the network entity object.
2809fcf3ce44SJohn Forte  * child_type - the type of the child object.
2810fcf3ce44SJohn Forte  * child_uid  - the uid of the child object.
2811fcf3ce44SJohn Forte  * return - error code.
2812fcf3ce44SJohn Forte  *
2813fcf3ce44SJohn Forte  * ****************************************************************************
2814fcf3ce44SJohn Forte  */
2815fcf3ce44SJohn Forte int
update_child_obj(const isns_type_t ptype,const uint32_t puid,void const *** child,int child_flag)2816fcf3ce44SJohn Forte update_child_obj(
2817fcf3ce44SJohn Forte 	const isns_type_t ptype,
2818fcf3ce44SJohn Forte 	const uint32_t puid,
2819fcf3ce44SJohn Forte 	void const ***child,
2820fcf3ce44SJohn Forte 	int child_flag
2821fcf3ce44SJohn Forte )
2822fcf3ce44SJohn Forte {
2823fcf3ce44SJohn Forte 	int ec = 0;
2824fcf3ce44SJohn Forte 
2825fcf3ce44SJohn Forte 	lookup_ctrl_t lc;
2826fcf3ce44SJohn Forte 
2827fcf3ce44SJohn Forte 	SET_UID_LCP(&lc, ptype, puid);
2828fcf3ce44SJohn Forte 
2829fcf3ce44SJohn Forte 	lc.data[1].ptr = (uchar_t *)child;
2830fcf3ce44SJohn Forte 	lc.data[2].ui = child_flag;
2831fcf3ce44SJohn Forte 
2832fcf3ce44SJohn Forte 	ec = cache_lookup(&lc, NULL, cb_add_child);
2833fcf3ce44SJohn Forte 
2834fcf3ce44SJohn Forte 	return (ec);
2835fcf3ce44SJohn Forte }
2836fcf3ce44SJohn Forte 
2837fcf3ce44SJohn Forte int
update_ref_obj(const isns_obj_t * obj)2838fcf3ce44SJohn Forte update_ref_obj(
2839fcf3ce44SJohn Forte 	const isns_obj_t *obj
2840fcf3ce44SJohn Forte )
2841fcf3ce44SJohn Forte {
2842fcf3ce44SJohn Forte 	uint32_t uid;
2843fcf3ce44SJohn Forte 	lookup_ctrl_t lc;
2844fcf3ce44SJohn Forte 	isns_type_t t;
2845fcf3ce44SJohn Forte 
2846fcf3ce44SJohn Forte 	t = obj->type;
2847fcf3ce44SJohn Forte 
2848fcf3ce44SJohn Forte 	if (TYPE_OF_REF[t][0] != 0) {
2849fcf3ce44SJohn Forte 		(void) setup_ref_lcp(&lc, obj, NULL);
2850fcf3ce44SJohn Forte 
2851fcf3ce44SJohn Forte 		lc.id[2] = t;
2852fcf3ce44SJohn Forte 		lc.data[2].ui = get_obj_uid(obj);
2853fcf3ce44SJohn Forte 
2854fcf3ce44SJohn Forte 		uid = 0;
2855fcf3ce44SJohn Forte 		do {
2856fcf3ce44SJohn Forte 			lc.curr_uid = uid;
2857fcf3ce44SJohn Forte 			(void) cache_lookup(&lc, &uid, cb_set_ref);
2858fcf3ce44SJohn Forte 		} while (uid != 0);
2859fcf3ce44SJohn Forte 	}
2860fcf3ce44SJohn Forte 
2861fcf3ce44SJohn Forte 	return (0);
2862fcf3ce44SJohn Forte }
2863fcf3ce44SJohn Forte 
2864fcf3ce44SJohn Forte /*
2865fcf3ce44SJohn Forte  * ****************************************************************************
2866fcf3ce44SJohn Forte  *
2867fcf3ce44SJohn Forte  * verify_ref_obj:
2868fcf3ce44SJohn Forte  *	update the reference bit of a portal group object.
2869fcf3ce44SJohn Forte  *
2870fcf3ce44SJohn Forte  * obj - the object being ref'ed.
2871fcf3ce44SJohn Forte  * return - error code.
2872fcf3ce44SJohn Forte  *
2873fcf3ce44SJohn Forte  * ****************************************************************************
2874fcf3ce44SJohn Forte  */
2875fcf3ce44SJohn Forte int
verify_ref_obj(const isns_type_t ptype,const uint32_t puid,void const *** child)2876fcf3ce44SJohn Forte verify_ref_obj(
2877fcf3ce44SJohn Forte 	const isns_type_t ptype,
2878fcf3ce44SJohn Forte 	const uint32_t puid,
2879fcf3ce44SJohn Forte 	void const ***child
2880fcf3ce44SJohn Forte )
2881fcf3ce44SJohn Forte {
2882fcf3ce44SJohn Forte 	int ec = 0;
2883fcf3ce44SJohn Forte 
2884fcf3ce44SJohn Forte 	lookup_ctrl_t lc;
2885fcf3ce44SJohn Forte 
2886fcf3ce44SJohn Forte 	SET_UID_LCP(&lc, ptype, puid);
2887fcf3ce44SJohn Forte 
2888fcf3ce44SJohn Forte 	lc.data[1].ptr = (uchar_t *)child;
2889fcf3ce44SJohn Forte 
2890fcf3ce44SJohn Forte 	ec = cache_lookup(&lc, NULL, cb_verify_ref);
2891fcf3ce44SJohn Forte 
2892fcf3ce44SJohn Forte 	return (ec);
2893fcf3ce44SJohn Forte }
2894fcf3ce44SJohn Forte 
2895fcf3ce44SJohn Forte int
update_deref_obj(isns_obj_t * obj)2896fcf3ce44SJohn Forte update_deref_obj(
2897fcf3ce44SJohn Forte 	isns_obj_t *obj
2898fcf3ce44SJohn Forte )
2899fcf3ce44SJohn Forte {
2900fcf3ce44SJohn Forte 	int ec = 0;
2901fcf3ce44SJohn Forte 
2902fcf3ce44SJohn Forte 	isns_type_t t, rt;
2903fcf3ce44SJohn Forte 	lookup_ctrl_t lc;
2904fcf3ce44SJohn Forte 	int i, ref_count;
2905fcf3ce44SJohn Forte 
2906fcf3ce44SJohn Forte 	uint32_t uid, *refp;
2907fcf3ce44SJohn Forte 
2908fcf3ce44SJohn Forte 	t = obj->type;
2909fcf3ce44SJohn Forte 	i = ref_count = 0;
2910fcf3ce44SJohn Forte 	while (i < NUM_OF_REF[t]) {
2911fcf3ce44SJohn Forte 		rt = TYPE_OF_REF[t][i + 1];
2912fcf3ce44SJohn Forte 		(void) setup_deref_lcp(&lc, obj, rt);
2913fcf3ce44SJohn Forte 		uid = is_obj_there(&lc);
2914fcf3ce44SJohn Forte 		if (uid != 0) {
2915fcf3ce44SJohn Forte 			refp = get_ref_p(obj, lc.type);
2916fcf3ce44SJohn Forte 			*refp = uid;
2917fcf3ce44SJohn Forte 			ref_count ++;
2918fcf3ce44SJohn Forte 		}
2919fcf3ce44SJohn Forte 		i ++;
2920fcf3ce44SJohn Forte 	}
2921fcf3ce44SJohn Forte 
2922fcf3ce44SJohn Forte 	if (i > 0 && ref_count == 0) {
2923fcf3ce44SJohn Forte 		ec = ISNS_RSP_INVALID_REGIS;
2924fcf3ce44SJohn Forte 	}
2925fcf3ce44SJohn Forte 
2926fcf3ce44SJohn Forte 	return (ec);
2927fcf3ce44SJohn Forte }
2928fcf3ce44SJohn Forte 
2929fcf3ce44SJohn Forte /*
2930fcf3ce44SJohn Forte  * ****************************************************************************
2931fcf3ce44SJohn Forte  *
2932fcf3ce44SJohn Forte  * register_object:
2933fcf3ce44SJohn Forte  *	add one object to the object container.
2934fcf3ce44SJohn Forte  *
2935fcf3ce44SJohn Forte  * obj	- the object being added.
2936fcf3ce44SJohn Forte  * uid_p- the pointer for returning object UID.
2937fcf3ce44SJohn Forte  * update_p- the pointer for returning flag which indicates if the object
2938fcf3ce44SJohn Forte  *		is newly registered or updated with an existing one.
2939fcf3ce44SJohn Forte  * return - error code.
2940fcf3ce44SJohn Forte  *
2941fcf3ce44SJohn Forte  * ****************************************************************************
2942fcf3ce44SJohn Forte  */
2943fcf3ce44SJohn Forte int
register_object(isns_obj_t * obj,uint32_t * uid_p,int * update_p)2944fcf3ce44SJohn Forte register_object(
2945fcf3ce44SJohn Forte 	isns_obj_t *obj,
2946fcf3ce44SJohn Forte 	uint32_t *uid_p,
2947fcf3ce44SJohn Forte 	int *update_p
2948fcf3ce44SJohn Forte )
2949fcf3ce44SJohn Forte {
2950fcf3ce44SJohn Forte 	return (cache_add(obj, 0, uid_p, update_p));
2951fcf3ce44SJohn Forte }
2952fcf3ce44SJohn Forte 
2953fcf3ce44SJohn Forte /*
2954fcf3ce44SJohn Forte  * ****************************************************************************
2955fcf3ce44SJohn Forte  *
2956fcf3ce44SJohn Forte  * register_assoc:
2957fcf3ce44SJohn Forte  *	add one association object to the object container, the association
2958fcf3ce44SJohn Forte  *	object has only the information for discovery domain membership, i.e.
2959fcf3ce44SJohn Forte  *	a name and UID only.
2960fcf3ce44SJohn Forte  *
2961fcf3ce44SJohn Forte  * obj	- the association object being added.
2962fcf3ce44SJohn Forte  * uid_p- the pointer for returning object UID.
2963fcf3ce44SJohn Forte  * return - error code.
2964fcf3ce44SJohn Forte  *
2965fcf3ce44SJohn Forte  * ****************************************************************************
2966fcf3ce44SJohn Forte  */
2967fcf3ce44SJohn Forte int
register_assoc(isns_obj_t * obj,uint32_t * uid_p)2968fcf3ce44SJohn Forte register_assoc(
2969fcf3ce44SJohn Forte 	isns_obj_t *obj,
2970fcf3ce44SJohn Forte 	uint32_t *uid_p
2971fcf3ce44SJohn Forte )
2972fcf3ce44SJohn Forte {
2973fcf3ce44SJohn Forte 	return (cache_add(obj, 1, uid_p, NULL));
2974fcf3ce44SJohn Forte }
2975fcf3ce44SJohn Forte 
2976fcf3ce44SJohn Forte /*
2977fcf3ce44SJohn Forte  * ****************************************************************************
2978fcf3ce44SJohn Forte  *
2979fcf3ce44SJohn Forte  * is_obj_there:
2980fcf3ce44SJohn Forte  *	check if the object is registered or not.
2981fcf3ce44SJohn Forte  *
2982fcf3ce44SJohn Forte  * lcp	- the lookup control data.
2983fcf3ce44SJohn Forte  * return - the object UID.
2984fcf3ce44SJohn Forte  *
2985fcf3ce44SJohn Forte  * ****************************************************************************
2986fcf3ce44SJohn Forte  */
2987fcf3ce44SJohn Forte uint32_t
is_obj_there(lookup_ctrl_t * lcp)2988fcf3ce44SJohn Forte is_obj_there(
2989fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp
2990fcf3ce44SJohn Forte )
2991fcf3ce44SJohn Forte {
2992fcf3ce44SJohn Forte 	uint32_t uid;
2993fcf3ce44SJohn Forte 
2994fcf3ce44SJohn Forte 	(void) cache_lookup(lcp, &uid, NULL);
2995fcf3ce44SJohn Forte 
2996fcf3ce44SJohn Forte 	return (uid);
2997fcf3ce44SJohn Forte }
2998fcf3ce44SJohn Forte 
2999fcf3ce44SJohn Forte uint32_t
is_parent_there(uchar_t * src)3000fcf3ce44SJohn Forte is_parent_there(
3001fcf3ce44SJohn Forte 	uchar_t *src
3002fcf3ce44SJohn Forte )
3003fcf3ce44SJohn Forte {
3004fcf3ce44SJohn Forte 	lookup_ctrl_t lc;
3005fcf3ce44SJohn Forte 
3006fcf3ce44SJohn Forte 	lc.curr_uid = 0;
3007fcf3ce44SJohn Forte 	lc.type = OBJ_ISCSI;
3008fcf3ce44SJohn Forte 	lc.id[0] = ATTR_INDEX_ISCSI(ISNS_ISCSI_NAME_ATTR_ID);
3009fcf3ce44SJohn Forte 	lc.op[0] = OP_STRING;
3010fcf3ce44SJohn Forte 	lc.data[0].ptr = src;
3011fcf3ce44SJohn Forte 	lc.op[1] = 0;
3012fcf3ce44SJohn Forte 
3013fcf3ce44SJohn Forte 	return (cache_lookup(&lc, NULL, cb_get_parent));
3014fcf3ce44SJohn Forte }
3015fcf3ce44SJohn Forte 
3016fcf3ce44SJohn Forte /*
3017fcf3ce44SJohn Forte  * ****************************************************************************
3018fcf3ce44SJohn Forte  *
3019fcf3ce44SJohn Forte  * setup_ref_lcp:
3020fcf3ce44SJohn Forte  *	prepare the lookup control data for looking up a portal group
3021fcf3ce44SJohn Forte  *	object which references to a iscsi stroage node and/or a portal
3022fcf3ce44SJohn Forte  *	object.
3023fcf3ce44SJohn Forte  *
3024fcf3ce44SJohn Forte  * lcp	- the lookup control data.
3025fcf3ce44SJohn Forte  * iscsi- the ref'ed iscsi storage node object.
3026fcf3ce44SJohn Forte  * portal- the ref'ed portal object.
3027fcf3ce44SJohn Forte  * return - error code.
3028fcf3ce44SJohn Forte  *
3029fcf3ce44SJohn Forte  * ****************************************************************************
3030fcf3ce44SJohn Forte  */
3031fcf3ce44SJohn Forte static int
setup_ref_lcp(lookup_ctrl_t * lcp,const isns_obj_t * iscsi,const isns_obj_t * portal)3032fcf3ce44SJohn Forte setup_ref_lcp(
3033fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp,
3034fcf3ce44SJohn Forte 	const isns_obj_t *iscsi,
3035fcf3ce44SJohn Forte 	const isns_obj_t *portal
3036fcf3ce44SJohn Forte )
3037fcf3ce44SJohn Forte {
3038fcf3ce44SJohn Forte 	int i = 0, j = 0;
3039fcf3ce44SJohn Forte 
3040fcf3ce44SJohn Forte 	lcp->curr_uid = 0;
3041fcf3ce44SJohn Forte 	lcp->type = TYPE_OF_REF[iscsi->type][0];
3042fcf3ce44SJohn Forte 
3043fcf3ce44SJohn Forte 	/* extrace the matching attributes from iscsi storage node object */
3044fcf3ce44SJohn Forte 	while (iscsi != NULL &&
3045fcf3ce44SJohn Forte 	    i < MAX_REF_MATCH &&
3046fcf3ce44SJohn Forte 	    REF_MATCH_OPS[iscsi->type][i] > 0) {
3047fcf3ce44SJohn Forte 		lcp->id[i] = REF_MATCH_ID2[iscsi->type][i];
3048fcf3ce44SJohn Forte 		lcp->op[i] = REF_MATCH_OPS[iscsi->type][i];
3049fcf3ce44SJohn Forte 		lcp->data[i].ptr = iscsi->attrs[
3050fcf3ce44SJohn Forte 		    REF_MATCH_ID1[iscsi->type][i]].value.ptr;
3051fcf3ce44SJohn Forte 		i ++;
3052fcf3ce44SJohn Forte 	}
3053fcf3ce44SJohn Forte 
3054fcf3ce44SJohn Forte 	/* extrace the matching attributes from portal object */
3055fcf3ce44SJohn Forte 	while (portal != NULL &&
3056fcf3ce44SJohn Forte 	    i < MAX_LOOKUP_CTRL &&
3057fcf3ce44SJohn Forte 	    j < MAX_REF_MATCH &&
3058fcf3ce44SJohn Forte 	    REF_MATCH_OPS[portal->type][j] > 0) {
3059fcf3ce44SJohn Forte 		lcp->id[i] = REF_MATCH_ID2[portal->type][j];
3060fcf3ce44SJohn Forte 		lcp->op[i] = REF_MATCH_OPS[portal->type][j];
3061fcf3ce44SJohn Forte 		lcp->data[i].ptr = portal->attrs[
3062fcf3ce44SJohn Forte 		    REF_MATCH_ID1[portal->type][j]].value.ptr;
3063fcf3ce44SJohn Forte 		j ++;
3064fcf3ce44SJohn Forte 		i ++;
3065fcf3ce44SJohn Forte 	}
3066fcf3ce44SJohn Forte 
3067fcf3ce44SJohn Forte 	if (i < MAX_LOOKUP_CTRL) {
3068fcf3ce44SJohn Forte 		lcp->op[i] = 0;
3069fcf3ce44SJohn Forte 	}
3070fcf3ce44SJohn Forte 
3071fcf3ce44SJohn Forte 	return (0);
3072fcf3ce44SJohn Forte }
3073fcf3ce44SJohn Forte 
3074fcf3ce44SJohn Forte static int
setup_deref_lcp(lookup_ctrl_t * lcp,const isns_obj_t * pg,isns_type_t t)3075fcf3ce44SJohn Forte setup_deref_lcp(
3076fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp,
3077fcf3ce44SJohn Forte 	const isns_obj_t *pg,
3078fcf3ce44SJohn Forte 	isns_type_t t
3079fcf3ce44SJohn Forte )
3080fcf3ce44SJohn Forte {
3081fcf3ce44SJohn Forte 	int i = 0;
3082fcf3ce44SJohn Forte 
3083fcf3ce44SJohn Forte 	lcp->curr_uid = 0;
3084fcf3ce44SJohn Forte 	lcp->type = t;
3085fcf3ce44SJohn Forte 
3086fcf3ce44SJohn Forte 	/* extrace the matching attributes from iscsi storage node object */
3087fcf3ce44SJohn Forte 	while (i < MAX_REF_MATCH &&
3088fcf3ce44SJohn Forte 	    REF_MATCH_OPS[t][i] > 0) {
3089fcf3ce44SJohn Forte 		lcp->id[i] = REF_MATCH_ID1[t][i];
3090fcf3ce44SJohn Forte 		lcp->op[i] = REF_MATCH_OPS[t][i];
3091fcf3ce44SJohn Forte 		lcp->data[i].ptr = pg->attrs[
3092fcf3ce44SJohn Forte 		    REF_MATCH_ID2[t][i]].value.ptr;
3093fcf3ce44SJohn Forte 		i ++;
3094fcf3ce44SJohn Forte 	}
3095fcf3ce44SJohn Forte 
3096fcf3ce44SJohn Forte 	if (i < MAX_LOOKUP_CTRL) {
3097fcf3ce44SJohn Forte 		lcp->op[i] = 0;
3098fcf3ce44SJohn Forte 	}
3099fcf3ce44SJohn Forte 
3100fcf3ce44SJohn Forte 	return (0);
3101fcf3ce44SJohn Forte }
3102fcf3ce44SJohn Forte 
3103fcf3ce44SJohn Forte /*
3104fcf3ce44SJohn Forte  * ****************************************************************************
3105fcf3ce44SJohn Forte  *
3106fcf3ce44SJohn Forte  * setup_parent_lcp:
3107fcf3ce44SJohn Forte  *	prepare the lookup control data for looking up parent object
3108fcf3ce44SJohn Forte  *	with a child object.
3109fcf3ce44SJohn Forte  *
3110fcf3ce44SJohn Forte  * lcp	- the lookup control data.
3111fcf3ce44SJohn Forte  * obj	- the child object.
3112fcf3ce44SJohn Forte  * return - parent object UID.
3113fcf3ce44SJohn Forte  *
3114fcf3ce44SJohn Forte  * ****************************************************************************
3115fcf3ce44SJohn Forte  */
3116fcf3ce44SJohn Forte static uint32_t
setup_parent_lcp(lookup_ctrl_t * lcp,isns_obj_t * obj)3117fcf3ce44SJohn Forte setup_parent_lcp(
3118fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp,
3119fcf3ce44SJohn Forte 	isns_obj_t *obj
3120fcf3ce44SJohn Forte )
3121fcf3ce44SJohn Forte {
3122fcf3ce44SJohn Forte 	isns_type_t ptype;
3123fcf3ce44SJohn Forte 	uint32_t puid;
3124fcf3ce44SJohn Forte 
3125fcf3ce44SJohn Forte 	puid = get_parent_uid(obj);
3126fcf3ce44SJohn Forte 	if (puid != 0) {
3127fcf3ce44SJohn Forte 		ptype = TYPE_OF_PARENT[obj->type];
3128fcf3ce44SJohn Forte 		SET_UID_LCP(lcp, ptype, puid);
3129fcf3ce44SJohn Forte 		lcp->data[1].ui = obj->type;
3130fcf3ce44SJohn Forte 		lcp->data[2].ui = get_obj_uid(obj);
3131fcf3ce44SJohn Forte 	}
3132fcf3ce44SJohn Forte 
3133fcf3ce44SJohn Forte 	return (puid);
3134fcf3ce44SJohn Forte }
3135fcf3ce44SJohn Forte 
3136fcf3ce44SJohn Forte static int
cb_get_parent(void * p1,void * p2)3137fcf3ce44SJohn Forte cb_get_parent(
3138fcf3ce44SJohn Forte 	void *p1,
3139fcf3ce44SJohn Forte 	/* LINTED E_FUNC_ARG_UNUSED */
3140fcf3ce44SJohn Forte 	void *p2
3141fcf3ce44SJohn Forte )
3142fcf3ce44SJohn Forte {
3143fcf3ce44SJohn Forte 	return (get_parent_uid(p1));
3144fcf3ce44SJohn Forte }
3145fcf3ce44SJohn Forte 
3146fcf3ce44SJohn Forte static int
cb_node_child(void * p1,void * p2)3147fcf3ce44SJohn Forte cb_node_child(
3148fcf3ce44SJohn Forte 	void *p1,
3149fcf3ce44SJohn Forte 	/* LINTED E_FUNC_ARG_UNUSED */
3150fcf3ce44SJohn Forte 	void *p2
3151fcf3ce44SJohn Forte )
3152fcf3ce44SJohn Forte {
3153fcf3ce44SJohn Forte 	isns_obj_t *obj = (isns_obj_t *)p1;
3154fcf3ce44SJohn Forte 
3155fcf3ce44SJohn Forte 	uint32_t num, uid;
3156fcf3ce44SJohn Forte 
3157fcf3ce44SJohn Forte 	uint32_t *cuid = get_child_t(obj, OBJ_ISCSI);
3158fcf3ce44SJohn Forte 
3159fcf3ce44SJohn Forte 	if (cuid != NULL) {
3160fcf3ce44SJohn Forte 		num = *cuid;
3161fcf3ce44SJohn Forte 	} else {
3162fcf3ce44SJohn Forte 		num = 0;
3163fcf3ce44SJohn Forte 	}
3164fcf3ce44SJohn Forte 
3165fcf3ce44SJohn Forte 	while (num > 0) {
3166fcf3ce44SJohn Forte 		uid = *++cuid;
3167fcf3ce44SJohn Forte 		if (uid != 0) {
3168fcf3ce44SJohn Forte 			return (uid);
3169fcf3ce44SJohn Forte 		}
3170fcf3ce44SJohn Forte 		num --;
3171fcf3ce44SJohn Forte 	}
3172fcf3ce44SJohn Forte 
3173fcf3ce44SJohn Forte 	return (0);
3174fcf3ce44SJohn Forte }
3175fcf3ce44SJohn Forte 
3176fcf3ce44SJohn Forte /*
3177fcf3ce44SJohn Forte  * ****************************************************************************
3178fcf3ce44SJohn Forte  *
3179fcf3ce44SJohn Forte  * cb_set_ref:
3180fcf3ce44SJohn Forte  *	callback function which sets the reference bit to 1 according to
3181fcf3ce44SJohn Forte  *	the type of object.
3182fcf3ce44SJohn Forte  *
3183fcf3ce44SJohn Forte  * p1	- the object.
3184fcf3ce44SJohn Forte  * p2	- the lcp.
3185fcf3ce44SJohn Forte  * return - error code.
3186fcf3ce44SJohn Forte  *
3187fcf3ce44SJohn Forte  * ****************************************************************************
3188fcf3ce44SJohn Forte  */
3189fcf3ce44SJohn Forte static int
cb_set_ref(void * p1,void * p2)3190fcf3ce44SJohn Forte cb_set_ref(
3191fcf3ce44SJohn Forte 	void *p1,
3192fcf3ce44SJohn Forte 	void *p2
3193fcf3ce44SJohn Forte )
3194fcf3ce44SJohn Forte {
3195fcf3ce44SJohn Forte 	isns_obj_t *obj = (isns_obj_t *)p1;
3196fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp = (lookup_ctrl_t *)p2;
3197fcf3ce44SJohn Forte 
3198fcf3ce44SJohn Forte 	isns_type_t t;
3199fcf3ce44SJohn Forte 	uint32_t u;
3200fcf3ce44SJohn Forte 
3201fcf3ce44SJohn Forte 	uint32_t *refp;
3202fcf3ce44SJohn Forte 
3203fcf3ce44SJohn Forte 	t = lcp->id[2];
3204fcf3ce44SJohn Forte 	u = lcp->data[2].ui;
3205fcf3ce44SJohn Forte 	refp = get_ref_p(obj, t);
3206fcf3ce44SJohn Forte 	*refp = u;
3207fcf3ce44SJohn Forte 
3208fcf3ce44SJohn Forte 	/* successful */
3209fcf3ce44SJohn Forte 	return (0);
3210fcf3ce44SJohn Forte }
3211fcf3ce44SJohn Forte 
3212fcf3ce44SJohn Forte /*
3213fcf3ce44SJohn Forte  * ****************************************************************************
3214fcf3ce44SJohn Forte  *
3215fcf3ce44SJohn Forte  * cb_clear_ref:
3216fcf3ce44SJohn Forte  *	callback function which clears the reference bit according to
3217fcf3ce44SJohn Forte  *	the type of object.
3218fcf3ce44SJohn Forte  *
3219fcf3ce44SJohn Forte  * p1	- the object.
3220fcf3ce44SJohn Forte  * p2	- the lcp.
3221fcf3ce44SJohn Forte  * return - 1: the object is no longer ref'ed, 0: otherwise.
3222fcf3ce44SJohn Forte  *
3223fcf3ce44SJohn Forte  * ****************************************************************************
3224fcf3ce44SJohn Forte  */
3225fcf3ce44SJohn Forte static int
cb_clear_ref(void * p1,void * p2)3226fcf3ce44SJohn Forte cb_clear_ref(
3227fcf3ce44SJohn Forte 	void *p1,
3228fcf3ce44SJohn Forte 	void *p2
3229fcf3ce44SJohn Forte )
3230fcf3ce44SJohn Forte {
3231fcf3ce44SJohn Forte 	isns_obj_t *obj = (isns_obj_t *)p1;
3232fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp = (lookup_ctrl_t *)p2;
3233fcf3ce44SJohn Forte 
3234fcf3ce44SJohn Forte 	isns_type_t t;
3235fcf3ce44SJohn Forte 	uint32_t *refp;
3236fcf3ce44SJohn Forte 
3237fcf3ce44SJohn Forte 	int i = 0;
3238fcf3ce44SJohn Forte 	uint32_t ref;
3239fcf3ce44SJohn Forte 
3240fcf3ce44SJohn Forte 	t = lcp->data[2].ui;
3241fcf3ce44SJohn Forte 	refp = get_ref_p(obj, t);
3242fcf3ce44SJohn Forte 	*refp = 0;
3243fcf3ce44SJohn Forte 
3244fcf3ce44SJohn Forte 	while (i < NUM_OF_REF[obj->type]) {
3245fcf3ce44SJohn Forte 		ref = get_ref_n(obj, i);
3246fcf3ce44SJohn Forte 		if (ref != 0) {
3247fcf3ce44SJohn Forte 			return (0);
3248fcf3ce44SJohn Forte 		}
3249fcf3ce44SJohn Forte 		i ++;
3250fcf3ce44SJohn Forte 	}
3251fcf3ce44SJohn Forte 
3252fcf3ce44SJohn Forte 	return (1);
3253fcf3ce44SJohn Forte }
3254fcf3ce44SJohn Forte 
3255fcf3ce44SJohn Forte static int
cb_add_child(void * p1,void * p2)3256fcf3ce44SJohn Forte cb_add_child(
3257fcf3ce44SJohn Forte 	void *p1,
3258fcf3ce44SJohn Forte 	void *p2
3259fcf3ce44SJohn Forte )
3260fcf3ce44SJohn Forte {
3261fcf3ce44SJohn Forte 	isns_obj_t *obj = (isns_obj_t *)p1;
3262fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp = (lookup_ctrl_t *)p2;
3263fcf3ce44SJohn Forte 
3264fcf3ce44SJohn Forte 	const void ***child;
3265fcf3ce44SJohn Forte 	const void **vpp;
3266fcf3ce44SJohn Forte 	uint32_t vnum;
3267fcf3ce44SJohn Forte 	int child_flag;
3268fcf3ce44SJohn Forte 
3269fcf3ce44SJohn Forte 	uint32_t **upp, *up;
3270fcf3ce44SJohn Forte 	uint32_t num;
3271fcf3ce44SJohn Forte 
3272fcf3ce44SJohn Forte 	isns_obj_t *o;
3273fcf3ce44SJohn Forte 
3274fcf3ce44SJohn Forte 	int i = 0;
3275fcf3ce44SJohn Forte 
3276fcf3ce44SJohn Forte 	child = (const void ***)lcp->data[1].ptr;
3277fcf3ce44SJohn Forte 	child_flag = lcp->data[2].ui;
3278fcf3ce44SJohn Forte 
3279fcf3ce44SJohn Forte 	while (i < NUM_OF_CHILD[obj->type]) {
3280fcf3ce44SJohn Forte 		vpp = child[i];
3281fcf3ce44SJohn Forte 		if (vpp != NULL &&
3282fcf3ce44SJohn Forte 		    (vnum = (uint32_t)*vpp) > 0 &&
3283fcf3ce44SJohn Forte 		    *(vpp + 1) != NULL) {
3284fcf3ce44SJohn Forte 			upp = get_child_np(obj, i);
3285fcf3ce44SJohn Forte 			if (*upp == NULL) {
3286fcf3ce44SJohn Forte 				if (child_flag == 0 &&
3287fcf3ce44SJohn Forte 				    sizeof (typeof (**upp)) ==
3288fcf3ce44SJohn Forte 				    sizeof (typeof (**child))) {
3289fcf3ce44SJohn Forte 					*upp = (uint32_t *)vpp;
3290fcf3ce44SJohn Forte 					vpp = NULL;
3291fcf3ce44SJohn Forte 					child[i] = NULL;
3292fcf3ce44SJohn Forte 				}
3293fcf3ce44SJohn Forte 				num = vnum;
3294fcf3ce44SJohn Forte 			} else {
3295fcf3ce44SJohn Forte 				num = **upp + vnum;
3296fcf3ce44SJohn Forte 			}
3297fcf3ce44SJohn Forte 			if (vpp != NULL) {
3298fcf3ce44SJohn Forte 				/* copy required */
3299fcf3ce44SJohn Forte 				up = (uint32_t *)realloc(*upp,
3300fcf3ce44SJohn Forte 				    (num + 1) * sizeof (uint32_t));
3301fcf3ce44SJohn Forte 				if (up == NULL) {
3302fcf3ce44SJohn Forte 					return (ISNS_RSP_INTERNAL_ERROR);
3303fcf3ce44SJohn Forte 				}
3304fcf3ce44SJohn Forte 				*upp = up;
3305fcf3ce44SJohn Forte 				*up = num;
3306fcf3ce44SJohn Forte 				up += num;
3307fcf3ce44SJohn Forte 				vpp += vnum;
3308fcf3ce44SJohn Forte 				while (vnum > 0) {
3309fcf3ce44SJohn Forte 					if (*vpp == NULL) {
3310fcf3ce44SJohn Forte 						*up = 0;
3311fcf3ce44SJohn Forte 					} else if (child_flag == 0) {
3312fcf3ce44SJohn Forte 						*up = (uint32_t)*vpp;
3313fcf3ce44SJohn Forte 						*vpp = NULL;
3314fcf3ce44SJohn Forte 					} else {
3315fcf3ce44SJohn Forte 						o = (isns_obj_t *)*vpp;
3316fcf3ce44SJohn Forte 						*up = get_obj_uid(o);
3317fcf3ce44SJohn Forte 						if (is_obj_online(o) == 0) {
3318fcf3ce44SJohn Forte 							free_object(o);
3319fcf3ce44SJohn Forte 						}
3320fcf3ce44SJohn Forte 						*vpp = NULL;
3321fcf3ce44SJohn Forte 					}
3322fcf3ce44SJohn Forte 					up --;
3323fcf3ce44SJohn Forte 					vpp --;
3324fcf3ce44SJohn Forte 					vnum --;
3325fcf3ce44SJohn Forte 				}
3326fcf3ce44SJohn Forte 			}
3327fcf3ce44SJohn Forte 		}
3328fcf3ce44SJohn Forte 		i ++;
3329fcf3ce44SJohn Forte 	}
3330fcf3ce44SJohn Forte 
3331fcf3ce44SJohn Forte 	return (0);
3332fcf3ce44SJohn Forte }
3333fcf3ce44SJohn Forte 
3334fcf3ce44SJohn Forte /*
3335fcf3ce44SJohn Forte  * ****************************************************************************
3336fcf3ce44SJohn Forte  *
3337fcf3ce44SJohn Forte  * cb_remove_child:
3338fcf3ce44SJohn Forte  *	callback function which removes a child object UID from the
3339fcf3ce44SJohn Forte  *	children objet UID array of the parent object.
3340fcf3ce44SJohn Forte  *
3341fcf3ce44SJohn Forte  * p1	- the object.
3342fcf3ce44SJohn Forte  * p2	- the lcp.
3343fcf3ce44SJohn Forte  * return - 1: no more such type of child object, 0: otherwise.
3344fcf3ce44SJohn Forte  *
3345fcf3ce44SJohn Forte  * ****************************************************************************
3346fcf3ce44SJohn Forte  */
3347fcf3ce44SJohn Forte static int
cb_remove_child(void * p1,void * p2)3348fcf3ce44SJohn Forte cb_remove_child(
3349fcf3ce44SJohn Forte 	void *p1,
3350fcf3ce44SJohn Forte 	void *p2
3351fcf3ce44SJohn Forte )
3352fcf3ce44SJohn Forte {
3353fcf3ce44SJohn Forte 	isns_obj_t *obj = (isns_obj_t *)p1;
3354fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp = (lookup_ctrl_t *)p2;
3355fcf3ce44SJohn Forte 	uint32_t child_type = lcp->data[1].ui;
3356fcf3ce44SJohn Forte 	uint32_t child_uid = lcp->data[2].ui;
3357fcf3ce44SJohn Forte 	uint32_t *cuidp, cuid, num_of_child = 0;
3358fcf3ce44SJohn Forte 	int i;
3359fcf3ce44SJohn Forte 
3360fcf3ce44SJohn Forte 	/* get the children object UID array */
3361fcf3ce44SJohn Forte 	cuidp = get_child_t(obj, child_type);
3362fcf3ce44SJohn Forte 	if (cuidp != NULL) {
3363fcf3ce44SJohn Forte 		num_of_child = *cuidp;
3364fcf3ce44SJohn Forte 	}
3365fcf3ce44SJohn Forte 
3366fcf3ce44SJohn Forte 	/* remove it */
3367fcf3ce44SJohn Forte 	while (num_of_child > 0) {
3368fcf3ce44SJohn Forte 		cuid = *++cuidp;
3369fcf3ce44SJohn Forte 		if (cuid == child_uid) {
3370fcf3ce44SJohn Forte 			*cuidp = 0;
3371fcf3ce44SJohn Forte 			break;
3372fcf3ce44SJohn Forte 		}
3373fcf3ce44SJohn Forte 		num_of_child --;
3374fcf3ce44SJohn Forte 	}
3375fcf3ce44SJohn Forte 
3376fcf3ce44SJohn Forte 	/* check if all of child object UIDs are removed */
3377fcf3ce44SJohn Forte 	i = 0;
3378fcf3ce44SJohn Forte 	while (i < NUM_OF_CHILD[obj->type]) {
3379fcf3ce44SJohn Forte 		cuidp = get_child_n(obj, i);
3380fcf3ce44SJohn Forte 		if (cuidp != NULL) {
3381fcf3ce44SJohn Forte 			num_of_child = *cuidp;
3382fcf3ce44SJohn Forte 			while (num_of_child > 0) {
3383fcf3ce44SJohn Forte 				cuid = *++cuidp;
3384fcf3ce44SJohn Forte 				if (cuid != 0) {
3385fcf3ce44SJohn Forte 					return (0);
3386fcf3ce44SJohn Forte 				}
3387fcf3ce44SJohn Forte 				num_of_child --;
3388fcf3ce44SJohn Forte 			}
3389fcf3ce44SJohn Forte 		}
3390fcf3ce44SJohn Forte 		i ++;
3391fcf3ce44SJohn Forte 	}
3392fcf3ce44SJohn Forte 
3393fcf3ce44SJohn Forte 	return (1);
3394fcf3ce44SJohn Forte }
3395fcf3ce44SJohn Forte 
3396fcf3ce44SJohn Forte static int
cb_verify_ref(void * p1,void * p2)3397fcf3ce44SJohn Forte cb_verify_ref(
3398fcf3ce44SJohn Forte 	void *p1,
3399fcf3ce44SJohn Forte 	void *p2
3400fcf3ce44SJohn Forte )
3401fcf3ce44SJohn Forte {
3402fcf3ce44SJohn Forte 	int ec = 0;
3403fcf3ce44SJohn Forte 
3404fcf3ce44SJohn Forte 	isns_obj_t *parent = (isns_obj_t *)p1;
3405fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp = (lookup_ctrl_t *)p2;
3406fcf3ce44SJohn Forte 
3407fcf3ce44SJohn Forte 	const void ***child;
3408fcf3ce44SJohn Forte 
3409fcf3ce44SJohn Forte 	const void **vpp;
3410fcf3ce44SJohn Forte 	const void *vp;
3411fcf3ce44SJohn Forte 	uint32_t vnum;
3412fcf3ce44SJohn Forte 
3413fcf3ce44SJohn Forte 	const void **evpp;
3414fcf3ce44SJohn Forte 	const void *evp;
3415fcf3ce44SJohn Forte 	uint32_t evnum;
3416fcf3ce44SJohn Forte 
3417fcf3ce44SJohn Forte 	isns_type_t pt; /* parent object type */
3418fcf3ce44SJohn Forte 	isns_type_t ct; /* child object type */
3419fcf3ce44SJohn Forte 	isns_type_t rt; /* ref object type */
3420fcf3ce44SJohn Forte 	isns_type_t et; /* peer object type */
3421fcf3ce44SJohn Forte 
3422fcf3ce44SJohn Forte 	uint32_t *up;
3423fcf3ce44SJohn Forte 	uint32_t u;
3424fcf3ce44SJohn Forte 	uint32_t unum;
3425fcf3ce44SJohn Forte 
3426fcf3ce44SJohn Forte 	lookup_ctrl_t lc;
3427fcf3ce44SJohn Forte 	uint8_t flag[MAX_OBJ_TYPE + 1] = { 0 };
3428fcf3ce44SJohn Forte 
3429fcf3ce44SJohn Forte 	int i, j, k;
3430fcf3ce44SJohn Forte 
3431fcf3ce44SJohn Forte 	pt = parent->type;
3432fcf3ce44SJohn Forte 
3433fcf3ce44SJohn Forte 	child = (const void ***)lcp->data[1].ptr;
3434fcf3ce44SJohn Forte 
3435fcf3ce44SJohn Forte 	for (i = 0; i < NUM_OF_CHILD[pt]; i++) {
3436fcf3ce44SJohn Forte 		ct = TYPE_OF_CHILD[pt][i];
3437fcf3ce44SJohn Forte 		rt = TYPE_OF_REF[ct][0];
3438fcf3ce44SJohn Forte 		if (rt == 0) {
3439fcf3ce44SJohn Forte 			continue;
3440fcf3ce44SJohn Forte 		}
3441fcf3ce44SJohn Forte 
3442fcf3ce44SJohn Forte 		et = TYPE_OF_REF[ct][1];
3443fcf3ce44SJohn Forte 		vpp = child[i];
3444fcf3ce44SJohn Forte 		if (vpp != NULL) {
3445fcf3ce44SJohn Forte 			vnum = (uint32_t)*vpp;
3446fcf3ce44SJohn Forte 			up = get_child_t(parent, et);
3447fcf3ce44SJohn Forte 			if (up != NULL) {
3448fcf3ce44SJohn Forte 				unum = *up;
3449fcf3ce44SJohn Forte 			} else {
3450fcf3ce44SJohn Forte 				unum = 0;
3451fcf3ce44SJohn Forte 			}
3452fcf3ce44SJohn Forte 		} else {
3453fcf3ce44SJohn Forte 			vnum = 0;
3454fcf3ce44SJohn Forte 		}
3455fcf3ce44SJohn Forte 
3456fcf3ce44SJohn Forte 		j = vnum;
3457fcf3ce44SJohn Forte 		while (j > 0) {
3458fcf3ce44SJohn Forte 			vp = vpp[j];
3459fcf3ce44SJohn Forte 			if (vp != NULL) {
3460fcf3ce44SJohn Forte 				(void) setup_ref_lcp(&lc, vp, NULL);
3461fcf3ce44SJohn Forte 				k = unum;
3462fcf3ce44SJohn Forte 				while (k > 0) {
3463fcf3ce44SJohn Forte 					u = up[k];
3464fcf3ce44SJohn Forte 					if (u != 0) {
3465fcf3ce44SJohn Forte 						ec = ref_new2old(
3466fcf3ce44SJohn Forte 						    &lc, et, u, vp);
3467fcf3ce44SJohn Forte 						if (ec != 0) {
3468fcf3ce44SJohn Forte 							return (ec);
3469fcf3ce44SJohn Forte 						}
3470fcf3ce44SJohn Forte 					}
3471fcf3ce44SJohn Forte 					k --;
3472fcf3ce44SJohn Forte 				} /* End of while each unum */
3473fcf3ce44SJohn Forte 			}
3474fcf3ce44SJohn Forte 			j --;
3475fcf3ce44SJohn Forte 		} /* End of while each vnum */
3476fcf3ce44SJohn Forte 
3477fcf3ce44SJohn Forte 		if (flag[ct] != 0) {
3478fcf3ce44SJohn Forte 			continue;
3479fcf3ce44SJohn Forte 		}
3480fcf3ce44SJohn Forte 
3481fcf3ce44SJohn Forte 		evnum = 0;
3482fcf3ce44SJohn Forte 		j = 0;
3483fcf3ce44SJohn Forte 		while (j < NUM_OF_CHILD[pt]) {
3484fcf3ce44SJohn Forte 			if (TYPE_OF_CHILD[pt][j] == et) {
3485fcf3ce44SJohn Forte 				evpp = child[j];
3486fcf3ce44SJohn Forte 				if (evpp != NULL) {
3487fcf3ce44SJohn Forte 					evnum = (uint32_t)*evpp;
3488fcf3ce44SJohn Forte 				}
3489fcf3ce44SJohn Forte 				break;
3490fcf3ce44SJohn Forte 			}
3491fcf3ce44SJohn Forte 			j ++;
3492fcf3ce44SJohn Forte 		}
3493fcf3ce44SJohn Forte 
3494fcf3ce44SJohn Forte 		j = vnum;
3495fcf3ce44SJohn Forte 		while (j > 0) {
3496fcf3ce44SJohn Forte 			vp = vpp[j];
3497fcf3ce44SJohn Forte 			k = evnum;
3498fcf3ce44SJohn Forte 			while (k > 0) {
3499fcf3ce44SJohn Forte 				evp = evpp[k];
3500fcf3ce44SJohn Forte 				if (vp != NULL && evp != NULL) {
3501fcf3ce44SJohn Forte 					(void) setup_ref_lcp(&lc, vp, evp);
3502fcf3ce44SJohn Forte 					ec = ref_new2new(&lc, vp, evp);
3503fcf3ce44SJohn Forte 					if (ec != 0) {
3504fcf3ce44SJohn Forte 						return (ec);
3505fcf3ce44SJohn Forte 					}
3506fcf3ce44SJohn Forte 				}
3507fcf3ce44SJohn Forte 				k --;
3508fcf3ce44SJohn Forte 			}
3509fcf3ce44SJohn Forte 			j --;
3510fcf3ce44SJohn Forte 		} /* End of while each vnum */
3511fcf3ce44SJohn Forte 
3512fcf3ce44SJohn Forte 		flag[et] = 1;
3513fcf3ce44SJohn Forte 	} /* End of for each type of child */
3514fcf3ce44SJohn Forte 
3515fcf3ce44SJohn Forte 	return (ec);
3516fcf3ce44SJohn Forte }
3517fcf3ce44SJohn Forte 
3518fcf3ce44SJohn Forte static int
cb_ref_new2old(void * p1,void * p2)3519fcf3ce44SJohn Forte cb_ref_new2old(
3520fcf3ce44SJohn Forte 	void *p1,
3521fcf3ce44SJohn Forte 	void *p2
3522fcf3ce44SJohn Forte )
3523fcf3ce44SJohn Forte {
3524fcf3ce44SJohn Forte 	isns_obj_t *obj = (isns_obj_t *)p1;
3525fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp = (lookup_ctrl_t *)p2;
3526fcf3ce44SJohn Forte 
3527fcf3ce44SJohn Forte 	isns_type_t et;
3528fcf3ce44SJohn Forte 	uint32_t uu;
3529fcf3ce44SJohn Forte 
3530fcf3ce44SJohn Forte 	uint32_t ref;
3531fcf3ce44SJohn Forte 
3532fcf3ce44SJohn Forte 	int match;
3533fcf3ce44SJohn Forte 
3534fcf3ce44SJohn Forte 	et = lcp->id[2];
3535fcf3ce44SJohn Forte 	uu = lcp->data[2].ui;
3536fcf3ce44SJohn Forte 
3537fcf3ce44SJohn Forte 	ref = get_ref_t(obj, et);
3538fcf3ce44SJohn Forte 
3539fcf3ce44SJohn Forte 	if (ref == uu) {
3540fcf3ce44SJohn Forte 		match = 1;
3541fcf3ce44SJohn Forte 	} else {
3542fcf3ce44SJohn Forte 		match = 0;
3543fcf3ce44SJohn Forte 	}
3544fcf3ce44SJohn Forte 
3545fcf3ce44SJohn Forte 	return (match);
3546fcf3ce44SJohn Forte }
3547fcf3ce44SJohn Forte 
3548fcf3ce44SJohn Forte static int
cb_new_ref(void * p1,void * p2)3549fcf3ce44SJohn Forte cb_new_ref(
3550fcf3ce44SJohn Forte 	void *p1,
3551fcf3ce44SJohn Forte 	void *p2
3552fcf3ce44SJohn Forte )
3553fcf3ce44SJohn Forte {
3554fcf3ce44SJohn Forte 	int ec = 0;
3555fcf3ce44SJohn Forte 
3556fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp = (lookup_ctrl_t *)p2;
3557fcf3ce44SJohn Forte 	isns_obj_t *a = (isns_obj_t *)p1;
3558fcf3ce44SJohn Forte 	isns_obj_t *b = (isns_obj_t *)lcp->data[2].ptr;
3559fcf3ce44SJohn Forte 
3560fcf3ce44SJohn Forte 	ec = new_ref(a, b);
3561fcf3ce44SJohn Forte 
3562fcf3ce44SJohn Forte 	return (ec);
3563fcf3ce44SJohn Forte }
3564fcf3ce44SJohn Forte 
3565fcf3ce44SJohn Forte static int
ref_new2old(lookup_ctrl_t * lcp,isns_type_t et,uint32_t uu,const isns_obj_t * vp)3566fcf3ce44SJohn Forte ref_new2old(
3567fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp,
3568fcf3ce44SJohn Forte 	isns_type_t et,
3569fcf3ce44SJohn Forte 	uint32_t uu,
3570fcf3ce44SJohn Forte 	const isns_obj_t *vp
3571fcf3ce44SJohn Forte )
3572fcf3ce44SJohn Forte {
3573fcf3ce44SJohn Forte 	int ec = 0;
3574fcf3ce44SJohn Forte 
3575fcf3ce44SJohn Forte 	int match;
3576fcf3ce44SJohn Forte 	uint32_t uid;
3577fcf3ce44SJohn Forte 
3578fcf3ce44SJohn Forte 	lookup_ctrl_t lc;
3579fcf3ce44SJohn Forte 
3580fcf3ce44SJohn Forte 	lcp->id[2] = et;
3581fcf3ce44SJohn Forte 	lcp->data[2].ui = uu;
3582fcf3ce44SJohn Forte 
3583fcf3ce44SJohn Forte 	uid = 0;
3584fcf3ce44SJohn Forte 	do {
3585fcf3ce44SJohn Forte 		lcp->curr_uid = uid;
3586fcf3ce44SJohn Forte 		match = cache_lookup(lcp, &uid, cb_ref_new2old);
3587fcf3ce44SJohn Forte 	} while (match == 0 && uid != 0);
3588fcf3ce44SJohn Forte 
3589fcf3ce44SJohn Forte 	if (match == 0) {
3590fcf3ce44SJohn Forte 		/* no such ref, create a default one */
3591fcf3ce44SJohn Forte 		SET_UID_LCP(&lc, et, uu);
3592fcf3ce44SJohn Forte 
3593fcf3ce44SJohn Forte 		lc.data[2].ptr = (uchar_t *)vp;
3594fcf3ce44SJohn Forte 
3595fcf3ce44SJohn Forte 		ec = cache_lookup(&lc, NULL, cb_new_ref);
3596fcf3ce44SJohn Forte 	}
3597fcf3ce44SJohn Forte 
3598fcf3ce44SJohn Forte 	return (ec);
3599fcf3ce44SJohn Forte }
3600fcf3ce44SJohn Forte 
3601fcf3ce44SJohn Forte static int
ref_new2new(lookup_ctrl_t * lcp,const isns_obj_t * p1,const isns_obj_t * p2)3602fcf3ce44SJohn Forte ref_new2new(
3603fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp,
3604fcf3ce44SJohn Forte 	const isns_obj_t *p1,
3605fcf3ce44SJohn Forte 	const isns_obj_t *p2
3606fcf3ce44SJohn Forte )
3607fcf3ce44SJohn Forte {
3608fcf3ce44SJohn Forte 	int ec = 0;
3609fcf3ce44SJohn Forte 
3610fcf3ce44SJohn Forte 	if (is_obj_there(lcp) != 0) {
3611fcf3ce44SJohn Forte 		return (0);
3612fcf3ce44SJohn Forte 	}
3613fcf3ce44SJohn Forte 
3614fcf3ce44SJohn Forte 	ec = new_ref(p1, p2);
3615fcf3ce44SJohn Forte 
3616fcf3ce44SJohn Forte 	return (ec);
3617fcf3ce44SJohn Forte }
3618fcf3ce44SJohn Forte 
3619fcf3ce44SJohn Forte static int
new_ref(const isns_obj_t * p1,const isns_obj_t * p2)3620fcf3ce44SJohn Forte new_ref(
3621fcf3ce44SJohn Forte 	const isns_obj_t *p1,
3622fcf3ce44SJohn Forte 	const isns_obj_t *p2
3623fcf3ce44SJohn Forte )
3624fcf3ce44SJohn Forte {
3625fcf3ce44SJohn Forte 	int ec = 0;
3626fcf3ce44SJohn Forte 
3627fcf3ce44SJohn Forte 	isns_obj_t *obj;
3628fcf3ce44SJohn Forte 
3629fcf3ce44SJohn Forte 	obj = make_ref[p1->type](p1, p2);
3630fcf3ce44SJohn Forte 	if (obj != NULL) {
3631fcf3ce44SJohn Forte 		ec = register_object(obj, NULL, NULL);
3632fcf3ce44SJohn Forte 	} else {
3633fcf3ce44SJohn Forte 		ec = ISNS_RSP_INTERNAL_ERROR;
3634fcf3ce44SJohn Forte 	}
3635fcf3ce44SJohn Forte 
3636fcf3ce44SJohn Forte 	return (ec);
3637fcf3ce44SJohn Forte }
3638fcf3ce44SJohn Forte 
3639fcf3ce44SJohn Forte /*
3640fcf3ce44SJohn Forte  * ****************************************************************************
3641fcf3ce44SJohn Forte  *
3642fcf3ce44SJohn Forte  * do_dereg:
3643fcf3ce44SJohn Forte  *	Physically remove an object along with the children objects,
3644fcf3ce44SJohn Forte  *	the reference object and the parent object recursively.
3645fcf3ce44SJohn Forte  *	Apporiate SCN is triggered.
3646fcf3ce44SJohn Forte  *
3647fcf3ce44SJohn Forte  * lcp	- the lookup control for the object being removed.
3648fcf3ce44SJohn Forte  * parent_flag	- 1: the object being removed is the parent object;
3649fcf3ce44SJohn Forte  *		  0: otherwise.
3650fcf3ce44SJohn Forte  * child_flag	- 1: the object being removed is a child object;
3651fcf3ce44SJohn Forte  *		  0: otherwise.
3652fcf3ce44SJohn Forte  * pending	- 1: do not remove the ESI entry immediately;
3653fcf3ce44SJohn Forte  *		  0: remove the ESI entry without any delay.
3654fcf3ce44SJohn Forte  * return - error code.
3655fcf3ce44SJohn Forte  *
3656fcf3ce44SJohn Forte  * ****************************************************************************
3657fcf3ce44SJohn Forte  */
3658fcf3ce44SJohn Forte static int
do_dereg(lookup_ctrl_t * lcp,int parent_flag,int child_flag,int pending)3659fcf3ce44SJohn Forte do_dereg(
3660fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp,
3661fcf3ce44SJohn Forte 	int parent_flag,
3662fcf3ce44SJohn Forte 	int child_flag,
3663fcf3ce44SJohn Forte 	int pending
3664fcf3ce44SJohn Forte )
3665fcf3ce44SJohn Forte {
3666fcf3ce44SJohn Forte 	int ec = 0;
3667fcf3ce44SJohn Forte 
3668fcf3ce44SJohn Forte 	isns_obj_t *obj;
3669fcf3ce44SJohn Forte 	uint32_t *cuidp, num;
3670fcf3ce44SJohn Forte 	isns_type_t type;
3671fcf3ce44SJohn Forte 	uint32_t uid;
3672fcf3ce44SJohn Forte 	int i;
3673fcf3ce44SJohn Forte 
3674fcf3ce44SJohn Forte 	/* remove the object from object container */
3675fcf3ce44SJohn Forte 	obj = cache_remove(lcp, 0);
3676fcf3ce44SJohn Forte 
3677fcf3ce44SJohn Forte 	if (obj == NULL) {
3678fcf3ce44SJohn Forte 		return (0);
3679fcf3ce44SJohn Forte 	}
3680fcf3ce44SJohn Forte 
3681fcf3ce44SJohn Forte 	/* trigger a scn */
3682fcf3ce44SJohn Forte 	if (scn_q != NULL) {
3683fcf3ce44SJohn Forte 		(void) make_scn(ISNS_OBJECT_REMOVED, obj);
3684fcf3ce44SJohn Forte 	}
3685fcf3ce44SJohn Forte 
3686fcf3ce44SJohn Forte 	/* dereg children */
3687fcf3ce44SJohn Forte 	i = 0;
3688fcf3ce44SJohn Forte 	while (ec == 0 && !parent_flag &&
3689fcf3ce44SJohn Forte 	    i < NUM_OF_CHILD[obj->type]) {
3690fcf3ce44SJohn Forte 		type = TYPE_OF_CHILD[obj->type][i];
3691fcf3ce44SJohn Forte 		cuidp = get_child_n(obj, i);
3692fcf3ce44SJohn Forte 		if (cuidp != NULL) {
3693fcf3ce44SJohn Forte 			num = *cuidp;
3694fcf3ce44SJohn Forte 		} else {
3695fcf3ce44SJohn Forte 			num = 0;
3696fcf3ce44SJohn Forte 		}
3697fcf3ce44SJohn Forte 		while (ec == 0 && num > 0) {
3698fcf3ce44SJohn Forte 			uid = cuidp[num];
3699fcf3ce44SJohn Forte 			if (uid != 0) {
3700fcf3ce44SJohn Forte 				SET_UID_LCP(lcp, type, uid);
3701fcf3ce44SJohn Forte 				ec = do_dereg(lcp,
3702fcf3ce44SJohn Forte 				    parent_flag,
3703fcf3ce44SJohn Forte 				    1,
3704fcf3ce44SJohn Forte 				    pending);
3705fcf3ce44SJohn Forte 			}
3706fcf3ce44SJohn Forte 			num --;
3707fcf3ce44SJohn Forte 		}
3708fcf3ce44SJohn Forte 		i ++;
3709fcf3ce44SJohn Forte 	}
3710fcf3ce44SJohn Forte 
3711fcf3ce44SJohn Forte 	/* clear the ref bit on the ref'd object */
3712fcf3ce44SJohn Forte 	if (ec == 0 && TYPE_OF_REF[obj->type][0] > 0) {
3713fcf3ce44SJohn Forte 		uid = 0;
3714fcf3ce44SJohn Forte 		do {
3715fcf3ce44SJohn Forte 			(void) setup_ref_lcp(lcp, obj, NULL);
3716fcf3ce44SJohn Forte 			lcp->curr_uid = uid;
3717fcf3ce44SJohn Forte 			lcp->data[2].ui = obj->type;
3718fcf3ce44SJohn Forte 			if (cache_lookup(lcp, &uid, cb_clear_ref) != 0) {
3719fcf3ce44SJohn Forte 				UPDATE_LCP_UID(lcp, uid);
3720fcf3ce44SJohn Forte 				ec = do_dereg(lcp,
3721fcf3ce44SJohn Forte 				    parent_flag,
3722fcf3ce44SJohn Forte 				    child_flag,
3723fcf3ce44SJohn Forte 				    pending);
3724fcf3ce44SJohn Forte 			}
3725fcf3ce44SJohn Forte 		} while (uid != 0);
3726fcf3ce44SJohn Forte 	}
3727fcf3ce44SJohn Forte 
3728fcf3ce44SJohn Forte 	/* remove it from the parent */
3729fcf3ce44SJohn Forte 	if (ec == 0 && !child_flag &&
3730fcf3ce44SJohn Forte 	    TYPE_OF_PARENT[obj->type] > 0 &&
3731fcf3ce44SJohn Forte 	    (uid = setup_parent_lcp(lcp, obj)) != 0) {
3732fcf3ce44SJohn Forte 		if (cache_lookup(lcp, NULL, cb_remove_child) != 0) {
3733fcf3ce44SJohn Forte 			UPDATE_LCP_UID(lcp, uid);
3734fcf3ce44SJohn Forte 			ec = do_dereg(lcp,
3735fcf3ce44SJohn Forte 			    1,
3736fcf3ce44SJohn Forte 			    child_flag,
3737fcf3ce44SJohn Forte 			    0);
3738fcf3ce44SJohn Forte 		}
3739fcf3ce44SJohn Forte 	}
3740fcf3ce44SJohn Forte 
3741fcf3ce44SJohn Forte 	if (ec == 0 && !child_flag) {
3742fcf3ce44SJohn Forte 		/* remove it from persistent data store */
3743fcf3ce44SJohn Forte 		if (sys_q) {
3744fcf3ce44SJohn Forte 			ec = write_data(DATA_DELETE, obj);
3745fcf3ce44SJohn Forte 		}
3746fcf3ce44SJohn Forte 		/* remove esi event entry */
3747fcf3ce44SJohn Forte 		if (ec == 0) {
3748fcf3ce44SJohn Forte 			(void) esi_remove_obj(obj, pending);
3749fcf3ce44SJohn Forte 		}
3750fcf3ce44SJohn Forte 
3751fcf3ce44SJohn Forte 		/* save the parent uid for caller */
3752fcf3ce44SJohn Forte 		if (TYPE_OF_PARENT[obj->type] != 0) {
3753fcf3ce44SJohn Forte 			lcp->curr_uid = get_parent_uid(obj);
3754fcf3ce44SJohn Forte 		} else {
3755fcf3ce44SJohn Forte 			/* it's the parent itself */
3756fcf3ce44SJohn Forte 			lcp->curr_uid = get_obj_uid(obj);
3757fcf3ce44SJohn Forte 		}
3758fcf3ce44SJohn Forte 	}
3759fcf3ce44SJohn Forte 
3760fcf3ce44SJohn Forte 	/* remove this portal from scn registry */
3761fcf3ce44SJohn Forte 	if (ec == 0 &&
3762fcf3ce44SJohn Forte 	    obj->type == OBJ_PORTAL) {
3763fcf3ce44SJohn Forte 		(void) remove_scn_portal(get_obj_uid(obj));
3764fcf3ce44SJohn Forte 	}
3765fcf3ce44SJohn Forte 
3766fcf3ce44SJohn Forte 	/* free the object */
3767fcf3ce44SJohn Forte 	(void) free_object(obj);
3768fcf3ce44SJohn Forte 
3769fcf3ce44SJohn Forte 	return (ec);
3770fcf3ce44SJohn Forte }
3771fcf3ce44SJohn Forte 
3772fcf3ce44SJohn Forte /*
3773fcf3ce44SJohn Forte  * ****************************************************************************
3774fcf3ce44SJohn Forte  *
3775fcf3ce44SJohn Forte  * dereg_assoc:
3776fcf3ce44SJohn Forte  *	Remove one association object from object container.
3777fcf3ce44SJohn Forte  *
3778fcf3ce44SJohn Forte  * lcp	- the lookup control for the object being removed.
3779fcf3ce44SJohn Forte  * return - error code.
3780fcf3ce44SJohn Forte  *
3781fcf3ce44SJohn Forte  * ****************************************************************************
3782fcf3ce44SJohn Forte  */
3783fcf3ce44SJohn Forte int
dereg_assoc(lookup_ctrl_t * lcp)3784fcf3ce44SJohn Forte dereg_assoc(
3785fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp
3786fcf3ce44SJohn Forte )
3787fcf3ce44SJohn Forte {
3788fcf3ce44SJohn Forte 	isns_obj_t *obj;
3789fcf3ce44SJohn Forte 
3790fcf3ce44SJohn Forte 	obj = cache_remove(lcp, 1);
3791fcf3ce44SJohn Forte 
3792fcf3ce44SJohn Forte 	/* free the object */
3793fcf3ce44SJohn Forte 	if (obj != NULL) {
3794fcf3ce44SJohn Forte 		free_object(obj);
3795fcf3ce44SJohn Forte 	}
3796fcf3ce44SJohn Forte 
3797fcf3ce44SJohn Forte 	return (0);
3798fcf3ce44SJohn Forte }
3799fcf3ce44SJohn Forte 
3800fcf3ce44SJohn Forte /*
3801fcf3ce44SJohn Forte  * ****************************************************************************
3802fcf3ce44SJohn Forte  *
3803fcf3ce44SJohn Forte  * dereg_object:
3804fcf3ce44SJohn Forte  *	Remove one object from object container.
3805fcf3ce44SJohn Forte  *
3806fcf3ce44SJohn Forte  * lcp	- the lookup control for the object being removed.
3807fcf3ce44SJohn Forte  * return - error code.
3808fcf3ce44SJohn Forte  *
3809fcf3ce44SJohn Forte  * ****************************************************************************
3810fcf3ce44SJohn Forte  */
3811fcf3ce44SJohn Forte int
dereg_object(lookup_ctrl_t * lcp,int pending)3812fcf3ce44SJohn Forte dereg_object(
3813fcf3ce44SJohn Forte 	lookup_ctrl_t *lcp,
3814fcf3ce44SJohn Forte 	int pending
3815fcf3ce44SJohn Forte )
3816fcf3ce44SJohn Forte {
3817fcf3ce44SJohn Forte 	return (do_dereg(lcp, 0, 0, pending));
3818fcf3ce44SJohn Forte }
3819fcf3ce44SJohn Forte 
3820fcf3ce44SJohn Forte /*
3821fcf3ce44SJohn Forte  * ****************************************************************************
3822fcf3ce44SJohn Forte  *
3823fcf3ce44SJohn Forte  * data_sync:
3824fcf3ce44SJohn Forte  *	Synchronize the cache with persistent data store.
3825fcf3ce44SJohn Forte  *	Flush the cache data to data store if the input ec is zero,
3826fcf3ce44SJohn Forte  *	retreat the changes in cache and ignore data store update
3827fcf3ce44SJohn Forte  *	if there is an error.
3828fcf3ce44SJohn Forte  *
3829fcf3ce44SJohn Forte  * ec	- error code.
3830fcf3ce44SJohn Forte  * return - error code.
3831fcf3ce44SJohn Forte  *
3832fcf3ce44SJohn Forte  * ****************************************************************************
3833fcf3ce44SJohn Forte  */
3834fcf3ce44SJohn Forte int
data_sync(int ec)3835fcf3ce44SJohn Forte data_sync(
3836fcf3ce44SJohn Forte 	int ec
3837fcf3ce44SJohn Forte )
3838fcf3ce44SJohn Forte {
3839fcf3ce44SJohn Forte 	/* cache is updated successfully, commit the data to data store */
3840fcf3ce44SJohn Forte 	if (IS_CACHE_UPDATED()) {
3841fcf3ce44SJohn Forte 		if (ec == 0) {
3842fcf3ce44SJohn Forte 			ec = write_data(DATA_COMMIT, NULL);
3843fcf3ce44SJohn Forte 		}
3844fcf3ce44SJohn Forte 		if (ec == 0) {
3845fcf3ce44SJohn Forte 			/* successful, trigger the SCN */
3846fcf3ce44SJohn Forte 			(void) queue_msg_set(scn_q, SCN_TRIGGER, (void *)NULL);
3847fcf3ce44SJohn Forte 		} else {
3848fcf3ce44SJohn Forte 			shutdown_server();
3849fcf3ce44SJohn Forte 		}
3850fcf3ce44SJohn Forte 	} else {
3851fcf3ce44SJohn Forte 		/* ignore all SCNs which have been generated */
3852fcf3ce44SJohn Forte 		(void) queue_msg_set(scn_q, SCN_IGNORE, (void *)NULL);
3853fcf3ce44SJohn Forte 
3854fcf3ce44SJohn Forte 		(void) write_data(DATA_RETREAT, NULL);
3855fcf3ce44SJohn Forte 	}
3856fcf3ce44SJohn Forte 
3857fcf3ce44SJohn Forte 	return (ec);
3858fcf3ce44SJohn Forte }
3859fcf3ce44SJohn Forte 
3860fcf3ce44SJohn Forte static pthread_mutex_t name_mtx[3] = {
3861fcf3ce44SJohn Forte 	PTHREAD_MUTEX_INITIALIZER,
3862fcf3ce44SJohn Forte 	PTHREAD_MUTEX_INITIALIZER,
3863fcf3ce44SJohn Forte 	PTHREAD_MUTEX_INITIALIZER
3864fcf3ce44SJohn Forte };
3865fcf3ce44SJohn Forte static const char *name_pattern[3] = {
3866fcf3ce44SJohn Forte 	"ENTITY_ID_%d",
3867fcf3ce44SJohn Forte 	"DD_%d",
3868fcf3ce44SJohn Forte 	"DD-Set_%d"
3869fcf3ce44SJohn Forte };
3870fcf3ce44SJohn Forte static uint32_t name_count[3] = {
3871fcf3ce44SJohn Forte 	0,
3872fcf3ce44SJohn Forte 	0,
3873fcf3ce44SJohn Forte 	0
3874fcf3ce44SJohn Forte };
3875fcf3ce44SJohn Forte 
3876fcf3ce44SJohn Forte /*
3877fcf3ce44SJohn Forte  * ****************************************************************************
3878fcf3ce44SJohn Forte  *
3879fcf3ce44SJohn Forte  * make_unique_name:
3880fcf3ce44SJohn Forte  *	make a default unique name for a newly registered network entity,
3881fcf3ce44SJohn Forte  *	discovery domain or discovery domain set object.
3882fcf3ce44SJohn Forte  *
3883fcf3ce44SJohn Forte  * len	- pointer of the length of the new name for returning.
3884fcf3ce44SJohn Forte  * tag	- which attribute of the new name is for.
3885fcf3ce44SJohn Forte  * return - the name being made.
3886fcf3ce44SJohn Forte  *
3887fcf3ce44SJohn Forte  * ****************************************************************************
3888fcf3ce44SJohn Forte  */
3889fcf3ce44SJohn Forte static char *
make_unique_name(int * len,uint32_t tag)3890fcf3ce44SJohn Forte make_unique_name(
3891fcf3ce44SJohn Forte 	int *len,
3892fcf3ce44SJohn Forte 	uint32_t tag
3893fcf3ce44SJohn Forte )
3894fcf3ce44SJohn Forte {
3895fcf3ce44SJohn Forte 	int i;
3896fcf3ce44SJohn Forte 	int count;
3897fcf3ce44SJohn Forte 	char name[32] = { 0 };
3898fcf3ce44SJohn Forte 
3899fcf3ce44SJohn Forte 	char *p;
3900fcf3ce44SJohn Forte 
3901fcf3ce44SJohn Forte 	lookup_ctrl_t lc;
3902fcf3ce44SJohn Forte 
3903fcf3ce44SJohn Forte 	lc.curr_uid = 0;
3904fcf3ce44SJohn Forte 
3905fcf3ce44SJohn Forte 	switch (tag) {
3906fcf3ce44SJohn Forte 	case ISNS_EID_ATTR_ID:
3907fcf3ce44SJohn Forte 		i = 0;
3908fcf3ce44SJohn Forte 		lc.type = OBJ_ENTITY;
3909fcf3ce44SJohn Forte 		lc.id[0] = ATTR_INDEX_ENTITY(ISNS_EID_ATTR_ID);
3910fcf3ce44SJohn Forte 		break;
3911fcf3ce44SJohn Forte 	case ISNS_DD_NAME_ATTR_ID:
3912fcf3ce44SJohn Forte 		i = 1;
3913fcf3ce44SJohn Forte 		lc.type = OBJ_DD;
3914fcf3ce44SJohn Forte 		lc.id[0] = ATTR_INDEX_DD(ISNS_DD_NAME_ATTR_ID);
3915fcf3ce44SJohn Forte 		break;
3916fcf3ce44SJohn Forte 	case ISNS_DD_SET_NAME_ATTR_ID:
3917fcf3ce44SJohn Forte 		i = 2;
3918fcf3ce44SJohn Forte 		lc.type = OBJ_DDS;
3919fcf3ce44SJohn Forte 		lc.id[0] = ATTR_INDEX_DDS(ISNS_DD_SET_NAME_ATTR_ID);
3920fcf3ce44SJohn Forte 		break;
3921fcf3ce44SJohn Forte 	default:
3922fcf3ce44SJohn Forte 		ASSERT(0);
3923fcf3ce44SJohn Forte 		break;
3924fcf3ce44SJohn Forte 	}
3925fcf3ce44SJohn Forte 
3926fcf3ce44SJohn Forte 	lc.op[0] = OP_STRING;
3927fcf3ce44SJohn Forte 	lc.op[1] = 0;
3928fcf3ce44SJohn Forte 	do {
3929fcf3ce44SJohn Forte 		(void) pthread_mutex_lock(&name_mtx[i]);
3930fcf3ce44SJohn Forte 		count = ++ name_count[i];
3931fcf3ce44SJohn Forte 		(void) pthread_mutex_unlock(&name_mtx[i]);
3932fcf3ce44SJohn Forte 		/* no more space, failure */
3933fcf3ce44SJohn Forte 		if (count == 0) {
3934fcf3ce44SJohn Forte 			return (NULL);
3935fcf3ce44SJohn Forte 		}
3936fcf3ce44SJohn Forte 		(void) sprintf(name, name_pattern[i], count);
3937fcf3ce44SJohn Forte 		lc.data[0].ptr = (uchar_t *)name;
3938fcf3ce44SJohn Forte 	} while (is_obj_there(&lc) != 0);
3939fcf3ce44SJohn Forte 
3940fcf3ce44SJohn Forte 	/* 4-bytes aligned length */
3941fcf3ce44SJohn Forte 	*len = strlen(name);
3942fcf3ce44SJohn Forte 	*len = *len + (4 - *len % 4);
3943fcf3ce44SJohn Forte 	p = (char *)malloc(*len);
3944fcf3ce44SJohn Forte 	if (p != NULL) {
3945fcf3ce44SJohn Forte 		(void) strcpy(p, name);
3946fcf3ce44SJohn Forte 	}
3947fcf3ce44SJohn Forte 	return (p);
3948fcf3ce44SJohn Forte }
3949fcf3ce44SJohn Forte 
3950fcf3ce44SJohn Forte #ifdef DEBUG
3951fcf3ce44SJohn Forte void
obj_dump(void * p)3952fcf3ce44SJohn Forte obj_dump(
3953fcf3ce44SJohn Forte 	void *p
3954fcf3ce44SJohn Forte )
3955fcf3ce44SJohn Forte {
3956fcf3ce44SJohn Forte 	print_object(NULL, (isns_obj_t *)p);
3957fcf3ce44SJohn Forte }
3958fcf3ce44SJohn Forte #endif
3959