xref: /titanic_50/usr/src/cmd/isns/isnsd/dseng.c (revision 7c2fbfb345896881c631598ee3852ce9ce33fb07)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #include "isns_server.h"
31 #include "isns_cache.h"
32 #include "isns_obj.h"
33 #include "isns_dsapi.h"
34 #include "isns_dseng.h"
35 #include "isns_msgq.h"
36 #include "isns_log.h"
37 #include "isns_scn.h"
38 #include "isns_esi.h"
39 
40 /*
41  * extern variables
42  */
43 extern const int NUM_OF_CHILD[MAX_OBJ_TYPE];
44 extern const int TYPE_OF_PARENT[MAX_OBJ_TYPE_FOR_SIZE];
45 
46 int
47 init_data(
48 )
49 {
50 	return (target_init_data());
51 }
52 
53 int
54 load_data(
55 )
56 {
57 	int ec = 0;
58 	void *prev = NULL;
59 	isns_obj_t *obj;
60 	uint32_t uid = 0, type = 0;
61 	uint32_t puid = 0;
62 	isns_type_t ptype = 0;
63 	void const **child[MAX_CHILD_TYPE] = { NULL };
64 	uchar_t phase;
65 
66 	isns_attr_t *scn_bitmap;
67 	isns_attr_t *scn_name;
68 
69 	isns_attr_t *eid_attr;
70 
71 	/* lock the cache */
72 	(void) cache_lock_write();
73 
74 	ec = target_load_obj(&prev, &obj, &phase);
75 	while (ec == 0 && obj != NULL) {
76 		scn_bitmap = NULL;
77 		switch (obj->type) {
78 		case OBJ_DD:
79 		case OBJ_DDS:
80 			ptype = obj->type;
81 			ec = register_object(obj, &puid, NULL);
82 			break;
83 		case OBJ_ENTITY:
84 			ptype = OBJ_ENTITY;
85 			ec = register_object(obj, &puid, NULL);
86 			if (ec == 0) {
87 				eid_attr = &obj->attrs[
88 				    ATTR_INDEX_ENTITY(ISNS_EID_ATTR_ID)];
89 				ec = esi_load(puid,
90 				    eid_attr->value.ptr,
91 				    eid_attr->len);
92 			}
93 			break;
94 		case OBJ_ISCSI:
95 			scn_bitmap = &obj->attrs[ATTR_INDEX_ISCSI(
96 			    ISNS_ISCSI_SCN_BITMAP_ATTR_ID)];
97 			scn_name = &obj->attrs[ATTR_INDEX_ISCSI(
98 			    ISNS_ISCSI_NAME_ATTR_ID)];
99 		case OBJ_PORTAL:
100 			if (puid != 0 &&
101 			    TYPE_OF_PARENT[obj->type] == ptype) {
102 				(void) set_parent_obj(obj, puid);
103 				type = obj->type;
104 				ec = register_object(obj, &uid, NULL);
105 			} else {
106 				ec = ISNS_RSP_INTERNAL_ERROR;
107 			}
108 			if (ec == 0) {
109 				ec = buff_child_obj(ptype, type,
110 				    (void *)uid, child);
111 			}
112 			if (ec == 0 && scn_bitmap != NULL) {
113 				/* register scn */
114 				ec = scn_list_load(
115 				    uid,
116 				    scn_name->value.ptr,
117 				    scn_name->len,
118 				    scn_bitmap->value.ui);
119 			}
120 			break;
121 		case OBJ_PG:
122 			if (puid != 0 &&
123 			    TYPE_OF_PARENT[OBJ_PG] == ptype) {
124 				(void) set_parent_obj(obj, puid);
125 				ec = register_object(obj, NULL, NULL);
126 			} else {
127 				ec = ISNS_RSP_INTERNAL_ERROR;
128 			}
129 			break;
130 		case OBJ_ASSOC_ISCSI:
131 			if (puid != 0 &&
132 			    TYPE_OF_PARENT[OBJ_ASSOC_ISCSI] == ptype) {
133 				/* ignore adding member to default dd */
134 				/* during loading objects from data store */
135 				if (puid != ISNS_DEFAULT_DD_ID) {
136 					(void) set_parent_obj(obj, puid);
137 					ec = add_dd_member(obj);
138 				}
139 			} else {
140 				ec = ISNS_RSP_INTERNAL_ERROR;
141 			}
142 			free_object(obj);
143 			break;
144 		case OBJ_ASSOC_DD:
145 			if (puid != 0 &&
146 			    TYPE_OF_PARENT[OBJ_ASSOC_DD] == ptype) {
147 				/* ignore adding member to default dd-set */
148 				/* and adding default dd to any dd-set */
149 				/* during loading objects from data store */
150 				if (puid != ISNS_DEFAULT_DD_SET_ID &&
151 				    get_obj_uid(obj) != ISNS_DEFAULT_DD_ID) {
152 					(void) set_parent_obj(obj, puid);
153 					ec = add_dds_member(obj);
154 				}
155 			} else {
156 				ec = ISNS_RSP_INTERNAL_ERROR;
157 			}
158 			free_object(obj);
159 			break;
160 		default:
161 			ASSERT(0);
162 			ec = ISNS_RSP_INTERNAL_ERROR;
163 			break;
164 		}
165 		if (ec == 0) {
166 			ec = target_load_obj(&prev, &obj, &phase);
167 		}
168 		if (ec == 0 &&
169 		    puid != 0 &&
170 		    NUM_OF_CHILD[ptype] > 0 &&
171 		    (obj == NULL ||
172 		    TYPE_OF_PARENT[obj->type] != ptype)) {
173 			ec = update_child_obj(ptype, puid, child, 0);
174 		}
175 	}
176 
177 	/* unlock the cache */
178 	(void) cache_unlock_sync(0);
179 
180 	/* free the child buffer */
181 	uid = 0;
182 	while (uid < MAX_CHILD_TYPE) {
183 		if (child[uid] != NULL) {
184 			free(child[uid]);
185 		}
186 		uid ++;
187 	}
188 
189 	return (ec);
190 }
191 
192 int
193 write_data(
194 	int op,
195 	const isns_obj_t *obj
196 )
197 {
198 	int ec = 0;
199 
200 	switch (op) {
201 		case DATA_ADD:
202 			ec = target_add_obj(obj);
203 			break;
204 		case DATA_UPDATE:
205 			ec = target_modify_obj(obj);
206 			break;
207 		case DATA_DELETE:
208 			ec = target_delete_obj(obj);
209 			break;
210 		case DATA_DELETE_ASSOC:
211 			ec = target_delete_assoc(obj);
212 			break;
213 		case DATA_COMMIT:
214 			ec = target_update_commit();
215 			break;
216 		case DATA_RETREAT:
217 			ec = target_update_retreat();
218 			break;
219 		default:
220 			break;
221 	}
222 
223 	return (ec);
224 }
225