xref: /titanic_44/usr/src/lib/libstmf/common/stmf.c (revision 76602b8d8c37ab03ff72d289b721cf827dbae7da)
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fcf3ce44SJohn Forte  * Use is subject to license terms.
24fcf3ce44SJohn Forte  */
25fcf3ce44SJohn Forte 
26fcf3ce44SJohn Forte #include <stdlib.h>
27fcf3ce44SJohn Forte #include <stdio.h>
28fcf3ce44SJohn Forte #include <wchar.h>
29fcf3ce44SJohn Forte #include <strings.h>
30fcf3ce44SJohn Forte #include <sys/types.h>
31fcf3ce44SJohn Forte #include <sys/stat.h>
32fcf3ce44SJohn Forte #include <fcntl.h>
33fcf3ce44SJohn Forte #include <unistd.h>
34fcf3ce44SJohn Forte #include <libintl.h>
35fcf3ce44SJohn Forte #include <errno.h>
36fcf3ce44SJohn Forte #include <string.h>
37fcf3ce44SJohn Forte #include <assert.h>
38fcf3ce44SJohn Forte #include <libnvpair.h>
39fcf3ce44SJohn Forte #include <pthread.h>
40fcf3ce44SJohn Forte #include <syslog.h>
41fcf3ce44SJohn Forte #include <libstmf.h>
42fcf3ce44SJohn Forte #include <netinet/in.h>
43fcf3ce44SJohn Forte #include <inttypes.h>
44fcf3ce44SJohn Forte #include <store.h>
45fcf3ce44SJohn Forte #include <locale.h>
46fcf3ce44SJohn Forte #include <sys/stmf_ioctl.h>
47fcf3ce44SJohn Forte 
48fcf3ce44SJohn Forte #define	STMF_PATH    "/devices/pseudo/stmf@0:admin"
49fcf3ce44SJohn Forte 
50fcf3ce44SJohn Forte #define	EUI "eui."
51fcf3ce44SJohn Forte #define	WWN "wwn."
52fcf3ce44SJohn Forte #define	IQN "iqn."
53fcf3ce44SJohn Forte #define	WWN_ASCII_SIZE 16
54fcf3ce44SJohn Forte #define	IDENT_LENGTH_BYTE 3
55fcf3ce44SJohn Forte 
56fcf3ce44SJohn Forte #define	MAX_LU		2<<16 - 1
57fcf3ce44SJohn Forte #define	MAX_TARGET_PORT	1024
58fcf3ce44SJohn Forte #define	MAX_PROVIDER	1024
59fcf3ce44SJohn Forte #define	MAX_GROUP	1024
60fcf3ce44SJohn Forte #define	MAX_SESSION	1024
61fcf3ce44SJohn Forte #define	MAX_ISCSI_NAME	223
62fcf3ce44SJohn Forte 
63fcf3ce44SJohn Forte #define	OPEN_STMF 0
64fcf3ce44SJohn Forte #define	OPEN_EXCL_STMF O_EXCL
65fcf3ce44SJohn Forte 
66fcf3ce44SJohn Forte #define	LOGICAL_UNIT_TYPE 0
67fcf3ce44SJohn Forte #define	TARGET_TYPE 1
68fcf3ce44SJohn Forte #define	STMF_SERVICE_TYPE 2
69fcf3ce44SJohn Forte 
70fcf3ce44SJohn Forte static int openStmf(int, int *fd);
71fcf3ce44SJohn Forte static int groupIoctl(int fd, int cmd, stmfGroupName *);
72fcf3ce44SJohn Forte static int loadStore(int fd);
73fcf3ce44SJohn Forte static int initializeConfig();
74fcf3ce44SJohn Forte static int groupMemberIoctl(int fd, int cmd, stmfGroupName *, stmfDevid *);
75fcf3ce44SJohn Forte static int guidCompare(const void *, const void *);
76fcf3ce44SJohn Forte static int addViewEntryIoctl(int fd, stmfGuid *, stmfViewEntry *);
77fcf3ce44SJohn Forte static int loadHostGroups(int fd, stmfGroupList *);
78fcf3ce44SJohn Forte static int loadTargetGroups(int fd, stmfGroupList *);
79fcf3ce44SJohn Forte static int getStmfState(stmf_state_desc_t *);
80fcf3ce44SJohn Forte static int setStmfState(int fd, stmf_state_desc_t *, int);
81fcf3ce44SJohn Forte static int setProviderData(int fd, char *, nvlist_t *, int);
82fcf3ce44SJohn Forte 
83fcf3ce44SJohn Forte /*
84fcf3ce44SJohn Forte  * Open for stmf module
85fcf3ce44SJohn Forte  *
86fcf3ce44SJohn Forte  * flag - open flag (OPEN_STMF, OPEN_EXCL_STMF)
87fcf3ce44SJohn Forte  * fd - pointer to integer. On success, contains the stmf file descriptor
88fcf3ce44SJohn Forte  */
89fcf3ce44SJohn Forte static int
90fcf3ce44SJohn Forte openStmf(int flag, int *fd)
91fcf3ce44SJohn Forte {
92fcf3ce44SJohn Forte 	int ret = STMF_STATUS_ERROR;
93fcf3ce44SJohn Forte 
94fcf3ce44SJohn Forte 	if ((*fd = open(STMF_PATH, O_NDELAY | O_RDONLY | flag)) != -1) {
95fcf3ce44SJohn Forte 		ret = STMF_STATUS_SUCCESS;
96fcf3ce44SJohn Forte 	} else {
97fcf3ce44SJohn Forte 		if (errno == EBUSY) {
98fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
99fcf3ce44SJohn Forte 		} else {
100fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
101fcf3ce44SJohn Forte 		}
102fcf3ce44SJohn Forte 		syslog(LOG_DEBUG, "openStmf:open failure:%s:errno(%d)",
103fcf3ce44SJohn Forte 		    STMF_PATH, errno);
104fcf3ce44SJohn Forte 	}
105fcf3ce44SJohn Forte 
106fcf3ce44SJohn Forte 	return (ret);
107fcf3ce44SJohn Forte }
108fcf3ce44SJohn Forte 
109fcf3ce44SJohn Forte /*
110fcf3ce44SJohn Forte  * initializeConfig
111fcf3ce44SJohn Forte  *
112fcf3ce44SJohn Forte  * This routine should be called before any ioctl requiring initialization
113fcf3ce44SJohn Forte  * which is basically everything except stmfGetState(), setStmfState() and
114fcf3ce44SJohn Forte  * stmfLoadConfig().
115fcf3ce44SJohn Forte  */
116fcf3ce44SJohn Forte static int
117fcf3ce44SJohn Forte initializeConfig()
118fcf3ce44SJohn Forte {
119fcf3ce44SJohn Forte 	int ret;
120fcf3ce44SJohn Forte 	stmfState state;
121fcf3ce44SJohn Forte 
122fcf3ce44SJohn Forte 
123fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
124fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
125fcf3ce44SJohn Forte 		return (ret);
126fcf3ce44SJohn Forte 	}
127fcf3ce44SJohn Forte 
128fcf3ce44SJohn Forte 	/* if we've already initialized or in the process, return success */
129fcf3ce44SJohn Forte 	if (state.configState == STMF_CONFIG_STATE_INIT_DONE ||
130fcf3ce44SJohn Forte 	    state.configState == STMF_CONFIG_STATE_INIT) {
131fcf3ce44SJohn Forte 		return (STMF_STATUS_SUCCESS);
132fcf3ce44SJohn Forte 	}
133fcf3ce44SJohn Forte 
134fcf3ce44SJohn Forte 	ret = stmfLoadConfig();
135fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
136fcf3ce44SJohn Forte 		syslog(LOG_DEBUG,
137fcf3ce44SJohn Forte 		    "initializeConfig:stmfLoadConfig:error(%d)", ret);
138fcf3ce44SJohn Forte 		return (ret);
139fcf3ce44SJohn Forte 	}
140fcf3ce44SJohn Forte 
141fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
142fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
143fcf3ce44SJohn Forte 		syslog(LOG_DEBUG,
144fcf3ce44SJohn Forte 		    "initializeConfig:stmfGetState:error(%d)", ret);
145fcf3ce44SJohn Forte 		return (ret);
146fcf3ce44SJohn Forte 	}
147fcf3ce44SJohn Forte 
148fcf3ce44SJohn Forte 	if (state.configState != STMF_CONFIG_STATE_INIT_DONE) {
149fcf3ce44SJohn Forte 		syslog(LOG_DEBUG, "initializeConfig:state.configState(%d)",
150fcf3ce44SJohn Forte 		    state.configState);
151fcf3ce44SJohn Forte 		ret = STMF_STATUS_ERROR;
152fcf3ce44SJohn Forte 	}
153fcf3ce44SJohn Forte 
154fcf3ce44SJohn Forte 	return (ret);
155fcf3ce44SJohn Forte }
156fcf3ce44SJohn Forte 
157fcf3ce44SJohn Forte 
158fcf3ce44SJohn Forte /*
159fcf3ce44SJohn Forte  * groupIoctl
160fcf3ce44SJohn Forte  *
161fcf3ce44SJohn Forte  * Purpose: issue ioctl for create/delete on group
162fcf3ce44SJohn Forte  *
163fcf3ce44SJohn Forte  * cmd - valid STMF ioctl group cmd
164fcf3ce44SJohn Forte  * groupName - groupName to create or delete
165fcf3ce44SJohn Forte  */
166fcf3ce44SJohn Forte static int
167fcf3ce44SJohn Forte groupIoctl(int fd, int cmd, stmfGroupName *groupName)
168fcf3ce44SJohn Forte {
169fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
170fcf3ce44SJohn Forte 	int ioctlRet;
171fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
172fcf3ce44SJohn Forte 	stmf_group_name_t iGroupName;
173fcf3ce44SJohn Forte 
174fcf3ce44SJohn Forte 	bzero(&iGroupName, sizeof (iGroupName));
175fcf3ce44SJohn Forte 
176fcf3ce44SJohn Forte 	bcopy(groupName, &iGroupName.name, strlen((char *)groupName));
177fcf3ce44SJohn Forte 
178fcf3ce44SJohn Forte 	iGroupName.name_size = strlen((char *)groupName);
179fcf3ce44SJohn Forte 
180fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
181fcf3ce44SJohn Forte 	/*
182fcf3ce44SJohn Forte 	 * Issue ioctl to create the host group
183fcf3ce44SJohn Forte 	 */
184fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
185fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (iGroupName);
186fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&iGroupName;
187fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
188fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
189fcf3ce44SJohn Forte 		switch (errno) {
190fcf3ce44SJohn Forte 			case EACCES:
191fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
192fcf3ce44SJohn Forte 				break;
193fcf3ce44SJohn Forte 			default:
194fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
195fcf3ce44SJohn Forte 					case STMF_IOCERR_TG_EXISTS:
196fcf3ce44SJohn Forte 					case STMF_IOCERR_HG_EXISTS:
197fcf3ce44SJohn Forte 						ret = STMF_ERROR_EXISTS;
198fcf3ce44SJohn Forte 						break;
199fcf3ce44SJohn Forte 					case STMF_IOCERR_TG_IN_USE:
200fcf3ce44SJohn Forte 					case STMF_IOCERR_HG_IN_USE:
201fcf3ce44SJohn Forte 						ret = STMF_ERROR_GROUP_IN_USE;
202fcf3ce44SJohn Forte 						break;
203fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG:
204fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG:
205fcf3ce44SJohn Forte 						ret = STMF_ERROR_NOT_FOUND;
206fcf3ce44SJohn Forte 						break;
207fcf3ce44SJohn Forte 					default:
208fcf3ce44SJohn Forte 						syslog(LOG_DEBUG,
209fcf3ce44SJohn Forte 						    "groupIoctl:error(%d)",
210fcf3ce44SJohn Forte 						    stmfIoctl.stmf_error);
211fcf3ce44SJohn Forte 						ret = STMF_STATUS_ERROR;
212fcf3ce44SJohn Forte 						break;
213fcf3ce44SJohn Forte 				}
214fcf3ce44SJohn Forte 				break;
215fcf3ce44SJohn Forte 		}
216fcf3ce44SJohn Forte 	}
217fcf3ce44SJohn Forte done:
218fcf3ce44SJohn Forte 	return (ret);
219fcf3ce44SJohn Forte }
220fcf3ce44SJohn Forte 
221fcf3ce44SJohn Forte /*
222fcf3ce44SJohn Forte  * groupIoctl
223fcf3ce44SJohn Forte  *
224fcf3ce44SJohn Forte  * Purpose: issue ioctl for add/remove member on group
225fcf3ce44SJohn Forte  *
226fcf3ce44SJohn Forte  * cmd - valid STMF ioctl group member cmd
227fcf3ce44SJohn Forte  * groupName - groupName to add to or remove from
228fcf3ce44SJohn Forte  * devid - group member to add or remove
229fcf3ce44SJohn Forte  */
230fcf3ce44SJohn Forte static int
231fcf3ce44SJohn Forte groupMemberIoctl(int fd, int cmd, stmfGroupName *groupName, stmfDevid *devid)
232fcf3ce44SJohn Forte {
233fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
234fcf3ce44SJohn Forte 	int ioctlRet;
235fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
236fcf3ce44SJohn Forte 	stmf_group_op_data_t stmfGroupData;
237fcf3ce44SJohn Forte 
238fcf3ce44SJohn Forte 	bzero(&stmfGroupData, sizeof (stmfGroupData));
239fcf3ce44SJohn Forte 
240fcf3ce44SJohn Forte 	bcopy(groupName, &stmfGroupData.group.name, strlen((char *)groupName));
241fcf3ce44SJohn Forte 
242fcf3ce44SJohn Forte 	stmfGroupData.group.name_size = strlen((char *)groupName);
243fcf3ce44SJohn Forte 	stmfGroupData.ident[IDENT_LENGTH_BYTE] = devid->identLength;
244fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &stmfGroupData.ident[IDENT_LENGTH_BYTE + 1],
245fcf3ce44SJohn Forte 	    devid->identLength);
246fcf3ce44SJohn Forte 
247fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
248fcf3ce44SJohn Forte 	/*
249fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
250fcf3ce44SJohn Forte 	 */
251fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
252fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGroupData);
253fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&stmfGroupData;
254fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
255fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
256fcf3ce44SJohn Forte 		switch (errno) {
257fcf3ce44SJohn Forte 			case EBUSY:
258fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
259fcf3ce44SJohn Forte 				break;
260fcf3ce44SJohn Forte 			case EACCES:
261fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
262fcf3ce44SJohn Forte 				break;
263fcf3ce44SJohn Forte 			default:
264fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
265fcf3ce44SJohn Forte 					case STMF_IOCERR_TG_ENTRY_EXISTS:
266fcf3ce44SJohn Forte 					case STMF_IOCERR_HG_ENTRY_EXISTS:
267fcf3ce44SJohn Forte 						ret = STMF_ERROR_EXISTS;
268fcf3ce44SJohn Forte 						break;
269fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG_ENTRY:
270fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG_ENTRY:
271fcf3ce44SJohn Forte 						ret =
272fcf3ce44SJohn Forte 						    STMF_ERROR_MEMBER_NOT_FOUND;
273fcf3ce44SJohn Forte 						break;
274fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG:
275fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG:
276fcf3ce44SJohn Forte 						ret =
277fcf3ce44SJohn Forte 						    STMF_ERROR_GROUP_NOT_FOUND;
278fcf3ce44SJohn Forte 						break;
279fcf3ce44SJohn Forte 					default:
280fcf3ce44SJohn Forte 						syslog(LOG_DEBUG,
281fcf3ce44SJohn Forte 						    "groupMemberIoctl:error"
282fcf3ce44SJohn Forte 						    "(%d)",
283fcf3ce44SJohn Forte 						    stmfIoctl.stmf_error);
284fcf3ce44SJohn Forte 						ret = STMF_STATUS_ERROR;
285fcf3ce44SJohn Forte 						break;
286fcf3ce44SJohn Forte 				}
287fcf3ce44SJohn Forte 				break;
288fcf3ce44SJohn Forte 		}
289fcf3ce44SJohn Forte 	}
290fcf3ce44SJohn Forte done:
291fcf3ce44SJohn Forte 	return (ret);
292fcf3ce44SJohn Forte }
293fcf3ce44SJohn Forte 
294fcf3ce44SJohn Forte /*
295fcf3ce44SJohn Forte  * guidCompare
296fcf3ce44SJohn Forte  *
297fcf3ce44SJohn Forte  * qsort function
298fcf3ce44SJohn Forte  * sort on guid
299fcf3ce44SJohn Forte  */
300fcf3ce44SJohn Forte static int
301fcf3ce44SJohn Forte guidCompare(const void *p1, const void *p2)
302fcf3ce44SJohn Forte {
303fcf3ce44SJohn Forte 
304fcf3ce44SJohn Forte 	stmfGuid *g1 = (stmfGuid *)p1, *g2 = (stmfGuid *)p2;
305fcf3ce44SJohn Forte 	int i;
306fcf3ce44SJohn Forte 
307fcf3ce44SJohn Forte 	for (i = 0; i < sizeof (stmfGuid); i++) {
308fcf3ce44SJohn Forte 		if (g1->guid[i] > g2->guid[i])
309fcf3ce44SJohn Forte 			return (1);
310fcf3ce44SJohn Forte 		if (g1->guid[i] < g2->guid[i])
311fcf3ce44SJohn Forte 			return (-1);
312fcf3ce44SJohn Forte 	}
313fcf3ce44SJohn Forte 
314fcf3ce44SJohn Forte 	return (0);
315fcf3ce44SJohn Forte }
316fcf3ce44SJohn Forte 
317fcf3ce44SJohn Forte /*
318fcf3ce44SJohn Forte  * stmfAddToHostGroup
319fcf3ce44SJohn Forte  *
320fcf3ce44SJohn Forte  * Purpose: Adds an initiator to an existing host group
321fcf3ce44SJohn Forte  *
322fcf3ce44SJohn Forte  * hostGroupName - name of an existing host group
323fcf3ce44SJohn Forte  * hostName - name of initiator to add
324fcf3ce44SJohn Forte  */
325fcf3ce44SJohn Forte int
326fcf3ce44SJohn Forte stmfAddToHostGroup(stmfGroupName *hostGroupName, stmfDevid *hostName)
327fcf3ce44SJohn Forte {
328fcf3ce44SJohn Forte 	int ret;
329fcf3ce44SJohn Forte 	int fd;
330fcf3ce44SJohn Forte 
331fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
332fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
333fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || hostName == NULL) {
334fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
335fcf3ce44SJohn Forte 	}
336fcf3ce44SJohn Forte 
337fcf3ce44SJohn Forte 	/* call init */
338fcf3ce44SJohn Forte 	ret = initializeConfig();
339fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
340fcf3ce44SJohn Forte 		return (ret);
341fcf3ce44SJohn Forte 	}
342fcf3ce44SJohn Forte 
343fcf3ce44SJohn Forte 	/*
344fcf3ce44SJohn Forte 	 * Open control node for stmf
345fcf3ce44SJohn Forte 	 */
346fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
347fcf3ce44SJohn Forte 		return (ret);
348fcf3ce44SJohn Forte 
349fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_HG_ENTRY, hostGroupName,
350fcf3ce44SJohn Forte 	    hostName)) != STMF_STATUS_SUCCESS) {
351fcf3ce44SJohn Forte 		goto done;
352fcf3ce44SJohn Forte 	}
353fcf3ce44SJohn Forte 
354fcf3ce44SJohn Forte 	ret = psAddHostGroupMember((char *)hostGroupName,
355fcf3ce44SJohn Forte 	    (char *)hostName->ident);
356fcf3ce44SJohn Forte 	switch (ret) {
357fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
358fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
359fcf3ce44SJohn Forte 			break;
360fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
361fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
362fcf3ce44SJohn Forte 			break;
363fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
364fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
365fcf3ce44SJohn Forte 			break;
366fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
367fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
368fcf3ce44SJohn Forte 			break;
369fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
370fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
371fcf3ce44SJohn Forte 			break;
372fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
373fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
374fcf3ce44SJohn Forte 			break;
375fcf3ce44SJohn Forte 		default:
376fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
377fcf3ce44SJohn Forte 			    "stmfAddToHostGroup:psAddHostGroupMember:error(%d)",
378fcf3ce44SJohn Forte 			    ret);
379fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
380fcf3ce44SJohn Forte 			break;
381fcf3ce44SJohn Forte 	}
382fcf3ce44SJohn Forte 
383fcf3ce44SJohn Forte done:
384fcf3ce44SJohn Forte 	(void) close(fd);
385fcf3ce44SJohn Forte 	return (ret);
386fcf3ce44SJohn Forte }
387fcf3ce44SJohn Forte 
388fcf3ce44SJohn Forte /*
389fcf3ce44SJohn Forte  * stmfAddToTargetGroup
390fcf3ce44SJohn Forte  *
391fcf3ce44SJohn Forte  * Purpose: Adds a local port to an existing target group
392fcf3ce44SJohn Forte  *
393fcf3ce44SJohn Forte  * targetGroupName - name of an existing target group
394fcf3ce44SJohn Forte  * targetName - name of target to add
395fcf3ce44SJohn Forte  */
396fcf3ce44SJohn Forte int
397fcf3ce44SJohn Forte stmfAddToTargetGroup(stmfGroupName *targetGroupName, stmfDevid *targetName)
398fcf3ce44SJohn Forte {
399fcf3ce44SJohn Forte 	int ret;
400fcf3ce44SJohn Forte 	int fd;
401fcf3ce44SJohn Forte 	stmfState state;
402fcf3ce44SJohn Forte 
403fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
404fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
405fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || targetName == NULL) {
406fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
407fcf3ce44SJohn Forte 	}
408fcf3ce44SJohn Forte 
409fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
410fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
411fcf3ce44SJohn Forte 		if (state.operationalState != STMF_SERVICE_STATE_OFFLINE) {
412fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_ONLINE);
413fcf3ce44SJohn Forte 		}
414fcf3ce44SJohn Forte 	} else {
415fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
416fcf3ce44SJohn Forte 	}
417fcf3ce44SJohn Forte 
418fcf3ce44SJohn Forte 	/* call init */
419fcf3ce44SJohn Forte 	ret = initializeConfig();
420fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
421fcf3ce44SJohn Forte 		return (ret);
422fcf3ce44SJohn Forte 	}
423fcf3ce44SJohn Forte 
424fcf3ce44SJohn Forte 	/*
425fcf3ce44SJohn Forte 	 * Open control node for stmf
426fcf3ce44SJohn Forte 	 */
427fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
428fcf3ce44SJohn Forte 		return (ret);
429fcf3ce44SJohn Forte 
430fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_TG_ENTRY,
431fcf3ce44SJohn Forte 	    targetGroupName, targetName)) != STMF_STATUS_SUCCESS) {
432fcf3ce44SJohn Forte 		goto done;
433fcf3ce44SJohn Forte 	}
434fcf3ce44SJohn Forte 
435fcf3ce44SJohn Forte 	ret = psAddTargetGroupMember((char *)targetGroupName,
436fcf3ce44SJohn Forte 	    (char *)targetName->ident);
437fcf3ce44SJohn Forte 	switch (ret) {
438fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
439fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
440fcf3ce44SJohn Forte 			break;
441fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
442fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
443fcf3ce44SJohn Forte 			break;
444fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
445fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
446fcf3ce44SJohn Forte 			break;
447fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
448fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
449fcf3ce44SJohn Forte 			break;
450fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
451fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
452fcf3ce44SJohn Forte 			break;
453fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
454fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
455fcf3ce44SJohn Forte 			break;
456fcf3ce44SJohn Forte 		default:
457fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
458fcf3ce44SJohn Forte 			    "stmfAddToTargetGroup:psAddTargetGroupMember:"
459fcf3ce44SJohn Forte 			    "error(%d)", ret);
460fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
461fcf3ce44SJohn Forte 			break;
462fcf3ce44SJohn Forte 	}
463fcf3ce44SJohn Forte 
464fcf3ce44SJohn Forte done:
465fcf3ce44SJohn Forte 	(void) close(fd);
466fcf3ce44SJohn Forte 	return (ret);
467fcf3ce44SJohn Forte }
468fcf3ce44SJohn Forte 
469fcf3ce44SJohn Forte /*
470fcf3ce44SJohn Forte  * addViewEntryIoctl
471fcf3ce44SJohn Forte  *
472fcf3ce44SJohn Forte  * Purpose: Issues ioctl to add a view entry
473fcf3ce44SJohn Forte  *
474fcf3ce44SJohn Forte  * lu - Logical Unit identifier to which the view entry is added
475fcf3ce44SJohn Forte  * viewEntry - view entry to add
476fcf3ce44SJohn Forte  * init - When set to B_TRUE, we are in the init state, i.e. don't call open
477fcf3ce44SJohn Forte  */
478fcf3ce44SJohn Forte static int
479fcf3ce44SJohn Forte addViewEntryIoctl(int fd, stmfGuid *lu, stmfViewEntry *viewEntry)
480fcf3ce44SJohn Forte {
481fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
482fcf3ce44SJohn Forte 	int ioctlRet;
483fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
484fcf3ce44SJohn Forte 	stmf_view_op_entry_t ioctlViewEntry;
485fcf3ce44SJohn Forte 
486fcf3ce44SJohn Forte 	bzero(&ioctlViewEntry, sizeof (ioctlViewEntry));
487fcf3ce44SJohn Forte 	/*
488fcf3ce44SJohn Forte 	 * don't set ve_ndx or ve_ndx_valid as ve_ndx_valid should be
489fcf3ce44SJohn Forte 	 * false on input
490fcf3ce44SJohn Forte 	 */
491fcf3ce44SJohn Forte 	ioctlViewEntry.ve_lu_number_valid = viewEntry->luNbrValid;
492fcf3ce44SJohn Forte 	ioctlViewEntry.ve_all_hosts = viewEntry->allHosts;
493fcf3ce44SJohn Forte 	ioctlViewEntry.ve_all_targets = viewEntry->allTargets;
494fcf3ce44SJohn Forte 
495fcf3ce44SJohn Forte 	if (viewEntry->allHosts == B_FALSE) {
496fcf3ce44SJohn Forte 		bcopy(viewEntry->hostGroup, &ioctlViewEntry.ve_host_group.name,
497fcf3ce44SJohn Forte 		    sizeof (stmfGroupName));
498fcf3ce44SJohn Forte 		ioctlViewEntry.ve_host_group.name_size =
499fcf3ce44SJohn Forte 		    strlen((char *)viewEntry->hostGroup);
500fcf3ce44SJohn Forte 	}
501fcf3ce44SJohn Forte 	if (viewEntry->allTargets == B_FALSE) {
502fcf3ce44SJohn Forte 		bcopy(viewEntry->targetGroup,
503fcf3ce44SJohn Forte 		    &ioctlViewEntry.ve_target_group.name,
504fcf3ce44SJohn Forte 		    sizeof (stmfGroupName));
505fcf3ce44SJohn Forte 		ioctlViewEntry.ve_target_group.name_size =
506fcf3ce44SJohn Forte 		    strlen((char *)viewEntry->targetGroup);
507fcf3ce44SJohn Forte 	}
508fcf3ce44SJohn Forte 	if (viewEntry->luNbrValid) {
509fcf3ce44SJohn Forte 		bcopy(viewEntry->luNbr, &ioctlViewEntry.ve_lu_nbr,
510fcf3ce44SJohn Forte 		    sizeof (ioctlViewEntry.ve_lu_nbr));
511fcf3ce44SJohn Forte 	}
512fcf3ce44SJohn Forte 	bcopy(lu, &ioctlViewEntry.ve_guid, sizeof (stmfGuid));
513fcf3ce44SJohn Forte 
514fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
515fcf3ce44SJohn Forte 	/*
516fcf3ce44SJohn Forte 	 * Issue ioctl to add to the view entry
517fcf3ce44SJohn Forte 	 */
518fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
519fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ioctlViewEntry);
520fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ioctlViewEntry;
521fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (ioctlViewEntry);
522fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&ioctlViewEntry;
523fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_ADD_VIEW_ENTRY, &stmfIoctl);
524fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
525fcf3ce44SJohn Forte 		switch (errno) {
526fcf3ce44SJohn Forte 			case EBUSY:
527fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
528fcf3ce44SJohn Forte 				break;
529fcf3ce44SJohn Forte 			case EACCES:
530fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
531fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
532fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
533fcf3ce44SJohn Forte 						break;
534fcf3ce44SJohn Forte 					default:
535fcf3ce44SJohn Forte 						ret = STMF_ERROR_PERM;
536fcf3ce44SJohn Forte 						break;
537fcf3ce44SJohn Forte 				}
538fcf3ce44SJohn Forte 				break;
539fcf3ce44SJohn Forte 			default:
540fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
541fcf3ce44SJohn Forte 					case STMF_IOCERR_LU_NUMBER_IN_USE:
542fcf3ce44SJohn Forte 						ret = STMF_ERROR_LUN_IN_USE;
543fcf3ce44SJohn Forte 						break;
544fcf3ce44SJohn Forte 					case STMF_IOCERR_VIEW_ENTRY_CONFLICT:
545fcf3ce44SJohn Forte 						ret = STMF_ERROR_VE_CONFLICT;
546fcf3ce44SJohn Forte 						break;
547fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
548fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
549fcf3ce44SJohn Forte 						break;
550fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG:
551fcf3ce44SJohn Forte 						ret = STMF_ERROR_INVALID_HG;
552fcf3ce44SJohn Forte 						break;
553fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG:
554fcf3ce44SJohn Forte 						ret = STMF_ERROR_INVALID_TG;
555fcf3ce44SJohn Forte 						break;
556fcf3ce44SJohn Forte 					default:
557fcf3ce44SJohn Forte 						syslog(LOG_DEBUG,
558fcf3ce44SJohn Forte 						    "addViewEntryIoctl"
559fcf3ce44SJohn Forte 						    ":error(%d)",
560fcf3ce44SJohn Forte 						    stmfIoctl.stmf_error);
561fcf3ce44SJohn Forte 						ret = STMF_STATUS_ERROR;
562fcf3ce44SJohn Forte 						break;
563fcf3ce44SJohn Forte 				}
564fcf3ce44SJohn Forte 				break;
565fcf3ce44SJohn Forte 		}
566fcf3ce44SJohn Forte 		goto done;
567fcf3ce44SJohn Forte 	}
568fcf3ce44SJohn Forte 
569fcf3ce44SJohn Forte 	/* copy lu nbr back to caller's view entry on success */
570fcf3ce44SJohn Forte 	viewEntry->veIndex = ioctlViewEntry.ve_ndx;
571fcf3ce44SJohn Forte 	if (ioctlViewEntry.ve_lu_number_valid) {
572fcf3ce44SJohn Forte 		bcopy(&ioctlViewEntry.ve_lu_nbr, viewEntry->luNbr,
573fcf3ce44SJohn Forte 		    sizeof (ioctlViewEntry.ve_lu_nbr));
574fcf3ce44SJohn Forte 	}
575fcf3ce44SJohn Forte 	viewEntry->luNbrValid = B_TRUE;
576fcf3ce44SJohn Forte 
577fcf3ce44SJohn Forte done:
578fcf3ce44SJohn Forte 	return (ret);
579fcf3ce44SJohn Forte }
580fcf3ce44SJohn Forte 
581fcf3ce44SJohn Forte /*
582fcf3ce44SJohn Forte  * stmfAddViewEntry
583fcf3ce44SJohn Forte  *
584fcf3ce44SJohn Forte  * Purpose: Adds a view entry to a logical unit
585fcf3ce44SJohn Forte  *
586fcf3ce44SJohn Forte  * lu - guid of the logical unit to which the view entry is added
587fcf3ce44SJohn Forte  * viewEntry - view entry structure to add
588fcf3ce44SJohn Forte  */
589fcf3ce44SJohn Forte int
590fcf3ce44SJohn Forte stmfAddViewEntry(stmfGuid *lu, stmfViewEntry *viewEntry)
591fcf3ce44SJohn Forte {
592fcf3ce44SJohn Forte 	int ret;
593fcf3ce44SJohn Forte 	int fd;
594fcf3ce44SJohn Forte 	stmfViewEntry iViewEntry;
595fcf3ce44SJohn Forte 
596fcf3ce44SJohn Forte 	if (lu == NULL || viewEntry == NULL) {
597fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
598fcf3ce44SJohn Forte 	}
599fcf3ce44SJohn Forte 
600fcf3ce44SJohn Forte 	/* initialize and set internal view entry */
601fcf3ce44SJohn Forte 	bzero(&iViewEntry, sizeof (iViewEntry));
602fcf3ce44SJohn Forte 
603fcf3ce44SJohn Forte 	if (!viewEntry->allHosts) {
604fcf3ce44SJohn Forte 		bcopy(viewEntry->hostGroup, iViewEntry.hostGroup,
605fcf3ce44SJohn Forte 		    sizeof (iViewEntry.hostGroup));
606fcf3ce44SJohn Forte 	} else {
607fcf3ce44SJohn Forte 		iViewEntry.allHosts = B_TRUE;
608fcf3ce44SJohn Forte 	}
609fcf3ce44SJohn Forte 
610fcf3ce44SJohn Forte 	if (!viewEntry->allTargets) {
611fcf3ce44SJohn Forte 		bcopy(viewEntry->targetGroup, iViewEntry.targetGroup,
612fcf3ce44SJohn Forte 		    sizeof (iViewEntry.targetGroup));
613fcf3ce44SJohn Forte 	} else {
614fcf3ce44SJohn Forte 		iViewEntry.allTargets = B_TRUE;
615fcf3ce44SJohn Forte 	}
616fcf3ce44SJohn Forte 
617fcf3ce44SJohn Forte 	if (viewEntry->luNbrValid) {
618fcf3ce44SJohn Forte 		iViewEntry.luNbrValid = B_TRUE;
619fcf3ce44SJohn Forte 		bcopy(viewEntry->luNbr, iViewEntry.luNbr,
620fcf3ce44SJohn Forte 		    sizeof (iViewEntry.luNbr));
621fcf3ce44SJohn Forte 	}
622fcf3ce44SJohn Forte 
623fcf3ce44SJohn Forte 	/*
624fcf3ce44SJohn Forte 	 * set users return view entry index valid flag to false
625fcf3ce44SJohn Forte 	 * in case of failure
626fcf3ce44SJohn Forte 	 */
627fcf3ce44SJohn Forte 	viewEntry->veIndexValid = B_FALSE;
628fcf3ce44SJohn Forte 
629fcf3ce44SJohn Forte 	/* Check to ensure service exists */
630fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
631fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
632fcf3ce44SJohn Forte 	}
633fcf3ce44SJohn Forte 
634fcf3ce44SJohn Forte 	/* call init */
635fcf3ce44SJohn Forte 	ret = initializeConfig();
636fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
637fcf3ce44SJohn Forte 		return (ret);
638fcf3ce44SJohn Forte 	}
639fcf3ce44SJohn Forte 
640fcf3ce44SJohn Forte 	/*
641fcf3ce44SJohn Forte 	 * Open control node for stmf
642fcf3ce44SJohn Forte 	 */
643fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
644fcf3ce44SJohn Forte 		return (ret);
645fcf3ce44SJohn Forte 
646fcf3ce44SJohn Forte 	/*
647fcf3ce44SJohn Forte 	 * First add the view entry to the driver
648fcf3ce44SJohn Forte 	 */
649fcf3ce44SJohn Forte 	ret = addViewEntryIoctl(fd, lu, &iViewEntry);
650fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
651fcf3ce44SJohn Forte 		goto done;
652fcf3ce44SJohn Forte 	}
653fcf3ce44SJohn Forte 
654fcf3ce44SJohn Forte 	/*
655fcf3ce44SJohn Forte 	 * If the add to driver was successful, add it to the persistent
656fcf3ce44SJohn Forte 	 * store.
657fcf3ce44SJohn Forte 	 */
658fcf3ce44SJohn Forte 	ret = psAddViewEntry(lu, &iViewEntry);
659fcf3ce44SJohn Forte 	switch (ret) {
660fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
661fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
662fcf3ce44SJohn Forte 			break;
663fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
664fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
665fcf3ce44SJohn Forte 			break;
666fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
667fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
668fcf3ce44SJohn Forte 			break;
669fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
670fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
671fcf3ce44SJohn Forte 			break;
672fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
673fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
674fcf3ce44SJohn Forte 			break;
675fcf3ce44SJohn Forte 		default:
676fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
677fcf3ce44SJohn Forte 			    "stmfAddViewEntry:psAddViewEntry:error(%d)", ret);
678fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
679fcf3ce44SJohn Forte 			break;
680fcf3ce44SJohn Forte 	}
681fcf3ce44SJohn Forte 
682fcf3ce44SJohn Forte done:
683fcf3ce44SJohn Forte 	(void) close(fd);
684fcf3ce44SJohn Forte 
685fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
686fcf3ce44SJohn Forte 		/* set caller's view entry on success */
687fcf3ce44SJohn Forte 		viewEntry->veIndexValid = iViewEntry.veIndexValid;
688fcf3ce44SJohn Forte 		viewEntry->veIndex = iViewEntry.veIndex;
689fcf3ce44SJohn Forte 		viewEntry->luNbrValid = B_TRUE;
690fcf3ce44SJohn Forte 		bcopy(iViewEntry.luNbr, viewEntry->luNbr,
691fcf3ce44SJohn Forte 		    sizeof (iViewEntry.luNbr));
692fcf3ce44SJohn Forte 	}
693fcf3ce44SJohn Forte 	return (ret);
694fcf3ce44SJohn Forte }
695fcf3ce44SJohn Forte 
696fcf3ce44SJohn Forte /*
697fcf3ce44SJohn Forte  * stmfClearProviderData
698fcf3ce44SJohn Forte  *
699fcf3ce44SJohn Forte  * Purpose: delete all provider data for specified provider
700fcf3ce44SJohn Forte  *
701fcf3ce44SJohn Forte  * providerName - name of provider for which data should be deleted
702fcf3ce44SJohn Forte  */
703fcf3ce44SJohn Forte int
704fcf3ce44SJohn Forte stmfClearProviderData(char *providerName, int providerType)
705fcf3ce44SJohn Forte {
706fcf3ce44SJohn Forte 	int ret;
707fcf3ce44SJohn Forte 	int fd;
708fcf3ce44SJohn Forte 	int ioctlRet;
709fcf3ce44SJohn Forte 	int savedErrno;
710fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
711fcf3ce44SJohn Forte 	stmf_ppioctl_data_t ppi;
712fcf3ce44SJohn Forte 
713fcf3ce44SJohn Forte 	/* call init */
714fcf3ce44SJohn Forte 	ret = initializeConfig();
715fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
716fcf3ce44SJohn Forte 		return (ret);
717fcf3ce44SJohn Forte 	}
718fcf3ce44SJohn Forte 
719fcf3ce44SJohn Forte 	if (providerName == NULL) {
720fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
721fcf3ce44SJohn Forte 	}
722fcf3ce44SJohn Forte 
723fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
724fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
725fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
726fcf3ce44SJohn Forte 	}
727fcf3ce44SJohn Forte 
728fcf3ce44SJohn Forte 	/*
729fcf3ce44SJohn Forte 	 * Open control node for stmf
730fcf3ce44SJohn Forte 	 */
731fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
732fcf3ce44SJohn Forte 		return (ret);
733fcf3ce44SJohn Forte 
734fcf3ce44SJohn Forte 	bzero(&ppi, sizeof (ppi));
735fcf3ce44SJohn Forte 
736fcf3ce44SJohn Forte 	(void) strncpy(ppi.ppi_name, providerName, sizeof (ppi.ppi_name));
737fcf3ce44SJohn Forte 
738fcf3ce44SJohn Forte 	switch (providerType) {
739fcf3ce44SJohn Forte 		case STMF_LU_PROVIDER_TYPE:
740fcf3ce44SJohn Forte 			ppi.ppi_lu_provider = 1;
741fcf3ce44SJohn Forte 			break;
742fcf3ce44SJohn Forte 		case STMF_PORT_PROVIDER_TYPE:
743fcf3ce44SJohn Forte 			ppi.ppi_port_provider = 1;
744fcf3ce44SJohn Forte 			break;
745fcf3ce44SJohn Forte 		default:
746fcf3ce44SJohn Forte 			ret = STMF_ERROR_INVALID_ARG;
747fcf3ce44SJohn Forte 			goto done;
748fcf3ce44SJohn Forte 	}
749fcf3ce44SJohn Forte 
750fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
751fcf3ce44SJohn Forte 
752fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
753fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_ppioctl_data_t);
754fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ppi;
755fcf3ce44SJohn Forte 
756fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_CLEAR_PP_DATA, &stmfIoctl);
757fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
758fcf3ce44SJohn Forte 		savedErrno = errno;
759fcf3ce44SJohn Forte 		switch (savedErrno) {
760fcf3ce44SJohn Forte 			case EBUSY:
761fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
762fcf3ce44SJohn Forte 				break;
763fcf3ce44SJohn Forte 			case EACCES:
764fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
765fcf3ce44SJohn Forte 				break;
766fcf3ce44SJohn Forte 			default:
767fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
768fcf3ce44SJohn Forte 				    "stmfClearProviderData:ioctl error(%d)",
769fcf3ce44SJohn Forte 				    ioctlRet);
770fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
771fcf3ce44SJohn Forte 				break;
772fcf3ce44SJohn Forte 		}
773fcf3ce44SJohn Forte 		if (savedErrno != ENOENT) {
774fcf3ce44SJohn Forte 			goto done;
775fcf3ce44SJohn Forte 		}
776fcf3ce44SJohn Forte 	}
777fcf3ce44SJohn Forte 
778fcf3ce44SJohn Forte 	ret = psClearProviderData(providerName, providerType);
779fcf3ce44SJohn Forte 	switch (ret) {
780fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
781fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
782fcf3ce44SJohn Forte 			break;
783fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
784fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
785fcf3ce44SJohn Forte 			break;
786fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
787fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
788fcf3ce44SJohn Forte 			break;
789fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
790fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
791fcf3ce44SJohn Forte 			break;
792fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
793fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
794fcf3ce44SJohn Forte 			break;
795fcf3ce44SJohn Forte 		default:
796fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
797fcf3ce44SJohn Forte 			    "stmfClearProviderData:psClearProviderData"
798fcf3ce44SJohn Forte 			    ":error(%d)", ret);
799fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
800fcf3ce44SJohn Forte 			break;
801fcf3ce44SJohn Forte 	}
802fcf3ce44SJohn Forte 
803fcf3ce44SJohn Forte done:
804fcf3ce44SJohn Forte 	(void) close(fd);
805fcf3ce44SJohn Forte 	return (ret);
806fcf3ce44SJohn Forte }
807fcf3ce44SJohn Forte 
808fcf3ce44SJohn Forte /*
809fcf3ce44SJohn Forte  * stmfCreateHostGroup
810fcf3ce44SJohn Forte  *
811fcf3ce44SJohn Forte  * Purpose: Create a new initiator group
812fcf3ce44SJohn Forte  *
813fcf3ce44SJohn Forte  * hostGroupName - name of host group to create
814fcf3ce44SJohn Forte  */
815fcf3ce44SJohn Forte int
816fcf3ce44SJohn Forte stmfCreateHostGroup(stmfGroupName *hostGroupName)
817fcf3ce44SJohn Forte {
818fcf3ce44SJohn Forte 	int ret;
819fcf3ce44SJohn Forte 	int fd;
820fcf3ce44SJohn Forte 
821fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
822fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
823fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName))) {
824fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
825fcf3ce44SJohn Forte 	}
826fcf3ce44SJohn Forte 
827fcf3ce44SJohn Forte 	/* Check to ensure service exists */
828fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
829fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
830fcf3ce44SJohn Forte 	}
831fcf3ce44SJohn Forte 
832fcf3ce44SJohn Forte 	/* call init */
833fcf3ce44SJohn Forte 	ret = initializeConfig();
834fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
835fcf3ce44SJohn Forte 		return (ret);
836fcf3ce44SJohn Forte 	}
837fcf3ce44SJohn Forte 
838fcf3ce44SJohn Forte 	/*
839fcf3ce44SJohn Forte 	 * Open control node for stmf
840fcf3ce44SJohn Forte 	 */
841fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
842fcf3ce44SJohn Forte 		return (ret);
843fcf3ce44SJohn Forte 
844fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_HOST_GROUP,
845fcf3ce44SJohn Forte 	    hostGroupName)) != STMF_STATUS_SUCCESS) {
846fcf3ce44SJohn Forte 		goto done;
847fcf3ce44SJohn Forte 	}
848fcf3ce44SJohn Forte 
849fcf3ce44SJohn Forte 	ret = psCreateHostGroup((char *)hostGroupName);
850fcf3ce44SJohn Forte 	switch (ret) {
851fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
852fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
853fcf3ce44SJohn Forte 			break;
854fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
855fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
856fcf3ce44SJohn Forte 			break;
857fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
858fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
859fcf3ce44SJohn Forte 			break;
860fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
861fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
862fcf3ce44SJohn Forte 			break;
863fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
864fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
865fcf3ce44SJohn Forte 			break;
866fcf3ce44SJohn Forte 		default:
867fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
868fcf3ce44SJohn Forte 			    "stmfCreateHostGroup:psCreateHostGroup:error(%d)",
869fcf3ce44SJohn Forte 			    ret);
870fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
871fcf3ce44SJohn Forte 			break;
872fcf3ce44SJohn Forte 	}
873fcf3ce44SJohn Forte 
874fcf3ce44SJohn Forte done:
875fcf3ce44SJohn Forte 	(void) close(fd);
876fcf3ce44SJohn Forte 	return (ret);
877fcf3ce44SJohn Forte }
878fcf3ce44SJohn Forte 
879fcf3ce44SJohn Forte /*
880fcf3ce44SJohn Forte  * stmfCreateTargetGroup
881fcf3ce44SJohn Forte  *
882fcf3ce44SJohn Forte  * Purpose: Create a local port group
883fcf3ce44SJohn Forte  *
884fcf3ce44SJohn Forte  * targetGroupName - name of local port group to create
885fcf3ce44SJohn Forte  */
886fcf3ce44SJohn Forte int
887fcf3ce44SJohn Forte stmfCreateTargetGroup(stmfGroupName *targetGroupName)
888fcf3ce44SJohn Forte {
889fcf3ce44SJohn Forte 	int ret;
890fcf3ce44SJohn Forte 	int fd;
891fcf3ce44SJohn Forte 
892fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
893fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
894fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName))) {
895fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
896fcf3ce44SJohn Forte 	}
897fcf3ce44SJohn Forte 
898fcf3ce44SJohn Forte 	/* Check to ensure service exists */
899fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
900fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
901fcf3ce44SJohn Forte 	}
902fcf3ce44SJohn Forte 
903fcf3ce44SJohn Forte 	/* call init */
904fcf3ce44SJohn Forte 	ret = initializeConfig();
905fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
906fcf3ce44SJohn Forte 		return (ret);
907fcf3ce44SJohn Forte 	}
908fcf3ce44SJohn Forte 
909fcf3ce44SJohn Forte 	/*
910fcf3ce44SJohn Forte 	 * Open control node for stmf
911fcf3ce44SJohn Forte 	 */
912fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
913fcf3ce44SJohn Forte 		return (ret);
914fcf3ce44SJohn Forte 
915fcf3ce44SJohn Forte 	/*
916fcf3ce44SJohn Forte 	 * Add the group to the driver
917fcf3ce44SJohn Forte 	 */
918fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_TARGET_GROUP,
919fcf3ce44SJohn Forte 	    targetGroupName)) != STMF_STATUS_SUCCESS) {
920fcf3ce44SJohn Forte 		goto done;
921fcf3ce44SJohn Forte 	}
922fcf3ce44SJohn Forte 
923fcf3ce44SJohn Forte 	/*
924fcf3ce44SJohn Forte 	 * If the add to the driver was successful, add it to the persistent
925fcf3ce44SJohn Forte 	 * store.
926fcf3ce44SJohn Forte 	 */
927fcf3ce44SJohn Forte 	ret = psCreateTargetGroup((char *)targetGroupName);
928fcf3ce44SJohn Forte 	switch (ret) {
929fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
930fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
931fcf3ce44SJohn Forte 			break;
932fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
933fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
934fcf3ce44SJohn Forte 			break;
935fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
936fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
937fcf3ce44SJohn Forte 			break;
938fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
939fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
940fcf3ce44SJohn Forte 			break;
941fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
942fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
943fcf3ce44SJohn Forte 			break;
944fcf3ce44SJohn Forte 		default:
945fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
946fcf3ce44SJohn Forte 			    "stmfCreateTargetGroup:psCreateTargetGroup"
947fcf3ce44SJohn Forte 			    ":error(%d)", ret);
948fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
949fcf3ce44SJohn Forte 			break;
950fcf3ce44SJohn Forte 	}
951fcf3ce44SJohn Forte 
952fcf3ce44SJohn Forte done:
953fcf3ce44SJohn Forte 	(void) close(fd);
954fcf3ce44SJohn Forte 	return (ret);
955fcf3ce44SJohn Forte }
956fcf3ce44SJohn Forte 
957fcf3ce44SJohn Forte /*
958fcf3ce44SJohn Forte  * stmfDeleteHostGroup
959fcf3ce44SJohn Forte  *
960fcf3ce44SJohn Forte  * Purpose: Delete an initiator or local port group
961fcf3ce44SJohn Forte  *
962fcf3ce44SJohn Forte  * hostGroupName - group to delete
963fcf3ce44SJohn Forte  */
964fcf3ce44SJohn Forte int
965fcf3ce44SJohn Forte stmfDeleteHostGroup(stmfGroupName *hostGroupName)
966fcf3ce44SJohn Forte {
967fcf3ce44SJohn Forte 	int ret;
968fcf3ce44SJohn Forte 	int fd;
969fcf3ce44SJohn Forte 
970fcf3ce44SJohn Forte 	if (hostGroupName == NULL) {
971fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
972fcf3ce44SJohn Forte 	}
973fcf3ce44SJohn Forte 
974fcf3ce44SJohn Forte 	/* Check to ensure service exists */
975fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
976fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
977fcf3ce44SJohn Forte 	}
978fcf3ce44SJohn Forte 
979fcf3ce44SJohn Forte 	/* call init */
980fcf3ce44SJohn Forte 	ret = initializeConfig();
981fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
982fcf3ce44SJohn Forte 		return (ret);
983fcf3ce44SJohn Forte 	}
984fcf3ce44SJohn Forte 
985fcf3ce44SJohn Forte 	/*
986fcf3ce44SJohn Forte 	 * Open control node for stmf
987fcf3ce44SJohn Forte 	 */
988fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
989fcf3ce44SJohn Forte 		return (ret);
990fcf3ce44SJohn Forte 
991fcf3ce44SJohn Forte 	/*
992fcf3ce44SJohn Forte 	 * Remove the group from the driver
993fcf3ce44SJohn Forte 	 */
994fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_REMOVE_HOST_GROUP,
995fcf3ce44SJohn Forte 	    hostGroupName)) != STMF_STATUS_SUCCESS) {
996fcf3ce44SJohn Forte 		goto done;
997fcf3ce44SJohn Forte 	}
998fcf3ce44SJohn Forte 
999fcf3ce44SJohn Forte 	/*
1000fcf3ce44SJohn Forte 	 * If the remove from the driver was successful, remove it from the
1001fcf3ce44SJohn Forte 	 * persistent store.
1002fcf3ce44SJohn Forte 	 */
1003fcf3ce44SJohn Forte 	ret = psDeleteHostGroup((char *)hostGroupName);
1004fcf3ce44SJohn Forte 	switch (ret) {
1005fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
1006fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
1007fcf3ce44SJohn Forte 			break;
1008fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
1009fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
1010fcf3ce44SJohn Forte 			break;
1011fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
1012fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1013fcf3ce44SJohn Forte 			break;
1014fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
1015fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
1016fcf3ce44SJohn Forte 			break;
1017fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
1018fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
1019fcf3ce44SJohn Forte 			break;
1020fcf3ce44SJohn Forte 		default:
1021fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
1022fcf3ce44SJohn Forte 			    "stmfDeleteHostGroup:psDeleteHostGroup:error(%d)",
1023fcf3ce44SJohn Forte 			    ret);
1024fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
1025fcf3ce44SJohn Forte 			break;
1026fcf3ce44SJohn Forte 	}
1027fcf3ce44SJohn Forte 
1028fcf3ce44SJohn Forte done:
1029fcf3ce44SJohn Forte 	(void) close(fd);
1030fcf3ce44SJohn Forte 	return (ret);
1031fcf3ce44SJohn Forte }
1032fcf3ce44SJohn Forte 
1033fcf3ce44SJohn Forte /*
1034fcf3ce44SJohn Forte  * stmfDeleteTargetGroup
1035fcf3ce44SJohn Forte  *
1036fcf3ce44SJohn Forte  * Purpose: Delete an initiator or local port group
1037fcf3ce44SJohn Forte  *
1038fcf3ce44SJohn Forte  * targetGroupName - group to delete
1039fcf3ce44SJohn Forte  */
1040fcf3ce44SJohn Forte int
1041fcf3ce44SJohn Forte stmfDeleteTargetGroup(stmfGroupName *targetGroupName)
1042fcf3ce44SJohn Forte {
1043fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
1044fcf3ce44SJohn Forte 	int fd;
1045fcf3ce44SJohn Forte 
1046fcf3ce44SJohn Forte 	if (targetGroupName == NULL) {
1047fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1048fcf3ce44SJohn Forte 	}
1049fcf3ce44SJohn Forte 
1050fcf3ce44SJohn Forte 	/* Check to ensure service exists */
1051fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
1052fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
1053fcf3ce44SJohn Forte 	}
1054fcf3ce44SJohn Forte 
1055fcf3ce44SJohn Forte 	/* call init */
1056fcf3ce44SJohn Forte 	ret = initializeConfig();
1057fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
1058fcf3ce44SJohn Forte 		return (ret);
1059fcf3ce44SJohn Forte 	}
1060fcf3ce44SJohn Forte 
1061fcf3ce44SJohn Forte 	/*
1062fcf3ce44SJohn Forte 	 * Open control node for stmf
1063fcf3ce44SJohn Forte 	 */
1064fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
1065fcf3ce44SJohn Forte 		return (ret);
1066fcf3ce44SJohn Forte 
1067fcf3ce44SJohn Forte 	/*
1068fcf3ce44SJohn Forte 	 * Remove the group from the driver
1069fcf3ce44SJohn Forte 	 */
1070fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_REMOVE_TARGET_GROUP,
1071fcf3ce44SJohn Forte 	    targetGroupName)) != STMF_STATUS_SUCCESS) {
1072fcf3ce44SJohn Forte 		goto done;
1073fcf3ce44SJohn Forte 	}
1074fcf3ce44SJohn Forte 
1075fcf3ce44SJohn Forte 	/*
1076fcf3ce44SJohn Forte 	 * If the remove from the driver was successful, remove it from the
1077fcf3ce44SJohn Forte 	 * persistent store.
1078fcf3ce44SJohn Forte 	 */
1079fcf3ce44SJohn Forte 	ret = psDeleteTargetGroup((char *)targetGroupName);
1080fcf3ce44SJohn Forte 	switch (ret) {
1081fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
1082fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
1083fcf3ce44SJohn Forte 			break;
1084fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
1085fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
1086fcf3ce44SJohn Forte 			break;
1087fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
1088fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1089fcf3ce44SJohn Forte 			break;
1090fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
1091fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
1092fcf3ce44SJohn Forte 			break;
1093fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
1094fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
1095fcf3ce44SJohn Forte 			break;
1096fcf3ce44SJohn Forte 		default:
1097fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
1098fcf3ce44SJohn Forte 			    "stmfDeleteTargetGroup:psDeleteTargetGroup"
1099fcf3ce44SJohn Forte 			    ":error(%d)", ret);
1100fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
1101fcf3ce44SJohn Forte 			break;
1102fcf3ce44SJohn Forte 	}
1103fcf3ce44SJohn Forte 
1104fcf3ce44SJohn Forte done:
1105fcf3ce44SJohn Forte 	(void) close(fd);
1106fcf3ce44SJohn Forte 	return (ret);
1107fcf3ce44SJohn Forte }
1108fcf3ce44SJohn Forte 
1109fcf3ce44SJohn Forte /*
1110fcf3ce44SJohn Forte  * stmfDevidFromIscsiName
1111fcf3ce44SJohn Forte  *
1112fcf3ce44SJohn Forte  * Purpose: convert an iSCSI name to an stmf devid
1113fcf3ce44SJohn Forte  *
1114fcf3ce44SJohn Forte  * iscsiName - unicode nul terminated utf-8 encoded iSCSI name
1115fcf3ce44SJohn Forte  * devid - on success, contains the converted iscsi name
1116fcf3ce44SJohn Forte  */
1117fcf3ce44SJohn Forte int
1118fcf3ce44SJohn Forte stmfDevidFromIscsiName(char *iscsiName, stmfDevid *devid)
1119fcf3ce44SJohn Forte {
1120fcf3ce44SJohn Forte 	if (devid == NULL || iscsiName == NULL)
1121fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1122fcf3ce44SJohn Forte 
1123fcf3ce44SJohn Forte 	bzero(devid, sizeof (stmfDevid));
1124fcf3ce44SJohn Forte 
1125fcf3ce44SJohn Forte 	/* Validate size of target */
1126fcf3ce44SJohn Forte 	if ((devid->identLength = strlen(iscsiName)) > MAX_ISCSI_NAME ||
1127fcf3ce44SJohn Forte 	    devid->identLength < strlen(EUI) ||
1128fcf3ce44SJohn Forte 	    devid->identLength < strlen(IQN)) {
1129fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1130fcf3ce44SJohn Forte 	}
1131fcf3ce44SJohn Forte 
1132fcf3ce44SJohn Forte 	if ((strncmp(iscsiName, EUI, strlen(EUI)) != 0) &&
1133fcf3ce44SJohn Forte 	    strncmp(iscsiName, IQN, strlen(IQN)) != 0) {
1134fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1135fcf3ce44SJohn Forte 	}
1136fcf3ce44SJohn Forte 
1137fcf3ce44SJohn Forte 	/* copy UTF-8 bytes to ident */
1138fcf3ce44SJohn Forte 	bcopy(iscsiName, devid->ident, devid->identLength);
1139fcf3ce44SJohn Forte 
1140fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
1141fcf3ce44SJohn Forte }
1142fcf3ce44SJohn Forte 
1143fcf3ce44SJohn Forte /*
1144fcf3ce44SJohn Forte  * stmfDevidFromWwn
1145fcf3ce44SJohn Forte  *
1146fcf3ce44SJohn Forte  * Purpose: convert a WWN to an stmf devid
1147fcf3ce44SJohn Forte  *
1148fcf3ce44SJohn Forte  * wwn - 8-byte wwn identifier
1149fcf3ce44SJohn Forte  * devid - on success, contains the converted wwn
1150fcf3ce44SJohn Forte  */
1151fcf3ce44SJohn Forte int
1152fcf3ce44SJohn Forte stmfDevidFromWwn(uchar_t *wwn, stmfDevid *devid)
1153fcf3ce44SJohn Forte {
1154fcf3ce44SJohn Forte 	if (wwn == NULL || devid == NULL)
1155fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1156fcf3ce44SJohn Forte 
1157fcf3ce44SJohn Forte 	bzero(devid, sizeof (stmfDevid));
1158fcf3ce44SJohn Forte 
1159fcf3ce44SJohn Forte 	/* Copy eui prefix */
1160fcf3ce44SJohn Forte 	(void) bcopy(WWN, devid->ident, strlen(WWN));
1161fcf3ce44SJohn Forte 
1162fcf3ce44SJohn Forte 	/* Convert to ASCII uppercase hexadecimal string */
1163fcf3ce44SJohn Forte 	(void) snprintf((char *)&devid->ident[strlen(WWN)],
1164fcf3ce44SJohn Forte 	    sizeof (devid->ident), "%02X%02X%02X%02X%02X%02X%02X%02X",
1165fcf3ce44SJohn Forte 	    wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
1166fcf3ce44SJohn Forte 
1167fcf3ce44SJohn Forte 	devid->identLength = strlen((char *)devid->ident);
1168fcf3ce44SJohn Forte 
1169fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
1170fcf3ce44SJohn Forte }
1171fcf3ce44SJohn Forte 
1172fcf3ce44SJohn Forte /*
1173fcf3ce44SJohn Forte  * stmfFreeMemory
1174fcf3ce44SJohn Forte  *
1175fcf3ce44SJohn Forte  * Purpose: Free memory allocated by this library
1176fcf3ce44SJohn Forte  *
1177fcf3ce44SJohn Forte  * memory - previously allocated pointer of memory managed by library
1178fcf3ce44SJohn Forte  */
1179fcf3ce44SJohn Forte void
1180fcf3ce44SJohn Forte stmfFreeMemory(void *memory)
1181fcf3ce44SJohn Forte {
1182fcf3ce44SJohn Forte 	free(memory);
1183fcf3ce44SJohn Forte }
1184fcf3ce44SJohn Forte 
1185fcf3ce44SJohn Forte /*
1186fcf3ce44SJohn Forte  * stmfGetHostGroupList
1187fcf3ce44SJohn Forte  *
1188fcf3ce44SJohn Forte  * Purpose: Retrieves the list of initiator group oids
1189fcf3ce44SJohn Forte  *
1190fcf3ce44SJohn Forte  * hostGroupList - pointer to pointer to hostGroupList structure
1191fcf3ce44SJohn Forte  *                 on success, this contains the host group list.
1192fcf3ce44SJohn Forte  */
1193fcf3ce44SJohn Forte int
1194fcf3ce44SJohn Forte stmfGetHostGroupList(stmfGroupList **hostGroupList)
1195fcf3ce44SJohn Forte {
1196fcf3ce44SJohn Forte 	int ret;
1197fcf3ce44SJohn Forte 
1198fcf3ce44SJohn Forte 	if (hostGroupList == NULL) {
1199fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1200fcf3ce44SJohn Forte 	}
1201fcf3ce44SJohn Forte 
1202fcf3ce44SJohn Forte 	ret = psGetHostGroupList(hostGroupList);
1203fcf3ce44SJohn Forte 	switch (ret) {
1204fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
1205fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
1206fcf3ce44SJohn Forte 			break;
1207fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
1208fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
1209fcf3ce44SJohn Forte 			break;
1210fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
1211fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1212fcf3ce44SJohn Forte 			break;
1213fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
1214fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
1215fcf3ce44SJohn Forte 			break;
1216fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
1217fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
1218fcf3ce44SJohn Forte 			break;
1219fcf3ce44SJohn Forte 		default:
1220fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
1221fcf3ce44SJohn Forte 			    "stmfGetHostGroupList:psGetHostGroupList:error(%d)",
1222fcf3ce44SJohn Forte 			    ret);
1223fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
1224fcf3ce44SJohn Forte 			break;
1225fcf3ce44SJohn Forte 	}
1226fcf3ce44SJohn Forte 
1227fcf3ce44SJohn Forte 	return (ret);
1228fcf3ce44SJohn Forte }
1229fcf3ce44SJohn Forte 
1230fcf3ce44SJohn Forte /*
1231fcf3ce44SJohn Forte  * stmfGetHostGroupMembers
1232fcf3ce44SJohn Forte  *
1233fcf3ce44SJohn Forte  * Purpose: Retrieves the group properties for a host group
1234fcf3ce44SJohn Forte  *
1235fcf3ce44SJohn Forte  * groupName - name of group for which to retrieve host group members.
1236fcf3ce44SJohn Forte  * groupProp - pointer to pointer to stmfGroupProperties structure
1237fcf3ce44SJohn Forte  *             on success, this contains the list of group members.
1238fcf3ce44SJohn Forte  */
1239fcf3ce44SJohn Forte int
1240fcf3ce44SJohn Forte stmfGetHostGroupMembers(stmfGroupName *groupName,
1241fcf3ce44SJohn Forte     stmfGroupProperties **groupProp)
1242fcf3ce44SJohn Forte {
1243fcf3ce44SJohn Forte 	int ret;
1244fcf3ce44SJohn Forte 
1245fcf3ce44SJohn Forte 	if (groupName == NULL || groupProp == NULL) {
1246fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1247fcf3ce44SJohn Forte 	}
1248fcf3ce44SJohn Forte 
1249fcf3ce44SJohn Forte 	ret = psGetHostGroupMemberList((char *)groupName, groupProp);
1250fcf3ce44SJohn Forte 	switch (ret) {
1251fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
1252fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
1253fcf3ce44SJohn Forte 			break;
1254fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
1255fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
1256fcf3ce44SJohn Forte 			break;
1257fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
1258fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1259fcf3ce44SJohn Forte 			break;
1260fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
1261fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
1262fcf3ce44SJohn Forte 			break;
1263fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
1264fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
1265fcf3ce44SJohn Forte 			break;
1266fcf3ce44SJohn Forte 		default:
1267fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
1268fcf3ce44SJohn Forte 			    "stmfGetHostGroupMembers:psGetHostGroupMembers"
1269fcf3ce44SJohn Forte 			    ":error(%d)", ret);
1270fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
1271fcf3ce44SJohn Forte 			break;
1272fcf3ce44SJohn Forte 	}
1273fcf3ce44SJohn Forte 
1274fcf3ce44SJohn Forte 	return (ret);
1275fcf3ce44SJohn Forte }
1276fcf3ce44SJohn Forte 
1277fcf3ce44SJohn Forte /*
1278fcf3ce44SJohn Forte  * stmfGetProviderData
1279fcf3ce44SJohn Forte  *
1280fcf3ce44SJohn Forte  * Purpose: Get provider data list
1281fcf3ce44SJohn Forte  *
1282fcf3ce44SJohn Forte  * providerName - name of provider for which to retrieve the data
1283fcf3ce44SJohn Forte  * nvl - pointer to nvlist_t pointer which will contain the nvlist data
1284fcf3ce44SJohn Forte  *       retrieved.
1285fcf3ce44SJohn Forte  * providerType - type of provider for which to retrieve data.
1286fcf3ce44SJohn Forte  *		    STMF_LU_PROVIDER_TYPE
1287fcf3ce44SJohn Forte  *		    STMF_PORT_PROVIDER_TYPE
1288fcf3ce44SJohn Forte  */
1289fcf3ce44SJohn Forte int
1290fcf3ce44SJohn Forte stmfGetProviderData(char *providerName, nvlist_t **nvl, int providerType)
1291fcf3ce44SJohn Forte {
1292fcf3ce44SJohn Forte 	return (stmfGetProviderDataProt(providerName, nvl, providerType,
1293fcf3ce44SJohn Forte 	    NULL));
1294fcf3ce44SJohn Forte }
1295fcf3ce44SJohn Forte 
1296fcf3ce44SJohn Forte /*
1297fcf3ce44SJohn Forte  * stmfGetProviderDataProt
1298fcf3ce44SJohn Forte  *
1299fcf3ce44SJohn Forte  * Purpose: Get provider data list with token
1300fcf3ce44SJohn Forte  *
1301fcf3ce44SJohn Forte  * providerName - name of provider for which to retrieve the data
1302fcf3ce44SJohn Forte  * nvl - pointer to nvlist_t pointer which will contain the nvlist data
1303fcf3ce44SJohn Forte  *       retrieved.
1304fcf3ce44SJohn Forte  * providerType - type of provider for which to retrieve data.
1305fcf3ce44SJohn Forte  *		    STMF_LU_PROVIDER_TYPE
1306fcf3ce44SJohn Forte  *		    STMF_PORT_PROVIDER_TYPE
1307fcf3ce44SJohn Forte  * setToken - Returns the stale data token
1308fcf3ce44SJohn Forte  */
1309fcf3ce44SJohn Forte int
1310fcf3ce44SJohn Forte stmfGetProviderDataProt(char *providerName, nvlist_t **nvl, int providerType,
1311fcf3ce44SJohn Forte     uint64_t *setToken)
1312fcf3ce44SJohn Forte {
1313fcf3ce44SJohn Forte 	int ret;
1314fcf3ce44SJohn Forte 
1315fcf3ce44SJohn Forte 	if (providerName == NULL || nvl == NULL) {
1316fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1317fcf3ce44SJohn Forte 	}
1318fcf3ce44SJohn Forte 
1319fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
1320fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
1321fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1322fcf3ce44SJohn Forte 	}
1323fcf3ce44SJohn Forte 
1324fcf3ce44SJohn Forte 	/* call init */
1325fcf3ce44SJohn Forte 	ret = initializeConfig();
1326fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
1327fcf3ce44SJohn Forte 		return (ret);
1328fcf3ce44SJohn Forte 	}
1329fcf3ce44SJohn Forte 
1330fcf3ce44SJohn Forte 	ret = psGetProviderData(providerName, nvl, providerType, setToken);
1331fcf3ce44SJohn Forte 	switch (ret) {
1332fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
1333fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
1334fcf3ce44SJohn Forte 			break;
1335fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
1336fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1337fcf3ce44SJohn Forte 			break;
1338fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
1339fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
1340fcf3ce44SJohn Forte 			break;
1341fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
1342fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
1343fcf3ce44SJohn Forte 			break;
1344fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
1345fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
1346fcf3ce44SJohn Forte 			break;
1347fcf3ce44SJohn Forte 		default:
1348fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
1349fcf3ce44SJohn Forte 			    "stmfGetProviderData:psGetProviderData:error(%d)",
1350fcf3ce44SJohn Forte 			    ret);
1351fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
1352fcf3ce44SJohn Forte 			break;
1353fcf3ce44SJohn Forte 	}
1354fcf3ce44SJohn Forte 
1355fcf3ce44SJohn Forte 	return (ret);
1356fcf3ce44SJohn Forte }
1357fcf3ce44SJohn Forte 
1358fcf3ce44SJohn Forte /*
1359fcf3ce44SJohn Forte  * stmfGetProviderDataList
1360fcf3ce44SJohn Forte  *
1361fcf3ce44SJohn Forte  * Purpose: Get the list of providers currently persisting data
1362fcf3ce44SJohn Forte  *
1363fcf3ce44SJohn Forte  * providerList - pointer to pointer to an stmfProviderList structure allocated
1364fcf3ce44SJohn Forte  *                by the caller. Will contain the list of providers on success.
1365fcf3ce44SJohn Forte  */
1366fcf3ce44SJohn Forte int
1367fcf3ce44SJohn Forte stmfGetProviderDataList(stmfProviderList **providerList)
1368fcf3ce44SJohn Forte {
1369fcf3ce44SJohn Forte 	int ret;
1370fcf3ce44SJohn Forte 
1371fcf3ce44SJohn Forte 	ret = psGetProviderDataList(providerList);
1372fcf3ce44SJohn Forte 	switch (ret) {
1373fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
1374fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
1375fcf3ce44SJohn Forte 			break;
1376fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
1377fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1378fcf3ce44SJohn Forte 			break;
1379fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
1380fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
1381fcf3ce44SJohn Forte 			break;
1382fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
1383fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
1384fcf3ce44SJohn Forte 			break;
1385fcf3ce44SJohn Forte 		default:
1386fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
1387fcf3ce44SJohn Forte 			    "stmfGetProviderDataList:psGetProviderDataList"
1388fcf3ce44SJohn Forte 			    ":error(%d)", ret);
1389fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
1390fcf3ce44SJohn Forte 			break;
1391fcf3ce44SJohn Forte 	}
1392fcf3ce44SJohn Forte 
1393fcf3ce44SJohn Forte 	return (ret);
1394fcf3ce44SJohn Forte }
1395fcf3ce44SJohn Forte 
1396fcf3ce44SJohn Forte 
1397fcf3ce44SJohn Forte /*
1398fcf3ce44SJohn Forte  * stmfGetSessionList
1399fcf3ce44SJohn Forte  *
1400fcf3ce44SJohn Forte  * Purpose: Retrieves the session list for a target (devid)
1401fcf3ce44SJohn Forte  *
1402fcf3ce44SJohn Forte  * devid - devid of target for which to retrieve session information.
1403fcf3ce44SJohn Forte  * sessionList - pointer to pointer to stmfSessionList structure
1404fcf3ce44SJohn Forte  *             on success, this contains the list of initiator sessions.
1405fcf3ce44SJohn Forte  */
1406fcf3ce44SJohn Forte int
1407fcf3ce44SJohn Forte stmfGetSessionList(stmfDevid *devid, stmfSessionList **sessionList)
1408fcf3ce44SJohn Forte {
1409fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
1410fcf3ce44SJohn Forte 	int fd;
1411fcf3ce44SJohn Forte 	int ioctlRet;
1412fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_SESSION_LIST;
1413fcf3ce44SJohn Forte 	int i;
1414fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
1415fcf3ce44SJohn Forte 	slist_scsi_session_t *fSessionList;
1416fcf3ce44SJohn Forte 	uint8_t ident[260];
1417fcf3ce44SJohn Forte 	uint32_t fSessionListSize;
1418fcf3ce44SJohn Forte 
1419fcf3ce44SJohn Forte 	if (sessionList == NULL || devid == NULL) {
1420fcf3ce44SJohn Forte 		ret = STMF_ERROR_INVALID_ARG;
1421fcf3ce44SJohn Forte 	}
1422fcf3ce44SJohn Forte 
1423fcf3ce44SJohn Forte 	/* call init */
1424fcf3ce44SJohn Forte 	ret = initializeConfig();
1425fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
1426fcf3ce44SJohn Forte 		return (ret);
1427fcf3ce44SJohn Forte 	}
1428fcf3ce44SJohn Forte 
1429fcf3ce44SJohn Forte 	/*
1430fcf3ce44SJohn Forte 	 * Open control node for stmf
1431fcf3ce44SJohn Forte 	 */
1432fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
1433fcf3ce44SJohn Forte 		return (ret);
1434fcf3ce44SJohn Forte 
1435fcf3ce44SJohn Forte 	/*
1436fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
1437fcf3ce44SJohn Forte 	 */
1438fcf3ce44SJohn Forte 	fSessionListSize = MAX_SESSION;
1439fcf3ce44SJohn Forte 	fSessionListSize = fSessionListSize * (sizeof (slist_scsi_session_t));
1440fcf3ce44SJohn Forte 	fSessionList = (slist_scsi_session_t *)calloc(1, fSessionListSize);
1441fcf3ce44SJohn Forte 	if (fSessionList == NULL) {
1442fcf3ce44SJohn Forte 		return (STMF_ERROR_NOMEM);
1443fcf3ce44SJohn Forte 	}
1444fcf3ce44SJohn Forte 
1445fcf3ce44SJohn Forte 	ident[IDENT_LENGTH_BYTE] = devid->identLength;
1446fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &ident[IDENT_LENGTH_BYTE + 1],
1447fcf3ce44SJohn Forte 	    devid->identLength);
1448fcf3ce44SJohn Forte 
1449fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
1450fcf3ce44SJohn Forte 	/*
1451fcf3ce44SJohn Forte 	 * Issue ioctl to get the session list
1452fcf3ce44SJohn Forte 	 */
1453fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
1454fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ident;
1455fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ident);
1456fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fSessionListSize;
1457fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fSessionList;
1458fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
1459fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
1460fcf3ce44SJohn Forte 		switch (errno) {
1461fcf3ce44SJohn Forte 			case EBUSY:
1462fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
1463fcf3ce44SJohn Forte 				break;
1464fcf3ce44SJohn Forte 			case EACCES:
1465fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
1466fcf3ce44SJohn Forte 				break;
1467fcf3ce44SJohn Forte 			default:
1468fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
1469fcf3ce44SJohn Forte 				    "stmfGetSessionList:ioctl errno(%d)",
1470fcf3ce44SJohn Forte 				    errno);
1471fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
1472fcf3ce44SJohn Forte 				break;
1473fcf3ce44SJohn Forte 		}
1474fcf3ce44SJohn Forte 		goto done;
1475fcf3ce44SJohn Forte 	}
1476fcf3ce44SJohn Forte 	/*
1477fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
1478fcf3ce44SJohn Forte 	 */
1479fcf3ce44SJohn Forte 	if (stmfIoctl.stmf_obuf_max_nentries > MAX_SESSION) {
1480fcf3ce44SJohn Forte 		fSessionListSize = stmfIoctl.stmf_obuf_max_nentries *
1481fcf3ce44SJohn Forte 		    sizeof (slist_scsi_session_t);
1482fcf3ce44SJohn Forte 		fSessionList = realloc(fSessionList, fSessionListSize);
1483fcf3ce44SJohn Forte 		if (fSessionList == NULL) {
1484fcf3ce44SJohn Forte 			return (STMF_ERROR_NOMEM);
1485fcf3ce44SJohn Forte 		}
1486fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fSessionListSize;
1487fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fSessionList;
1488fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
1489fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
1490fcf3ce44SJohn Forte 			switch (errno) {
1491fcf3ce44SJohn Forte 				case EBUSY:
1492fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
1493fcf3ce44SJohn Forte 					break;
1494fcf3ce44SJohn Forte 				case EACCES:
1495fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
1496fcf3ce44SJohn Forte 					break;
1497fcf3ce44SJohn Forte 				default:
1498fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
1499fcf3ce44SJohn Forte 					    "stmfGetSessionList:ioctl "
1500fcf3ce44SJohn Forte 					    "errno(%d)", errno);
1501fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
1502fcf3ce44SJohn Forte 					break;
1503fcf3ce44SJohn Forte 			}
1504fcf3ce44SJohn Forte 			goto done;
1505fcf3ce44SJohn Forte 		}
1506fcf3ce44SJohn Forte 	}
1507fcf3ce44SJohn Forte 
1508fcf3ce44SJohn Forte 	/*
1509fcf3ce44SJohn Forte 	 * allocate caller's buffer with the final size
1510fcf3ce44SJohn Forte 	 */
1511fcf3ce44SJohn Forte 	*sessionList = (stmfSessionList *)calloc(1, sizeof (stmfSessionList) +
1512fcf3ce44SJohn Forte 	    stmfIoctl.stmf_obuf_max_nentries * sizeof (stmfSession));
1513fcf3ce44SJohn Forte 	if (*sessionList == NULL) {
1514fcf3ce44SJohn Forte 		ret = STMF_ERROR_NOMEM;
1515fcf3ce44SJohn Forte 		free(sessionList);
1516fcf3ce44SJohn Forte 		goto done;
1517fcf3ce44SJohn Forte 	}
1518fcf3ce44SJohn Forte 
1519fcf3ce44SJohn Forte 	(*sessionList)->cnt = stmfIoctl.stmf_obuf_max_nentries;
1520fcf3ce44SJohn Forte 
1521fcf3ce44SJohn Forte 	/*
1522fcf3ce44SJohn Forte 	 * copy session info to caller's buffer
1523fcf3ce44SJohn Forte 	 */
1524fcf3ce44SJohn Forte 	for (i = 0; i < (*sessionList)->cnt; i++) {
1525fcf3ce44SJohn Forte 		(*sessionList)->session[i].initiator.identLength =
1526fcf3ce44SJohn Forte 		    fSessionList->initiator[IDENT_LENGTH_BYTE];
1527fcf3ce44SJohn Forte 		bcopy(&(fSessionList->initiator[IDENT_LENGTH_BYTE + 1]),
1528fcf3ce44SJohn Forte 		    (*sessionList)->session[i].initiator.ident,
1529fcf3ce44SJohn Forte 		    STMF_IDENT_LENGTH);
1530fcf3ce44SJohn Forte 		bcopy(&(fSessionList->alias),
1531fcf3ce44SJohn Forte 		    &((*sessionList)->session[i].alias),
1532fcf3ce44SJohn Forte 		    sizeof ((*sessionList)->session[i].alias));
1533fcf3ce44SJohn Forte 		bcopy(&(fSessionList++->creation_time),
1534fcf3ce44SJohn Forte 		    &((*sessionList)->session[i].creationTime),
1535fcf3ce44SJohn Forte 		    sizeof (time_t));
1536fcf3ce44SJohn Forte 	}
1537fcf3ce44SJohn Forte done:
1538fcf3ce44SJohn Forte 	(void) close(fd);
1539fcf3ce44SJohn Forte 	return (ret);
1540fcf3ce44SJohn Forte }
1541fcf3ce44SJohn Forte 
1542fcf3ce44SJohn Forte /*
1543fcf3ce44SJohn Forte  * stmfGetTargetGroupList
1544fcf3ce44SJohn Forte  *
1545fcf3ce44SJohn Forte  * Purpose: Retrieves the list of target groups
1546fcf3ce44SJohn Forte  *
1547fcf3ce44SJohn Forte  * targetGroupList - pointer to a pointer to an stmfGroupList structure. On
1548fcf3ce44SJohn Forte  *		     success, it contains the list of target groups.
1549fcf3ce44SJohn Forte  */
1550fcf3ce44SJohn Forte int
1551fcf3ce44SJohn Forte stmfGetTargetGroupList(stmfGroupList **targetGroupList)
1552fcf3ce44SJohn Forte {
1553fcf3ce44SJohn Forte 	int ret;
1554fcf3ce44SJohn Forte 
1555fcf3ce44SJohn Forte 	if (targetGroupList == NULL) {
1556fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1557fcf3ce44SJohn Forte 	}
1558fcf3ce44SJohn Forte 
1559fcf3ce44SJohn Forte 	ret = psGetTargetGroupList(targetGroupList);
1560fcf3ce44SJohn Forte 	switch (ret) {
1561fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
1562fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
1563fcf3ce44SJohn Forte 			break;
1564fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
1565fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
1566fcf3ce44SJohn Forte 			break;
1567fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
1568fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1569fcf3ce44SJohn Forte 			break;
1570fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
1571fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
1572fcf3ce44SJohn Forte 			break;
1573fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
1574fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
1575fcf3ce44SJohn Forte 			break;
1576fcf3ce44SJohn Forte 		default:
1577fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
1578fcf3ce44SJohn Forte 			    "stmfGetTargetGroupList:psGetTargetGroupList:"
1579fcf3ce44SJohn Forte 			    "error(%d)", ret);
1580fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
1581fcf3ce44SJohn Forte 			break;
1582fcf3ce44SJohn Forte 	}
1583fcf3ce44SJohn Forte 
1584fcf3ce44SJohn Forte 	return (ret);
1585fcf3ce44SJohn Forte }
1586fcf3ce44SJohn Forte 
1587fcf3ce44SJohn Forte /*
1588fcf3ce44SJohn Forte  * stmfGetTargetGroupMembers
1589fcf3ce44SJohn Forte  *
1590fcf3ce44SJohn Forte  * Purpose: Retrieves the group members for a target group
1591fcf3ce44SJohn Forte  *
1592fcf3ce44SJohn Forte  * groupName - name of target group for which to retrieve members.
1593fcf3ce44SJohn Forte  * groupProp - pointer to pointer to stmfGroupProperties structure
1594fcf3ce44SJohn Forte  *             on success, this contains the list of group members.
1595fcf3ce44SJohn Forte  */
1596fcf3ce44SJohn Forte int
1597fcf3ce44SJohn Forte stmfGetTargetGroupMembers(stmfGroupName *groupName,
1598fcf3ce44SJohn Forte     stmfGroupProperties **groupProp)
1599fcf3ce44SJohn Forte {
1600fcf3ce44SJohn Forte 	int ret;
1601fcf3ce44SJohn Forte 
1602fcf3ce44SJohn Forte 	if (groupName == NULL || groupProp == NULL) {
1603fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1604fcf3ce44SJohn Forte 	}
1605fcf3ce44SJohn Forte 
1606fcf3ce44SJohn Forte 	ret = psGetTargetGroupMemberList((char *)groupName, groupProp);
1607fcf3ce44SJohn Forte 	switch (ret) {
1608fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
1609fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
1610fcf3ce44SJohn Forte 			break;
1611fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
1612fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
1613fcf3ce44SJohn Forte 			break;
1614fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
1615fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1616fcf3ce44SJohn Forte 			break;
1617fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
1618fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
1619fcf3ce44SJohn Forte 			break;
1620fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
1621fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
1622fcf3ce44SJohn Forte 			break;
1623fcf3ce44SJohn Forte 		default:
1624fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
1625fcf3ce44SJohn Forte 			    "stmfGetTargetGroupMembers:psGetTargetGroupMembers:"
1626fcf3ce44SJohn Forte 			    "error(%d)", ret);
1627fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
1628fcf3ce44SJohn Forte 			break;
1629fcf3ce44SJohn Forte 	}
1630fcf3ce44SJohn Forte 
1631fcf3ce44SJohn Forte 	return (ret);
1632fcf3ce44SJohn Forte }
1633fcf3ce44SJohn Forte 
1634fcf3ce44SJohn Forte /*
1635fcf3ce44SJohn Forte  * stmfGetTargetList
1636fcf3ce44SJohn Forte  *
1637fcf3ce44SJohn Forte  * Purpose: Retrieves the list of target ports
1638fcf3ce44SJohn Forte  *
1639fcf3ce44SJohn Forte  * targetList - pointer to a pointer to an stmfDevidList structure.
1640fcf3ce44SJohn Forte  *		    On success, it contains the list of local ports (target).
1641fcf3ce44SJohn Forte  */
1642fcf3ce44SJohn Forte int
1643fcf3ce44SJohn Forte stmfGetTargetList(stmfDevidList **targetList)
1644fcf3ce44SJohn Forte {
1645fcf3ce44SJohn Forte 	int ret;
1646fcf3ce44SJohn Forte 	int fd;
1647fcf3ce44SJohn Forte 	int ioctlRet;
1648fcf3ce44SJohn Forte 	int i;
1649fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
1650fcf3ce44SJohn Forte 	/* framework target port list */
1651fcf3ce44SJohn Forte 	slist_target_port_t *fTargetList;
1652fcf3ce44SJohn Forte 	uint32_t fTargetListSize;
1653fcf3ce44SJohn Forte 
1654fcf3ce44SJohn Forte 	if (targetList == NULL) {
1655fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1656fcf3ce44SJohn Forte 	}
1657fcf3ce44SJohn Forte 
1658fcf3ce44SJohn Forte 	/* call init */
1659fcf3ce44SJohn Forte 	ret = initializeConfig();
1660fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
1661fcf3ce44SJohn Forte 		return (ret);
1662fcf3ce44SJohn Forte 	}
1663fcf3ce44SJohn Forte 
1664fcf3ce44SJohn Forte 	/*
1665fcf3ce44SJohn Forte 	 * Open control node for stmf
1666fcf3ce44SJohn Forte 	 */
1667fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
1668fcf3ce44SJohn Forte 		return (ret);
1669fcf3ce44SJohn Forte 
1670fcf3ce44SJohn Forte 	/*
1671fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
1672fcf3ce44SJohn Forte 	 */
1673fcf3ce44SJohn Forte 	fTargetListSize = MAX_TARGET_PORT * sizeof (slist_target_port_t);
1674fcf3ce44SJohn Forte 	fTargetList = (slist_target_port_t *)calloc(1, fTargetListSize);
1675fcf3ce44SJohn Forte 	if (fTargetList == NULL) {
1676fcf3ce44SJohn Forte 		goto done;
1677fcf3ce44SJohn Forte 	}
1678fcf3ce44SJohn Forte 
1679fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
1680fcf3ce44SJohn Forte 	/*
1681fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
1682fcf3ce44SJohn Forte 	 */
1683fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
1684fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fTargetListSize;
1685fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fTargetList;
1686fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_TARGET_PORT_LIST, &stmfIoctl);
1687fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
1688fcf3ce44SJohn Forte 		switch (errno) {
1689fcf3ce44SJohn Forte 			case EBUSY:
1690fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
1691fcf3ce44SJohn Forte 				break;
1692fcf3ce44SJohn Forte 			case EACCES:
1693fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
1694fcf3ce44SJohn Forte 				break;
1695fcf3ce44SJohn Forte 			default:
1696fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
1697fcf3ce44SJohn Forte 				    "stmfGetTargetList:ioctl errno(%d)", errno);
1698fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
1699fcf3ce44SJohn Forte 				break;
1700fcf3ce44SJohn Forte 		}
1701fcf3ce44SJohn Forte 		goto done;
1702fcf3ce44SJohn Forte 	}
1703fcf3ce44SJohn Forte 	/*
1704fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
1705fcf3ce44SJohn Forte 	 */
1706fcf3ce44SJohn Forte 	if (stmfIoctl.stmf_obuf_max_nentries > MAX_TARGET_PORT) {
1707fcf3ce44SJohn Forte 		fTargetListSize = stmfIoctl.stmf_obuf_max_nentries *
1708*76602b8dSJohn Forte 		    sizeof (slist_target_port_t);
1709fcf3ce44SJohn Forte 		fTargetList = realloc(fTargetList, fTargetListSize);
1710fcf3ce44SJohn Forte 		if (fTargetList == NULL) {
1711fcf3ce44SJohn Forte 			return (STMF_ERROR_NOMEM);
1712fcf3ce44SJohn Forte 		}
1713fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fTargetListSize;
1714fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fTargetList;
1715fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, STMF_IOCTL_TARGET_PORT_LIST,
1716fcf3ce44SJohn Forte 		    &stmfIoctl);
1717fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
1718fcf3ce44SJohn Forte 			switch (errno) {
1719fcf3ce44SJohn Forte 				case EBUSY:
1720fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
1721fcf3ce44SJohn Forte 					break;
1722fcf3ce44SJohn Forte 				case EACCES:
1723fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
1724fcf3ce44SJohn Forte 					break;
1725fcf3ce44SJohn Forte 				default:
1726fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
1727fcf3ce44SJohn Forte 					    "stmfGetTargetList:ioctl errno(%d)",
1728fcf3ce44SJohn Forte 					    errno);
1729fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
1730fcf3ce44SJohn Forte 					break;
1731fcf3ce44SJohn Forte 			}
1732fcf3ce44SJohn Forte 			goto done;
1733fcf3ce44SJohn Forte 		}
1734fcf3ce44SJohn Forte 	}
1735fcf3ce44SJohn Forte 
1736fcf3ce44SJohn Forte 	*targetList = (stmfDevidList *)calloc(1,
1737fcf3ce44SJohn Forte 	    stmfIoctl.stmf_obuf_max_nentries * sizeof (stmfDevid) +
1738fcf3ce44SJohn Forte 	    sizeof (stmfDevidList));
1739fcf3ce44SJohn Forte 
1740fcf3ce44SJohn Forte 	(*targetList)->cnt = stmfIoctl.stmf_obuf_max_nentries;
1741fcf3ce44SJohn Forte 	for (i = 0; i < stmfIoctl.stmf_obuf_max_nentries; i++, fTargetList++) {
1742fcf3ce44SJohn Forte 		(*targetList)->devid[i].identLength =
1743fcf3ce44SJohn Forte 		    fTargetList->target[IDENT_LENGTH_BYTE];
1744fcf3ce44SJohn Forte 		bcopy(&fTargetList->target[IDENT_LENGTH_BYTE + 1],
1745fcf3ce44SJohn Forte 		    &(*targetList)->devid[i].ident,
1746fcf3ce44SJohn Forte 		    fTargetList->target[IDENT_LENGTH_BYTE]);
1747fcf3ce44SJohn Forte 	}
1748fcf3ce44SJohn Forte 
1749fcf3ce44SJohn Forte done:
1750fcf3ce44SJohn Forte 	(void) close(fd);
1751fcf3ce44SJohn Forte 	free(fTargetList);
1752fcf3ce44SJohn Forte 	return (ret);
1753fcf3ce44SJohn Forte }
1754fcf3ce44SJohn Forte 
1755fcf3ce44SJohn Forte /*
1756fcf3ce44SJohn Forte  * stmfGetTargetProperties
1757fcf3ce44SJohn Forte  *
1758fcf3ce44SJohn Forte  * Purpose:  Retrieves the properties for a logical unit
1759fcf3ce44SJohn Forte  *
1760fcf3ce44SJohn Forte  * devid - devid of the target for which to retrieve properties
1761fcf3ce44SJohn Forte  * targetProps - pointer to an stmfTargetProperties structure.
1762fcf3ce44SJohn Forte  *		On success, it contains the target properties for
1763fcf3ce44SJohn Forte  *		the specified devid.
1764fcf3ce44SJohn Forte  */
1765fcf3ce44SJohn Forte int
1766fcf3ce44SJohn Forte stmfGetTargetProperties(stmfDevid *devid, stmfTargetProperties *targetProps)
1767fcf3ce44SJohn Forte {
1768fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
1769fcf3ce44SJohn Forte 	int fd;
1770fcf3ce44SJohn Forte 	int ioctlRet;
1771fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
1772fcf3ce44SJohn Forte 	sioc_target_port_props_t targetProperties;
1773fcf3ce44SJohn Forte 
1774fcf3ce44SJohn Forte 	if (devid == NULL || targetProps == NULL) {
1775fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1776fcf3ce44SJohn Forte 	}
1777fcf3ce44SJohn Forte 
1778fcf3ce44SJohn Forte 	/* call init */
1779fcf3ce44SJohn Forte 	ret = initializeConfig();
1780fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
1781fcf3ce44SJohn Forte 		return (ret);
1782fcf3ce44SJohn Forte 	}
1783fcf3ce44SJohn Forte 
1784fcf3ce44SJohn Forte 	/*
1785fcf3ce44SJohn Forte 	 * Open control node for stmf
1786fcf3ce44SJohn Forte 	 */
1787fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
1788fcf3ce44SJohn Forte 		return (ret);
1789fcf3ce44SJohn Forte 
1790fcf3ce44SJohn Forte 	targetProperties.tgt_id[IDENT_LENGTH_BYTE] = devid->identLength;
1791fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetProperties.tgt_id[IDENT_LENGTH_BYTE + 1],
1792fcf3ce44SJohn Forte 	    devid->identLength);
1793fcf3ce44SJohn Forte 
1794fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
1795fcf3ce44SJohn Forte 	/*
1796fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
1797fcf3ce44SJohn Forte 	 */
1798fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
1799fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (targetProperties.tgt_id);
1800fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&targetProperties.tgt_id;
1801fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&targetProperties;
1802fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (targetProperties);
1803fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_GET_TARGET_PORT_PROPERTIES,
1804fcf3ce44SJohn Forte 	    &stmfIoctl);
1805fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
1806fcf3ce44SJohn Forte 		switch (errno) {
1807fcf3ce44SJohn Forte 			case EBUSY:
1808fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
1809fcf3ce44SJohn Forte 				break;
1810fcf3ce44SJohn Forte 			case EACCES:
1811fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
1812fcf3ce44SJohn Forte 				break;
1813fcf3ce44SJohn Forte 			case ENOENT:
1814fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
1815fcf3ce44SJohn Forte 				break;
1816fcf3ce44SJohn Forte 			default:
1817fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
1818fcf3ce44SJohn Forte 				    "stmfGetTargetProperties:ioctl errno(%d)",
1819fcf3ce44SJohn Forte 				    errno);
1820fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
1821fcf3ce44SJohn Forte 				break;
1822fcf3ce44SJohn Forte 		}
1823fcf3ce44SJohn Forte 		goto done;
1824fcf3ce44SJohn Forte 	}
1825fcf3ce44SJohn Forte 
1826fcf3ce44SJohn Forte 	bcopy(targetProperties.tgt_provider_name, targetProps->providerName,
1827fcf3ce44SJohn Forte 	    sizeof (targetProperties.tgt_provider_name));
1828fcf3ce44SJohn Forte 	if (targetProperties.tgt_state == STMF_STATE_ONLINE) {
1829fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_ONLINE;
1830fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_OFFLINE) {
1831fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_OFFLINE;
1832fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_ONLINING) {
1833fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_ONLINING;
1834fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_OFFLINING) {
1835fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_OFFLINING;
1836fcf3ce44SJohn Forte 	}
1837fcf3ce44SJohn Forte 	bcopy(targetProperties.tgt_alias, targetProps->alias,
1838fcf3ce44SJohn Forte 	    sizeof (targetProps->alias));
1839fcf3ce44SJohn Forte done:
1840fcf3ce44SJohn Forte 	(void) close(fd);
1841fcf3ce44SJohn Forte 	return (ret);
1842fcf3ce44SJohn Forte }
1843fcf3ce44SJohn Forte 
1844fcf3ce44SJohn Forte /*
1845fcf3ce44SJohn Forte  * stmfGetLogicalUnitList
1846fcf3ce44SJohn Forte  *
1847fcf3ce44SJohn Forte  * Purpose: Retrieves list of logical unit Object IDs
1848fcf3ce44SJohn Forte  *
1849fcf3ce44SJohn Forte  * luList - pointer to a pointer to a stmfGuidList structure. On success,
1850fcf3ce44SJohn Forte  *          it contains the list of logical unit guids.
1851fcf3ce44SJohn Forte  *
1852fcf3ce44SJohn Forte  */
1853fcf3ce44SJohn Forte int
1854fcf3ce44SJohn Forte stmfGetLogicalUnitList(stmfGuidList **luList)
1855fcf3ce44SJohn Forte {
1856fcf3ce44SJohn Forte 	int ret;
1857fcf3ce44SJohn Forte 	int fd;
1858fcf3ce44SJohn Forte 	int ioctlRet;
1859fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_LU_LIST;
1860fcf3ce44SJohn Forte 	int i, k;
1861fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
1862fcf3ce44SJohn Forte 	/* framework lu list */
1863fcf3ce44SJohn Forte 	slist_lu_t *fLuList;
1864fcf3ce44SJohn Forte 	/* persistent store lu list */
1865fcf3ce44SJohn Forte 	stmfGuidList *sLuList = NULL;
1866fcf3ce44SJohn Forte 	int finalListSize = 0;
1867fcf3ce44SJohn Forte 	int newAllocSize;
1868fcf3ce44SJohn Forte 	uint32_t fLuListSize;
1869fcf3ce44SJohn Forte 	uint32_t endList;
1870fcf3ce44SJohn Forte 
1871fcf3ce44SJohn Forte 	if (luList == NULL) {
1872fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
1873fcf3ce44SJohn Forte 	}
1874fcf3ce44SJohn Forte 
1875fcf3ce44SJohn Forte 	/* call init */
1876fcf3ce44SJohn Forte 	ret = initializeConfig();
1877fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
1878fcf3ce44SJohn Forte 		return (ret);
1879fcf3ce44SJohn Forte 	}
1880fcf3ce44SJohn Forte 
1881fcf3ce44SJohn Forte 	/*
1882fcf3ce44SJohn Forte 	 * Open control node for stmf
1883fcf3ce44SJohn Forte 	 */
1884fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
1885fcf3ce44SJohn Forte 		return (ret);
1886fcf3ce44SJohn Forte 
1887fcf3ce44SJohn Forte 	/*
1888fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
1889fcf3ce44SJohn Forte 	 */
1890fcf3ce44SJohn Forte 	fLuListSize = MAX_LU;
1891fcf3ce44SJohn Forte 	fLuListSize = fLuListSize * (sizeof (slist_lu_t));
1892fcf3ce44SJohn Forte 	fLuList = (slist_lu_t *)calloc(1, fLuListSize);
1893fcf3ce44SJohn Forte 	if (fLuList == NULL) {
1894fcf3ce44SJohn Forte 		return (STMF_ERROR_NOMEM);
1895fcf3ce44SJohn Forte 	}
1896fcf3ce44SJohn Forte 
1897fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
1898fcf3ce44SJohn Forte 	/*
1899fcf3ce44SJohn Forte 	 * Issue ioctl to get the LU list
1900fcf3ce44SJohn Forte 	 */
1901fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
1902fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fLuListSize;
1903fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fLuList;
1904fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
1905fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
1906fcf3ce44SJohn Forte 		switch (errno) {
1907fcf3ce44SJohn Forte 			case EBUSY:
1908fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
1909fcf3ce44SJohn Forte 				break;
1910fcf3ce44SJohn Forte 			case EACCES:
1911fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
1912fcf3ce44SJohn Forte 				break;
1913fcf3ce44SJohn Forte 			default:
1914fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
1915fcf3ce44SJohn Forte 				    "stmfGetLogicalUnitList:ioctl errno(%d)",
1916fcf3ce44SJohn Forte 				    errno);
1917fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
1918fcf3ce44SJohn Forte 				break;
1919fcf3ce44SJohn Forte 		}
1920fcf3ce44SJohn Forte 		goto done;
1921fcf3ce44SJohn Forte 	}
1922fcf3ce44SJohn Forte 	/*
1923fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
1924fcf3ce44SJohn Forte 	 */
1925fcf3ce44SJohn Forte 	if (stmfIoctl.stmf_obuf_max_nentries > MAX_LU) {
1926fcf3ce44SJohn Forte 		fLuListSize = stmfIoctl.stmf_obuf_max_nentries *
1927fcf3ce44SJohn Forte 		    sizeof (slist_lu_t);
1928fcf3ce44SJohn Forte 		fLuList = realloc(fLuList, fLuListSize);
1929fcf3ce44SJohn Forte 		if (fLuList == NULL) {
1930fcf3ce44SJohn Forte 			return (STMF_ERROR_NOMEM);
1931fcf3ce44SJohn Forte 		}
1932fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fLuListSize;
1933fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fLuList;
1934fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
1935fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
1936fcf3ce44SJohn Forte 			switch (errno) {
1937fcf3ce44SJohn Forte 				case EBUSY:
1938fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
1939fcf3ce44SJohn Forte 					break;
1940fcf3ce44SJohn Forte 				case EACCES:
1941fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
1942fcf3ce44SJohn Forte 					break;
1943fcf3ce44SJohn Forte 				default:
1944fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
1945fcf3ce44SJohn Forte 					    "stmfGetLogicalUnitList:"
1946fcf3ce44SJohn Forte 					    "ioctl errno(%d)", errno);
1947fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
1948fcf3ce44SJohn Forte 					break;
1949fcf3ce44SJohn Forte 			}
1950fcf3ce44SJohn Forte 			goto done;
1951fcf3ce44SJohn Forte 		}
1952fcf3ce44SJohn Forte 	}
1953fcf3ce44SJohn Forte 
1954fcf3ce44SJohn Forte 	ret = psGetLogicalUnitList(&sLuList);
1955fcf3ce44SJohn Forte 	switch (ret) {
1956fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
1957fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
1958fcf3ce44SJohn Forte 			break;
1959fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
1960fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1961fcf3ce44SJohn Forte 			break;
1962fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
1963fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
1964fcf3ce44SJohn Forte 			break;
1965fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
1966fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
1967fcf3ce44SJohn Forte 			break;
1968fcf3ce44SJohn Forte 		default:
1969fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
1970fcf3ce44SJohn Forte 			    "stmfGetLogicalUnitList:psGetLogicalUnitList"
1971fcf3ce44SJohn Forte 			    ":error(%d)", ret);
1972fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
1973fcf3ce44SJohn Forte 			break;
1974fcf3ce44SJohn Forte 	}
1975fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
1976fcf3ce44SJohn Forte 		goto done;
1977fcf3ce44SJohn Forte 	}
1978fcf3ce44SJohn Forte 
1979fcf3ce44SJohn Forte 	/*
1980fcf3ce44SJohn Forte 	 * 2 lists must be merged
1981fcf3ce44SJohn Forte 	 * reallocate the store list to add the list from the
1982fcf3ce44SJohn Forte 	 * framework
1983fcf3ce44SJohn Forte 	 */
1984fcf3ce44SJohn Forte 	newAllocSize = sLuList->cnt * sizeof (stmfGuid) + sizeof (stmfGuidList)
1985fcf3ce44SJohn Forte 	    + stmfIoctl.stmf_obuf_nentries * sizeof (stmfGuid);
1986fcf3ce44SJohn Forte 
1987fcf3ce44SJohn Forte 	sLuList = realloc(sLuList, newAllocSize);
1988fcf3ce44SJohn Forte 	if (sLuList == NULL) {
1989fcf3ce44SJohn Forte 		ret = STMF_ERROR_NOMEM;
1990fcf3ce44SJohn Forte 		goto done;
1991fcf3ce44SJohn Forte 	}
1992fcf3ce44SJohn Forte 
1993fcf3ce44SJohn Forte 	/*
1994fcf3ce44SJohn Forte 	 * add list from ioctl. Start from end of list retrieved from store.
1995fcf3ce44SJohn Forte 	 */
1996fcf3ce44SJohn Forte 	endList = sLuList->cnt + stmfIoctl.stmf_obuf_nentries;
1997fcf3ce44SJohn Forte 	for (k = 0, i = sLuList->cnt; i < endList; i++, k++) {
1998fcf3ce44SJohn Forte 		bcopy(&fLuList[k].lu_guid, sLuList->guid[i].guid,
1999fcf3ce44SJohn Forte 		    sizeof (stmfGuid));
2000fcf3ce44SJohn Forte 	}
2001fcf3ce44SJohn Forte 	sLuList->cnt = endList;
2002fcf3ce44SJohn Forte 
2003fcf3ce44SJohn Forte 	/*
2004fcf3ce44SJohn Forte 	 * sort the list for merging
2005fcf3ce44SJohn Forte 	 */
2006fcf3ce44SJohn Forte 	qsort((void *)&(sLuList->guid[0]), sLuList->cnt,
2007fcf3ce44SJohn Forte 	    sizeof (stmfGuid), guidCompare);
2008fcf3ce44SJohn Forte 
2009fcf3ce44SJohn Forte 	/*
2010fcf3ce44SJohn Forte 	 * get final list count
2011fcf3ce44SJohn Forte 	 */
2012fcf3ce44SJohn Forte 	for (i = 0; i < sLuList->cnt; i++) {
2013fcf3ce44SJohn Forte 		if ((i + 1) <= sLuList->cnt) {
2014fcf3ce44SJohn Forte 			if (bcmp(sLuList->guid[i].guid, sLuList->guid[i+1].guid,
2015fcf3ce44SJohn Forte 			    sizeof (stmfGuid)) == 0) {
2016fcf3ce44SJohn Forte 				continue;
2017fcf3ce44SJohn Forte 			}
2018fcf3ce44SJohn Forte 		}
2019fcf3ce44SJohn Forte 		finalListSize++;
2020fcf3ce44SJohn Forte 	}
2021fcf3ce44SJohn Forte 
2022fcf3ce44SJohn Forte 	/*
2023fcf3ce44SJohn Forte 	 * allocate caller's buffer with the final size
2024fcf3ce44SJohn Forte 	 */
2025fcf3ce44SJohn Forte 	*luList = (stmfGuidList *)calloc(1, sizeof (stmfGuidList) +
2026fcf3ce44SJohn Forte 	    finalListSize * sizeof (stmfGuid));
2027fcf3ce44SJohn Forte 	if (*luList == NULL) {
2028fcf3ce44SJohn Forte 		ret = STMF_ERROR_NOMEM;
2029fcf3ce44SJohn Forte 		goto done;
2030fcf3ce44SJohn Forte 	}
2031fcf3ce44SJohn Forte 
2032fcf3ce44SJohn Forte 	/*
2033fcf3ce44SJohn Forte 	 * copy guids to caller's buffer
2034fcf3ce44SJohn Forte 	 */
2035fcf3ce44SJohn Forte 	for (k = 0, i = 0; i < sLuList->cnt; i++) {
2036fcf3ce44SJohn Forte 		if ((i + 1) <= sLuList->cnt) {
2037fcf3ce44SJohn Forte 			if (bcmp(sLuList->guid[i].guid, sLuList->guid[i+1].guid,
2038fcf3ce44SJohn Forte 			    sizeof (stmfGuid)) == 0) {
2039fcf3ce44SJohn Forte 				continue;
2040fcf3ce44SJohn Forte 			}
2041fcf3ce44SJohn Forte 		}
2042fcf3ce44SJohn Forte 		bcopy(&(sLuList->guid[i].guid), (*luList)->guid[k++].guid,
2043fcf3ce44SJohn Forte 		    sizeof (stmfGuid));
2044fcf3ce44SJohn Forte 	}
2045fcf3ce44SJohn Forte 
2046fcf3ce44SJohn Forte 	(*luList)->cnt = finalListSize;
2047fcf3ce44SJohn Forte 
2048fcf3ce44SJohn Forte done:
2049fcf3ce44SJohn Forte 	(void) close(fd);
2050fcf3ce44SJohn Forte 	/*
2051fcf3ce44SJohn Forte 	 * free internal buffers
2052fcf3ce44SJohn Forte 	 */
2053fcf3ce44SJohn Forte 	free(fLuList);
2054fcf3ce44SJohn Forte 	free(sLuList);
2055fcf3ce44SJohn Forte 	return (ret);
2056fcf3ce44SJohn Forte }
2057fcf3ce44SJohn Forte 
2058fcf3ce44SJohn Forte /*
2059fcf3ce44SJohn Forte  * stmfGetLogicalUnitProperties
2060fcf3ce44SJohn Forte  *
2061fcf3ce44SJohn Forte  * Purpose:  Retrieves the properties for a logical unit
2062fcf3ce44SJohn Forte  *
2063fcf3ce44SJohn Forte  * lu - guid of the logical unit for which to retrieve properties
2064fcf3ce44SJohn Forte  * stmfLuProps - pointer to an stmfLogicalUnitProperties structure. On success,
2065fcf3ce44SJohn Forte  *               it contains the logical unit properties for the specified guid.
2066fcf3ce44SJohn Forte  */
2067fcf3ce44SJohn Forte int
2068fcf3ce44SJohn Forte stmfGetLogicalUnitProperties(stmfGuid *lu, stmfLogicalUnitProperties *luProps)
2069fcf3ce44SJohn Forte {
2070fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
2071fcf3ce44SJohn Forte 	int stmfRet;
2072fcf3ce44SJohn Forte 	int fd;
2073fcf3ce44SJohn Forte 	int ioctlRet;
2074fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_GET_LU_PROPERTIES;
2075fcf3ce44SJohn Forte 	stmfViewEntryList *viewEntryList = NULL;
2076fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
2077fcf3ce44SJohn Forte 	sioc_lu_props_t fLuProps;
2078fcf3ce44SJohn Forte 
2079fcf3ce44SJohn Forte 	if (luProps == NULL || luProps == NULL) {
2080fcf3ce44SJohn Forte 		ret = STMF_ERROR_INVALID_ARG;
2081fcf3ce44SJohn Forte 	}
2082fcf3ce44SJohn Forte 
2083fcf3ce44SJohn Forte 	bzero(luProps, sizeof (stmfLogicalUnitProperties));
2084fcf3ce44SJohn Forte 
2085fcf3ce44SJohn Forte 	/* call init */
2086fcf3ce44SJohn Forte 	ret = initializeConfig();
2087fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2088fcf3ce44SJohn Forte 		return (ret);
2089fcf3ce44SJohn Forte 	}
2090fcf3ce44SJohn Forte 
2091fcf3ce44SJohn Forte 	/*
2092fcf3ce44SJohn Forte 	 * Open control node for stmf
2093fcf3ce44SJohn Forte 	 */
2094fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
2095fcf3ce44SJohn Forte 		return (ret);
2096fcf3ce44SJohn Forte 
2097fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
2098fcf3ce44SJohn Forte 	/*
2099fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
2100fcf3ce44SJohn Forte 	 */
2101fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
2102fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGuid);
2103fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)lu;
2104fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&fLuProps;
2105fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (fLuProps);
2106fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
2107fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
2108fcf3ce44SJohn Forte 		switch (errno) {
2109fcf3ce44SJohn Forte 			case EBUSY:
2110fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
2111fcf3ce44SJohn Forte 				break;
2112fcf3ce44SJohn Forte 			case EACCES:
2113fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
2114fcf3ce44SJohn Forte 				break;
2115fcf3ce44SJohn Forte 			case ENOENT:
2116fcf3ce44SJohn Forte 				stmfRet = stmfGetViewEntryList(lu,
2117fcf3ce44SJohn Forte 				    &viewEntryList);
2118fcf3ce44SJohn Forte 				if (stmfRet == STMF_STATUS_SUCCESS) {
2119fcf3ce44SJohn Forte 					luProps->status =
2120fcf3ce44SJohn Forte 					    STMF_LOGICAL_UNIT_UNREGISTERED;
2121fcf3ce44SJohn Forte 					if (viewEntryList->cnt > 0) {
2122fcf3ce44SJohn Forte 						ret = STMF_STATUS_SUCCESS;
2123fcf3ce44SJohn Forte 					} else {
2124fcf3ce44SJohn Forte 						ret = STMF_ERROR_NOT_FOUND;
2125fcf3ce44SJohn Forte 					}
2126fcf3ce44SJohn Forte 				} else {
2127fcf3ce44SJohn Forte 					ret = STMF_ERROR_NOT_FOUND;
2128fcf3ce44SJohn Forte 				}
2129fcf3ce44SJohn Forte 				stmfFreeMemory(viewEntryList);
2130fcf3ce44SJohn Forte 				break;
2131fcf3ce44SJohn Forte 			default:
2132fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
2133fcf3ce44SJohn Forte 				    "stmfGetLogicalUnit:ioctl errno(%d)",
2134fcf3ce44SJohn Forte 				    errno);
2135fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
2136fcf3ce44SJohn Forte 				break;
2137fcf3ce44SJohn Forte 		}
2138fcf3ce44SJohn Forte 		goto done;
2139fcf3ce44SJohn Forte 	}
2140fcf3ce44SJohn Forte 
2141fcf3ce44SJohn Forte 	bcopy(fLuProps.lu_provider_name, luProps->providerName,
2142fcf3ce44SJohn Forte 	    sizeof (fLuProps.lu_provider_name));
2143fcf3ce44SJohn Forte 	if (fLuProps.lu_state == STMF_STATE_ONLINE) {
2144fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_ONLINE;
2145fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_OFFLINE) {
2146fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_OFFLINE;
2147fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_ONLINING) {
2148fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_ONLINING;
2149fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_OFFLINING) {
2150fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_OFFLINING;
2151fcf3ce44SJohn Forte 	}
2152fcf3ce44SJohn Forte 	bcopy(fLuProps.lu_alias, luProps->alias, sizeof (luProps->alias));
2153fcf3ce44SJohn Forte done:
2154fcf3ce44SJohn Forte 	(void) close(fd);
2155fcf3ce44SJohn Forte 	return (ret);
2156fcf3ce44SJohn Forte }
2157fcf3ce44SJohn Forte 
2158fcf3ce44SJohn Forte /*
2159fcf3ce44SJohn Forte  * stmfGetState
2160fcf3ce44SJohn Forte  *
2161fcf3ce44SJohn Forte  * Purpose: retrieve the current state of the stmf module
2162fcf3ce44SJohn Forte  *
2163fcf3ce44SJohn Forte  * state - pointer to stmfState structure allocated by the caller
2164fcf3ce44SJohn Forte  *         On success, contains the state of stmf
2165fcf3ce44SJohn Forte  */
2166fcf3ce44SJohn Forte int
2167fcf3ce44SJohn Forte stmfGetState(stmfState *state)
2168fcf3ce44SJohn Forte {
2169fcf3ce44SJohn Forte 	int ret;
2170fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
2171fcf3ce44SJohn Forte 
2172fcf3ce44SJohn Forte 	if (state == NULL) {
2173fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2174fcf3ce44SJohn Forte 	}
2175fcf3ce44SJohn Forte 
2176fcf3ce44SJohn Forte 	ret = getStmfState(&iState);
2177fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2178fcf3ce44SJohn Forte 		return (ret);
2179fcf3ce44SJohn Forte 	}
2180fcf3ce44SJohn Forte 	switch (iState.state) {
2181fcf3ce44SJohn Forte 		case STMF_STATE_ONLINE:
2182fcf3ce44SJohn Forte 			state->operationalState =
2183fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_ONLINE;
2184fcf3ce44SJohn Forte 			break;
2185fcf3ce44SJohn Forte 		case STMF_STATE_OFFLINE:
2186fcf3ce44SJohn Forte 			state->operationalState =
2187fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_OFFLINE;
2188fcf3ce44SJohn Forte 			break;
2189fcf3ce44SJohn Forte 		case STMF_STATE_ONLINING:
2190fcf3ce44SJohn Forte 			state->operationalState =
2191fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_ONLINING;
2192fcf3ce44SJohn Forte 			break;
2193fcf3ce44SJohn Forte 		case STMF_STATE_OFFLINING:
2194fcf3ce44SJohn Forte 			state->operationalState =
2195fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_OFFLINING;
2196fcf3ce44SJohn Forte 			break;
2197fcf3ce44SJohn Forte 		default:
2198fcf3ce44SJohn Forte 			state->operationalState =
2199fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_UNKNOWN;
2200fcf3ce44SJohn Forte 			break;
2201fcf3ce44SJohn Forte 	}
2202fcf3ce44SJohn Forte 	switch (iState.config_state) {
2203fcf3ce44SJohn Forte 		case STMF_CONFIG_NONE:
2204fcf3ce44SJohn Forte 			state->configState = STMF_CONFIG_STATE_NONE;
2205fcf3ce44SJohn Forte 			break;
2206fcf3ce44SJohn Forte 		case STMF_CONFIG_INIT:
2207fcf3ce44SJohn Forte 			state->configState = STMF_CONFIG_STATE_INIT;
2208fcf3ce44SJohn Forte 			break;
2209fcf3ce44SJohn Forte 		case STMF_CONFIG_INIT_DONE:
2210fcf3ce44SJohn Forte 			state->configState =
2211fcf3ce44SJohn Forte 			    STMF_CONFIG_STATE_INIT_DONE;
2212fcf3ce44SJohn Forte 			break;
2213fcf3ce44SJohn Forte 		default:
2214fcf3ce44SJohn Forte 			state->configState =
2215fcf3ce44SJohn Forte 			    STMF_CONFIG_STATE_UNKNOWN;
2216fcf3ce44SJohn Forte 			break;
2217fcf3ce44SJohn Forte 	}
2218fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
2219fcf3ce44SJohn Forte }
2220fcf3ce44SJohn Forte 
2221fcf3ce44SJohn Forte /*
2222fcf3ce44SJohn Forte  * stmfGetViewEntryList
2223fcf3ce44SJohn Forte  *
2224fcf3ce44SJohn Forte  * Purpose: Retrieves the list of view entries for the specified
2225fcf3ce44SJohn Forte  *          logical unit.
2226fcf3ce44SJohn Forte  *
2227fcf3ce44SJohn Forte  * lu - the guid of the logical unit for which to retrieve the view entry list
2228fcf3ce44SJohn Forte  * viewEntryList - a pointer to a pointer to a stmfViewEntryList structure. On
2229fcf3ce44SJohn Forte  *                 success, contains the list of view entries.
2230fcf3ce44SJohn Forte  */
2231fcf3ce44SJohn Forte int
2232fcf3ce44SJohn Forte stmfGetViewEntryList(stmfGuid *lu, stmfViewEntryList **viewEntryList)
2233fcf3ce44SJohn Forte {
2234fcf3ce44SJohn Forte 	int ret;
2235fcf3ce44SJohn Forte 
2236fcf3ce44SJohn Forte 	if (lu == NULL || viewEntryList == NULL) {
2237fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2238fcf3ce44SJohn Forte 	}
2239fcf3ce44SJohn Forte 
2240fcf3ce44SJohn Forte 	ret = psGetViewEntryList(lu, viewEntryList);
2241fcf3ce44SJohn Forte 	switch (ret) {
2242fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
2243fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
2244fcf3ce44SJohn Forte 			break;
2245fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
2246fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
2247fcf3ce44SJohn Forte 			break;
2248fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
2249fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
2250fcf3ce44SJohn Forte 			break;
2251fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2252fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
2253fcf3ce44SJohn Forte 			break;
2254fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
2255fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
2256fcf3ce44SJohn Forte 			break;
2257fcf3ce44SJohn Forte 		default:
2258fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
2259fcf3ce44SJohn Forte 			    "stmfGetViewEntryList:error(%d)", ret);
2260fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
2261fcf3ce44SJohn Forte 			break;
2262fcf3ce44SJohn Forte 	}
2263fcf3ce44SJohn Forte 
2264fcf3ce44SJohn Forte 	return (ret);
2265fcf3ce44SJohn Forte }
2266fcf3ce44SJohn Forte 
2267fcf3ce44SJohn Forte /*
2268fcf3ce44SJohn Forte  * loadHostGroups
2269fcf3ce44SJohn Forte  *
2270fcf3ce44SJohn Forte  * Purpose - issues the ioctl to load the host groups into stmf
2271fcf3ce44SJohn Forte  *
2272fcf3ce44SJohn Forte  * fd - file descriptor for the control node of stmf.
2273fcf3ce44SJohn Forte  * groupList - populated host group list
2274fcf3ce44SJohn Forte  */
2275fcf3ce44SJohn Forte static int
2276fcf3ce44SJohn Forte loadHostGroups(int fd, stmfGroupList *groupList)
2277fcf3ce44SJohn Forte {
2278fcf3ce44SJohn Forte 	int i, j;
2279fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
2280fcf3ce44SJohn Forte 	stmfGroupProperties *groupProps = NULL;
2281fcf3ce44SJohn Forte 
2282fcf3ce44SJohn Forte 	for (i = 0; i < groupList->cnt; i++) {
2283fcf3ce44SJohn Forte 		if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_HOST_GROUP,
2284fcf3ce44SJohn Forte 		    &(groupList->name[i]))) != STMF_STATUS_SUCCESS) {
2285fcf3ce44SJohn Forte 			goto out;
2286fcf3ce44SJohn Forte 		}
2287fcf3ce44SJohn Forte 		ret = stmfGetHostGroupMembers(&(groupList->name[i]),
2288fcf3ce44SJohn Forte 		    &groupProps);
2289fcf3ce44SJohn Forte 		for (j = 0; j < groupProps->cnt; j++) {
2290fcf3ce44SJohn Forte 			if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_HG_ENTRY,
2291fcf3ce44SJohn Forte 			    &(groupList->name[i]), &(groupProps->name[j])))
2292fcf3ce44SJohn Forte 			    != STMF_STATUS_SUCCESS) {
2293fcf3ce44SJohn Forte 				goto out;
2294fcf3ce44SJohn Forte 			}
2295fcf3ce44SJohn Forte 		}
2296fcf3ce44SJohn Forte 	}
2297fcf3ce44SJohn Forte 
2298fcf3ce44SJohn Forte 
2299fcf3ce44SJohn Forte out:
2300fcf3ce44SJohn Forte 	stmfFreeMemory(groupProps);
2301fcf3ce44SJohn Forte 	return (ret);
2302fcf3ce44SJohn Forte }
2303fcf3ce44SJohn Forte 
2304fcf3ce44SJohn Forte /*
2305fcf3ce44SJohn Forte  * loadTargetGroups
2306fcf3ce44SJohn Forte  *
2307fcf3ce44SJohn Forte  * Purpose - issues the ioctl to load the target groups into stmf
2308fcf3ce44SJohn Forte  *
2309fcf3ce44SJohn Forte  * fd - file descriptor for the control node of stmf.
2310fcf3ce44SJohn Forte  * groupList - populated target group list.
2311fcf3ce44SJohn Forte  */
2312fcf3ce44SJohn Forte static int
2313fcf3ce44SJohn Forte loadTargetGroups(int fd, stmfGroupList *groupList)
2314fcf3ce44SJohn Forte {
2315fcf3ce44SJohn Forte 	int i, j;
2316fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
2317fcf3ce44SJohn Forte 	stmfGroupProperties *groupProps = NULL;
2318fcf3ce44SJohn Forte 
2319fcf3ce44SJohn Forte 	for (i = 0; i < groupList->cnt; i++) {
2320fcf3ce44SJohn Forte 		if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_TARGET_GROUP,
2321fcf3ce44SJohn Forte 		    &(groupList->name[i]))) != STMF_STATUS_SUCCESS) {
2322fcf3ce44SJohn Forte 			goto out;
2323fcf3ce44SJohn Forte 		}
2324fcf3ce44SJohn Forte 		ret = stmfGetTargetGroupMembers(&(groupList->name[i]),
2325fcf3ce44SJohn Forte 		    &groupProps);
2326fcf3ce44SJohn Forte 		for (j = 0; j < groupProps->cnt; j++) {
2327fcf3ce44SJohn Forte 			if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_TG_ENTRY,
2328fcf3ce44SJohn Forte 			    &(groupList->name[i]), &(groupProps->name[j])))
2329fcf3ce44SJohn Forte 			    != STMF_STATUS_SUCCESS) {
2330fcf3ce44SJohn Forte 				goto out;
2331fcf3ce44SJohn Forte 			}
2332fcf3ce44SJohn Forte 		}
2333fcf3ce44SJohn Forte 	}
2334fcf3ce44SJohn Forte 
2335fcf3ce44SJohn Forte 
2336fcf3ce44SJohn Forte out:
2337fcf3ce44SJohn Forte 	stmfFreeMemory(groupProps);
2338fcf3ce44SJohn Forte 	return (ret);
2339fcf3ce44SJohn Forte }
2340fcf3ce44SJohn Forte 
2341fcf3ce44SJohn Forte 
2342fcf3ce44SJohn Forte /*
2343fcf3ce44SJohn Forte  * loadStore
2344fcf3ce44SJohn Forte  *
2345fcf3ce44SJohn Forte  * Purpose: Load the configuration data from the store
2346fcf3ce44SJohn Forte  *
2347fcf3ce44SJohn Forte  * First load the host groups and target groups, then the view entries
2348fcf3ce44SJohn Forte  * and finally the provider data
2349fcf3ce44SJohn Forte  *
2350fcf3ce44SJohn Forte  * fd - file descriptor of control node for stmf.
2351fcf3ce44SJohn Forte  */
2352fcf3ce44SJohn Forte static int
2353fcf3ce44SJohn Forte loadStore(int fd)
2354fcf3ce44SJohn Forte {
2355fcf3ce44SJohn Forte 	int ret;
2356fcf3ce44SJohn Forte 	int i, j;
2357fcf3ce44SJohn Forte 	stmfGroupList *groupList = NULL;
2358fcf3ce44SJohn Forte 	stmfGuidList *guidList = NULL;
2359fcf3ce44SJohn Forte 	stmfViewEntryList *viewEntryList = NULL;
2360fcf3ce44SJohn Forte 	stmfProviderList *providerList = NULL;
2361fcf3ce44SJohn Forte 	int providerType;
2362fcf3ce44SJohn Forte 	nvlist_t *nvl = NULL;
2363fcf3ce44SJohn Forte 
2364fcf3ce44SJohn Forte 
2365fcf3ce44SJohn Forte 
2366fcf3ce44SJohn Forte 	/* load host groups */
2367fcf3ce44SJohn Forte 	ret = stmfGetHostGroupList(&groupList);
2368fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2369fcf3ce44SJohn Forte 		return (ret);
2370fcf3ce44SJohn Forte 	}
2371fcf3ce44SJohn Forte 	ret = loadHostGroups(fd, groupList);
2372fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2373fcf3ce44SJohn Forte 		goto out;
2374fcf3ce44SJohn Forte 	}
2375fcf3ce44SJohn Forte 
2376fcf3ce44SJohn Forte 	stmfFreeMemory(groupList);
2377fcf3ce44SJohn Forte 	groupList = NULL;
2378fcf3ce44SJohn Forte 
2379fcf3ce44SJohn Forte 	/* load target groups */
2380fcf3ce44SJohn Forte 	ret = stmfGetTargetGroupList(&groupList);
2381fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2382fcf3ce44SJohn Forte 		goto out;
2383fcf3ce44SJohn Forte 	}
2384fcf3ce44SJohn Forte 	ret = loadTargetGroups(fd, groupList);
2385fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2386fcf3ce44SJohn Forte 		goto out;
2387fcf3ce44SJohn Forte 	}
2388fcf3ce44SJohn Forte 
2389fcf3ce44SJohn Forte 	stmfFreeMemory(groupList);
2390fcf3ce44SJohn Forte 	groupList = NULL;
2391fcf3ce44SJohn Forte 
2392fcf3ce44SJohn Forte 	/* Get the guid list */
2393fcf3ce44SJohn Forte 	ret = psGetLogicalUnitList(&guidList);
2394fcf3ce44SJohn Forte 	switch (ret) {
2395fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
2396fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
2397fcf3ce44SJohn Forte 			break;
2398fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
2399fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
2400fcf3ce44SJohn Forte 			break;
2401fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
2402fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
2403fcf3ce44SJohn Forte 			break;
2404fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2405fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
2406fcf3ce44SJohn Forte 			break;
2407fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
2408fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
2409fcf3ce44SJohn Forte 			break;
2410fcf3ce44SJohn Forte 		default:
2411fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
2412fcf3ce44SJohn Forte 			break;
2413fcf3ce44SJohn Forte 	}
2414fcf3ce44SJohn Forte 
2415fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2416fcf3ce44SJohn Forte 		goto out;
2417fcf3ce44SJohn Forte 	}
2418fcf3ce44SJohn Forte 
2419fcf3ce44SJohn Forte 	/*
2420fcf3ce44SJohn Forte 	 * We have the guid list, now get the corresponding
2421fcf3ce44SJohn Forte 	 * view entries for each guid
2422fcf3ce44SJohn Forte 	 */
2423fcf3ce44SJohn Forte 	for (i = 0; i < guidList->cnt; i++) {
2424fcf3ce44SJohn Forte 		ret = psGetViewEntryList(&guidList->guid[i], &viewEntryList);
2425fcf3ce44SJohn Forte 		switch (ret) {
2426fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
2427fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
2428fcf3ce44SJohn Forte 				break;
2429fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
2430fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
2431fcf3ce44SJohn Forte 				break;
2432fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
2433fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
2434fcf3ce44SJohn Forte 				break;
2435fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2436fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
2437fcf3ce44SJohn Forte 				break;
2438fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
2439fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
2440fcf3ce44SJohn Forte 				break;
2441fcf3ce44SJohn Forte 			default:
2442fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
2443fcf3ce44SJohn Forte 				break;
2444fcf3ce44SJohn Forte 		}
2445fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
2446fcf3ce44SJohn Forte 			goto out;
2447fcf3ce44SJohn Forte 		}
2448fcf3ce44SJohn Forte 		for (j = 0; j < viewEntryList->cnt; j++) {
2449fcf3ce44SJohn Forte 			ret = addViewEntryIoctl(fd, &guidList->guid[i],
2450fcf3ce44SJohn Forte 			    &viewEntryList->ve[j]);
2451fcf3ce44SJohn Forte 			if (ret != STMF_STATUS_SUCCESS) {
2452fcf3ce44SJohn Forte 				goto out;
2453fcf3ce44SJohn Forte 			}
2454fcf3ce44SJohn Forte 		}
2455fcf3ce44SJohn Forte 	}
2456fcf3ce44SJohn Forte 
2457fcf3ce44SJohn Forte 	/* get the list of providers that have data */
2458fcf3ce44SJohn Forte 	ret = psGetProviderDataList(&providerList);
2459fcf3ce44SJohn Forte 	switch (ret) {
2460fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
2461fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
2462fcf3ce44SJohn Forte 			break;
2463fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
2464fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
2465fcf3ce44SJohn Forte 			break;
2466fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
2467fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
2468fcf3ce44SJohn Forte 			break;
2469fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2470fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
2471fcf3ce44SJohn Forte 			break;
2472fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
2473fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
2474fcf3ce44SJohn Forte 			break;
2475fcf3ce44SJohn Forte 		default:
2476fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
2477fcf3ce44SJohn Forte 			break;
2478fcf3ce44SJohn Forte 	}
2479fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2480fcf3ce44SJohn Forte 		goto out;
2481fcf3ce44SJohn Forte 	}
2482fcf3ce44SJohn Forte 
2483fcf3ce44SJohn Forte 	for (i = 0; i < providerList->cnt; i++) {
2484fcf3ce44SJohn Forte 		providerType = providerList->provider[i].providerType;
2485fcf3ce44SJohn Forte 		ret = psGetProviderData(providerList->provider[i].name,
2486fcf3ce44SJohn Forte 		    &nvl, providerType, NULL);
2487fcf3ce44SJohn Forte 		switch (ret) {
2488fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
2489fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
2490fcf3ce44SJohn Forte 				break;
2491fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
2492fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
2493fcf3ce44SJohn Forte 				break;
2494fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
2495fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
2496fcf3ce44SJohn Forte 				break;
2497fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2498fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
2499fcf3ce44SJohn Forte 				break;
2500fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
2501fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
2502fcf3ce44SJohn Forte 				break;
2503fcf3ce44SJohn Forte 			default:
2504fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
2505fcf3ce44SJohn Forte 				break;
2506fcf3ce44SJohn Forte 		}
2507fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
2508fcf3ce44SJohn Forte 			goto out;
2509fcf3ce44SJohn Forte 		}
2510fcf3ce44SJohn Forte 
2511fcf3ce44SJohn Forte 		/* call setProviderData */
2512fcf3ce44SJohn Forte 		ret = setProviderData(fd, providerList->provider[i].name, nvl,
2513fcf3ce44SJohn Forte 		    providerType);
2514fcf3ce44SJohn Forte 		switch (ret) {
2515fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
2516fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
2517fcf3ce44SJohn Forte 				break;
2518fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
2519fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
2520fcf3ce44SJohn Forte 				break;
2521fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
2522fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
2523fcf3ce44SJohn Forte 				break;
2524fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2525fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
2526fcf3ce44SJohn Forte 				break;
2527fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
2528fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
2529fcf3ce44SJohn Forte 				break;
2530fcf3ce44SJohn Forte 			default:
2531fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
2532fcf3ce44SJohn Forte 				break;
2533fcf3ce44SJohn Forte 		}
2534fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
2535fcf3ce44SJohn Forte 			goto out;
2536fcf3ce44SJohn Forte 		}
2537fcf3ce44SJohn Forte 
2538fcf3ce44SJohn Forte 		nvlist_free(nvl);
2539fcf3ce44SJohn Forte 		nvl = NULL;
2540fcf3ce44SJohn Forte 	}
2541fcf3ce44SJohn Forte out:
2542fcf3ce44SJohn Forte 	if (groupList != NULL) {
2543fcf3ce44SJohn Forte 		free(groupList);
2544fcf3ce44SJohn Forte 	}
2545fcf3ce44SJohn Forte 	if (guidList != NULL) {
2546fcf3ce44SJohn Forte 		free(guidList);
2547fcf3ce44SJohn Forte 	}
2548fcf3ce44SJohn Forte 	if (viewEntryList != NULL) {
2549fcf3ce44SJohn Forte 		free(viewEntryList);
2550fcf3ce44SJohn Forte 	}
2551fcf3ce44SJohn Forte 	if (nvl != NULL) {
2552fcf3ce44SJohn Forte 		nvlist_free(nvl);
2553fcf3ce44SJohn Forte 	}
2554fcf3ce44SJohn Forte 	return (ret);
2555fcf3ce44SJohn Forte }
2556fcf3ce44SJohn Forte 
2557fcf3ce44SJohn Forte /*
2558fcf3ce44SJohn Forte  * stmfLoadConfig
2559fcf3ce44SJohn Forte  *
2560fcf3ce44SJohn Forte  * Purpose - load the configuration data from smf into stmf
2561fcf3ce44SJohn Forte  *
2562fcf3ce44SJohn Forte  */
2563fcf3ce44SJohn Forte int
2564fcf3ce44SJohn Forte stmfLoadConfig(void)
2565fcf3ce44SJohn Forte {
2566fcf3ce44SJohn Forte 	int ret;
2567fcf3ce44SJohn Forte 	int fd;
2568fcf3ce44SJohn Forte 	stmf_state_desc_t stmfStateSet;
2569fcf3ce44SJohn Forte 	stmfState state;
2570fcf3ce44SJohn Forte 
2571fcf3ce44SJohn Forte 
2572fcf3ce44SJohn Forte 	/* Check to ensure service exists */
2573fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
2574fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
2575fcf3ce44SJohn Forte 	}
2576fcf3ce44SJohn Forte 
2577fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
2578fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
2579fcf3ce44SJohn Forte 		if (state.operationalState != STMF_SERVICE_STATE_OFFLINE) {
2580fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_ONLINE);
2581fcf3ce44SJohn Forte 		}
2582fcf3ce44SJohn Forte 	} else {
2583fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
2584fcf3ce44SJohn Forte 	}
2585fcf3ce44SJohn Forte 
2586fcf3ce44SJohn Forte 
2587fcf3ce44SJohn Forte 	stmfStateSet.state = STMF_STATE_OFFLINE;
2588fcf3ce44SJohn Forte 	stmfStateSet.config_state = STMF_CONFIG_INIT;
2589fcf3ce44SJohn Forte 
2590fcf3ce44SJohn Forte 	/*
2591fcf3ce44SJohn Forte 	 * Open control node for stmf
2592fcf3ce44SJohn Forte 	 */
2593fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
2594fcf3ce44SJohn Forte 		return (ret);
2595fcf3ce44SJohn Forte 
2596fcf3ce44SJohn Forte 	ret = setStmfState(fd, &stmfStateSet, STMF_SERVICE_TYPE);
2597fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2598fcf3ce44SJohn Forte 		goto done;
2599fcf3ce44SJohn Forte 	}
2600fcf3ce44SJohn Forte 
2601fcf3ce44SJohn Forte 	/* Load the persistent configuration data */
2602fcf3ce44SJohn Forte 	ret = loadStore(fd);
2603fcf3ce44SJohn Forte 	if (ret != 0) {
2604fcf3ce44SJohn Forte 		goto done;
2605fcf3ce44SJohn Forte 	}
2606fcf3ce44SJohn Forte 
2607fcf3ce44SJohn Forte 	stmfStateSet.state = STMF_STATE_OFFLINE;
2608fcf3ce44SJohn Forte 	stmfStateSet.config_state = STMF_CONFIG_INIT_DONE;
2609fcf3ce44SJohn Forte 
2610fcf3ce44SJohn Forte done:
2611fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
2612fcf3ce44SJohn Forte 		ret = setStmfState(fd, &stmfStateSet, STMF_SERVICE_TYPE);
2613fcf3ce44SJohn Forte 	}
2614fcf3ce44SJohn Forte 	(void) close(fd);
2615fcf3ce44SJohn Forte 	return (ret);
2616fcf3ce44SJohn Forte }
2617fcf3ce44SJohn Forte 
2618fcf3ce44SJohn Forte /*
2619fcf3ce44SJohn Forte  * getStmfState
2620fcf3ce44SJohn Forte  *
2621fcf3ce44SJohn Forte  * stmfState - pointer to stmf_state_desc_t structure. Will contain the state
2622fcf3ce44SJohn Forte  *             information of the stmf service on success.
2623fcf3ce44SJohn Forte  */
2624fcf3ce44SJohn Forte static int
2625fcf3ce44SJohn Forte getStmfState(stmf_state_desc_t *stmfState)
2626fcf3ce44SJohn Forte {
2627fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
2628fcf3ce44SJohn Forte 	int fd;
2629fcf3ce44SJohn Forte 	int ioctlRet;
2630fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
2631fcf3ce44SJohn Forte 
2632fcf3ce44SJohn Forte 	/*
2633fcf3ce44SJohn Forte 	 * Open control node for stmf
2634fcf3ce44SJohn Forte 	 */
2635fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
2636fcf3ce44SJohn Forte 		return (ret);
2637fcf3ce44SJohn Forte 
2638fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
2639fcf3ce44SJohn Forte 	/*
2640fcf3ce44SJohn Forte 	 * Issue ioctl to get the stmf state
2641fcf3ce44SJohn Forte 	 */
2642fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
2643fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_state_desc_t);
2644fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)stmfState;
2645fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (stmf_state_desc_t);
2646fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)stmfState;
2647fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_GET_STMF_STATE, &stmfIoctl);
2648fcf3ce44SJohn Forte 
2649fcf3ce44SJohn Forte 	(void) close(fd);
2650fcf3ce44SJohn Forte 
2651fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
2652fcf3ce44SJohn Forte 		switch (errno) {
2653fcf3ce44SJohn Forte 			case EBUSY:
2654fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
2655fcf3ce44SJohn Forte 				break;
2656fcf3ce44SJohn Forte 			case EPERM:
2657fcf3ce44SJohn Forte 			case EACCES:
2658fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
2659fcf3ce44SJohn Forte 				break;
2660fcf3ce44SJohn Forte 			default:
2661fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
2662fcf3ce44SJohn Forte 				    "getStmfState:ioctl errno(%d)", errno);
2663fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
2664fcf3ce44SJohn Forte 				break;
2665fcf3ce44SJohn Forte 		}
2666fcf3ce44SJohn Forte 	}
2667fcf3ce44SJohn Forte 	return (ret);
2668fcf3ce44SJohn Forte }
2669fcf3ce44SJohn Forte 
2670fcf3ce44SJohn Forte 
2671fcf3ce44SJohn Forte /*
2672fcf3ce44SJohn Forte  * setStmfState
2673fcf3ce44SJohn Forte  *
2674fcf3ce44SJohn Forte  * stmfState - pointer to caller set state structure
2675fcf3ce44SJohn Forte  * objectType - one of:
2676fcf3ce44SJohn Forte  *		LOGICAL_UNIT_TYPE
2677fcf3ce44SJohn Forte  *		TARGET_TYPE
2678fcf3ce44SJohn Forte  *		STMF_SERVICE_TYPE
2679fcf3ce44SJohn Forte  */
2680fcf3ce44SJohn Forte static int
2681fcf3ce44SJohn Forte setStmfState(int fd, stmf_state_desc_t *stmfState, int objectType)
2682fcf3ce44SJohn Forte {
2683fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
2684fcf3ce44SJohn Forte 	int ioctlRet;
2685fcf3ce44SJohn Forte 	int cmd;
2686fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
2687fcf3ce44SJohn Forte 
2688fcf3ce44SJohn Forte 	switch (objectType) {
2689fcf3ce44SJohn Forte 		case LOGICAL_UNIT_TYPE:
2690fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_LU_STATE;
2691fcf3ce44SJohn Forte 			break;
2692fcf3ce44SJohn Forte 		case TARGET_TYPE:
2693fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_TARGET_PORT_STATE;
2694fcf3ce44SJohn Forte 			break;
2695fcf3ce44SJohn Forte 		case STMF_SERVICE_TYPE:
2696fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_STMF_STATE;
2697fcf3ce44SJohn Forte 			break;
2698fcf3ce44SJohn Forte 		default:
2699fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
2700fcf3ce44SJohn Forte 			goto done;
2701fcf3ce44SJohn Forte 	}
2702fcf3ce44SJohn Forte 
2703fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
2704fcf3ce44SJohn Forte 	/*
2705fcf3ce44SJohn Forte 	 * Issue ioctl to set the stmf state
2706fcf3ce44SJohn Forte 	 */
2707fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
2708fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_state_desc_t);
2709fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)stmfState;
2710fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
2711fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
2712fcf3ce44SJohn Forte 		switch (errno) {
2713fcf3ce44SJohn Forte 			case EBUSY:
2714fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
2715fcf3ce44SJohn Forte 				break;
2716fcf3ce44SJohn Forte 			case EACCES:
2717fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
2718fcf3ce44SJohn Forte 				break;
2719fcf3ce44SJohn Forte 			case ENOENT:
2720fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
2721fcf3ce44SJohn Forte 				break;
2722fcf3ce44SJohn Forte 			default:
2723fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
2724fcf3ce44SJohn Forte 				    "setStmfState:ioctl errno(%d)", errno);
2725fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
2726fcf3ce44SJohn Forte 				break;
2727fcf3ce44SJohn Forte 		}
2728fcf3ce44SJohn Forte 	}
2729fcf3ce44SJohn Forte done:
2730fcf3ce44SJohn Forte 	return (ret);
2731fcf3ce44SJohn Forte }
2732fcf3ce44SJohn Forte 
2733fcf3ce44SJohn Forte /*
2734fcf3ce44SJohn Forte  * stmfOnline
2735fcf3ce44SJohn Forte  *
2736fcf3ce44SJohn Forte  * Purpose: Online stmf service
2737fcf3ce44SJohn Forte  *
2738fcf3ce44SJohn Forte  */
2739fcf3ce44SJohn Forte int
2740fcf3ce44SJohn Forte stmfOnline(void)
2741fcf3ce44SJohn Forte {
2742fcf3ce44SJohn Forte 	int ret;
2743fcf3ce44SJohn Forte 	int fd;
2744fcf3ce44SJohn Forte 	stmfState state;
2745fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
2746fcf3ce44SJohn Forte 
2747fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
2748fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
2749fcf3ce44SJohn Forte 		if (state.operationalState == STMF_SERVICE_STATE_ONLINE) {
2750fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_ONLINE);
2751fcf3ce44SJohn Forte 		}
2752fcf3ce44SJohn Forte 	} else {
2753fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
2754fcf3ce44SJohn Forte 	}
2755fcf3ce44SJohn Forte 	iState.state = STMF_STATE_ONLINE;
2756fcf3ce44SJohn Forte 	iState.config_state = STMF_CONFIG_NONE;
2757fcf3ce44SJohn Forte 	/*
2758fcf3ce44SJohn Forte 	 * Open control node for stmf
2759fcf3ce44SJohn Forte 	 * to make call to setStmfState()
2760fcf3ce44SJohn Forte 	 */
2761fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
2762fcf3ce44SJohn Forte 		return (ret);
2763fcf3ce44SJohn Forte 	ret = setStmfState(fd, &iState, STMF_SERVICE_TYPE);
2764fcf3ce44SJohn Forte 	(void) close(fd);
2765fcf3ce44SJohn Forte 	return (ret);
2766fcf3ce44SJohn Forte }
2767fcf3ce44SJohn Forte 
2768fcf3ce44SJohn Forte /*
2769fcf3ce44SJohn Forte  * stmfOffline
2770fcf3ce44SJohn Forte  *
2771fcf3ce44SJohn Forte  * Purpose: Offline stmf service
2772fcf3ce44SJohn Forte  *
2773fcf3ce44SJohn Forte  */
2774fcf3ce44SJohn Forte int
2775fcf3ce44SJohn Forte stmfOffline(void)
2776fcf3ce44SJohn Forte {
2777fcf3ce44SJohn Forte 	int ret;
2778fcf3ce44SJohn Forte 	int fd;
2779fcf3ce44SJohn Forte 	stmfState state;
2780fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
2781fcf3ce44SJohn Forte 
2782fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
2783fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
2784fcf3ce44SJohn Forte 		if (state.operationalState == STMF_SERVICE_STATE_OFFLINE) {
2785fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_OFFLINE);
2786fcf3ce44SJohn Forte 		}
2787fcf3ce44SJohn Forte 	} else {
2788fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
2789fcf3ce44SJohn Forte 	}
2790fcf3ce44SJohn Forte 	iState.state = STMF_STATE_OFFLINE;
2791fcf3ce44SJohn Forte 	iState.config_state = STMF_CONFIG_NONE;
2792fcf3ce44SJohn Forte 
2793fcf3ce44SJohn Forte 	/*
2794fcf3ce44SJohn Forte 	 * Open control node for stmf
2795fcf3ce44SJohn Forte 	 * to make call to setStmfState()
2796fcf3ce44SJohn Forte 	 */
2797fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
2798fcf3ce44SJohn Forte 		return (ret);
2799fcf3ce44SJohn Forte 	ret = setStmfState(fd, &iState, STMF_SERVICE_TYPE);
2800fcf3ce44SJohn Forte 	(void) close(fd);
2801fcf3ce44SJohn Forte 	return (ret);
2802fcf3ce44SJohn Forte }
2803fcf3ce44SJohn Forte 
2804fcf3ce44SJohn Forte 
2805fcf3ce44SJohn Forte /*
2806fcf3ce44SJohn Forte  * stmfOfflineTarget
2807fcf3ce44SJohn Forte  *
2808fcf3ce44SJohn Forte  * Purpose: Change state of target to offline
2809fcf3ce44SJohn Forte  *
2810fcf3ce44SJohn Forte  * devid - devid of the target to offline
2811fcf3ce44SJohn Forte  */
2812fcf3ce44SJohn Forte int
2813fcf3ce44SJohn Forte stmfOfflineTarget(stmfDevid *devid)
2814fcf3ce44SJohn Forte {
2815fcf3ce44SJohn Forte 	stmf_state_desc_t targetState;
2816fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
2817fcf3ce44SJohn Forte 	int fd;
2818fcf3ce44SJohn Forte 
2819fcf3ce44SJohn Forte 	if (devid == NULL) {
2820fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2821fcf3ce44SJohn Forte 	}
2822fcf3ce44SJohn Forte 	bzero(&targetState, sizeof (targetState));
2823fcf3ce44SJohn Forte 
2824fcf3ce44SJohn Forte 	targetState.state = STMF_STATE_OFFLINE;
2825fcf3ce44SJohn Forte 	targetState.ident[IDENT_LENGTH_BYTE] = devid->identLength;
2826fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetState.ident[IDENT_LENGTH_BYTE + 1],
2827fcf3ce44SJohn Forte 	    devid->identLength);
2828fcf3ce44SJohn Forte 	/*
2829fcf3ce44SJohn Forte 	 * Open control node for stmf
2830fcf3ce44SJohn Forte 	 * to make call to setStmfState()
2831fcf3ce44SJohn Forte 	 */
2832fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
2833fcf3ce44SJohn Forte 		return (ret);
2834fcf3ce44SJohn Forte 	ret = setStmfState(fd, &targetState, TARGET_TYPE);
2835fcf3ce44SJohn Forte 	(void) close(fd);
2836fcf3ce44SJohn Forte 	return (ret);
2837fcf3ce44SJohn Forte }
2838fcf3ce44SJohn Forte 
2839fcf3ce44SJohn Forte /*
2840fcf3ce44SJohn Forte  * stmfOfflineLogicalUnit
2841fcf3ce44SJohn Forte  *
2842fcf3ce44SJohn Forte  * Purpose: Change state of logical unit to offline
2843fcf3ce44SJohn Forte  *
2844fcf3ce44SJohn Forte  * lu - guid of the logical unit to offline
2845fcf3ce44SJohn Forte  */
2846fcf3ce44SJohn Forte int
2847fcf3ce44SJohn Forte stmfOfflineLogicalUnit(stmfGuid *lu)
2848fcf3ce44SJohn Forte {
2849fcf3ce44SJohn Forte 	stmf_state_desc_t luState;
2850fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
2851fcf3ce44SJohn Forte 	int fd;
2852fcf3ce44SJohn Forte 
2853fcf3ce44SJohn Forte 	if (lu == NULL) {
2854fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2855fcf3ce44SJohn Forte 	}
2856fcf3ce44SJohn Forte 
2857fcf3ce44SJohn Forte 	bzero(&luState, sizeof (luState));
2858fcf3ce44SJohn Forte 
2859fcf3ce44SJohn Forte 	luState.state = STMF_STATE_OFFLINE;
2860fcf3ce44SJohn Forte 	bcopy(lu, &luState.ident, sizeof (stmfGuid));
2861fcf3ce44SJohn Forte 	/*
2862fcf3ce44SJohn Forte 	 * Open control node for stmf
2863fcf3ce44SJohn Forte 	 * to make call to setStmfState()
2864fcf3ce44SJohn Forte 	 */
2865fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
2866fcf3ce44SJohn Forte 		return (ret);
2867fcf3ce44SJohn Forte 	ret = setStmfState(fd, &luState, LOGICAL_UNIT_TYPE);
2868fcf3ce44SJohn Forte 	(void) close(fd);
2869fcf3ce44SJohn Forte 	return (ret);
2870fcf3ce44SJohn Forte }
2871fcf3ce44SJohn Forte 
2872fcf3ce44SJohn Forte /*
2873fcf3ce44SJohn Forte  * stmfOnlineTarget
2874fcf3ce44SJohn Forte  *
2875fcf3ce44SJohn Forte  * Purpose: Change state of target to online
2876fcf3ce44SJohn Forte  *
2877fcf3ce44SJohn Forte  * devid - devid of the target to online
2878fcf3ce44SJohn Forte  */
2879fcf3ce44SJohn Forte int
2880fcf3ce44SJohn Forte stmfOnlineTarget(stmfDevid *devid)
2881fcf3ce44SJohn Forte {
2882fcf3ce44SJohn Forte 	stmf_state_desc_t targetState;
2883fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
2884fcf3ce44SJohn Forte 	int fd;
2885fcf3ce44SJohn Forte 
2886fcf3ce44SJohn Forte 	if (devid == NULL) {
2887fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2888fcf3ce44SJohn Forte 	}
2889fcf3ce44SJohn Forte 	bzero(&targetState, sizeof (targetState));
2890fcf3ce44SJohn Forte 
2891fcf3ce44SJohn Forte 	targetState.state = STMF_STATE_ONLINE;
2892fcf3ce44SJohn Forte 	targetState.ident[IDENT_LENGTH_BYTE] = devid->identLength;
2893fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetState.ident[IDENT_LENGTH_BYTE + 1],
2894fcf3ce44SJohn Forte 	    devid->identLength);
2895fcf3ce44SJohn Forte 	/*
2896fcf3ce44SJohn Forte 	 * Open control node for stmf
2897fcf3ce44SJohn Forte 	 * to make call to setStmfState()
2898fcf3ce44SJohn Forte 	 */
2899fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
2900fcf3ce44SJohn Forte 		return (ret);
2901fcf3ce44SJohn Forte 	ret = setStmfState(fd, &targetState, TARGET_TYPE);
2902fcf3ce44SJohn Forte 	(void) close(fd);
2903fcf3ce44SJohn Forte 	return (ret);
2904fcf3ce44SJohn Forte }
2905fcf3ce44SJohn Forte 
2906fcf3ce44SJohn Forte /*
2907fcf3ce44SJohn Forte  * stmfOnlineLogicalUnit
2908fcf3ce44SJohn Forte  *
2909fcf3ce44SJohn Forte  * Purpose: Change state of logical unit to online
2910fcf3ce44SJohn Forte  *
2911fcf3ce44SJohn Forte  * lu - guid of the logical unit to online
2912fcf3ce44SJohn Forte  */
2913fcf3ce44SJohn Forte int
2914fcf3ce44SJohn Forte stmfOnlineLogicalUnit(stmfGuid *lu)
2915fcf3ce44SJohn Forte {
2916fcf3ce44SJohn Forte 	stmf_state_desc_t luState;
2917fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
2918fcf3ce44SJohn Forte 	int fd;
2919fcf3ce44SJohn Forte 
2920fcf3ce44SJohn Forte 	if (lu == NULL) {
2921fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2922fcf3ce44SJohn Forte 	}
2923fcf3ce44SJohn Forte 
2924fcf3ce44SJohn Forte 	bzero(&luState, sizeof (luState));
2925fcf3ce44SJohn Forte 
2926fcf3ce44SJohn Forte 	luState.state = STMF_STATE_ONLINE;
2927fcf3ce44SJohn Forte 	bcopy(lu, &luState.ident, sizeof (stmfGuid));
2928fcf3ce44SJohn Forte 	/*
2929fcf3ce44SJohn Forte 	 * Open control node for stmf
2930fcf3ce44SJohn Forte 	 * to make call to setStmfState()
2931fcf3ce44SJohn Forte 	 */
2932fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
2933fcf3ce44SJohn Forte 		return (ret);
2934fcf3ce44SJohn Forte 	ret = setStmfState(fd, &luState, LOGICAL_UNIT_TYPE);
2935fcf3ce44SJohn Forte 	(void) close(fd);
2936fcf3ce44SJohn Forte 	return (ret);
2937fcf3ce44SJohn Forte }
2938fcf3ce44SJohn Forte 
2939fcf3ce44SJohn Forte /*
2940fcf3ce44SJohn Forte  * stmfRemoveFromHostGroup
2941fcf3ce44SJohn Forte  *
2942fcf3ce44SJohn Forte  * Purpose: Removes an initiator from an initiator group
2943fcf3ce44SJohn Forte  *
2944fcf3ce44SJohn Forte  * hostGroupName - name of an initiator group
2945fcf3ce44SJohn Forte  * hostName - name of host group member to remove
2946fcf3ce44SJohn Forte  */
2947fcf3ce44SJohn Forte int
2948fcf3ce44SJohn Forte stmfRemoveFromHostGroup(stmfGroupName *hostGroupName, stmfDevid *hostName)
2949fcf3ce44SJohn Forte {
2950fcf3ce44SJohn Forte 	int ret;
2951fcf3ce44SJohn Forte 	int fd;
2952fcf3ce44SJohn Forte 
2953fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
2954fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
2955fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || hostName == NULL) {
2956fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2957fcf3ce44SJohn Forte 	}
2958fcf3ce44SJohn Forte 
2959fcf3ce44SJohn Forte 	/* call init */
2960fcf3ce44SJohn Forte 	ret = initializeConfig();
2961fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2962fcf3ce44SJohn Forte 		return (ret);
2963fcf3ce44SJohn Forte 	}
2964fcf3ce44SJohn Forte 
2965fcf3ce44SJohn Forte 	/*
2966fcf3ce44SJohn Forte 	 * Open control node for stmf
2967fcf3ce44SJohn Forte 	 */
2968fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
2969fcf3ce44SJohn Forte 		return (ret);
2970fcf3ce44SJohn Forte 
2971fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_REMOVE_HG_ENTRY,
2972fcf3ce44SJohn Forte 	    hostGroupName, hostName)) != STMF_STATUS_SUCCESS) {
2973fcf3ce44SJohn Forte 		goto done;
2974fcf3ce44SJohn Forte 	}
2975fcf3ce44SJohn Forte 
2976fcf3ce44SJohn Forte 	ret = psRemoveHostGroupMember((char *)hostGroupName,
2977fcf3ce44SJohn Forte 	    (char *)hostName->ident);
2978fcf3ce44SJohn Forte 	switch (ret) {
2979fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
2980fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
2981fcf3ce44SJohn Forte 			break;
2982fcf3ce44SJohn Forte 		case STMF_PS_ERROR_MEMBER_NOT_FOUND:
2983fcf3ce44SJohn Forte 			ret = STMF_ERROR_MEMBER_NOT_FOUND;
2984fcf3ce44SJohn Forte 			break;
2985fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
2986fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
2987fcf3ce44SJohn Forte 			break;
2988fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
2989fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
2990fcf3ce44SJohn Forte 			break;
2991fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2992fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
2993fcf3ce44SJohn Forte 			break;
2994fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
2995fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
2996fcf3ce44SJohn Forte 			break;
2997fcf3ce44SJohn Forte 		default:
2998fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
2999fcf3ce44SJohn Forte 			    "stmfRemoveFromHostGroup"
3000fcf3ce44SJohn Forte 			    "psRemoveHostGroupMember:error(%d)", ret);
3001fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3002fcf3ce44SJohn Forte 			break;
3003fcf3ce44SJohn Forte 	}
3004fcf3ce44SJohn Forte 
3005fcf3ce44SJohn Forte done:
3006fcf3ce44SJohn Forte 	(void) close(fd);
3007fcf3ce44SJohn Forte 	return (ret);
3008fcf3ce44SJohn Forte }
3009fcf3ce44SJohn Forte 
3010fcf3ce44SJohn Forte /*
3011fcf3ce44SJohn Forte  * stmfRemoveFromTargetGroup
3012fcf3ce44SJohn Forte  *
3013fcf3ce44SJohn Forte  * Purpose: Removes a local port from a local port group
3014fcf3ce44SJohn Forte  *
3015fcf3ce44SJohn Forte  * targetGroupName - name of a target group
3016fcf3ce44SJohn Forte  * targetName - name of target to remove
3017fcf3ce44SJohn Forte  */
3018fcf3ce44SJohn Forte int
3019fcf3ce44SJohn Forte stmfRemoveFromTargetGroup(stmfGroupName *targetGroupName, stmfDevid *targetName)
3020fcf3ce44SJohn Forte {
3021fcf3ce44SJohn Forte 	int ret;
3022fcf3ce44SJohn Forte 	int fd;
3023fcf3ce44SJohn Forte 
3024fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
3025fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
3026fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || targetName == NULL) {
3027fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3028fcf3ce44SJohn Forte 	}
3029fcf3ce44SJohn Forte 
3030fcf3ce44SJohn Forte 	/* call init */
3031fcf3ce44SJohn Forte 	ret = initializeConfig();
3032fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3033fcf3ce44SJohn Forte 		return (ret);
3034fcf3ce44SJohn Forte 	}
3035fcf3ce44SJohn Forte 
3036fcf3ce44SJohn Forte 	/*
3037fcf3ce44SJohn Forte 	 * Open control node for stmf
3038fcf3ce44SJohn Forte 	 */
3039fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3040fcf3ce44SJohn Forte 		return (ret);
3041fcf3ce44SJohn Forte 
3042fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_REMOVE_TG_ENTRY,
3043fcf3ce44SJohn Forte 	    targetGroupName, targetName)) != STMF_STATUS_SUCCESS) {
3044fcf3ce44SJohn Forte 		goto done;
3045fcf3ce44SJohn Forte 	}
3046fcf3ce44SJohn Forte 
3047fcf3ce44SJohn Forte 	ret = psRemoveTargetGroupMember((char *)targetGroupName,
3048fcf3ce44SJohn Forte 	    (char *)targetName->ident);
3049fcf3ce44SJohn Forte 	switch (ret) {
3050fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3051fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3052fcf3ce44SJohn Forte 			break;
3053fcf3ce44SJohn Forte 		case STMF_PS_ERROR_MEMBER_NOT_FOUND:
3054fcf3ce44SJohn Forte 			ret = STMF_ERROR_MEMBER_NOT_FOUND;
3055fcf3ce44SJohn Forte 			break;
3056fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
3057fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
3058fcf3ce44SJohn Forte 			break;
3059fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3060fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3061fcf3ce44SJohn Forte 			break;
3062fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3063fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3064fcf3ce44SJohn Forte 			break;
3065fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3066fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3067fcf3ce44SJohn Forte 			break;
3068fcf3ce44SJohn Forte 		default:
3069fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3070fcf3ce44SJohn Forte 			    "stmfRemoveFromTargetGroup"
3071fcf3ce44SJohn Forte 			    "psRemoveTargetGroupMember:error(%d)", ret);
3072fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3073fcf3ce44SJohn Forte 			break;
3074fcf3ce44SJohn Forte 	}
3075fcf3ce44SJohn Forte 
3076fcf3ce44SJohn Forte done:
3077fcf3ce44SJohn Forte 	(void) close(fd);
3078fcf3ce44SJohn Forte 	return (ret);
3079fcf3ce44SJohn Forte }
3080fcf3ce44SJohn Forte 
3081fcf3ce44SJohn Forte /*
3082fcf3ce44SJohn Forte  * stmfRemoveViewEntry
3083fcf3ce44SJohn Forte  *
3084fcf3ce44SJohn Forte  * Purpose: Removes a view entry from a logical unit
3085fcf3ce44SJohn Forte  *
3086fcf3ce44SJohn Forte  * lu - guid of lu for which view entry is being removed
3087fcf3ce44SJohn Forte  * viewEntryIndex - index of view entry to remove
3088fcf3ce44SJohn Forte  *
3089fcf3ce44SJohn Forte  */
3090fcf3ce44SJohn Forte int
3091fcf3ce44SJohn Forte stmfRemoveViewEntry(stmfGuid *lu, uint32_t viewEntryIndex)
3092fcf3ce44SJohn Forte {
3093fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
3094fcf3ce44SJohn Forte 	int fd;
3095fcf3ce44SJohn Forte 	int ioctlRet;
3096fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
3097fcf3ce44SJohn Forte 	stmf_view_op_entry_t ioctlViewEntry;
3098fcf3ce44SJohn Forte 
3099fcf3ce44SJohn Forte 	if (lu == NULL) {
3100fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3101fcf3ce44SJohn Forte 	}
3102fcf3ce44SJohn Forte 
3103fcf3ce44SJohn Forte 	/* call init */
3104fcf3ce44SJohn Forte 	ret = initializeConfig();
3105fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3106fcf3ce44SJohn Forte 		return (ret);
3107fcf3ce44SJohn Forte 	}
3108fcf3ce44SJohn Forte 
3109fcf3ce44SJohn Forte 	/*
3110fcf3ce44SJohn Forte 	 * Open control node for stmf
3111fcf3ce44SJohn Forte 	 */
3112fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3113fcf3ce44SJohn Forte 		return (ret);
3114fcf3ce44SJohn Forte 
3115fcf3ce44SJohn Forte 	bzero(&ioctlViewEntry, sizeof (ioctlViewEntry));
3116fcf3ce44SJohn Forte 	ioctlViewEntry.ve_ndx_valid = B_TRUE;
3117fcf3ce44SJohn Forte 	ioctlViewEntry.ve_ndx = viewEntryIndex;
3118fcf3ce44SJohn Forte 	bcopy(lu, &ioctlViewEntry.ve_guid, sizeof (stmfGuid));
3119fcf3ce44SJohn Forte 
3120fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
3121fcf3ce44SJohn Forte 	/*
3122fcf3ce44SJohn Forte 	 * Issue ioctl to add to the view entry
3123fcf3ce44SJohn Forte 	 */
3124fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
3125fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ioctlViewEntry);
3126fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ioctlViewEntry;
3127fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_REMOVE_VIEW_ENTRY, &stmfIoctl);
3128fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
3129fcf3ce44SJohn Forte 		switch (errno) {
3130fcf3ce44SJohn Forte 			case EBUSY:
3131fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
3132fcf3ce44SJohn Forte 				break;
3133fcf3ce44SJohn Forte 			case EACCES:
3134fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
3135fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
3136fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
3137fcf3ce44SJohn Forte 						break;
3138fcf3ce44SJohn Forte 					default:
3139fcf3ce44SJohn Forte 						ret = STMF_ERROR_PERM;
3140fcf3ce44SJohn Forte 						break;
3141fcf3ce44SJohn Forte 				}
3142fcf3ce44SJohn Forte 				break;
3143fcf3ce44SJohn Forte 			case ENODEV:
3144fcf3ce44SJohn Forte 			case ENOENT:
3145fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
3146fcf3ce44SJohn Forte 				break;
3147fcf3ce44SJohn Forte 			default:
3148fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
3149fcf3ce44SJohn Forte 				    "stmfRemoveViewEntry:ioctl errno(%d)",
3150fcf3ce44SJohn Forte 				    errno);
3151fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
3152fcf3ce44SJohn Forte 				break;
3153fcf3ce44SJohn Forte 		}
3154fcf3ce44SJohn Forte 		goto done;
3155fcf3ce44SJohn Forte 	}
3156fcf3ce44SJohn Forte 
3157fcf3ce44SJohn Forte 	ret = psRemoveViewEntry(lu, viewEntryIndex);
3158fcf3ce44SJohn Forte 	switch (ret) {
3159fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3160fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3161fcf3ce44SJohn Forte 			break;
3162fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3163fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3164fcf3ce44SJohn Forte 			break;
3165fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3166fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3167fcf3ce44SJohn Forte 			break;
3168fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3169fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3170fcf3ce44SJohn Forte 			break;
3171fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3172fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3173fcf3ce44SJohn Forte 			break;
3174fcf3ce44SJohn Forte 		default:
3175fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3176fcf3ce44SJohn Forte 			    "stmfRemoveViewEntry" "psRemoveViewEntry:error(%d)",
3177fcf3ce44SJohn Forte 			    ret);
3178fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3179fcf3ce44SJohn Forte 			break;
3180fcf3ce44SJohn Forte 	}
3181fcf3ce44SJohn Forte 
3182fcf3ce44SJohn Forte done:
3183fcf3ce44SJohn Forte 	(void) close(fd);
3184fcf3ce44SJohn Forte 	return (ret);
3185fcf3ce44SJohn Forte }
3186fcf3ce44SJohn Forte 
3187fcf3ce44SJohn Forte /*
3188fcf3ce44SJohn Forte  * stmfSetProviderData
3189fcf3ce44SJohn Forte  *
3190fcf3ce44SJohn Forte  * Purpose: set the provider data
3191fcf3ce44SJohn Forte  *
3192fcf3ce44SJohn Forte  * providerName - unique name of provider
3193fcf3ce44SJohn Forte  * nvl - nvlist to set
3194fcf3ce44SJohn Forte  * providerType - type of provider for which to set data
3195fcf3ce44SJohn Forte  *		STMF_LU_PROVIDER_TYPE
3196fcf3ce44SJohn Forte  *		STMF_PORT_PROVIDER_TYPE
3197fcf3ce44SJohn Forte  */
3198fcf3ce44SJohn Forte int
3199fcf3ce44SJohn Forte stmfSetProviderData(char *providerName, nvlist_t *nvl, int providerType)
3200fcf3ce44SJohn Forte {
3201fcf3ce44SJohn Forte 	return (stmfSetProviderDataProt(providerName, nvl, providerType,
3202fcf3ce44SJohn Forte 	    NULL));
3203fcf3ce44SJohn Forte }
3204fcf3ce44SJohn Forte 
3205fcf3ce44SJohn Forte /*
3206fcf3ce44SJohn Forte  * stmfSetProviderDataProt
3207fcf3ce44SJohn Forte  *
3208fcf3ce44SJohn Forte  * Purpose: set the provider data
3209fcf3ce44SJohn Forte  *
3210fcf3ce44SJohn Forte  * providerName - unique name of provider
3211fcf3ce44SJohn Forte  * nvl - nvlist to set
3212fcf3ce44SJohn Forte  * providerType - type of provider for which to set data
3213fcf3ce44SJohn Forte  *		STMF_LU_PROVIDER_TYPE
3214fcf3ce44SJohn Forte  *		STMF_PORT_PROVIDER_TYPE
3215fcf3ce44SJohn Forte  * setToken - Stale data token returned in the stmfGetProviderDataProt()
3216fcf3ce44SJohn Forte  *	      call or NULL.
3217fcf3ce44SJohn Forte  */
3218fcf3ce44SJohn Forte int
3219fcf3ce44SJohn Forte stmfSetProviderDataProt(char *providerName, nvlist_t *nvl, int providerType,
3220fcf3ce44SJohn Forte     uint64_t *setToken)
3221fcf3ce44SJohn Forte {
3222fcf3ce44SJohn Forte 	int ret;
3223fcf3ce44SJohn Forte 	int fd;
3224fcf3ce44SJohn Forte 
3225fcf3ce44SJohn Forte 	if (providerName == NULL || nvl == NULL) {
3226fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3227fcf3ce44SJohn Forte 	}
3228fcf3ce44SJohn Forte 
3229fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
3230fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
3231fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3232fcf3ce44SJohn Forte 	}
3233fcf3ce44SJohn Forte 
3234fcf3ce44SJohn Forte 	/* call init */
3235fcf3ce44SJohn Forte 	ret = initializeConfig();
3236fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3237fcf3ce44SJohn Forte 		return (ret);
3238fcf3ce44SJohn Forte 	}
3239fcf3ce44SJohn Forte 
3240fcf3ce44SJohn Forte 	/*
3241fcf3ce44SJohn Forte 	 * Open control node for stmf
3242fcf3ce44SJohn Forte 	 */
3243fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3244fcf3ce44SJohn Forte 		return (ret);
3245fcf3ce44SJohn Forte 
3246fcf3ce44SJohn Forte 	ret = setProviderData(fd, providerName, nvl, providerType);
3247fcf3ce44SJohn Forte 
3248fcf3ce44SJohn Forte 	(void) close(fd);
3249fcf3ce44SJohn Forte 
3250fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3251fcf3ce44SJohn Forte 		goto done;
3252fcf3ce44SJohn Forte 	}
3253fcf3ce44SJohn Forte 
3254fcf3ce44SJohn Forte 	/* setting driver provider data successful. Now persist it */
3255fcf3ce44SJohn Forte 	ret = psSetProviderData(providerName, nvl, providerType, setToken);
3256fcf3ce44SJohn Forte 	switch (ret) {
3257fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3258fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3259fcf3ce44SJohn Forte 			break;
3260fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
3261fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
3262fcf3ce44SJohn Forte 			break;
3263fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3264fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3265fcf3ce44SJohn Forte 			break;
3266fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3267fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3268fcf3ce44SJohn Forte 			break;
3269fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3270fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3271fcf3ce44SJohn Forte 			break;
3272fcf3ce44SJohn Forte 		case STMF_PS_ERROR_PROV_DATA_STALE:
3273fcf3ce44SJohn Forte 			ret = STMF_ERROR_PROV_DATA_STALE;
3274fcf3ce44SJohn Forte 			break;
3275fcf3ce44SJohn Forte 		default:
3276fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3277fcf3ce44SJohn Forte 			    "stmfSetProviderData"
3278fcf3ce44SJohn Forte 			    "psSetProviderData:error(%d)", ret);
3279fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3280fcf3ce44SJohn Forte 			break;
3281fcf3ce44SJohn Forte 	}
3282fcf3ce44SJohn Forte 
3283fcf3ce44SJohn Forte done:
3284fcf3ce44SJohn Forte 	return (ret);
3285fcf3ce44SJohn Forte }
3286fcf3ce44SJohn Forte 
3287fcf3ce44SJohn Forte /*
3288fcf3ce44SJohn Forte  * setProviderData
3289fcf3ce44SJohn Forte  *
3290fcf3ce44SJohn Forte  * Purpose: set the provider data
3291fcf3ce44SJohn Forte  *
3292fcf3ce44SJohn Forte  * providerName - unique name of provider
3293fcf3ce44SJohn Forte  * nvl - nvlist to set
3294fcf3ce44SJohn Forte  * providerType - logical unit or port provider
3295fcf3ce44SJohn Forte  */
3296fcf3ce44SJohn Forte static int
3297fcf3ce44SJohn Forte setProviderData(int fd, char *providerName, nvlist_t *nvl, int providerType)
3298fcf3ce44SJohn Forte {
3299fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
3300fcf3ce44SJohn Forte 	int ioctlRet;
3301fcf3ce44SJohn Forte 	size_t nvlistEncodedSize;
3302fcf3ce44SJohn Forte 	stmf_ppioctl_data_t *ppi = NULL;
3303fcf3ce44SJohn Forte 	char *allocatedNvBuffer;
3304fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
3305fcf3ce44SJohn Forte 
3306fcf3ce44SJohn Forte 	if (providerName == NULL) {
3307fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3308fcf3ce44SJohn Forte 	}
3309fcf3ce44SJohn Forte 
3310fcf3ce44SJohn Forte 	/* get size of encoded nvlist */
3311fcf3ce44SJohn Forte 	if (nvlist_size(nvl, &nvlistEncodedSize, NV_ENCODE_XDR) != 0) {
3312fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
3313fcf3ce44SJohn Forte 	}
3314fcf3ce44SJohn Forte 
3315fcf3ce44SJohn Forte 	/* allocate memory for ioctl */
3316fcf3ce44SJohn Forte 	ppi = (stmf_ppioctl_data_t *)calloc(1, nvlistEncodedSize +
3317fcf3ce44SJohn Forte 	    sizeof (stmf_ppioctl_data_t));
3318fcf3ce44SJohn Forte 	if (ppi == NULL) {
3319fcf3ce44SJohn Forte 		return (STMF_ERROR_NOMEM);
3320fcf3ce44SJohn Forte 	}
3321fcf3ce44SJohn Forte 
3322fcf3ce44SJohn Forte 	allocatedNvBuffer = (char *)&ppi->ppi_data;
3323fcf3ce44SJohn Forte 	if (nvlist_pack(nvl, &allocatedNvBuffer, &nvlistEncodedSize,
3324fcf3ce44SJohn Forte 	    NV_ENCODE_XDR, 0) != 0) {
3325fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
3326fcf3ce44SJohn Forte 	}
3327fcf3ce44SJohn Forte 
3328fcf3ce44SJohn Forte 	/* set provider name and provider type */
3329fcf3ce44SJohn Forte 	(void) strncpy(ppi->ppi_name, providerName, sizeof (ppi->ppi_name));
3330fcf3ce44SJohn Forte 	switch (providerType) {
3331fcf3ce44SJohn Forte 		case STMF_LU_PROVIDER_TYPE:
3332fcf3ce44SJohn Forte 			ppi->ppi_lu_provider = 1;
3333fcf3ce44SJohn Forte 			break;
3334fcf3ce44SJohn Forte 		case STMF_PORT_PROVIDER_TYPE:
3335fcf3ce44SJohn Forte 			ppi->ppi_port_provider = 1;
3336fcf3ce44SJohn Forte 			break;
3337fcf3ce44SJohn Forte 		default:
3338fcf3ce44SJohn Forte 			return (STMF_ERROR_INVALID_ARG);
3339fcf3ce44SJohn Forte 	}
3340fcf3ce44SJohn Forte 
3341fcf3ce44SJohn Forte 	/* set the size of the ioctl data to packed data size */
3342fcf3ce44SJohn Forte 	ppi->ppi_data_size = nvlistEncodedSize;
3343fcf3ce44SJohn Forte 
3344fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
3345fcf3ce44SJohn Forte 
3346fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
3347fcf3ce44SJohn Forte 	/*
3348fcf3ce44SJohn Forte 	 * Subtracting 8 from the size as that is the size of the last member
3349fcf3ce44SJohn Forte 	 * of the structure where the packed data resides
3350fcf3ce44SJohn Forte 	 */
3351fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = nvlistEncodedSize +
3352fcf3ce44SJohn Forte 	    sizeof (stmf_ppioctl_data_t) - 8;
3353fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)ppi;
3354fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_LOAD_PP_DATA, &stmfIoctl);
3355fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
3356fcf3ce44SJohn Forte 		switch (errno) {
3357fcf3ce44SJohn Forte 			case EBUSY:
3358fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
3359fcf3ce44SJohn Forte 				break;
3360fcf3ce44SJohn Forte 			case EACCES:
3361fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
3362fcf3ce44SJohn Forte 				break;
3363fcf3ce44SJohn Forte 			default:
3364fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
3365fcf3ce44SJohn Forte 				    "setProviderData:ioctl errno(%d)", errno);
3366fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
3367fcf3ce44SJohn Forte 				break;
3368fcf3ce44SJohn Forte 		}
3369fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS)
3370fcf3ce44SJohn Forte 			goto done;
3371fcf3ce44SJohn Forte 	}
3372fcf3ce44SJohn Forte 
3373fcf3ce44SJohn Forte done:
3374fcf3ce44SJohn Forte 	free(ppi);
3375fcf3ce44SJohn Forte 	return (ret);
3376fcf3ce44SJohn Forte }
3377