xref: /illumos-gate/usr/src/cmd/isns/isnsd/dseng.c (revision 2aeafac3612e19716bf8164f89c3c9196342979c)
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 			/* FALLTHROUGH */
100 		case OBJ_PORTAL:
101 			if (puid != 0 &&
102 			    TYPE_OF_PARENT[obj->type] == ptype) {
103 				(void) set_parent_obj(obj, puid);
104 				type = obj->type;
105 				ec = register_object(obj, &uid, NULL);
106 			} else {
107 				ec = ISNS_RSP_INTERNAL_ERROR;
108 			}
109 			if (ec == 0) {
110 				ec = buff_child_obj(ptype, type,
111 				    (void *)uid, child);
112 			}
113 			if (ec == 0 && scn_bitmap != NULL) {
114 				/* register scn */
115 				ec = scn_list_load(
116 				    uid,
117 				    scn_name->value.ptr,
118 				    scn_name->len,
119 				    scn_bitmap->value.ui);
120 			}
121 			break;
122 		case OBJ_PG:
123 			if (puid != 0 &&
124 			    TYPE_OF_PARENT[OBJ_PG] == ptype) {
125 				(void) set_parent_obj(obj, puid);
126 				ec = register_object(obj, NULL, NULL);
127 			} else {
128 				ec = ISNS_RSP_INTERNAL_ERROR;
129 			}
130 			break;
131 		case OBJ_ASSOC_ISCSI:
132 			if (puid != 0 &&
133 			    TYPE_OF_PARENT[OBJ_ASSOC_ISCSI] == ptype) {
134 				/* ignore adding member to default dd */
135 				/* during loading objects from data store */
136 				if (puid != ISNS_DEFAULT_DD_ID) {
137 					(void) set_parent_obj(obj, puid);
138 					ec = add_dd_member(obj);
139 				}
140 			} else {
141 				ec = ISNS_RSP_INTERNAL_ERROR;
142 			}
143 			free_object(obj);
144 			break;
145 		case OBJ_ASSOC_DD:
146 			if (puid != 0 &&
147 			    TYPE_OF_PARENT[OBJ_ASSOC_DD] == ptype) {
148 				/* ignore adding member to default dd-set */
149 				/* and adding default dd to any dd-set */
150 				/* during loading objects from data store */
151 				if (puid != ISNS_DEFAULT_DD_SET_ID &&
152 				    get_obj_uid(obj) != ISNS_DEFAULT_DD_ID) {
153 					(void) set_parent_obj(obj, puid);
154 					ec = add_dds_member(obj);
155 				}
156 			} else {
157 				ec = ISNS_RSP_INTERNAL_ERROR;
158 			}
159 			free_object(obj);
160 			break;
161 		default:
162 			ASSERT(0);
163 			ec = ISNS_RSP_INTERNAL_ERROR;
164 			break;
165 		}
166 		if (ec == 0) {
167 			ec = target_load_obj(&prev, &obj, &phase);
168 		}
169 		if (ec == 0 &&
170 		    puid != 0 &&
171 		    NUM_OF_CHILD[ptype] > 0 &&
172 		    (obj == NULL ||
173 		    TYPE_OF_PARENT[obj->type] != ptype)) {
174 			ec = update_child_obj(ptype, puid, child, 0);
175 		}
176 	}
177 
178 	/* unlock the cache */
179 	(void) cache_unlock_sync(0);
180 
181 	/* free the child buffer */
182 	uid = 0;
183 	while (uid < MAX_CHILD_TYPE) {
184 		if (child[uid] != NULL) {
185 			free(child[uid]);
186 		}
187 		uid ++;
188 	}
189 
190 	return (ec);
191 }
192 
193 int
194 write_data(
195 	int op,
196 	const isns_obj_t *obj
197 )
198 {
199 	int ec = 0;
200 
201 	switch (op) {
202 		case DATA_ADD:
203 			ec = target_add_obj(obj);
204 			break;
205 		case DATA_UPDATE:
206 			ec = target_modify_obj(obj);
207 			break;
208 		case DATA_DELETE:
209 			ec = target_delete_obj(obj);
210 			break;
211 		case DATA_DELETE_ASSOC:
212 			ec = target_delete_assoc(obj);
213 			break;
214 		case DATA_COMMIT:
215 			ec = target_update_commit();
216 			break;
217 		case DATA_RETREAT:
218 			ec = target_update_retreat();
219 			break;
220 		default:
221 			break;
222 	}
223 
224 	return (ec);
225 }
226