xref: /titanic_51/usr/src/lib/libstmf/common/stmf.c (revision 5c8cac22647b12e79982cf052e5df46dc7402bd5)
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 /*
228fe96085Stim szeto  * Copyright 2009 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>
468fe96085Stim szeto #include <math.h>
478fe96085Stim szeto #include <libstmf_impl.h>
48fcf3ce44SJohn Forte #include <sys/stmf_ioctl.h>
498fe96085Stim szeto #include <sys/stmf_sbd_ioctl.h>
50fcf3ce44SJohn Forte 
51fcf3ce44SJohn Forte #define	STMF_PATH    "/devices/pseudo/stmf@0:admin"
528fe96085Stim szeto #define	SBD_PATH    "/devices/pseudo/stmf_sbd@0:admin"
53fcf3ce44SJohn Forte 
54fcf3ce44SJohn Forte #define	EUI "eui."
55fcf3ce44SJohn Forte #define	WWN "wwn."
56fcf3ce44SJohn Forte #define	IQN "iqn."
578fe96085Stim szeto #define	LU_ASCII_GUID_SIZE 32
588fe96085Stim szeto #define	LU_GUID_SIZE 16
598fe96085Stim szeto #define	OUI_ASCII_SIZE 6
608fe96085Stim szeto #define	OUI_SIZE 3
61fcf3ce44SJohn Forte #define	IDENT_LENGTH_BYTE 3
62fcf3ce44SJohn Forte 
638fe96085Stim szeto /* various initial allocation values */
648fe96085Stim szeto #define	ALLOC_LU		8192
658fe96085Stim szeto #define	ALLOC_TARGET_PORT	2048
668fe96085Stim szeto #define	ALLOC_PROVIDER		64
678fe96085Stim szeto #define	ALLOC_GROUP		2048
688fe96085Stim szeto #define	ALLOC_SESSION		2048
698fe96085Stim szeto #define	ALLOC_VE		256
708fe96085Stim szeto #define	ALLOC_PP_DATA_SIZE	128*1024
718fe96085Stim szeto #define	ALLOC_GRP_MEMBER	256
728fe96085Stim szeto 
73fcf3ce44SJohn Forte #define	MAX_ISCSI_NAME	223
748fe96085Stim szeto #define	MAX_SERIAL_SIZE 252 + 1
758fe96085Stim szeto #define	MAX_LU_ALIAS_SIZE 256
768fe96085Stim szeto #define	MAX_SBD_PROPS	MAXPATHLEN + MAX_SERIAL_SIZE + MAX_LU_ALIAS_SIZE
77fcf3ce44SJohn Forte 
78fcf3ce44SJohn Forte #define	OPEN_STMF 0
79fcf3ce44SJohn Forte #define	OPEN_EXCL_STMF O_EXCL
80fcf3ce44SJohn Forte 
818fe96085Stim szeto #define	OPEN_SBD 0
828fe96085Stim szeto #define	OPEN_EXCL_SBD O_EXCL
838fe96085Stim szeto 
84fcf3ce44SJohn Forte #define	LOGICAL_UNIT_TYPE 0
85fcf3ce44SJohn Forte #define	TARGET_TYPE 1
86fcf3ce44SJohn Forte #define	STMF_SERVICE_TYPE 2
87fcf3ce44SJohn Forte 
888fe96085Stim szeto #define	HOST_GROUP   1
898fe96085Stim szeto #define	TARGET_GROUP 2
908fe96085Stim szeto 
918fe96085Stim szeto /* set default persistence here */
928fe96085Stim szeto #define	STMF_DEFAULT_PERSIST	STMF_PERSIST_SMF
938fe96085Stim szeto 
948fe96085Stim szeto #define	MAX_PROVIDER_RETRY 30
958fe96085Stim szeto 
96fcf3ce44SJohn Forte static int openStmf(int, int *fd);
978fe96085Stim szeto static int openSbd(int, int *fd);
98fcf3ce44SJohn Forte static int groupIoctl(int fd, int cmd, stmfGroupName *);
99fcf3ce44SJohn Forte static int loadStore(int fd);
100fcf3ce44SJohn Forte static int initializeConfig();
101fcf3ce44SJohn Forte static int groupMemberIoctl(int fd, int cmd, stmfGroupName *, stmfDevid *);
102fcf3ce44SJohn Forte static int guidCompare(const void *, const void *);
103fcf3ce44SJohn Forte static int addViewEntryIoctl(int fd, stmfGuid *, stmfViewEntry *);
104fcf3ce44SJohn Forte static int loadHostGroups(int fd, stmfGroupList *);
105fcf3ce44SJohn Forte static int loadTargetGroups(int fd, stmfGroupList *);
106fcf3ce44SJohn Forte static int getStmfState(stmf_state_desc_t *);
107fcf3ce44SJohn Forte static int setStmfState(int fd, stmf_state_desc_t *, int);
1088fe96085Stim szeto static int setProviderData(int fd, char *, nvlist_t *, int, uint64_t *);
1098fe96085Stim szeto static int createDiskResource(luResourceImpl *);
1108fe96085Stim szeto static int createDiskLu(diskResource *, stmfGuid *);
1118fe96085Stim szeto static int deleteDiskLu(stmfGuid *luGuid);
1128fe96085Stim szeto static int getDiskProp(luResourceImpl *, uint32_t, char *, size_t *);
1138fe96085Stim szeto static int getDiskAllProps(stmfGuid *luGuid, luResource *hdl);
1148fe96085Stim szeto static int loadDiskPropsFromDriver(luResourceImpl *, sbd_lu_props_t *);
1158fe96085Stim szeto static int removeGuidFromDiskStore(stmfGuid *);
1168fe96085Stim szeto static int addGuidToDiskStore(stmfGuid *, char *);
1178fe96085Stim szeto static int persistDiskGuid(stmfGuid *, char *, boolean_t);
1188fe96085Stim szeto static int setDiskProp(luResourceImpl *, uint32_t, const char *);
1198fe96085Stim szeto static int checkHexUpper(char *);
1208fe96085Stim szeto static int strToShift(const char *);
1218fe96085Stim szeto static int niceStrToNum(const char *, uint64_t *);
1228fe96085Stim szeto static void diskError(uint32_t, int *);
1238fe96085Stim szeto static int importDiskLu(char *fname, stmfGuid *);
1248fe96085Stim szeto static int modifyDiskLu(diskResource *, stmfGuid *, const char *);
1258fe96085Stim szeto static int modifyDiskLuProp(stmfGuid *, const char *, uint32_t, const char *);
1268fe96085Stim szeto static int validateModifyDiskProp(uint32_t);
1278fe96085Stim szeto static uint8_t iGetPersistMethod();
1288fe96085Stim szeto static int groupListIoctl(stmfGroupList **, int);
1298fe96085Stim szeto static int iLoadGroupFromPs(stmfGroupList **, int);
1308fe96085Stim szeto static int groupMemberListIoctl(stmfGroupName *, stmfGroupProperties **, int);
1318fe96085Stim szeto static int getProviderData(char *, nvlist_t **, int, uint64_t *);
1328fe96085Stim szeto static int viewEntryCompare(const void *, const void *);
1338fe96085Stim szeto 
1348fe96085Stim szeto static pthread_mutex_t persistenceTypeLock = PTHREAD_MUTEX_INITIALIZER;
1358fe96085Stim szeto static int iPersistType = 0;
1368fe96085Stim szeto /* when B_TRUE, no need to access SMF anymore. Just use iPersistType */
1378fe96085Stim szeto static boolean_t iLibSetPersist = B_FALSE;
138fcf3ce44SJohn Forte 
139fcf3ce44SJohn Forte /*
140fcf3ce44SJohn Forte  * Open for stmf module
141fcf3ce44SJohn Forte  *
142fcf3ce44SJohn Forte  * flag - open flag (OPEN_STMF, OPEN_EXCL_STMF)
143fcf3ce44SJohn Forte  * fd - pointer to integer. On success, contains the stmf file descriptor
144fcf3ce44SJohn Forte  */
145fcf3ce44SJohn Forte static int
146fcf3ce44SJohn Forte openStmf(int flag, int *fd)
147fcf3ce44SJohn Forte {
148fcf3ce44SJohn Forte 	int ret = STMF_STATUS_ERROR;
149fcf3ce44SJohn Forte 
150fcf3ce44SJohn Forte 	if ((*fd = open(STMF_PATH, O_NDELAY | O_RDONLY | flag)) != -1) {
151fcf3ce44SJohn Forte 		ret = STMF_STATUS_SUCCESS;
152fcf3ce44SJohn Forte 	} else {
153fcf3ce44SJohn Forte 		if (errno == EBUSY) {
154fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1558fe96085Stim szeto 		} else if (errno == EACCES) {
1568fe96085Stim szeto 			ret = STMF_ERROR_PERM;
157fcf3ce44SJohn Forte 		} else {
158fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
159fcf3ce44SJohn Forte 		}
160fcf3ce44SJohn Forte 		syslog(LOG_DEBUG, "openStmf:open failure:%s:errno(%d)",
161fcf3ce44SJohn Forte 		    STMF_PATH, errno);
162fcf3ce44SJohn Forte 	}
163fcf3ce44SJohn Forte 
164fcf3ce44SJohn Forte 	return (ret);
165fcf3ce44SJohn Forte }
166fcf3ce44SJohn Forte 
167fcf3ce44SJohn Forte /*
1688fe96085Stim szeto  * Open for sbd module
1698fe96085Stim szeto  *
1708fe96085Stim szeto  * flag - open flag (OPEN_STMF, OPEN_EXCL_STMF)
1718fe96085Stim szeto  * fd - pointer to integer. On success, contains the stmf file descriptor
1728fe96085Stim szeto  */
1738fe96085Stim szeto static int
1748fe96085Stim szeto openSbd(int flag, int *fd)
1758fe96085Stim szeto {
1768fe96085Stim szeto 	int ret = STMF_STATUS_ERROR;
1778fe96085Stim szeto 
1788fe96085Stim szeto 	if ((*fd = open(SBD_PATH, O_NDELAY | O_RDONLY | flag)) != -1) {
1798fe96085Stim szeto 		ret = STMF_STATUS_SUCCESS;
1808fe96085Stim szeto 	} else {
1818fe96085Stim szeto 		if (errno == EBUSY) {
1828fe96085Stim szeto 			ret = STMF_ERROR_BUSY;
1838fe96085Stim szeto 		} else if (errno == EACCES) {
1848fe96085Stim szeto 			ret = STMF_ERROR_PERM;
1858fe96085Stim szeto 		} else {
1868fe96085Stim szeto 			ret = STMF_STATUS_ERROR;
1878fe96085Stim szeto 		}
1888fe96085Stim szeto 		syslog(LOG_DEBUG, "openSbd:open failure:%s:errno(%d)",
1898fe96085Stim szeto 		    SBD_PATH, errno);
1908fe96085Stim szeto 	}
1918fe96085Stim szeto 
1928fe96085Stim szeto 	return (ret);
1938fe96085Stim szeto }
1948fe96085Stim szeto 
1958fe96085Stim szeto /*
196fcf3ce44SJohn Forte  * initializeConfig
197fcf3ce44SJohn Forte  *
198fcf3ce44SJohn Forte  * This routine should be called before any ioctl requiring initialization
199fcf3ce44SJohn Forte  * which is basically everything except stmfGetState(), setStmfState() and
200fcf3ce44SJohn Forte  * stmfLoadConfig().
201fcf3ce44SJohn Forte  */
202fcf3ce44SJohn Forte static int
203fcf3ce44SJohn Forte initializeConfig()
204fcf3ce44SJohn Forte {
205fcf3ce44SJohn Forte 	int ret;
206fcf3ce44SJohn Forte 	stmfState state;
207fcf3ce44SJohn Forte 
208fcf3ce44SJohn Forte 
209fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
210fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
211fcf3ce44SJohn Forte 		return (ret);
212fcf3ce44SJohn Forte 	}
213fcf3ce44SJohn Forte 
214fcf3ce44SJohn Forte 	/* if we've already initialized or in the process, return success */
215fcf3ce44SJohn Forte 	if (state.configState == STMF_CONFIG_STATE_INIT_DONE ||
216fcf3ce44SJohn Forte 	    state.configState == STMF_CONFIG_STATE_INIT) {
217fcf3ce44SJohn Forte 		return (STMF_STATUS_SUCCESS);
218fcf3ce44SJohn Forte 	}
219fcf3ce44SJohn Forte 
220fcf3ce44SJohn Forte 	ret = stmfLoadConfig();
221fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
222fcf3ce44SJohn Forte 		syslog(LOG_DEBUG,
223fcf3ce44SJohn Forte 		    "initializeConfig:stmfLoadConfig:error(%d)", ret);
224fcf3ce44SJohn Forte 		return (ret);
225fcf3ce44SJohn Forte 	}
226fcf3ce44SJohn Forte 
227fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
228fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
229fcf3ce44SJohn Forte 		syslog(LOG_DEBUG,
230fcf3ce44SJohn Forte 		    "initializeConfig:stmfGetState:error(%d)", ret);
231fcf3ce44SJohn Forte 		return (ret);
232fcf3ce44SJohn Forte 	}
233fcf3ce44SJohn Forte 
234fcf3ce44SJohn Forte 	if (state.configState != STMF_CONFIG_STATE_INIT_DONE) {
235fcf3ce44SJohn Forte 		syslog(LOG_DEBUG, "initializeConfig:state.configState(%d)",
236fcf3ce44SJohn Forte 		    state.configState);
237fcf3ce44SJohn Forte 		ret = STMF_STATUS_ERROR;
238fcf3ce44SJohn Forte 	}
239fcf3ce44SJohn Forte 
240fcf3ce44SJohn Forte 	return (ret);
241fcf3ce44SJohn Forte }
242fcf3ce44SJohn Forte 
243fcf3ce44SJohn Forte 
244fcf3ce44SJohn Forte /*
245fcf3ce44SJohn Forte  * groupIoctl
246fcf3ce44SJohn Forte  *
247fcf3ce44SJohn Forte  * Purpose: issue ioctl for create/delete on group
248fcf3ce44SJohn Forte  *
249fcf3ce44SJohn Forte  * cmd - valid STMF ioctl group cmd
250fcf3ce44SJohn Forte  * groupName - groupName to create or delete
251fcf3ce44SJohn Forte  */
252fcf3ce44SJohn Forte static int
253fcf3ce44SJohn Forte groupIoctl(int fd, int cmd, stmfGroupName *groupName)
254fcf3ce44SJohn Forte {
255fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
256fcf3ce44SJohn Forte 	int ioctlRet;
257fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
258fcf3ce44SJohn Forte 	stmf_group_name_t iGroupName;
259fcf3ce44SJohn Forte 
260fcf3ce44SJohn Forte 	bzero(&iGroupName, sizeof (iGroupName));
261fcf3ce44SJohn Forte 
262fcf3ce44SJohn Forte 	bcopy(groupName, &iGroupName.name, strlen((char *)groupName));
263fcf3ce44SJohn Forte 
264fcf3ce44SJohn Forte 	iGroupName.name_size = strlen((char *)groupName);
265fcf3ce44SJohn Forte 
266fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
267fcf3ce44SJohn Forte 	/*
268fcf3ce44SJohn Forte 	 * Issue ioctl to create the host group
269fcf3ce44SJohn Forte 	 */
270fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
271fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (iGroupName);
272fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&iGroupName;
273fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
274fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
275fcf3ce44SJohn Forte 		switch (errno) {
2768fe96085Stim szeto 			case EPERM:
277fcf3ce44SJohn Forte 			case EACCES:
278fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
279fcf3ce44SJohn Forte 				break;
280fcf3ce44SJohn Forte 			default:
281fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
282fcf3ce44SJohn Forte 					case STMF_IOCERR_TG_EXISTS:
283fcf3ce44SJohn Forte 					case STMF_IOCERR_HG_EXISTS:
284fcf3ce44SJohn Forte 						ret = STMF_ERROR_EXISTS;
285fcf3ce44SJohn Forte 						break;
286fcf3ce44SJohn Forte 					case STMF_IOCERR_TG_IN_USE:
287fcf3ce44SJohn Forte 					case STMF_IOCERR_HG_IN_USE:
288fcf3ce44SJohn Forte 						ret = STMF_ERROR_GROUP_IN_USE;
289fcf3ce44SJohn Forte 						break;
290fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG:
291fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG:
292fcf3ce44SJohn Forte 						ret = STMF_ERROR_NOT_FOUND;
293fcf3ce44SJohn Forte 						break;
294fcf3ce44SJohn Forte 					default:
295fcf3ce44SJohn Forte 						syslog(LOG_DEBUG,
296fcf3ce44SJohn Forte 						    "groupIoctl:error(%d)",
297fcf3ce44SJohn Forte 						    stmfIoctl.stmf_error);
298fcf3ce44SJohn Forte 						ret = STMF_STATUS_ERROR;
299fcf3ce44SJohn Forte 						break;
300fcf3ce44SJohn Forte 				}
301fcf3ce44SJohn Forte 				break;
302fcf3ce44SJohn Forte 		}
303fcf3ce44SJohn Forte 	}
304fcf3ce44SJohn Forte done:
305fcf3ce44SJohn Forte 	return (ret);
306fcf3ce44SJohn Forte }
307fcf3ce44SJohn Forte 
308fcf3ce44SJohn Forte /*
3098fe96085Stim szeto  * groupMemberIoctl
310fcf3ce44SJohn Forte  *
311fcf3ce44SJohn Forte  * Purpose: issue ioctl for add/remove member on group
312fcf3ce44SJohn Forte  *
313fcf3ce44SJohn Forte  * cmd - valid STMF ioctl group member cmd
314fcf3ce44SJohn Forte  * groupName - groupName to add to or remove from
315fcf3ce44SJohn Forte  * devid - group member to add or remove
316fcf3ce44SJohn Forte  */
317fcf3ce44SJohn Forte static int
318fcf3ce44SJohn Forte groupMemberIoctl(int fd, int cmd, stmfGroupName *groupName, stmfDevid *devid)
319fcf3ce44SJohn Forte {
320fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
321fcf3ce44SJohn Forte 	int ioctlRet;
322fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
323fcf3ce44SJohn Forte 	stmf_group_op_data_t stmfGroupData;
324fcf3ce44SJohn Forte 
325fcf3ce44SJohn Forte 	bzero(&stmfGroupData, sizeof (stmfGroupData));
326fcf3ce44SJohn Forte 
327fcf3ce44SJohn Forte 	bcopy(groupName, &stmfGroupData.group.name, strlen((char *)groupName));
328fcf3ce44SJohn Forte 
329fcf3ce44SJohn Forte 	stmfGroupData.group.name_size = strlen((char *)groupName);
330fcf3ce44SJohn Forte 	stmfGroupData.ident[IDENT_LENGTH_BYTE] = devid->identLength;
331fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &stmfGroupData.ident[IDENT_LENGTH_BYTE + 1],
332fcf3ce44SJohn Forte 	    devid->identLength);
333fcf3ce44SJohn Forte 
334fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
335fcf3ce44SJohn Forte 	/*
336fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
337fcf3ce44SJohn Forte 	 */
338fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
339fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGroupData);
340fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&stmfGroupData;
341fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
342fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
343fcf3ce44SJohn Forte 		switch (errno) {
344fcf3ce44SJohn Forte 			case EBUSY:
345*5c8cac22Stim szeto 				switch (stmfIoctl.stmf_error) {
346*5c8cac22Stim szeto 					case STMF_IOCERR_TG_NEED_TG_OFFLINE:
347*5c8cac22Stim szeto 						ret = STMF_ERROR_TG_ONLINE;
348*5c8cac22Stim szeto 						break;
349*5c8cac22Stim szeto 					default:
350fcf3ce44SJohn Forte 						ret = STMF_ERROR_BUSY;
351fcf3ce44SJohn Forte 						break;
352*5c8cac22Stim szeto 				}
353*5c8cac22Stim szeto 				break;
3548fe96085Stim szeto 			case EPERM:
355fcf3ce44SJohn Forte 			case EACCES:
356fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
357fcf3ce44SJohn Forte 				break;
358fcf3ce44SJohn Forte 			default:
359fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
360fcf3ce44SJohn Forte 					case STMF_IOCERR_TG_ENTRY_EXISTS:
361fcf3ce44SJohn Forte 					case STMF_IOCERR_HG_ENTRY_EXISTS:
362fcf3ce44SJohn Forte 						ret = STMF_ERROR_EXISTS;
363fcf3ce44SJohn Forte 						break;
364fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG_ENTRY:
365fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG_ENTRY:
366fcf3ce44SJohn Forte 						ret =
367fcf3ce44SJohn Forte 						    STMF_ERROR_MEMBER_NOT_FOUND;
368fcf3ce44SJohn Forte 						break;
369fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG:
370fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG:
371fcf3ce44SJohn Forte 						ret =
372fcf3ce44SJohn Forte 						    STMF_ERROR_GROUP_NOT_FOUND;
373fcf3ce44SJohn Forte 						break;
374fcf3ce44SJohn Forte 					default:
375fcf3ce44SJohn Forte 						syslog(LOG_DEBUG,
376fcf3ce44SJohn Forte 						    "groupMemberIoctl:error"
377fcf3ce44SJohn Forte 						    "(%d)",
378fcf3ce44SJohn Forte 						    stmfIoctl.stmf_error);
379fcf3ce44SJohn Forte 						ret = STMF_STATUS_ERROR;
380fcf3ce44SJohn Forte 						break;
381fcf3ce44SJohn Forte 				}
382fcf3ce44SJohn Forte 				break;
383fcf3ce44SJohn Forte 		}
384fcf3ce44SJohn Forte 	}
385fcf3ce44SJohn Forte done:
386fcf3ce44SJohn Forte 	return (ret);
387fcf3ce44SJohn Forte }
388fcf3ce44SJohn Forte 
389fcf3ce44SJohn Forte /*
3908fe96085Stim szeto  * qsort function
3918fe96085Stim szeto  * sort on veIndex
3928fe96085Stim szeto  */
3938fe96085Stim szeto static int
3948fe96085Stim szeto viewEntryCompare(const void *p1, const void *p2)
3958fe96085Stim szeto {
3968fe96085Stim szeto 
3978fe96085Stim szeto 	stmfViewEntry *v1 = (stmfViewEntry *)p1, *v2 = (stmfViewEntry *)p2;
3988fe96085Stim szeto 	if (v1->veIndex > v2->veIndex)
3998fe96085Stim szeto 		return (1);
4008fe96085Stim szeto 	if (v1->veIndex < v2->veIndex)
4018fe96085Stim szeto 		return (-1);
4028fe96085Stim szeto 	return (0);
4038fe96085Stim szeto }
4048fe96085Stim szeto 
4058fe96085Stim szeto /*
406fcf3ce44SJohn Forte  * guidCompare
407fcf3ce44SJohn Forte  *
408fcf3ce44SJohn Forte  * qsort function
409fcf3ce44SJohn Forte  * sort on guid
410fcf3ce44SJohn Forte  */
411fcf3ce44SJohn Forte static int
412fcf3ce44SJohn Forte guidCompare(const void *p1, const void *p2)
413fcf3ce44SJohn Forte {
414fcf3ce44SJohn Forte 
415fcf3ce44SJohn Forte 	stmfGuid *g1 = (stmfGuid *)p1, *g2 = (stmfGuid *)p2;
416fcf3ce44SJohn Forte 	int i;
417fcf3ce44SJohn Forte 
418fcf3ce44SJohn Forte 	for (i = 0; i < sizeof (stmfGuid); i++) {
419fcf3ce44SJohn Forte 		if (g1->guid[i] > g2->guid[i])
420fcf3ce44SJohn Forte 			return (1);
421fcf3ce44SJohn Forte 		if (g1->guid[i] < g2->guid[i])
422fcf3ce44SJohn Forte 			return (-1);
423fcf3ce44SJohn Forte 	}
424fcf3ce44SJohn Forte 
425fcf3ce44SJohn Forte 	return (0);
426fcf3ce44SJohn Forte }
427fcf3ce44SJohn Forte 
428fcf3ce44SJohn Forte /*
429fcf3ce44SJohn Forte  * stmfAddToHostGroup
430fcf3ce44SJohn Forte  *
431fcf3ce44SJohn Forte  * Purpose: Adds an initiator to an existing host group
432fcf3ce44SJohn Forte  *
433fcf3ce44SJohn Forte  * hostGroupName - name of an existing host group
434fcf3ce44SJohn Forte  * hostName - name of initiator to add
435fcf3ce44SJohn Forte  */
436fcf3ce44SJohn Forte int
437fcf3ce44SJohn Forte stmfAddToHostGroup(stmfGroupName *hostGroupName, stmfDevid *hostName)
438fcf3ce44SJohn Forte {
439fcf3ce44SJohn Forte 	int ret;
440fcf3ce44SJohn Forte 	int fd;
441fcf3ce44SJohn Forte 
442fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
443fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
444fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || hostName == NULL) {
445fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
446fcf3ce44SJohn Forte 	}
447fcf3ce44SJohn Forte 
448fcf3ce44SJohn Forte 	/* call init */
449fcf3ce44SJohn Forte 	ret = initializeConfig();
450fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
451fcf3ce44SJohn Forte 		return (ret);
452fcf3ce44SJohn Forte 	}
453fcf3ce44SJohn Forte 
454fcf3ce44SJohn Forte 	/*
455fcf3ce44SJohn Forte 	 * Open control node for stmf
456fcf3ce44SJohn Forte 	 */
457fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
458fcf3ce44SJohn Forte 		return (ret);
459fcf3ce44SJohn Forte 
460fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_HG_ENTRY, hostGroupName,
461fcf3ce44SJohn Forte 	    hostName)) != STMF_STATUS_SUCCESS) {
462fcf3ce44SJohn Forte 		goto done;
463fcf3ce44SJohn Forte 	}
464fcf3ce44SJohn Forte 
4658fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
4668fe96085Stim szeto 		goto done;
4678fe96085Stim szeto 	}
4688fe96085Stim szeto 
469fcf3ce44SJohn Forte 	ret = psAddHostGroupMember((char *)hostGroupName,
470fcf3ce44SJohn Forte 	    (char *)hostName->ident);
471fcf3ce44SJohn Forte 	switch (ret) {
472fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
473fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
474fcf3ce44SJohn Forte 			break;
475fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
476fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
477fcf3ce44SJohn Forte 			break;
478fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
479fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
480fcf3ce44SJohn Forte 			break;
481fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
482fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
483fcf3ce44SJohn Forte 			break;
484fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
485fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
486fcf3ce44SJohn Forte 			break;
487fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
488fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
489fcf3ce44SJohn Forte 			break;
490fcf3ce44SJohn Forte 		default:
491fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
492fcf3ce44SJohn Forte 			    "stmfAddToHostGroup:psAddHostGroupMember:error(%d)",
493fcf3ce44SJohn Forte 			    ret);
494fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
495fcf3ce44SJohn Forte 			break;
496fcf3ce44SJohn Forte 	}
497fcf3ce44SJohn Forte 
498fcf3ce44SJohn Forte done:
499fcf3ce44SJohn Forte 	(void) close(fd);
500fcf3ce44SJohn Forte 	return (ret);
501fcf3ce44SJohn Forte }
502fcf3ce44SJohn Forte 
503fcf3ce44SJohn Forte /*
504fcf3ce44SJohn Forte  * stmfAddToTargetGroup
505fcf3ce44SJohn Forte  *
506fcf3ce44SJohn Forte  * Purpose: Adds a local port to an existing target group
507fcf3ce44SJohn Forte  *
508fcf3ce44SJohn Forte  * targetGroupName - name of an existing target group
509fcf3ce44SJohn Forte  * targetName - name of target to add
510fcf3ce44SJohn Forte  */
511fcf3ce44SJohn Forte int
512fcf3ce44SJohn Forte stmfAddToTargetGroup(stmfGroupName *targetGroupName, stmfDevid *targetName)
513fcf3ce44SJohn Forte {
514fcf3ce44SJohn Forte 	int ret;
515fcf3ce44SJohn Forte 	int fd;
516fcf3ce44SJohn Forte 
517fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
518fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
519fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || targetName == NULL) {
520fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
521fcf3ce44SJohn Forte 	}
522fcf3ce44SJohn Forte 
523fcf3ce44SJohn Forte 	/* call init */
524fcf3ce44SJohn Forte 	ret = initializeConfig();
525fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
526fcf3ce44SJohn Forte 		return (ret);
527fcf3ce44SJohn Forte 	}
528fcf3ce44SJohn Forte 
529fcf3ce44SJohn Forte 	/*
530fcf3ce44SJohn Forte 	 * Open control node for stmf
531fcf3ce44SJohn Forte 	 */
532fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
533fcf3ce44SJohn Forte 		return (ret);
534fcf3ce44SJohn Forte 
535fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_TG_ENTRY,
536fcf3ce44SJohn Forte 	    targetGroupName, targetName)) != STMF_STATUS_SUCCESS) {
537fcf3ce44SJohn Forte 		goto done;
538fcf3ce44SJohn Forte 	}
539fcf3ce44SJohn Forte 
5408fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
5418fe96085Stim szeto 		goto done;
5428fe96085Stim szeto 	}
5438fe96085Stim szeto 
544fcf3ce44SJohn Forte 	ret = psAddTargetGroupMember((char *)targetGroupName,
545fcf3ce44SJohn Forte 	    (char *)targetName->ident);
546fcf3ce44SJohn Forte 	switch (ret) {
547fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
548fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
549fcf3ce44SJohn Forte 			break;
550fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
551fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
552fcf3ce44SJohn Forte 			break;
553fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
554fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
555fcf3ce44SJohn Forte 			break;
556fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
557fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
558fcf3ce44SJohn Forte 			break;
559fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
560fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
561fcf3ce44SJohn Forte 			break;
562fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
563fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
564fcf3ce44SJohn Forte 			break;
565fcf3ce44SJohn Forte 		default:
566fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
567fcf3ce44SJohn Forte 			    "stmfAddToTargetGroup:psAddTargetGroupMember:"
568fcf3ce44SJohn Forte 			    "error(%d)", ret);
569fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
570fcf3ce44SJohn Forte 			break;
571fcf3ce44SJohn Forte 	}
572fcf3ce44SJohn Forte 
573fcf3ce44SJohn Forte done:
574fcf3ce44SJohn Forte 	(void) close(fd);
575fcf3ce44SJohn Forte 	return (ret);
576fcf3ce44SJohn Forte }
577fcf3ce44SJohn Forte 
578fcf3ce44SJohn Forte /*
579fcf3ce44SJohn Forte  * addViewEntryIoctl
580fcf3ce44SJohn Forte  *
581fcf3ce44SJohn Forte  * Purpose: Issues ioctl to add a view entry
582fcf3ce44SJohn Forte  *
583fcf3ce44SJohn Forte  * lu - Logical Unit identifier to which the view entry is added
584fcf3ce44SJohn Forte  * viewEntry - view entry to add
585fcf3ce44SJohn Forte  * init - When set to B_TRUE, we are in the init state, i.e. don't call open
586fcf3ce44SJohn Forte  */
587fcf3ce44SJohn Forte static int
588fcf3ce44SJohn Forte addViewEntryIoctl(int fd, stmfGuid *lu, stmfViewEntry *viewEntry)
589fcf3ce44SJohn Forte {
590fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
591fcf3ce44SJohn Forte 	int ioctlRet;
592fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
593fcf3ce44SJohn Forte 	stmf_view_op_entry_t ioctlViewEntry;
594fcf3ce44SJohn Forte 
595fcf3ce44SJohn Forte 	bzero(&ioctlViewEntry, sizeof (ioctlViewEntry));
596fcf3ce44SJohn Forte 	/*
597fcf3ce44SJohn Forte 	 * don't set ve_ndx or ve_ndx_valid as ve_ndx_valid should be
598fcf3ce44SJohn Forte 	 * false on input
599fcf3ce44SJohn Forte 	 */
600fcf3ce44SJohn Forte 	ioctlViewEntry.ve_lu_number_valid = viewEntry->luNbrValid;
601fcf3ce44SJohn Forte 	ioctlViewEntry.ve_all_hosts = viewEntry->allHosts;
602fcf3ce44SJohn Forte 	ioctlViewEntry.ve_all_targets = viewEntry->allTargets;
603fcf3ce44SJohn Forte 
604fcf3ce44SJohn Forte 	if (viewEntry->allHosts == B_FALSE) {
605fcf3ce44SJohn Forte 		bcopy(viewEntry->hostGroup, &ioctlViewEntry.ve_host_group.name,
606fcf3ce44SJohn Forte 		    sizeof (stmfGroupName));
607fcf3ce44SJohn Forte 		ioctlViewEntry.ve_host_group.name_size =
608fcf3ce44SJohn Forte 		    strlen((char *)viewEntry->hostGroup);
609fcf3ce44SJohn Forte 	}
610fcf3ce44SJohn Forte 	if (viewEntry->allTargets == B_FALSE) {
611fcf3ce44SJohn Forte 		bcopy(viewEntry->targetGroup,
612fcf3ce44SJohn Forte 		    &ioctlViewEntry.ve_target_group.name,
613fcf3ce44SJohn Forte 		    sizeof (stmfGroupName));
614fcf3ce44SJohn Forte 		ioctlViewEntry.ve_target_group.name_size =
615fcf3ce44SJohn Forte 		    strlen((char *)viewEntry->targetGroup);
616fcf3ce44SJohn Forte 	}
617fcf3ce44SJohn Forte 	if (viewEntry->luNbrValid) {
618fcf3ce44SJohn Forte 		bcopy(viewEntry->luNbr, &ioctlViewEntry.ve_lu_nbr,
619fcf3ce44SJohn Forte 		    sizeof (ioctlViewEntry.ve_lu_nbr));
620fcf3ce44SJohn Forte 	}
621fcf3ce44SJohn Forte 	bcopy(lu, &ioctlViewEntry.ve_guid, sizeof (stmfGuid));
622fcf3ce44SJohn Forte 
623fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
624fcf3ce44SJohn Forte 	/*
625fcf3ce44SJohn Forte 	 * Issue ioctl to add to the view entry
626fcf3ce44SJohn Forte 	 */
627fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
628fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ioctlViewEntry);
629fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ioctlViewEntry;
630fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (ioctlViewEntry);
631fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&ioctlViewEntry;
632fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_ADD_VIEW_ENTRY, &stmfIoctl);
633fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
634fcf3ce44SJohn Forte 		switch (errno) {
635fcf3ce44SJohn Forte 			case EBUSY:
636fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
637fcf3ce44SJohn Forte 				break;
6388fe96085Stim szeto 			case EPERM:
6398fe96085Stim szeto 				ret = STMF_ERROR_PERM;
6408fe96085Stim szeto 				break;
641fcf3ce44SJohn Forte 			case EACCES:
642fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
643fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
644fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
645fcf3ce44SJohn Forte 						break;
646fcf3ce44SJohn Forte 					default:
647fcf3ce44SJohn Forte 						ret = STMF_ERROR_PERM;
648fcf3ce44SJohn Forte 						break;
649fcf3ce44SJohn Forte 				}
650fcf3ce44SJohn Forte 				break;
651fcf3ce44SJohn Forte 			default:
652fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
653fcf3ce44SJohn Forte 					case STMF_IOCERR_LU_NUMBER_IN_USE:
654fcf3ce44SJohn Forte 						ret = STMF_ERROR_LUN_IN_USE;
655fcf3ce44SJohn Forte 						break;
656fcf3ce44SJohn Forte 					case STMF_IOCERR_VIEW_ENTRY_CONFLICT:
657fcf3ce44SJohn Forte 						ret = STMF_ERROR_VE_CONFLICT;
658fcf3ce44SJohn Forte 						break;
659fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
660fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
661fcf3ce44SJohn Forte 						break;
662fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG:
663fcf3ce44SJohn Forte 						ret = STMF_ERROR_INVALID_HG;
664fcf3ce44SJohn Forte 						break;
665fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG:
666fcf3ce44SJohn Forte 						ret = STMF_ERROR_INVALID_TG;
667fcf3ce44SJohn Forte 						break;
668fcf3ce44SJohn Forte 					default:
669fcf3ce44SJohn Forte 						syslog(LOG_DEBUG,
670fcf3ce44SJohn Forte 						    "addViewEntryIoctl"
671fcf3ce44SJohn Forte 						    ":error(%d)",
672fcf3ce44SJohn Forte 						    stmfIoctl.stmf_error);
673fcf3ce44SJohn Forte 						ret = STMF_STATUS_ERROR;
674fcf3ce44SJohn Forte 						break;
675fcf3ce44SJohn Forte 				}
676fcf3ce44SJohn Forte 				break;
677fcf3ce44SJohn Forte 		}
678fcf3ce44SJohn Forte 		goto done;
679fcf3ce44SJohn Forte 	}
680fcf3ce44SJohn Forte 
681fcf3ce44SJohn Forte 	/* copy lu nbr back to caller's view entry on success */
682fcf3ce44SJohn Forte 	viewEntry->veIndex = ioctlViewEntry.ve_ndx;
683fcf3ce44SJohn Forte 	if (ioctlViewEntry.ve_lu_number_valid) {
684fcf3ce44SJohn Forte 		bcopy(&ioctlViewEntry.ve_lu_nbr, viewEntry->luNbr,
685fcf3ce44SJohn Forte 		    sizeof (ioctlViewEntry.ve_lu_nbr));
686fcf3ce44SJohn Forte 	}
687fcf3ce44SJohn Forte 	viewEntry->luNbrValid = B_TRUE;
688fcf3ce44SJohn Forte 
689fcf3ce44SJohn Forte done:
690fcf3ce44SJohn Forte 	return (ret);
691fcf3ce44SJohn Forte }
692fcf3ce44SJohn Forte 
693fcf3ce44SJohn Forte /*
694fcf3ce44SJohn Forte  * stmfAddViewEntry
695fcf3ce44SJohn Forte  *
696fcf3ce44SJohn Forte  * Purpose: Adds a view entry to a logical unit
697fcf3ce44SJohn Forte  *
698fcf3ce44SJohn Forte  * lu - guid of the logical unit to which the view entry is added
699fcf3ce44SJohn Forte  * viewEntry - view entry structure to add
700fcf3ce44SJohn Forte  */
701fcf3ce44SJohn Forte int
702fcf3ce44SJohn Forte stmfAddViewEntry(stmfGuid *lu, stmfViewEntry *viewEntry)
703fcf3ce44SJohn Forte {
704fcf3ce44SJohn Forte 	int ret;
705fcf3ce44SJohn Forte 	int fd;
706fcf3ce44SJohn Forte 	stmfViewEntry iViewEntry;
707fcf3ce44SJohn Forte 
708fcf3ce44SJohn Forte 	if (lu == NULL || viewEntry == NULL) {
709fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
710fcf3ce44SJohn Forte 	}
711fcf3ce44SJohn Forte 
712fcf3ce44SJohn Forte 	/* initialize and set internal view entry */
713fcf3ce44SJohn Forte 	bzero(&iViewEntry, sizeof (iViewEntry));
714fcf3ce44SJohn Forte 
715fcf3ce44SJohn Forte 	if (!viewEntry->allHosts) {
716fcf3ce44SJohn Forte 		bcopy(viewEntry->hostGroup, iViewEntry.hostGroup,
717fcf3ce44SJohn Forte 		    sizeof (iViewEntry.hostGroup));
718fcf3ce44SJohn Forte 	} else {
719fcf3ce44SJohn Forte 		iViewEntry.allHosts = B_TRUE;
720fcf3ce44SJohn Forte 	}
721fcf3ce44SJohn Forte 
722fcf3ce44SJohn Forte 	if (!viewEntry->allTargets) {
723fcf3ce44SJohn Forte 		bcopy(viewEntry->targetGroup, iViewEntry.targetGroup,
724fcf3ce44SJohn Forte 		    sizeof (iViewEntry.targetGroup));
725fcf3ce44SJohn Forte 	} else {
726fcf3ce44SJohn Forte 		iViewEntry.allTargets = B_TRUE;
727fcf3ce44SJohn Forte 	}
728fcf3ce44SJohn Forte 
729fcf3ce44SJohn Forte 	if (viewEntry->luNbrValid) {
730fcf3ce44SJohn Forte 		iViewEntry.luNbrValid = B_TRUE;
731fcf3ce44SJohn Forte 		bcopy(viewEntry->luNbr, iViewEntry.luNbr,
732fcf3ce44SJohn Forte 		    sizeof (iViewEntry.luNbr));
733fcf3ce44SJohn Forte 	}
734fcf3ce44SJohn Forte 
735fcf3ce44SJohn Forte 	/*
736fcf3ce44SJohn Forte 	 * set users return view entry index valid flag to false
737fcf3ce44SJohn Forte 	 * in case of failure
738fcf3ce44SJohn Forte 	 */
739fcf3ce44SJohn Forte 	viewEntry->veIndexValid = B_FALSE;
740fcf3ce44SJohn Forte 
741fcf3ce44SJohn Forte 	/* Check to ensure service exists */
742fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
743fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
744fcf3ce44SJohn Forte 	}
745fcf3ce44SJohn Forte 
746fcf3ce44SJohn Forte 	/* call init */
747fcf3ce44SJohn Forte 	ret = initializeConfig();
748fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
749fcf3ce44SJohn Forte 		return (ret);
750fcf3ce44SJohn Forte 	}
751fcf3ce44SJohn Forte 
752fcf3ce44SJohn Forte 	/*
753fcf3ce44SJohn Forte 	 * Open control node for stmf
754fcf3ce44SJohn Forte 	 */
755fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
756fcf3ce44SJohn Forte 		return (ret);
757fcf3ce44SJohn Forte 
758fcf3ce44SJohn Forte 	/*
759fcf3ce44SJohn Forte 	 * First add the view entry to the driver
760fcf3ce44SJohn Forte 	 */
761fcf3ce44SJohn Forte 	ret = addViewEntryIoctl(fd, lu, &iViewEntry);
762fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
763fcf3ce44SJohn Forte 		goto done;
764fcf3ce44SJohn Forte 	}
765fcf3ce44SJohn Forte 
7668fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
7678fe96085Stim szeto 		goto done;
7688fe96085Stim szeto 	}
7698fe96085Stim szeto 
770fcf3ce44SJohn Forte 	/*
771fcf3ce44SJohn Forte 	 * If the add to driver was successful, add it to the persistent
772fcf3ce44SJohn Forte 	 * store.
773fcf3ce44SJohn Forte 	 */
774fcf3ce44SJohn Forte 	ret = psAddViewEntry(lu, &iViewEntry);
775fcf3ce44SJohn Forte 	switch (ret) {
776fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
777fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
778fcf3ce44SJohn Forte 			break;
779fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
780fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
781fcf3ce44SJohn Forte 			break;
782fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
783fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
784fcf3ce44SJohn Forte 			break;
785fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
786fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
787fcf3ce44SJohn Forte 			break;
788fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
789fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
790fcf3ce44SJohn Forte 			break;
791fcf3ce44SJohn Forte 		default:
792fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
793fcf3ce44SJohn Forte 			    "stmfAddViewEntry:psAddViewEntry:error(%d)", ret);
794fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
795fcf3ce44SJohn Forte 			break;
796fcf3ce44SJohn Forte 	}
797fcf3ce44SJohn Forte 
798fcf3ce44SJohn Forte done:
799fcf3ce44SJohn Forte 	(void) close(fd);
800fcf3ce44SJohn Forte 
801fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
802fcf3ce44SJohn Forte 		/* set caller's view entry on success */
803fcf3ce44SJohn Forte 		viewEntry->veIndexValid = iViewEntry.veIndexValid;
804fcf3ce44SJohn Forte 		viewEntry->veIndex = iViewEntry.veIndex;
805fcf3ce44SJohn Forte 		viewEntry->luNbrValid = B_TRUE;
806fcf3ce44SJohn Forte 		bcopy(iViewEntry.luNbr, viewEntry->luNbr,
807fcf3ce44SJohn Forte 		    sizeof (iViewEntry.luNbr));
808fcf3ce44SJohn Forte 	}
809fcf3ce44SJohn Forte 	return (ret);
810fcf3ce44SJohn Forte }
811fcf3ce44SJohn Forte 
812fcf3ce44SJohn Forte /*
813fcf3ce44SJohn Forte  * stmfClearProviderData
814fcf3ce44SJohn Forte  *
815fcf3ce44SJohn Forte  * Purpose: delete all provider data for specified provider
816fcf3ce44SJohn Forte  *
817fcf3ce44SJohn Forte  * providerName - name of provider for which data should be deleted
818fcf3ce44SJohn Forte  */
819fcf3ce44SJohn Forte int
820fcf3ce44SJohn Forte stmfClearProviderData(char *providerName, int providerType)
821fcf3ce44SJohn Forte {
822fcf3ce44SJohn Forte 	int ret;
823fcf3ce44SJohn Forte 	int fd;
824fcf3ce44SJohn Forte 	int ioctlRet;
825fcf3ce44SJohn Forte 	int savedErrno;
826fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
827fcf3ce44SJohn Forte 	stmf_ppioctl_data_t ppi;
828fcf3ce44SJohn Forte 
829fcf3ce44SJohn Forte 	/* call init */
830fcf3ce44SJohn Forte 	ret = initializeConfig();
831fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
832fcf3ce44SJohn Forte 		return (ret);
833fcf3ce44SJohn Forte 	}
834fcf3ce44SJohn Forte 
835fcf3ce44SJohn Forte 	if (providerName == NULL) {
836fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
837fcf3ce44SJohn Forte 	}
838fcf3ce44SJohn Forte 
839fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
840fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
841fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
842fcf3ce44SJohn Forte 	}
843fcf3ce44SJohn Forte 
844fcf3ce44SJohn Forte 	/*
845fcf3ce44SJohn Forte 	 * Open control node for stmf
846fcf3ce44SJohn Forte 	 */
847fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
848fcf3ce44SJohn Forte 		return (ret);
849fcf3ce44SJohn Forte 
850fcf3ce44SJohn Forte 	bzero(&ppi, sizeof (ppi));
851fcf3ce44SJohn Forte 
852fcf3ce44SJohn Forte 	(void) strncpy(ppi.ppi_name, providerName, sizeof (ppi.ppi_name));
853fcf3ce44SJohn Forte 
854fcf3ce44SJohn Forte 	switch (providerType) {
855fcf3ce44SJohn Forte 		case STMF_LU_PROVIDER_TYPE:
856fcf3ce44SJohn Forte 			ppi.ppi_lu_provider = 1;
857fcf3ce44SJohn Forte 			break;
858fcf3ce44SJohn Forte 		case STMF_PORT_PROVIDER_TYPE:
859fcf3ce44SJohn Forte 			ppi.ppi_port_provider = 1;
860fcf3ce44SJohn Forte 			break;
861fcf3ce44SJohn Forte 		default:
862fcf3ce44SJohn Forte 			ret = STMF_ERROR_INVALID_ARG;
863fcf3ce44SJohn Forte 			goto done;
864fcf3ce44SJohn Forte 	}
865fcf3ce44SJohn Forte 
866fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
867fcf3ce44SJohn Forte 
868fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
869fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_ppioctl_data_t);
870fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ppi;
871fcf3ce44SJohn Forte 
872fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_CLEAR_PP_DATA, &stmfIoctl);
873fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
874fcf3ce44SJohn Forte 		savedErrno = errno;
875fcf3ce44SJohn Forte 		switch (savedErrno) {
876fcf3ce44SJohn Forte 			case EBUSY:
877fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
878fcf3ce44SJohn Forte 				break;
8798fe96085Stim szeto 			case EPERM:
880fcf3ce44SJohn Forte 			case EACCES:
881fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
882fcf3ce44SJohn Forte 				break;
883fcf3ce44SJohn Forte 			default:
884fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
885fcf3ce44SJohn Forte 				    "stmfClearProviderData:ioctl error(%d)",
886fcf3ce44SJohn Forte 				    ioctlRet);
887fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
888fcf3ce44SJohn Forte 				break;
889fcf3ce44SJohn Forte 		}
890fcf3ce44SJohn Forte 		if (savedErrno != ENOENT) {
891fcf3ce44SJohn Forte 			goto done;
892fcf3ce44SJohn Forte 		}
893fcf3ce44SJohn Forte 	}
894fcf3ce44SJohn Forte 
8958fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
8968fe96085Stim szeto 		goto done;
8978fe96085Stim szeto 	}
8988fe96085Stim szeto 
899fcf3ce44SJohn Forte 	ret = psClearProviderData(providerName, providerType);
900fcf3ce44SJohn Forte 	switch (ret) {
901fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
902fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
903fcf3ce44SJohn Forte 			break;
904fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
905fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
906fcf3ce44SJohn Forte 			break;
907fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
908fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
909fcf3ce44SJohn Forte 			break;
910fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
911fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
912fcf3ce44SJohn Forte 			break;
913fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
914fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
915fcf3ce44SJohn Forte 			break;
916fcf3ce44SJohn Forte 		default:
917fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
918fcf3ce44SJohn Forte 			    "stmfClearProviderData:psClearProviderData"
919fcf3ce44SJohn Forte 			    ":error(%d)", ret);
920fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
921fcf3ce44SJohn Forte 			break;
922fcf3ce44SJohn Forte 	}
923fcf3ce44SJohn Forte 
924fcf3ce44SJohn Forte done:
925fcf3ce44SJohn Forte 	(void) close(fd);
926fcf3ce44SJohn Forte 	return (ret);
927fcf3ce44SJohn Forte }
928fcf3ce44SJohn Forte 
929fcf3ce44SJohn Forte /*
930fcf3ce44SJohn Forte  * stmfCreateHostGroup
931fcf3ce44SJohn Forte  *
932fcf3ce44SJohn Forte  * Purpose: Create a new initiator group
933fcf3ce44SJohn Forte  *
934fcf3ce44SJohn Forte  * hostGroupName - name of host group to create
935fcf3ce44SJohn Forte  */
936fcf3ce44SJohn Forte int
937fcf3ce44SJohn Forte stmfCreateHostGroup(stmfGroupName *hostGroupName)
938fcf3ce44SJohn Forte {
939fcf3ce44SJohn Forte 	int ret;
940fcf3ce44SJohn Forte 	int fd;
941fcf3ce44SJohn Forte 
942fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
943fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
944fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName))) {
945fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
946fcf3ce44SJohn Forte 	}
947fcf3ce44SJohn Forte 
948fcf3ce44SJohn Forte 	/* Check to ensure service exists */
949fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
950fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
951fcf3ce44SJohn Forte 	}
952fcf3ce44SJohn Forte 
953fcf3ce44SJohn Forte 	/* call init */
954fcf3ce44SJohn Forte 	ret = initializeConfig();
955fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
956fcf3ce44SJohn Forte 		return (ret);
957fcf3ce44SJohn Forte 	}
958fcf3ce44SJohn Forte 
959fcf3ce44SJohn Forte 	/*
960fcf3ce44SJohn Forte 	 * Open control node for stmf
961fcf3ce44SJohn Forte 	 */
962fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
963fcf3ce44SJohn Forte 		return (ret);
964fcf3ce44SJohn Forte 
965fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_HOST_GROUP,
966fcf3ce44SJohn Forte 	    hostGroupName)) != STMF_STATUS_SUCCESS) {
967fcf3ce44SJohn Forte 		goto done;
968fcf3ce44SJohn Forte 	}
969fcf3ce44SJohn Forte 
9708fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
9718fe96085Stim szeto 		goto done;
9728fe96085Stim szeto 	}
9738fe96085Stim szeto 
974fcf3ce44SJohn Forte 	ret = psCreateHostGroup((char *)hostGroupName);
975fcf3ce44SJohn Forte 	switch (ret) {
976fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
977fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
978fcf3ce44SJohn Forte 			break;
979fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
980fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
981fcf3ce44SJohn Forte 			break;
982fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
983fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
984fcf3ce44SJohn Forte 			break;
985fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
986fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
987fcf3ce44SJohn Forte 			break;
988fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
989fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
990fcf3ce44SJohn Forte 			break;
991fcf3ce44SJohn Forte 		default:
992fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
993fcf3ce44SJohn Forte 			    "stmfCreateHostGroup:psCreateHostGroup:error(%d)",
994fcf3ce44SJohn Forte 			    ret);
995fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
996fcf3ce44SJohn Forte 			break;
997fcf3ce44SJohn Forte 	}
998fcf3ce44SJohn Forte 
999fcf3ce44SJohn Forte done:
1000fcf3ce44SJohn Forte 	(void) close(fd);
1001fcf3ce44SJohn Forte 	return (ret);
1002fcf3ce44SJohn Forte }
1003fcf3ce44SJohn Forte 
1004fcf3ce44SJohn Forte /*
10058fe96085Stim szeto  * stmfCreateLu
10068fe96085Stim szeto  *
10078fe96085Stim szeto  * Purpose: Create a logical unit
10088fe96085Stim szeto  *
10098fe96085Stim szeto  * hdl - handle to logical unit resource created via stmfCreateLuResource
10108fe96085Stim szeto  *
10118fe96085Stim szeto  * luGuid - If non-NULL, on success, contains the guid of the created logical
10128fe96085Stim szeto  *	    unit
10138fe96085Stim szeto  */
10148fe96085Stim szeto int
10158fe96085Stim szeto stmfCreateLu(luResource hdl, stmfGuid *luGuid)
10168fe96085Stim szeto {
10178fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
10188fe96085Stim szeto 	luResourceImpl *luPropsHdl = hdl;
10198fe96085Stim szeto 
10208fe96085Stim szeto 	if (hdl == NULL) {
10218fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
10228fe96085Stim szeto 	}
10238fe96085Stim szeto 
10248fe96085Stim szeto 	if (luPropsHdl->type == STMF_DISK) {
10258fe96085Stim szeto 		ret = createDiskLu((diskResource *)luPropsHdl->resource,
10268fe96085Stim szeto 		    luGuid);
10278fe96085Stim szeto 	} else {
10288fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
10298fe96085Stim szeto 	}
10308fe96085Stim szeto 
10318fe96085Stim szeto 	return (ret);
10328fe96085Stim szeto }
10338fe96085Stim szeto 
10348fe96085Stim szeto /*
10358fe96085Stim szeto  * stmfCreateLuResource
10368fe96085Stim szeto  *
10378fe96085Stim szeto  * Purpose: Create resource handle for a logical unit
10388fe96085Stim szeto  *
10398fe96085Stim szeto  * dType - Type of logical unit resource to create
10408fe96085Stim szeto  *	   Can be: STMF_DISK
10418fe96085Stim szeto  *
10428fe96085Stim szeto  * hdl - pointer to luResource
10438fe96085Stim szeto  */
10448fe96085Stim szeto int
10458fe96085Stim szeto stmfCreateLuResource(uint16_t dType, luResource *hdl)
10468fe96085Stim szeto {
10478fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
10488fe96085Stim szeto 
10498fe96085Stim szeto 	if (dType != STMF_DISK || hdl == NULL) {
10508fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
10518fe96085Stim szeto 	}
10528fe96085Stim szeto 
10538fe96085Stim szeto 	*hdl = calloc(1, sizeof (luResourceImpl));
10548fe96085Stim szeto 	if (*hdl == NULL) {
10558fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
10568fe96085Stim szeto 	}
10578fe96085Stim szeto 
10588fe96085Stim szeto 	ret = createDiskResource((luResourceImpl *)*hdl);
10598fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
10608fe96085Stim szeto 		free(*hdl);
10618fe96085Stim szeto 		return (ret);
10628fe96085Stim szeto 	}
10638fe96085Stim szeto 
10648fe96085Stim szeto 	return (STMF_STATUS_SUCCESS);
10658fe96085Stim szeto }
10668fe96085Stim szeto 
10678fe96085Stim szeto /*
10688fe96085Stim szeto  * Creates a disk logical unit
10698fe96085Stim szeto  *
10708fe96085Stim szeto  * disk - pointer to diskResource structure that represents the properties
10718fe96085Stim szeto  *        for the disk logical unit to be created.
10728fe96085Stim szeto  */
10738fe96085Stim szeto static int
10748fe96085Stim szeto createDiskLu(diskResource *disk, stmfGuid *createdGuid)
10758fe96085Stim szeto {
10768fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
10778fe96085Stim szeto 	int dataFileNameLen = 0;
10788fe96085Stim szeto 	int metaFileNameLen = 0;
10798fe96085Stim szeto 	int serialNumLen = 0;
10808fe96085Stim szeto 	int luAliasLen = 0;
10818fe96085Stim szeto 	int sluBufSize = 0;
10828fe96085Stim szeto 	int bufOffset = 0;
10838fe96085Stim szeto 	int fd = 0;
10848fe96085Stim szeto 	int ioctlRet;
10858fe96085Stim szeto 	int savedErrno;
10868fe96085Stim szeto 	stmfGuid guid;
10878fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
10888fe96085Stim szeto 
10898fe96085Stim szeto 	sbd_create_and_reg_lu_t *sbdLu = NULL;
10908fe96085Stim szeto 
10918fe96085Stim szeto 	/*
10928fe96085Stim szeto 	 * Open control node for sbd
10938fe96085Stim szeto 	 */
10948fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
10958fe96085Stim szeto 		return (ret);
10968fe96085Stim szeto 
10978fe96085Stim szeto 	/* data file name must be specified */
10988fe96085Stim szeto 	if (disk->luDataFileNameValid) {
10998fe96085Stim szeto 		dataFileNameLen = strlen(disk->luDataFileName);
11008fe96085Stim szeto 	} else {
11018fe96085Stim szeto 		(void) close(fd);
11028fe96085Stim szeto 		return (STMF_ERROR_MISSING_PROP_VAL);
11038fe96085Stim szeto 	}
11048fe96085Stim szeto 
11058fe96085Stim szeto 	sluBufSize += dataFileNameLen + 1;
11068fe96085Stim szeto 
11078fe96085Stim szeto 	if (disk->luMetaFileNameValid) {
11088fe96085Stim szeto 		metaFileNameLen = strlen(disk->luMetaFileName);
11098fe96085Stim szeto 		sluBufSize += metaFileNameLen + 1;
11108fe96085Stim szeto 	}
11118fe96085Stim szeto 
11128fe96085Stim szeto 	serialNumLen = strlen(disk->serialNum);
11138fe96085Stim szeto 	sluBufSize += serialNumLen;
11148fe96085Stim szeto 
11158fe96085Stim szeto 	if (disk->luAliasValid) {
11168fe96085Stim szeto 		luAliasLen = strlen(disk->luAlias);
11178fe96085Stim szeto 		sluBufSize += luAliasLen + 1;
11188fe96085Stim szeto 	}
11198fe96085Stim szeto 
11208fe96085Stim szeto 	/*
11218fe96085Stim szeto 	 * 8 is the size of the buffer set aside for
11228fe96085Stim szeto 	 * concatenation of variable length fields
11238fe96085Stim szeto 	 */
11248fe96085Stim szeto 	sbdLu = (sbd_create_and_reg_lu_t *)calloc(1,
11258fe96085Stim szeto 	    sizeof (sbd_create_and_reg_lu_t) + sluBufSize - 8);
11268fe96085Stim szeto 	if (sbdLu == NULL) {
11278fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
11288fe96085Stim szeto 	}
11298fe96085Stim szeto 
11308fe96085Stim szeto 	sbdLu->slu_struct_size = sizeof (sbd_create_and_reg_lu_t) +
11318fe96085Stim szeto 	    sluBufSize - 8;
11328fe96085Stim szeto 
11338fe96085Stim szeto 	if (metaFileNameLen) {
11348fe96085Stim szeto 		sbdLu->slu_meta_fname_valid = 1;
11358fe96085Stim szeto 		sbdLu->slu_meta_fname_off = bufOffset;
11368fe96085Stim szeto 		bcopy(disk->luMetaFileName, &(sbdLu->slu_buf[bufOffset]),
11378fe96085Stim szeto 		    metaFileNameLen + 1);
11388fe96085Stim szeto 		bufOffset += metaFileNameLen + 1;
11398fe96085Stim szeto 	}
11408fe96085Stim szeto 
11418fe96085Stim szeto 	bcopy(disk->luDataFileName, &(sbdLu->slu_buf[bufOffset]),
11428fe96085Stim szeto 	    dataFileNameLen + 1);
11438fe96085Stim szeto 	sbdLu->slu_data_fname_off = bufOffset;
11448fe96085Stim szeto 	bufOffset += dataFileNameLen + 1;
11458fe96085Stim szeto 
11468fe96085Stim szeto 	/* currently, serial # is not passed null terminated to the driver */
11478fe96085Stim szeto 	if (disk->serialNumValid) {
11488fe96085Stim szeto 		sbdLu->slu_serial_valid = 1;
11498fe96085Stim szeto 		sbdLu->slu_serial_off = bufOffset;
11508fe96085Stim szeto 		sbdLu->slu_serial_size = serialNumLen;
11518fe96085Stim szeto 		bcopy(disk->serialNum, &(sbdLu->slu_buf[bufOffset]),
11528fe96085Stim szeto 		    serialNumLen);
11538fe96085Stim szeto 		bufOffset += serialNumLen;
11548fe96085Stim szeto 	}
11558fe96085Stim szeto 
11568fe96085Stim szeto 	if (disk->luAliasValid) {
11578fe96085Stim szeto 		sbdLu->slu_alias_valid = 1;
11588fe96085Stim szeto 		sbdLu->slu_alias_off = bufOffset;
11598fe96085Stim szeto 		bcopy(disk->luAlias, &(sbdLu->slu_buf[bufOffset]),
11608fe96085Stim szeto 		    luAliasLen + 1);
11618fe96085Stim szeto 		bufOffset += luAliasLen + 1;
11628fe96085Stim szeto 	}
11638fe96085Stim szeto 
11648fe96085Stim szeto 	if (disk->luSizeValid) {
11658fe96085Stim szeto 		sbdLu->slu_lu_size_valid = 1;
11668fe96085Stim szeto 		sbdLu->slu_lu_size = disk->luSize;
11678fe96085Stim szeto 	}
11688fe96085Stim szeto 
11698fe96085Stim szeto 	if (disk->luGuidValid) {
11708fe96085Stim szeto 		sbdLu->slu_guid_valid = 1;
11718fe96085Stim szeto 		bcopy(disk->luGuid, sbdLu->slu_guid, sizeof (disk->luGuid));
11728fe96085Stim szeto 	}
11738fe96085Stim szeto 
11748fe96085Stim szeto 	if (disk->vidValid) {
11758fe96085Stim szeto 		sbdLu->slu_vid_valid = 1;
11768fe96085Stim szeto 		bcopy(disk->vid, sbdLu->slu_vid, sizeof (disk->vid));
11778fe96085Stim szeto 	}
11788fe96085Stim szeto 
11798fe96085Stim szeto 	if (disk->pidValid) {
11808fe96085Stim szeto 		sbdLu->slu_pid_valid = 1;
11818fe96085Stim szeto 		bcopy(disk->pid, sbdLu->slu_pid, sizeof (disk->pid));
11828fe96085Stim szeto 	}
11838fe96085Stim szeto 
11848fe96085Stim szeto 	if (disk->revValid) {
11858fe96085Stim szeto 		sbdLu->slu_rev_valid = 1;
11868fe96085Stim szeto 		bcopy(disk->rev, sbdLu->slu_rev, sizeof (disk->rev));
11878fe96085Stim szeto 	}
11888fe96085Stim szeto 
11898fe96085Stim szeto 	if (disk->companyIdValid) {
11908fe96085Stim szeto 		sbdLu->slu_company_id_valid = 1;
11918fe96085Stim szeto 		sbdLu->slu_company_id = disk->companyId;
11928fe96085Stim szeto 	}
11938fe96085Stim szeto 
11948fe96085Stim szeto 	if (disk->blkSizeValid) {
11958fe96085Stim szeto 		sbdLu->slu_blksize_valid = 1;
11968fe96085Stim szeto 		sbdLu->slu_blksize = disk->blkSize;
11978fe96085Stim szeto 	}
11988fe96085Stim szeto 
11998fe96085Stim szeto 	if (disk->writeProtectEnableValid) {
12008fe96085Stim szeto 		if (disk->writeProtectEnable) {
12018fe96085Stim szeto 			sbdLu->slu_write_protected = 1;
12028fe96085Stim szeto 		}
12038fe96085Stim szeto 	}
12048fe96085Stim szeto 
12058fe96085Stim szeto 	if (disk->writebackCacheDisableValid) {
12068fe96085Stim szeto 		sbdLu->slu_writeback_cache_disable_valid = 1;
12078fe96085Stim szeto 		if (disk->writebackCacheDisable) {
12088fe96085Stim szeto 			sbdLu->slu_writeback_cache_disable = 1;
12098fe96085Stim szeto 		}
12108fe96085Stim szeto 	}
12118fe96085Stim szeto 
12128fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
12138fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdLu->slu_struct_size;
12148fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdLu;
12158fe96085Stim szeto 	sbdIoctl.stmf_obuf_size = sbdLu->slu_struct_size;
12168fe96085Stim szeto 	sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdLu;
12178fe96085Stim szeto 
12188fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_CREATE_AND_REGISTER_LU, &sbdIoctl);
12198fe96085Stim szeto 	if (ioctlRet != 0) {
12208fe96085Stim szeto 		savedErrno = errno;
12218fe96085Stim szeto 		switch (savedErrno) {
12228fe96085Stim szeto 			case EBUSY:
12238fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
12248fe96085Stim szeto 				break;
12258fe96085Stim szeto 			case EPERM:
12268fe96085Stim szeto 			case EACCES:
12278fe96085Stim szeto 				ret = STMF_ERROR_PERM;
12288fe96085Stim szeto 				break;
12298fe96085Stim szeto 			default:
12308fe96085Stim szeto 				diskError(sbdIoctl.stmf_error, &ret);
12318fe96085Stim szeto 				if (ret == STMF_STATUS_ERROR) {
12328fe96085Stim szeto 					syslog(LOG_DEBUG,
12338fe96085Stim szeto 					"createDiskLu:ioctl "
12348fe96085Stim szeto 					"error(%d) (%d) (%d)", ioctlRet,
12358fe96085Stim szeto 					    sbdIoctl.stmf_error, savedErrno);
12368fe96085Stim szeto 				}
12378fe96085Stim szeto 				break;
12388fe96085Stim szeto 		}
12398fe96085Stim szeto 	}
12408fe96085Stim szeto 
12418fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
12428fe96085Stim szeto 		goto done;
12438fe96085Stim szeto 	}
12448fe96085Stim szeto 
12458fe96085Stim szeto 	/*
12468fe96085Stim szeto 	 * on success, copy the resulting guid into the caller's guid if not
12478fe96085Stim szeto 	 * NULL
12488fe96085Stim szeto 	 */
12498fe96085Stim szeto 	if (createdGuid) {
12508fe96085Stim szeto 		bcopy(sbdLu->slu_guid, createdGuid->guid,
12518fe96085Stim szeto 		    sizeof (sbdLu->slu_guid));
12528fe96085Stim szeto 	}
12538fe96085Stim szeto 
12548fe96085Stim szeto 	bcopy(sbdLu->slu_guid, guid.guid, sizeof (sbdLu->slu_guid));
12558fe96085Stim szeto 	if (disk->luMetaFileNameValid) {
12568fe96085Stim szeto 		ret = addGuidToDiskStore(&guid, disk->luMetaFileName);
12578fe96085Stim szeto 	} else {
12588fe96085Stim szeto 		ret = addGuidToDiskStore(&guid, disk->luDataFileName);
12598fe96085Stim szeto 	}
12608fe96085Stim szeto done:
12618fe96085Stim szeto 	free(sbdLu);
12628fe96085Stim szeto 	(void) close(fd);
12638fe96085Stim szeto 	return (ret);
12648fe96085Stim szeto }
12658fe96085Stim szeto 
12668fe96085Stim szeto 
12678fe96085Stim szeto /*
12688fe96085Stim szeto  * stmfImportLu
12698fe96085Stim szeto  *
12708fe96085Stim szeto  * Purpose: Import a previously created logical unit
12718fe96085Stim szeto  *
12728fe96085Stim szeto  * dType - Type of logical unit
12738fe96085Stim szeto  *         Can be: STMF_DISK
12748fe96085Stim szeto  *
12758fe96085Stim szeto  * luGuid - If non-NULL, on success, contains the guid of the imported logical
12768fe96085Stim szeto  *	    unit
12778fe96085Stim szeto  *
12788fe96085Stim szeto  * fname - A file name where the metadata resides
12798fe96085Stim szeto  *
12808fe96085Stim szeto  */
12818fe96085Stim szeto int
12828fe96085Stim szeto stmfImportLu(uint16_t dType, char *fname, stmfGuid *luGuid)
12838fe96085Stim szeto {
12848fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
12858fe96085Stim szeto 
12868fe96085Stim szeto 	if (dType == STMF_DISK) {
12878fe96085Stim szeto 		ret = importDiskLu(fname, luGuid);
12888fe96085Stim szeto 	} else {
12898fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
12908fe96085Stim szeto 	}
12918fe96085Stim szeto 
12928fe96085Stim szeto 	return (ret);
12938fe96085Stim szeto }
12948fe96085Stim szeto 
12958fe96085Stim szeto /*
12968fe96085Stim szeto  * importDiskLu
12978fe96085Stim szeto  *
12988fe96085Stim szeto  * filename - filename to import
12998fe96085Stim szeto  * createdGuid - if not NULL, on success contains the imported guid
13008fe96085Stim szeto  *
13018fe96085Stim szeto  */
13028fe96085Stim szeto static int
13038fe96085Stim szeto importDiskLu(char *fname, stmfGuid *createdGuid)
13048fe96085Stim szeto {
13058fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
13068fe96085Stim szeto 	int fd = 0;
13078fe96085Stim szeto 	int ioctlRet;
13088fe96085Stim szeto 	int savedErrno;
13098fe96085Stim szeto 	int metaFileNameLen;
13108fe96085Stim szeto 	stmfGuid iGuid;
13118fe96085Stim szeto 	int iluBufSize = 0;
13128fe96085Stim szeto 	sbd_import_lu_t *sbdLu = NULL;
13138fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
13148fe96085Stim szeto 
13158fe96085Stim szeto 	if (fname == NULL) {
13168fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
13178fe96085Stim szeto 	}
13188fe96085Stim szeto 
13198fe96085Stim szeto 	/*
13208fe96085Stim szeto 	 * Open control node for sbd
13218fe96085Stim szeto 	 */
13228fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
13238fe96085Stim szeto 		return (ret);
13248fe96085Stim szeto 
13258fe96085Stim szeto 	metaFileNameLen = strlen(fname);
13268fe96085Stim szeto 	iluBufSize += metaFileNameLen + 1;
13278fe96085Stim szeto 
13288fe96085Stim szeto 	/*
13298fe96085Stim szeto 	 * 8 is the size of the buffer set aside for
13308fe96085Stim szeto 	 * concatenation of variable length fields
13318fe96085Stim szeto 	 */
13328fe96085Stim szeto 	sbdLu = (sbd_import_lu_t *)calloc(1,
13338fe96085Stim szeto 	    sizeof (sbd_import_lu_t) + iluBufSize - 8);
13348fe96085Stim szeto 	if (sbdLu == NULL) {
13358fe96085Stim szeto 		(void) close(fd);
13368fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
13378fe96085Stim szeto 	}
13388fe96085Stim szeto 
13398fe96085Stim szeto 	/*
13408fe96085Stim szeto 	 * Accept either a data file or meta data file.
13418fe96085Stim szeto 	 * sbd will do the right thing here either way.
13428fe96085Stim szeto 	 * i.e. if it's a data file, it assumes that the
13438fe96085Stim szeto 	 * meta data is shared with the data.
13448fe96085Stim szeto 	 */
13458fe96085Stim szeto 	(void) strncpy(sbdLu->ilu_meta_fname, fname, metaFileNameLen);
13468fe96085Stim szeto 
13478fe96085Stim szeto 	sbdLu->ilu_struct_size = sizeof (sbd_import_lu_t) + iluBufSize - 8;
13488fe96085Stim szeto 
13498fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
13508fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdLu->ilu_struct_size;
13518fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdLu;
13528fe96085Stim szeto 	sbdIoctl.stmf_obuf_size = sbdLu->ilu_struct_size;
13538fe96085Stim szeto 	sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdLu;
13548fe96085Stim szeto 
13558fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_IMPORT_LU, &sbdIoctl);
13568fe96085Stim szeto 	if (ioctlRet != 0) {
13578fe96085Stim szeto 		savedErrno = errno;
13588fe96085Stim szeto 		switch (savedErrno) {
13598fe96085Stim szeto 			case EBUSY:
13608fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
13618fe96085Stim szeto 				break;
13628fe96085Stim szeto 			case EPERM:
13638fe96085Stim szeto 			case EACCES:
13648fe96085Stim szeto 				ret = STMF_ERROR_PERM;
13658fe96085Stim szeto 				break;
13668fe96085Stim szeto 			default:
13678fe96085Stim szeto 				diskError(sbdIoctl.stmf_error, &ret);
13688fe96085Stim szeto 				if (ret == STMF_STATUS_ERROR) {
13698fe96085Stim szeto 					syslog(LOG_DEBUG,
13708fe96085Stim szeto 					"importDiskLu:ioctl "
13718fe96085Stim szeto 					"error(%d) (%d) (%d)", ioctlRet,
13728fe96085Stim szeto 					    sbdIoctl.stmf_error, savedErrno);
13738fe96085Stim szeto 				}
13748fe96085Stim szeto 				break;
13758fe96085Stim szeto 		}
13768fe96085Stim szeto 	}
13778fe96085Stim szeto 
13788fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
13798fe96085Stim szeto 		goto done;
13808fe96085Stim szeto 	}
13818fe96085Stim szeto 
13828fe96085Stim szeto 	/*
13838fe96085Stim szeto 	 * on success, copy the resulting guid into the caller's guid if not
13848fe96085Stim szeto 	 * NULL and add it to the persistent store for sbd
13858fe96085Stim szeto 	 */
13868fe96085Stim szeto 	if (createdGuid) {
13878fe96085Stim szeto 		bcopy(sbdLu->ilu_ret_guid, createdGuid->guid,
13888fe96085Stim szeto 		    sizeof (sbdLu->ilu_ret_guid));
13898fe96085Stim szeto 		ret = addGuidToDiskStore(createdGuid, fname);
13908fe96085Stim szeto 	} else {
13918fe96085Stim szeto 		bcopy(sbdLu->ilu_ret_guid, iGuid.guid,
13928fe96085Stim szeto 		    sizeof (sbdLu->ilu_ret_guid));
13938fe96085Stim szeto 		ret = addGuidToDiskStore(&iGuid, fname);
13948fe96085Stim szeto 	}
13958fe96085Stim szeto done:
13968fe96085Stim szeto 	free(sbdLu);
13978fe96085Stim szeto 	(void) close(fd);
13988fe96085Stim szeto 	return (ret);
13998fe96085Stim szeto }
14008fe96085Stim szeto 
14018fe96085Stim szeto /*
14028fe96085Stim szeto  * diskError
14038fe96085Stim szeto  *
14048fe96085Stim szeto  * Purpose: Translate sbd driver error
14058fe96085Stim szeto  */
14068fe96085Stim szeto static void
14078fe96085Stim szeto diskError(uint32_t stmfError, int *ret)
14088fe96085Stim szeto {
14098fe96085Stim szeto 	switch (stmfError) {
14108fe96085Stim szeto 		case SBD_RET_META_CREATION_FAILED:
14118fe96085Stim szeto 		case SBD_RET_ZFS_META_CREATE_FAILED:
14128fe96085Stim szeto 			*ret = STMF_ERROR_META_CREATION;
14138fe96085Stim szeto 			break;
14148fe96085Stim szeto 		case SBD_RET_INVALID_BLKSIZE:
14158fe96085Stim szeto 			*ret = STMF_ERROR_INVALID_BLKSIZE;
14168fe96085Stim szeto 			break;
14178fe96085Stim szeto 		case SBD_RET_FILE_ALREADY_REGISTERED:
14188fe96085Stim szeto 			*ret = STMF_ERROR_FILE_IN_USE;
14198fe96085Stim szeto 			break;
14208fe96085Stim szeto 		case SBD_RET_GUID_ALREADY_REGISTERED:
14218fe96085Stim szeto 			*ret = STMF_ERROR_GUID_IN_USE;
14228fe96085Stim szeto 			break;
14238fe96085Stim szeto 		case SBD_RET_META_PATH_NOT_ABSOLUTE:
14248fe96085Stim szeto 		case SBD_RET_META_FILE_LOOKUP_FAILED:
14258fe96085Stim szeto 		case SBD_RET_META_FILE_OPEN_FAILED:
14268fe96085Stim szeto 		case SBD_RET_META_FILE_GETATTR_FAILED:
14278fe96085Stim szeto 		case SBD_RET_NO_META:
14288fe96085Stim szeto 			*ret = STMF_ERROR_META_FILE_NAME;
14298fe96085Stim szeto 			break;
14308fe96085Stim szeto 		case SBD_RET_DATA_PATH_NOT_ABSOLUTE:
14318fe96085Stim szeto 		case SBD_RET_DATA_FILE_LOOKUP_FAILED:
14328fe96085Stim szeto 		case SBD_RET_DATA_FILE_OPEN_FAILED:
14338fe96085Stim szeto 		case SBD_RET_DATA_FILE_GETATTR_FAILED:
14348fe96085Stim szeto 			*ret = STMF_ERROR_DATA_FILE_NAME;
14358fe96085Stim szeto 			break;
14368fe96085Stim szeto 		case SBD_RET_FILE_SIZE_ERROR:
14378fe96085Stim szeto 			*ret = STMF_ERROR_FILE_SIZE_INVALID;
14388fe96085Stim szeto 			break;
14398fe96085Stim szeto 		case SBD_RET_SIZE_OUT_OF_RANGE:
14408fe96085Stim szeto 			*ret = STMF_ERROR_SIZE_OUT_OF_RANGE;
14418fe96085Stim szeto 			break;
14428fe96085Stim szeto 		case SBD_RET_LU_BUSY:
14438fe96085Stim szeto 			*ret = STMF_ERROR_LU_BUSY;
14448fe96085Stim szeto 			break;
14458fe96085Stim szeto 		case SBD_RET_WRITE_CACHE_SET_FAILED:
14468fe96085Stim szeto 			*ret = STMF_ERROR_WRITE_CACHE_SET;
14478fe96085Stim szeto 			break;
14488fe96085Stim szeto 		default:
14498fe96085Stim szeto 			*ret = STMF_STATUS_ERROR;
14508fe96085Stim szeto 			break;
14518fe96085Stim szeto 	}
14528fe96085Stim szeto }
14538fe96085Stim szeto 
14548fe96085Stim szeto /*
14558fe96085Stim szeto  * Creates a logical unit resource of type STMF_DISK.
14568fe96085Stim szeto  *
14578fe96085Stim szeto  * No defaults should be set here as all defaults are derived from the
14588fe96085Stim szeto  * driver's default settings.
14598fe96085Stim szeto  */
14608fe96085Stim szeto static int
14618fe96085Stim szeto createDiskResource(luResourceImpl *hdl)
14628fe96085Stim szeto {
14638fe96085Stim szeto 	hdl->type = STMF_DISK;
14648fe96085Stim szeto 
14658fe96085Stim szeto 	hdl->resource = calloc(1, sizeof (diskResource));
14668fe96085Stim szeto 	if (hdl->resource == NULL) {
14678fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
14688fe96085Stim szeto 	}
14698fe96085Stim szeto 
14708fe96085Stim szeto 	return (STMF_STATUS_SUCCESS);
14718fe96085Stim szeto }
14728fe96085Stim szeto 
14738fe96085Stim szeto /*
14748fe96085Stim szeto  * stmfDeleteLu
14758fe96085Stim szeto  *
14768fe96085Stim szeto  * Purpose: Delete a logical unit
14778fe96085Stim szeto  *
14788fe96085Stim szeto  * hdl - handle to logical unit resource created via stmfCreateLuResource
14798fe96085Stim szeto  *
14808fe96085Stim szeto  * luGuid - If non-NULL, on success, contains the guid of the created logical
14818fe96085Stim szeto  *	    unit
14828fe96085Stim szeto  */
14838fe96085Stim szeto int
14848fe96085Stim szeto stmfDeleteLu(stmfGuid *luGuid)
14858fe96085Stim szeto {
14868fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
14878fe96085Stim szeto 	stmfLogicalUnitProperties luProps;
14888fe96085Stim szeto 
14898fe96085Stim szeto 	if (luGuid == NULL) {
14908fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
14918fe96085Stim szeto 	}
14928fe96085Stim szeto 
14938fe96085Stim szeto 	/* Check logical unit provider name to call correct dtype function */
14948fe96085Stim szeto 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
14958fe96085Stim szeto 	    != STMF_STATUS_SUCCESS) {
14968fe96085Stim szeto 		return (ret);
14978fe96085Stim szeto 	} else {
14988fe96085Stim szeto 		if (strcmp(luProps.providerName, "sbd") == 0) {
14998fe96085Stim szeto 			ret = deleteDiskLu(luGuid);
15008fe96085Stim szeto 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
15018fe96085Stim szeto 			return (STMF_ERROR_NOT_FOUND);
15028fe96085Stim szeto 		} else {
15038fe96085Stim szeto 			return (STMF_ERROR_INVALID_ARG);
15048fe96085Stim szeto 		}
15058fe96085Stim szeto 	}
15068fe96085Stim szeto 
15078fe96085Stim szeto 	return (ret);
15088fe96085Stim szeto }
15098fe96085Stim szeto 
15108fe96085Stim szeto static int
15118fe96085Stim szeto deleteDiskLu(stmfGuid *luGuid)
15128fe96085Stim szeto {
15138fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
15148fe96085Stim szeto 	int fd;
15158fe96085Stim szeto 	int savedErrno;
15168fe96085Stim szeto 	int ioctlRet;
15178fe96085Stim szeto 	sbd_delete_lu_t deleteLu = {0};
15188fe96085Stim szeto 
15198fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
15208fe96085Stim szeto 
15218fe96085Stim szeto 	/*
15228fe96085Stim szeto 	 * Open control node for sbd
15238fe96085Stim szeto 	 */
15248fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
15258fe96085Stim szeto 		return (ret);
15268fe96085Stim szeto 
15278fe96085Stim szeto 	ret = removeGuidFromDiskStore(luGuid);
15288fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
15298fe96085Stim szeto 		goto done;
15308fe96085Stim szeto 	}
15318fe96085Stim szeto 
15328fe96085Stim szeto 	bcopy(luGuid, deleteLu.dlu_guid, sizeof (deleteLu.dlu_guid));
15338fe96085Stim szeto 	deleteLu.dlu_by_guid = 1;
15348fe96085Stim szeto 
15358fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
15368fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sizeof (deleteLu);
15378fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)&deleteLu;
15388fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_DELETE_LU, &sbdIoctl);
15398fe96085Stim szeto 	if (ioctlRet != 0) {
15408fe96085Stim szeto 		savedErrno = errno;
15418fe96085Stim szeto 		switch (savedErrno) {
15428fe96085Stim szeto 			case EBUSY:
15438fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
15448fe96085Stim szeto 				break;
15458fe96085Stim szeto 			case EPERM:
15468fe96085Stim szeto 			case EACCES:
15478fe96085Stim szeto 				ret = STMF_ERROR_PERM;
15488fe96085Stim szeto 				break;
15498fe96085Stim szeto 			case ENOENT:
15508fe96085Stim szeto 				ret = STMF_ERROR_NOT_FOUND;
15518fe96085Stim szeto 				break;
15528fe96085Stim szeto 			default:
15538fe96085Stim szeto 				syslog(LOG_DEBUG,
15548fe96085Stim szeto 				    "deleteDiskLu:ioctl error(%d) (%d) (%d)",
15558fe96085Stim szeto 				    ioctlRet, sbdIoctl.stmf_error, savedErrno);
15568fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
15578fe96085Stim szeto 				break;
15588fe96085Stim szeto 		}
15598fe96085Stim szeto 	}
15608fe96085Stim szeto 
15618fe96085Stim szeto done:
15628fe96085Stim szeto 	(void) close(fd);
15638fe96085Stim szeto 	return (ret);
15648fe96085Stim szeto }
15658fe96085Stim szeto 
15668fe96085Stim szeto /*
15678fe96085Stim szeto  * stmfModifyLu
15688fe96085Stim szeto  *
15698fe96085Stim szeto  * Purpose: Modify properties of a logical unit
15708fe96085Stim szeto  *
15718fe96085Stim szeto  * luGuid - guid of registered logical unit
15728fe96085Stim szeto  * prop - property to modify
15738fe96085Stim szeto  * propVal - property value to set
15748fe96085Stim szeto  *
15758fe96085Stim szeto  */
15768fe96085Stim szeto int
15778fe96085Stim szeto stmfModifyLu(stmfGuid *luGuid, uint32_t prop, const char *propVal)
15788fe96085Stim szeto {
15798fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
15808fe96085Stim szeto 	stmfLogicalUnitProperties luProps;
15818fe96085Stim szeto 
15828fe96085Stim szeto 	if (luGuid == NULL) {
15838fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
15848fe96085Stim szeto 	}
15858fe96085Stim szeto 
15868fe96085Stim szeto 	/* Check logical unit provider name to call correct dtype function */
15878fe96085Stim szeto 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
15888fe96085Stim szeto 	    != STMF_STATUS_SUCCESS) {
15898fe96085Stim szeto 		return (ret);
15908fe96085Stim szeto 	} else {
15918fe96085Stim szeto 		if (strcmp(luProps.providerName, "sbd") == 0) {
15928fe96085Stim szeto 			ret = modifyDiskLuProp(luGuid, NULL, prop, propVal);
15938fe96085Stim szeto 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
15948fe96085Stim szeto 			return (STMF_ERROR_NOT_FOUND);
15958fe96085Stim szeto 		} else {
15968fe96085Stim szeto 			return (STMF_ERROR_INVALID_ARG);
15978fe96085Stim szeto 		}
15988fe96085Stim szeto 	}
15998fe96085Stim szeto 
16008fe96085Stim szeto 	return (ret);
16018fe96085Stim szeto }
16028fe96085Stim szeto 
16038fe96085Stim szeto /*
16048fe96085Stim szeto  * stmfModifyLuByFname
16058fe96085Stim szeto  *
16068fe96085Stim szeto  * Purpose: Modify a device by filename. Device does not need to be registered.
16078fe96085Stim szeto  *
16088fe96085Stim szeto  * dType - type of device to modify
16098fe96085Stim szeto  *         STMF_DISK
16108fe96085Stim szeto  *
16118fe96085Stim szeto  * fname - filename or meta filename
16128fe96085Stim szeto  * prop - valid property identifier
16138fe96085Stim szeto  * propVal - property value
16148fe96085Stim szeto  *
16158fe96085Stim szeto  */
16168fe96085Stim szeto int
16178fe96085Stim szeto stmfModifyLuByFname(uint16_t dType, const char *fname, uint32_t prop,
16188fe96085Stim szeto     const char *propVal)
16198fe96085Stim szeto {
16208fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
16218fe96085Stim szeto 	if (fname == NULL) {
16228fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
16238fe96085Stim szeto 	}
16248fe96085Stim szeto 
16258fe96085Stim szeto 	if (dType == STMF_DISK) {
16268fe96085Stim szeto 		ret = modifyDiskLuProp(NULL, fname, prop, propVal);
16278fe96085Stim szeto 	} else {
16288fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
16298fe96085Stim szeto 	}
16308fe96085Stim szeto 
16318fe96085Stim szeto 	return (ret);
16328fe96085Stim szeto }
16338fe96085Stim szeto 
16348fe96085Stim szeto static int
16358fe96085Stim szeto modifyDiskLuProp(stmfGuid *luGuid, const char *fname, uint32_t prop,
16368fe96085Stim szeto     const char *propVal)
16378fe96085Stim szeto {
16388fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
16398fe96085Stim szeto 	luResource hdl = NULL;
16408fe96085Stim szeto 	luResourceImpl *luPropsHdl;
16418fe96085Stim szeto 
16428fe96085Stim szeto 	ret = stmfCreateLuResource(STMF_DISK, &hdl);
16438fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
16448fe96085Stim szeto 		return (ret);
16458fe96085Stim szeto 	}
16468fe96085Stim szeto 	ret = validateModifyDiskProp(prop);
16478fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
16488fe96085Stim szeto 		(void) stmfFreeLuResource(hdl);
16498fe96085Stim szeto 		return (STMF_ERROR_INVALID_PROP);
16508fe96085Stim szeto 	}
16518fe96085Stim szeto 	ret = stmfSetLuProp(hdl, prop, propVal);
16528fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
16538fe96085Stim szeto 		(void) stmfFreeLuResource(hdl);
16548fe96085Stim szeto 		return (ret);
16558fe96085Stim szeto 	}
16568fe96085Stim szeto 	luPropsHdl = hdl;
16578fe96085Stim szeto 	ret = modifyDiskLu((diskResource *)luPropsHdl->resource, luGuid, fname);
16588fe96085Stim szeto 	(void) stmfFreeLuResource(hdl);
16598fe96085Stim szeto 	return (ret);
16608fe96085Stim szeto }
16618fe96085Stim szeto 
16628fe96085Stim szeto static int
16638fe96085Stim szeto validateModifyDiskProp(uint32_t prop)
16648fe96085Stim szeto {
16658fe96085Stim szeto 	switch (prop) {
16668fe96085Stim szeto 		case STMF_LU_PROP_ALIAS:
16678fe96085Stim szeto 		case STMF_LU_PROP_SIZE:
16688fe96085Stim szeto 		case STMF_LU_PROP_WRITE_PROTECT:
16698fe96085Stim szeto 		case STMF_LU_PROP_WRITE_CACHE_DISABLE:
16708fe96085Stim szeto 			return (STMF_STATUS_SUCCESS);
16718fe96085Stim szeto 			break;
16728fe96085Stim szeto 		default:
16738fe96085Stim szeto 			return (STMF_STATUS_ERROR);
16748fe96085Stim szeto 			break;
16758fe96085Stim szeto 	}
16768fe96085Stim szeto }
16778fe96085Stim szeto 
16788fe96085Stim szeto static int
16798fe96085Stim szeto modifyDiskLu(diskResource *disk, stmfGuid *luGuid, const char *fname)
16808fe96085Stim szeto {
16818fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
16828fe96085Stim szeto 	int luAliasLen = 0;
16838fe96085Stim szeto 	int mluBufSize = 0;
16848fe96085Stim szeto 	int bufOffset = 0;
16858fe96085Stim szeto 	int fd = 0;
16868fe96085Stim szeto 	int ioctlRet;
16878fe96085Stim szeto 	int savedErrno;
16888fe96085Stim szeto 	int fnameSize = 0;
16898fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
16908fe96085Stim szeto 
16918fe96085Stim szeto 	sbd_modify_lu_t *sbdLu = NULL;
16928fe96085Stim szeto 
16938fe96085Stim szeto 	if (luGuid == NULL && fname == NULL) {
16948fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
16958fe96085Stim szeto 	}
16968fe96085Stim szeto 
16978fe96085Stim szeto 	if (fname) {
16988fe96085Stim szeto 		fnameSize = strlen(fname) + 1;
16998fe96085Stim szeto 		mluBufSize += fnameSize;
17008fe96085Stim szeto 	}
17018fe96085Stim szeto 
17028fe96085Stim szeto 	/*
17038fe96085Stim szeto 	 * Open control node for sbd
17048fe96085Stim szeto 	 */
17058fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
17068fe96085Stim szeto 		return (ret);
17078fe96085Stim szeto 
17088fe96085Stim szeto 	if (disk->luAliasValid) {
17098fe96085Stim szeto 		luAliasLen = strlen(disk->luAlias);
17108fe96085Stim szeto 		mluBufSize += luAliasLen + 1;
17118fe96085Stim szeto 	}
17128fe96085Stim szeto 
17138fe96085Stim szeto 	/*
17148fe96085Stim szeto 	 * 8 is the size of the buffer set aside for
17158fe96085Stim szeto 	 * concatenation of variable length fields
17168fe96085Stim szeto 	 */
17178fe96085Stim szeto 	sbdLu = (sbd_modify_lu_t *)calloc(1,
17188fe96085Stim szeto 	    sizeof (sbd_modify_lu_t) + mluBufSize - 8 + fnameSize);
17198fe96085Stim szeto 	if (sbdLu == NULL) {
17208fe96085Stim szeto 		(void) close(fd);
17218fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
17228fe96085Stim szeto 	}
17238fe96085Stim szeto 
17248fe96085Stim szeto 	sbdLu->mlu_struct_size = sizeof (sbd_modify_lu_t) +
17258fe96085Stim szeto 	    mluBufSize - 8 + fnameSize;
17268fe96085Stim szeto 
17278fe96085Stim szeto 	if (disk->luAliasValid) {
17288fe96085Stim szeto 		sbdLu->mlu_alias_valid = 1;
17298fe96085Stim szeto 		sbdLu->mlu_alias_off = bufOffset;
17308fe96085Stim szeto 		bcopy(disk->luAlias, &(sbdLu->mlu_buf[bufOffset]),
17318fe96085Stim szeto 		    luAliasLen + 1);
17328fe96085Stim szeto 		bufOffset += luAliasLen + 1;
17338fe96085Stim szeto 	}
17348fe96085Stim szeto 
17358fe96085Stim szeto 	if (disk->luSizeValid) {
17368fe96085Stim szeto 		sbdLu->mlu_lu_size_valid = 1;
17378fe96085Stim szeto 		sbdLu->mlu_lu_size = disk->luSize;
17388fe96085Stim szeto 	}
17398fe96085Stim szeto 
17408fe96085Stim szeto 	if (disk->writeProtectEnableValid) {
17418fe96085Stim szeto 		sbdLu->mlu_write_protected_valid = 1;
17428fe96085Stim szeto 		if (disk->writeProtectEnable) {
17438fe96085Stim szeto 			sbdLu->mlu_write_protected = 1;
17448fe96085Stim szeto 		}
17458fe96085Stim szeto 	}
17468fe96085Stim szeto 
17478fe96085Stim szeto 	if (disk->writebackCacheDisableValid) {
17488fe96085Stim szeto 		sbdLu->mlu_writeback_cache_disable_valid = 1;
17498fe96085Stim szeto 		if (disk->writebackCacheDisable) {
17508fe96085Stim szeto 			sbdLu->mlu_writeback_cache_disable = 1;
17518fe96085Stim szeto 		}
17528fe96085Stim szeto 	}
17538fe96085Stim szeto 
17548fe96085Stim szeto 	if (luGuid) {
17558fe96085Stim szeto 		bcopy(luGuid, sbdLu->mlu_input_guid, sizeof (stmfGuid));
17568fe96085Stim szeto 		sbdLu->mlu_by_guid = 1;
17578fe96085Stim szeto 	} else {
17588fe96085Stim szeto 		sbdLu->mlu_fname_off = bufOffset;
17598fe96085Stim szeto 		bcopy(fname, &(sbdLu->mlu_buf[bufOffset]), fnameSize + 1);
17608fe96085Stim szeto 		sbdLu->mlu_by_fname = 1;
17618fe96085Stim szeto 	}
17628fe96085Stim szeto 
17638fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
17648fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdLu->mlu_struct_size;
17658fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdLu;
17668fe96085Stim szeto 
17678fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_MODIFY_LU, &sbdIoctl);
17688fe96085Stim szeto 	if (ioctlRet != 0) {
17698fe96085Stim szeto 		savedErrno = errno;
17708fe96085Stim szeto 		switch (savedErrno) {
17718fe96085Stim szeto 			case EBUSY:
17728fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
17738fe96085Stim szeto 				break;
17748fe96085Stim szeto 			case EPERM:
17758fe96085Stim szeto 			case EACCES:
17768fe96085Stim szeto 				ret = STMF_ERROR_PERM;
17778fe96085Stim szeto 				break;
17788fe96085Stim szeto 			default:
17798fe96085Stim szeto 				diskError(sbdIoctl.stmf_error, &ret);
17808fe96085Stim szeto 				if (ret == STMF_STATUS_ERROR) {
17818fe96085Stim szeto 					syslog(LOG_DEBUG,
17828fe96085Stim szeto 					"modifyDiskLu:ioctl "
17838fe96085Stim szeto 					"error(%d) (%d) (%d)", ioctlRet,
17848fe96085Stim szeto 					    sbdIoctl.stmf_error, savedErrno);
17858fe96085Stim szeto 				}
17868fe96085Stim szeto 				break;
17878fe96085Stim szeto 		}
17888fe96085Stim szeto 	}
17898fe96085Stim szeto 
17908fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
17918fe96085Stim szeto 		goto done;
17928fe96085Stim szeto 	}
17938fe96085Stim szeto 
17948fe96085Stim szeto done:
17958fe96085Stim szeto 	free(sbdLu);
17968fe96085Stim szeto 	(void) close(fd);
17978fe96085Stim szeto 	return (ret);
17988fe96085Stim szeto }
17998fe96085Stim szeto 
18008fe96085Stim szeto /*
18018fe96085Stim szeto  * removeGuidFromDiskStore
18028fe96085Stim szeto  *
18038fe96085Stim szeto  * Purpose: delete a logical unit from the sbd provider data
18048fe96085Stim szeto  */
18058fe96085Stim szeto static int
18068fe96085Stim szeto removeGuidFromDiskStore(stmfGuid *guid)
18078fe96085Stim szeto {
18088fe96085Stim szeto 	return (persistDiskGuid(guid, NULL, B_FALSE));
18098fe96085Stim szeto }
18108fe96085Stim szeto 
18118fe96085Stim szeto 
18128fe96085Stim szeto /*
18138fe96085Stim szeto  * addGuidToDiskStore
18148fe96085Stim szeto  *
18158fe96085Stim szeto  * Purpose: add a logical unit to the sbd provider data
18168fe96085Stim szeto  */
18178fe96085Stim szeto static int
18188fe96085Stim szeto addGuidToDiskStore(stmfGuid *guid, char *filename)
18198fe96085Stim szeto {
18208fe96085Stim szeto 	return (persistDiskGuid(guid, filename, B_TRUE));
18218fe96085Stim szeto }
18228fe96085Stim szeto 
18238fe96085Stim szeto 
18248fe96085Stim szeto /*
18258fe96085Stim szeto  * persistDiskGuid
18268fe96085Stim szeto  *
18278fe96085Stim szeto  * Purpose: Persist or unpersist a guid for the sbd provider data
18288fe96085Stim szeto  *
18298fe96085Stim szeto  */
18308fe96085Stim szeto static int
18318fe96085Stim szeto persistDiskGuid(stmfGuid *guid, char *filename, boolean_t persist)
18328fe96085Stim szeto {
18338fe96085Stim szeto 	char	    guidAsciiBuf[LU_ASCII_GUID_SIZE + 1] = {0};
18348fe96085Stim szeto 	nvlist_t    *nvl = NULL;
18358fe96085Stim szeto 
18368fe96085Stim szeto 	uint64_t    setToken;
18378fe96085Stim szeto 	boolean_t   retryGetProviderData = B_FALSE;
18388fe96085Stim szeto 	boolean_t   newData = B_FALSE;
18398fe96085Stim szeto 	int	    ret = STMF_STATUS_SUCCESS;
18408fe96085Stim szeto 	int	    retryCnt = 0;
18418fe96085Stim szeto 	int	    stmfRet;
18428fe96085Stim szeto 
18438fe96085Stim szeto 	/* if we're persisting a guid, there must be a filename */
18448fe96085Stim szeto 	if (persist && !filename) {
18458fe96085Stim szeto 		return (1);
18468fe96085Stim szeto 	}
18478fe96085Stim szeto 
18488fe96085Stim szeto 	/* guid is stored in lowercase ascii hex */
18498fe96085Stim szeto 	(void) snprintf(guidAsciiBuf, sizeof (guidAsciiBuf),
18508fe96085Stim szeto 	    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
18518fe96085Stim szeto 	    "%02x%02x%02x%02x%02x%02x",
18528fe96085Stim szeto 	    guid->guid[0], guid->guid[1], guid->guid[2], guid->guid[3],
18538fe96085Stim szeto 	    guid->guid[4], guid->guid[5], guid->guid[6], guid->guid[7],
18548fe96085Stim szeto 	    guid->guid[8], guid->guid[9], guid->guid[10], guid->guid[11],
18558fe96085Stim szeto 	    guid->guid[12], guid->guid[13], guid->guid[14], guid->guid[15]);
18568fe96085Stim szeto 
18578fe96085Stim szeto 
18588fe96085Stim szeto 	do {
18598fe96085Stim szeto 		retryGetProviderData = B_FALSE;
18608fe96085Stim szeto 		stmfRet = stmfGetProviderDataProt("sbd", &nvl,
18618fe96085Stim szeto 		    STMF_LU_PROVIDER_TYPE, &setToken);
18628fe96085Stim szeto 		if (stmfRet != STMF_STATUS_SUCCESS) {
18638fe96085Stim szeto 			if (persist && stmfRet == STMF_ERROR_NOT_FOUND) {
18648fe96085Stim szeto 				ret = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
18658fe96085Stim szeto 				if (ret != 0) {
18668fe96085Stim szeto 					syslog(LOG_DEBUG,
18678fe96085Stim szeto 					    "unpersistGuid:nvlist_alloc(%d)",
18688fe96085Stim szeto 					    ret);
18698fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
18708fe96085Stim szeto 					goto done;
18718fe96085Stim szeto 				}
18728fe96085Stim szeto 				newData = B_TRUE;
18738fe96085Stim szeto 			} else {
18748fe96085Stim szeto 				ret = stmfRet;
18758fe96085Stim szeto 				goto done;
18768fe96085Stim szeto 			}
18778fe96085Stim szeto 		}
18788fe96085Stim szeto 		if (persist) {
18798fe96085Stim szeto 			ret = nvlist_add_string(nvl, guidAsciiBuf, filename);
18808fe96085Stim szeto 		} else {
18818fe96085Stim szeto 			ret = nvlist_remove(nvl, guidAsciiBuf,
18828fe96085Stim szeto 			    DATA_TYPE_STRING);
18838fe96085Stim szeto 			if (ret == ENOENT) {
18848fe96085Stim szeto 				ret = 0;
18858fe96085Stim szeto 			}
18868fe96085Stim szeto 		}
18878fe96085Stim szeto 		if (ret == 0) {
18888fe96085Stim szeto 			if (newData) {
18898fe96085Stim szeto 				stmfRet = stmfSetProviderDataProt("sbd", nvl,
18908fe96085Stim szeto 				    STMF_LU_PROVIDER_TYPE, NULL);
18918fe96085Stim szeto 			} else {
18928fe96085Stim szeto 				stmfRet = stmfSetProviderDataProt("sbd", nvl,
18938fe96085Stim szeto 				    STMF_LU_PROVIDER_TYPE, &setToken);
18948fe96085Stim szeto 			}
18958fe96085Stim szeto 			if (stmfRet != STMF_STATUS_SUCCESS) {
18968fe96085Stim szeto 				if (stmfRet == STMF_ERROR_BUSY) {
18978fe96085Stim szeto 					/* get/set failed, try again */
18988fe96085Stim szeto 					retryGetProviderData = B_TRUE;
18998fe96085Stim szeto 					if (retryCnt++ > MAX_PROVIDER_RETRY) {
19008fe96085Stim szeto 						ret = stmfRet;
19018fe96085Stim szeto 						break;
19028fe96085Stim szeto 					}
19038fe96085Stim szeto 					continue;
19048fe96085Stim szeto 				} else if (stmfRet ==
19058fe96085Stim szeto 				    STMF_ERROR_PROV_DATA_STALE) {
19068fe96085Stim szeto 					/* update failed, try again */
19078fe96085Stim szeto 					nvlist_free(nvl);
19088fe96085Stim szeto 					nvl = NULL;
19098fe96085Stim szeto 					retryGetProviderData = B_TRUE;
19108fe96085Stim szeto 					if (retryCnt++ > MAX_PROVIDER_RETRY) {
19118fe96085Stim szeto 						ret = stmfRet;
19128fe96085Stim szeto 						break;
19138fe96085Stim szeto 					}
19148fe96085Stim szeto 					continue;
19158fe96085Stim szeto 				} else {
19168fe96085Stim szeto 					syslog(LOG_DEBUG,
19178fe96085Stim szeto 					    "unpersistGuid:error(%x)", stmfRet);
19188fe96085Stim szeto 					ret = stmfRet;
19198fe96085Stim szeto 				}
19208fe96085Stim szeto 				break;
19218fe96085Stim szeto 			}
19228fe96085Stim szeto 		} else {
19238fe96085Stim szeto 			syslog(LOG_DEBUG,
19248fe96085Stim szeto 			    "unpersistGuid:error nvlist_add/remove(%d)",
19258fe96085Stim szeto 			    ret);
19268fe96085Stim szeto 			ret = STMF_STATUS_ERROR;
19278fe96085Stim szeto 		}
19288fe96085Stim szeto 	} while (retryGetProviderData);
19298fe96085Stim szeto 
19308fe96085Stim szeto done:
19318fe96085Stim szeto 	nvlist_free(nvl);
19328fe96085Stim szeto 	return (ret);
19338fe96085Stim szeto }
19348fe96085Stim szeto 
19358fe96085Stim szeto 
19368fe96085Stim szeto /*
19378fe96085Stim szeto  * stmfGetLuProp
19388fe96085Stim szeto  *
19398fe96085Stim szeto  * Purpose: Get current value for a resource property
19408fe96085Stim szeto  *
19418fe96085Stim szeto  * hdl - luResource from a previous call to stmfCreateLuResource
19428fe96085Stim szeto  *
19438fe96085Stim szeto  * resourceProp - a valid resource property type
19448fe96085Stim szeto  *
19458fe96085Stim szeto  * propVal - void pointer to a pointer of the value to be retrieved
19468fe96085Stim szeto  */
19478fe96085Stim szeto int
19488fe96085Stim szeto stmfGetLuProp(luResource hdl, uint32_t prop, char *propVal, size_t *propLen)
19498fe96085Stim szeto {
19508fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
19518fe96085Stim szeto 	luResourceImpl *luPropsHdl = hdl;
19528fe96085Stim szeto 	if (hdl == NULL || propLen == NULL || propVal == NULL) {
19538fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
19548fe96085Stim szeto 	}
19558fe96085Stim szeto 
19568fe96085Stim szeto 	if (luPropsHdl->type == STMF_DISK) {
19578fe96085Stim szeto 		ret = getDiskProp(luPropsHdl, prop, propVal, propLen);
19588fe96085Stim szeto 	} else {
19598fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
19608fe96085Stim szeto 	}
19618fe96085Stim szeto 
19628fe96085Stim szeto 	return (ret);
19638fe96085Stim szeto }
19648fe96085Stim szeto 
19658fe96085Stim szeto /*
19668fe96085Stim szeto  * stmfGetLuResource
19678fe96085Stim szeto  *
19688fe96085Stim szeto  * Purpose: Get a logical unit resource handle for a given logical unit.
19698fe96085Stim szeto  *
19708fe96085Stim szeto  * hdl - pointer to luResource
19718fe96085Stim szeto  */
19728fe96085Stim szeto int
19738fe96085Stim szeto stmfGetLuResource(stmfGuid *luGuid, luResource *hdl)
19748fe96085Stim szeto {
19758fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
19768fe96085Stim szeto 	stmfLogicalUnitProperties luProps;
19778fe96085Stim szeto 
19788fe96085Stim szeto 
19798fe96085Stim szeto 	/* Check logical unit provider name to call correct dtype function */
19808fe96085Stim szeto 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
19818fe96085Stim szeto 	    != STMF_STATUS_SUCCESS) {
19828fe96085Stim szeto 		return (ret);
19838fe96085Stim szeto 	} else {
19848fe96085Stim szeto 		if (strcmp(luProps.providerName, "sbd") == 0) {
19858fe96085Stim szeto 			ret = getDiskAllProps(luGuid, hdl);
19868fe96085Stim szeto 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
19878fe96085Stim szeto 			return (STMF_ERROR_NOT_FOUND);
19888fe96085Stim szeto 		} else {
19898fe96085Stim szeto 			return (STMF_ERROR_INVALID_ARG);
19908fe96085Stim szeto 		}
19918fe96085Stim szeto 	}
19928fe96085Stim szeto 
19938fe96085Stim szeto 	return (ret);
19948fe96085Stim szeto }
19958fe96085Stim szeto 
19968fe96085Stim szeto /*
19978fe96085Stim szeto  * getDiskAllProps
19988fe96085Stim szeto  *
19998fe96085Stim szeto  * Purpose: load all disk properties from sbd driver
20008fe96085Stim szeto  *
20018fe96085Stim szeto  * luGuid - guid of disk device for which properties are to be retrieved
20028fe96085Stim szeto  * hdl - allocated luResource into which properties are to be copied
20038fe96085Stim szeto  *
20048fe96085Stim szeto  */
20058fe96085Stim szeto static int
20068fe96085Stim szeto getDiskAllProps(stmfGuid *luGuid, luResource *hdl)
20078fe96085Stim szeto {
20088fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
20098fe96085Stim szeto 	int fd;
20108fe96085Stim szeto 	sbd_lu_props_t *sbdProps;
20118fe96085Stim szeto 	int ioctlRet;
20128fe96085Stim szeto 	int savedErrno;
20138fe96085Stim szeto 	int sbdPropsSize = sizeof (*sbdProps) + MAX_SBD_PROPS;
20148fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
20158fe96085Stim szeto 
20168fe96085Stim szeto 	/*
20178fe96085Stim szeto 	 * Open control node for sbd
20188fe96085Stim szeto 	 */
20198fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
20208fe96085Stim szeto 		return (ret);
20218fe96085Stim szeto 
20228fe96085Stim szeto 
20238fe96085Stim szeto 	*hdl = calloc(1, sizeof (luResourceImpl));
20248fe96085Stim szeto 	if (*hdl == NULL) {
20258fe96085Stim szeto 		(void) close(fd);
20268fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
20278fe96085Stim szeto 	}
20288fe96085Stim szeto 
20298fe96085Stim szeto 	sbdProps = calloc(1, sbdPropsSize);
20308fe96085Stim szeto 	if (sbdProps == NULL) {
20318fe96085Stim szeto 		free(*hdl);
20328fe96085Stim szeto 		(void) close(fd);
20338fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
20348fe96085Stim szeto 	}
20358fe96085Stim szeto 
20368fe96085Stim szeto 	ret = createDiskResource((luResourceImpl *)*hdl);
20378fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
20388fe96085Stim szeto 		free(*hdl);
20398fe96085Stim szeto 		(void) close(fd);
20408fe96085Stim szeto 		return (ret);
20418fe96085Stim szeto 	}
20428fe96085Stim szeto 
20438fe96085Stim szeto 	sbdProps->slp_input_guid = 1;
20448fe96085Stim szeto 	bcopy(luGuid, sbdProps->slp_guid, sizeof (sbdProps->slp_guid));
20458fe96085Stim szeto 
20468fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
20478fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdPropsSize;
20488fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdProps;
20498fe96085Stim szeto 	sbdIoctl.stmf_obuf_size = sbdPropsSize;
20508fe96085Stim szeto 	sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdProps;
20518fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_GET_LU_PROPS, &sbdIoctl);
20528fe96085Stim szeto 	if (ioctlRet != 0) {
20538fe96085Stim szeto 		savedErrno = errno;
20548fe96085Stim szeto 		switch (savedErrno) {
20558fe96085Stim szeto 			case EBUSY:
20568fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
20578fe96085Stim szeto 				break;
20588fe96085Stim szeto 			case EPERM:
20598fe96085Stim szeto 			case EACCES:
20608fe96085Stim szeto 				ret = STMF_ERROR_PERM;
20618fe96085Stim szeto 				break;
20628fe96085Stim szeto 			case ENOENT:
20638fe96085Stim szeto 				ret = STMF_ERROR_NOT_FOUND;
20648fe96085Stim szeto 				break;
20658fe96085Stim szeto 			default:
20668fe96085Stim szeto 				syslog(LOG_DEBUG,
20678fe96085Stim szeto 				    "getDiskAllProps:ioctl error(%d) (%d) (%d)",
20688fe96085Stim szeto 				    ioctlRet, sbdIoctl.stmf_error, savedErrno);
20698fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
20708fe96085Stim szeto 				break;
20718fe96085Stim szeto 		}
20728fe96085Stim szeto 	}
20738fe96085Stim szeto 
20748fe96085Stim szeto 	if (ret == STMF_STATUS_SUCCESS) {
20758fe96085Stim szeto 		ret = loadDiskPropsFromDriver((luResourceImpl *)*hdl, sbdProps);
20768fe96085Stim szeto 	}
20778fe96085Stim szeto 
20788fe96085Stim szeto 	(void) close(fd);
20798fe96085Stim szeto 	return (ret);
20808fe96085Stim szeto }
20818fe96085Stim szeto 
20828fe96085Stim szeto /*
20838fe96085Stim szeto  * loadDiskPropsFromDriver
20848fe96085Stim szeto  *
20858fe96085Stim szeto  * Purpose: Retrieve all disk type properties from sbd driver
20868fe96085Stim szeto  *
20878fe96085Stim szeto  * hdl - Allocated luResourceImpl
20888fe96085Stim szeto  * sbdProps - sbd_lu_props_t structure returned from sbd driver
20898fe96085Stim szeto  *
20908fe96085Stim szeto  */
20918fe96085Stim szeto static int
20928fe96085Stim szeto loadDiskPropsFromDriver(luResourceImpl *hdl, sbd_lu_props_t *sbdProps)
20938fe96085Stim szeto {
20948fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
20958fe96085Stim szeto 	diskResource *diskLu = hdl->resource;
20968fe96085Stim szeto 	/* copy guid */
20978fe96085Stim szeto 	diskLu->luGuidValid = B_TRUE;
20988fe96085Stim szeto 	bcopy(sbdProps->slp_guid, diskLu->luGuid, sizeof (sbdProps->slp_guid));
20998fe96085Stim szeto 
21008fe96085Stim szeto 	if (sbdProps->slp_separate_meta && sbdProps->slp_meta_fname_valid) {
21018fe96085Stim szeto 		diskLu->luMetaFileNameValid = B_TRUE;
21028fe96085Stim szeto 		if (strlcpy(diskLu->luMetaFileName,
21038fe96085Stim szeto 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_meta_fname_off]),
21048fe96085Stim szeto 		    sizeof (diskLu->luMetaFileName)) >=
21058fe96085Stim szeto 		    sizeof (diskLu->luMetaFileName)) {
21068fe96085Stim szeto 			return (STMF_STATUS_ERROR);
21078fe96085Stim szeto 		}
21088fe96085Stim szeto 	}
21098fe96085Stim szeto 
21108fe96085Stim szeto 	if (sbdProps->slp_data_fname_valid) {
21118fe96085Stim szeto 		diskLu->luDataFileNameValid = B_TRUE;
21128fe96085Stim szeto 		if (strlcpy(diskLu->luDataFileName,
21138fe96085Stim szeto 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_data_fname_off]),
21148fe96085Stim szeto 		    sizeof (diskLu->luDataFileName)) >=
21158fe96085Stim szeto 		    sizeof (diskLu->luDataFileName)) {
21168fe96085Stim szeto 			return (STMF_STATUS_ERROR);
21178fe96085Stim szeto 		}
21188fe96085Stim szeto 	}
21198fe96085Stim szeto 
21208fe96085Stim szeto 	if (sbdProps->slp_serial_valid) {
21218fe96085Stim szeto 		diskLu->serialNumValid = B_TRUE;
21228fe96085Stim szeto 		bcopy(&(sbdProps->slp_buf[sbdProps->slp_serial_off]),
21238fe96085Stim szeto 		    diskLu->serialNum, sbdProps->slp_serial_size);
21248fe96085Stim szeto 	}
21258fe96085Stim szeto 
21268fe96085Stim szeto 	if (sbdProps->slp_alias_valid) {
21278fe96085Stim szeto 		diskLu->luAliasValid = B_TRUE;
21288fe96085Stim szeto 		if (strlcpy(diskLu->luAlias,
21298fe96085Stim szeto 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_alias_off]),
21308fe96085Stim szeto 		    sizeof (diskLu->luAlias)) >=
21318fe96085Stim szeto 		    sizeof (diskLu->luAlias)) {
21328fe96085Stim szeto 			return (STMF_STATUS_ERROR);
21338fe96085Stim szeto 		}
21348fe96085Stim szeto 	} else { /* set alias to data filename if not set */
21358fe96085Stim szeto 		if (sbdProps->slp_data_fname_valid) {
21368fe96085Stim szeto 			diskLu->luAliasValid = B_TRUE;
21378fe96085Stim szeto 			if (strlcpy(diskLu->luAlias,
21388fe96085Stim szeto 			    (char *)&(sbdProps->slp_buf[
21398fe96085Stim szeto 			    sbdProps->slp_data_fname_off]),
21408fe96085Stim szeto 			    sizeof (diskLu->luAlias)) >=
21418fe96085Stim szeto 			    sizeof (diskLu->luAlias)) {
21428fe96085Stim szeto 				return (STMF_STATUS_ERROR);
21438fe96085Stim szeto 			}
21448fe96085Stim szeto 		}
21458fe96085Stim szeto 	}
21468fe96085Stim szeto 
21478fe96085Stim szeto 	diskLu->vidValid = B_TRUE;
21488fe96085Stim szeto 	bcopy(sbdProps->slp_vid, diskLu->vid, sizeof (diskLu->vid));
21498fe96085Stim szeto 
21508fe96085Stim szeto 	diskLu->pidValid = B_TRUE;
21518fe96085Stim szeto 	bcopy(sbdProps->slp_pid, diskLu->pid, sizeof (diskLu->pid));
21528fe96085Stim szeto 
21538fe96085Stim szeto 	diskLu->revValid = B_TRUE;
21548fe96085Stim szeto 	bcopy(sbdProps->slp_rev, diskLu->rev, sizeof (diskLu->rev));
21558fe96085Stim szeto 
21568fe96085Stim szeto 	diskLu->writeProtectEnableValid = B_TRUE;
21578fe96085Stim szeto 	if (sbdProps->slp_write_protected) {
21588fe96085Stim szeto 		diskLu->writeProtectEnable = B_TRUE;
21598fe96085Stim szeto 	}
21608fe96085Stim szeto 
21618fe96085Stim szeto 	diskLu->writebackCacheDisableValid = B_TRUE;
21628fe96085Stim szeto 	if (sbdProps->slp_writeback_cache_disable_cur) {
21638fe96085Stim szeto 		diskLu->writebackCacheDisable = B_TRUE;
21648fe96085Stim szeto 	}
21658fe96085Stim szeto 
21668fe96085Stim szeto 	diskLu->blkSizeValid = B_TRUE;
21678fe96085Stim szeto 	diskLu->blkSize = sbdProps->slp_blksize;
21688fe96085Stim szeto 
21698fe96085Stim szeto 	diskLu->luSizeValid = B_TRUE;
21708fe96085Stim szeto 	diskLu->luSize = sbdProps->slp_lu_size;
21718fe96085Stim szeto 
21728fe96085Stim szeto 	return (ret);
21738fe96085Stim szeto }
21748fe96085Stim szeto 
21758fe96085Stim szeto 
21768fe96085Stim szeto /*
21778fe96085Stim szeto  * stmfSetLuProp
21788fe96085Stim szeto  *
21798fe96085Stim szeto  * Purpose: set a property on an luResource
21808fe96085Stim szeto  *
21818fe96085Stim szeto  * hdl - allocated luResource
21828fe96085Stim szeto  * prop - property identifier
21838fe96085Stim szeto  * propVal - property value to be set
21848fe96085Stim szeto  */
21858fe96085Stim szeto int
21868fe96085Stim szeto stmfSetLuProp(luResource hdl, uint32_t prop, const char *propVal)
21878fe96085Stim szeto {
21888fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
21898fe96085Stim szeto 	luResourceImpl *luPropsHdl = hdl;
21908fe96085Stim szeto 	if (hdl == NULL) {
21918fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
21928fe96085Stim szeto 	}
21938fe96085Stim szeto 
21948fe96085Stim szeto 	if (luPropsHdl->type == STMF_DISK) {
21958fe96085Stim szeto 		ret = setDiskProp(luPropsHdl, prop, propVal);
21968fe96085Stim szeto 	} else {
21978fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
21988fe96085Stim szeto 	}
21998fe96085Stim szeto 
22008fe96085Stim szeto 	return (ret);
22018fe96085Stim szeto }
22028fe96085Stim szeto 
22038fe96085Stim szeto /*
22048fe96085Stim szeto  * getDiskProp
22058fe96085Stim szeto  *
22068fe96085Stim szeto  * Purpose: retrieve a given property from a logical unit resource of type disk
22078fe96085Stim szeto  *
22088fe96085Stim szeto  * hdl - allocated luResourceImpl
22098fe96085Stim szeto  * prop - property identifier
22108fe96085Stim szeto  * propVal - pointer to character to contain the retrieved property value
22118fe96085Stim szeto  * propLen - On input this is the length of propVal. On failure, it contains the
22128fe96085Stim szeto  *           number of bytes required for propVal
22138fe96085Stim szeto  */
22148fe96085Stim szeto static int
22158fe96085Stim szeto getDiskProp(luResourceImpl *hdl, uint32_t prop, char *propVal, size_t *propLen)
22168fe96085Stim szeto {
22178fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
22188fe96085Stim szeto 	diskResource *diskLu = hdl->resource;
22198fe96085Stim szeto 	size_t reqLen;
22208fe96085Stim szeto 
22218fe96085Stim szeto 	switch (prop) {
22228fe96085Stim szeto 		case STMF_LU_PROP_BLOCK_SIZE:
22238fe96085Stim szeto 			if (diskLu->blkSizeValid == B_FALSE) {
22248fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
22258fe96085Stim szeto 			}
22268fe96085Stim szeto 			reqLen = snprintf(propVal, *propLen, "%llu",
22278fe96085Stim szeto 			    (u_longlong_t)diskLu->blkSize);
22288fe96085Stim szeto 			if (reqLen >= *propLen) {
22298fe96085Stim szeto 				*propLen = reqLen + 1;
22308fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
22318fe96085Stim szeto 			}
22328fe96085Stim szeto 			break;
22338fe96085Stim szeto 		case STMF_LU_PROP_FILENAME:
22348fe96085Stim szeto 			if (diskLu->luDataFileNameValid == B_FALSE) {
22358fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
22368fe96085Stim szeto 			}
22378fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->luDataFileName,
22388fe96085Stim szeto 			    *propLen)) >= *propLen) {
22398fe96085Stim szeto 				*propLen = reqLen + 1;
22408fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
22418fe96085Stim szeto 			}
22428fe96085Stim szeto 			break;
22438fe96085Stim szeto 		case STMF_LU_PROP_META_FILENAME:
22448fe96085Stim szeto 			if (diskLu->luMetaFileNameValid == B_FALSE) {
22458fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
22468fe96085Stim szeto 			}
22478fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->luMetaFileName,
22488fe96085Stim szeto 			    *propLen)) >= *propLen) {
22498fe96085Stim szeto 				*propLen = reqLen + 1;
22508fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
22518fe96085Stim szeto 			}
22528fe96085Stim szeto 			break;
22538fe96085Stim szeto 		case STMF_LU_PROP_GUID:
22548fe96085Stim szeto 			if (diskLu->luGuidValid == B_FALSE) {
22558fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
22568fe96085Stim szeto 			}
22578fe96085Stim szeto 			reqLen = snprintf(propVal, *propLen,
22588fe96085Stim szeto 			    "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X"
22598fe96085Stim szeto 			    "%02X%02X%02X%02X",
22608fe96085Stim szeto 			    diskLu->luGuid[0], diskLu->luGuid[1],
22618fe96085Stim szeto 			    diskLu->luGuid[2], diskLu->luGuid[3],
22628fe96085Stim szeto 			    diskLu->luGuid[4], diskLu->luGuid[5],
22638fe96085Stim szeto 			    diskLu->luGuid[6], diskLu->luGuid[7],
22648fe96085Stim szeto 			    diskLu->luGuid[8], diskLu->luGuid[9],
22658fe96085Stim szeto 			    diskLu->luGuid[10], diskLu->luGuid[11],
22668fe96085Stim szeto 			    diskLu->luGuid[12], diskLu->luGuid[13],
22678fe96085Stim szeto 			    diskLu->luGuid[14], diskLu->luGuid[15]);
22688fe96085Stim szeto 			if (reqLen >= *propLen) {
22698fe96085Stim szeto 				*propLen = reqLen + 1;
22708fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
22718fe96085Stim szeto 			}
22728fe96085Stim szeto 			break;
22738fe96085Stim szeto 		case STMF_LU_PROP_SERIAL_NUM:
22748fe96085Stim szeto 			if (diskLu->serialNumValid == B_FALSE) {
22758fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
22768fe96085Stim szeto 			}
22778fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->serialNum,
22788fe96085Stim szeto 			    *propLen)) >= *propLen) {
22798fe96085Stim szeto 				*propLen = reqLen + 1;
22808fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
22818fe96085Stim szeto 			}
22828fe96085Stim szeto 			break;
22838fe96085Stim szeto 		case STMF_LU_PROP_SIZE:
22848fe96085Stim szeto 			if (diskLu->luSizeValid == B_FALSE) {
22858fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
22868fe96085Stim szeto 			}
22878fe96085Stim szeto 			(void) snprintf(propVal, *propLen, "%llu",
22888fe96085Stim szeto 			    (u_longlong_t)diskLu->luSize);
22898fe96085Stim szeto 			break;
22908fe96085Stim szeto 		case STMF_LU_PROP_ALIAS:
22918fe96085Stim szeto 			if (diskLu->luAliasValid == B_FALSE) {
22928fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
22938fe96085Stim szeto 			}
22948fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->luAlias,
22958fe96085Stim szeto 			    *propLen)) >= *propLen) {
22968fe96085Stim szeto 				*propLen = reqLen + 1;
22978fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
22988fe96085Stim szeto 			}
22998fe96085Stim szeto 			break;
23008fe96085Stim szeto 		case STMF_LU_PROP_VID:
23018fe96085Stim szeto 			if (diskLu->vidValid == B_FALSE) {
23028fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
23038fe96085Stim szeto 			}
23048fe96085Stim szeto 			if (*propLen <= sizeof (diskLu->vid)) {
23058fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
23068fe96085Stim szeto 			}
23078fe96085Stim szeto 			bcopy(diskLu->vid, propVal, sizeof (diskLu->vid));
23088fe96085Stim szeto 			propVal[sizeof (diskLu->vid)] = 0;
23098fe96085Stim szeto 			break;
23108fe96085Stim szeto 		case STMF_LU_PROP_PID:
23118fe96085Stim szeto 			if (diskLu->pidValid == B_FALSE) {
23128fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
23138fe96085Stim szeto 			}
23148fe96085Stim szeto 			if (*propLen <= sizeof (diskLu->pid)) {
23158fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
23168fe96085Stim szeto 			}
23178fe96085Stim szeto 			bcopy(diskLu->pid, propVal, sizeof (diskLu->pid));
23188fe96085Stim szeto 			propVal[sizeof (diskLu->pid)] = 0;
23198fe96085Stim szeto 			break;
23208fe96085Stim szeto 		case STMF_LU_PROP_WRITE_PROTECT:
23218fe96085Stim szeto 			if (diskLu->writeProtectEnableValid == B_FALSE) {
23228fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
23238fe96085Stim szeto 			}
23248fe96085Stim szeto 			if (diskLu->writeProtectEnable) {
23258fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "true",
23268fe96085Stim szeto 				    *propLen)) >= *propLen) {
23278fe96085Stim szeto 					*propLen = reqLen + 1;
23288fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
23298fe96085Stim szeto 				}
23308fe96085Stim szeto 			} else {
23318fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "false",
23328fe96085Stim szeto 				    *propLen)) >= *propLen) {
23338fe96085Stim szeto 					*propLen = reqLen + 1;
23348fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
23358fe96085Stim szeto 				}
23368fe96085Stim szeto 			}
23378fe96085Stim szeto 			break;
23388fe96085Stim szeto 		case STMF_LU_PROP_WRITE_CACHE_DISABLE:
23398fe96085Stim szeto 			if (diskLu->writebackCacheDisableValid == B_FALSE) {
23408fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
23418fe96085Stim szeto 			}
23428fe96085Stim szeto 			if (diskLu->writebackCacheDisable) {
23438fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "true",
23448fe96085Stim szeto 				    *propLen)) >= *propLen) {
23458fe96085Stim szeto 					*propLen = reqLen + 1;
23468fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
23478fe96085Stim szeto 				}
23488fe96085Stim szeto 			} else {
23498fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "false",
23508fe96085Stim szeto 				    *propLen)) >= *propLen) {
23518fe96085Stim szeto 					*propLen = reqLen + 1;
23528fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
23538fe96085Stim szeto 				}
23548fe96085Stim szeto 			}
23558fe96085Stim szeto 			break;
23568fe96085Stim szeto 		default:
23578fe96085Stim szeto 			ret = STMF_ERROR_NO_PROP;
23588fe96085Stim szeto 			break;
23598fe96085Stim szeto 	}
23608fe96085Stim szeto 
23618fe96085Stim szeto 	return (ret);
23628fe96085Stim szeto }
23638fe96085Stim szeto 
23648fe96085Stim szeto /*
23658fe96085Stim szeto  * setDiskProp
23668fe96085Stim szeto  *
23678fe96085Stim szeto  * Purpose: set properties for resource of type disk
23688fe96085Stim szeto  *
23698fe96085Stim szeto  * hdl - allocated luResourceImpl
23708fe96085Stim szeto  * resourceProp - valid resource identifier
23718fe96085Stim szeto  * propVal - valid resource value
23728fe96085Stim szeto  */
23738fe96085Stim szeto static int
23748fe96085Stim szeto setDiskProp(luResourceImpl *hdl, uint32_t resourceProp, const char *propVal)
23758fe96085Stim szeto {
23768fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
23778fe96085Stim szeto 	int i;
23788fe96085Stim szeto 	diskResource *diskLu = hdl->resource;
23798fe96085Stim szeto 	unsigned long long numericProp = 0;
23808fe96085Stim szeto 	char guidProp[LU_ASCII_GUID_SIZE + 1];
23818fe96085Stim szeto 	char ouiProp[OUI_ASCII_SIZE + 1];
23828fe96085Stim szeto 	unsigned int oui[OUI_SIZE];
23838fe96085Stim szeto 	unsigned int guid[LU_GUID_SIZE];
23848fe96085Stim szeto 	int propSize;
23858fe96085Stim szeto 
23868fe96085Stim szeto 
23878fe96085Stim szeto 	if (propVal == NULL) {
23888fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
23898fe96085Stim szeto 	}
23908fe96085Stim szeto 
23918fe96085Stim szeto 	switch (resourceProp) {
23928fe96085Stim szeto 		case STMF_LU_PROP_ALIAS:
23938fe96085Stim szeto 			if (strlcpy(diskLu->luAlias, propVal,
23948fe96085Stim szeto 			    sizeof (diskLu->luAlias)) >=
23958fe96085Stim szeto 			    sizeof (diskLu->luAlias)) {
23968fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
23978fe96085Stim szeto 			}
23988fe96085Stim szeto 			diskLu->luAliasValid = B_TRUE;
23998fe96085Stim szeto 			break;
24008fe96085Stim szeto 		case STMF_LU_PROP_BLOCK_SIZE:
24018fe96085Stim szeto 			(void) sscanf(propVal, "%llu", &numericProp);
24028fe96085Stim szeto 			if (numericProp > UINT16_MAX) {
24038fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
24048fe96085Stim szeto 			}
24058fe96085Stim szeto 			diskLu->blkSize = numericProp;
24068fe96085Stim szeto 			diskLu->blkSizeValid = B_TRUE;
24078fe96085Stim szeto 			break;
24088fe96085Stim szeto 		case STMF_LU_PROP_COMPANY_ID:
24098fe96085Stim szeto 			if ((strlcpy(ouiProp, propVal, sizeof (ouiProp))) >=
24108fe96085Stim szeto 			    sizeof (ouiProp)) {
24118fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24128fe96085Stim szeto 			}
24138fe96085Stim szeto 			if (checkHexUpper(ouiProp) != 0) {
24148fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24158fe96085Stim szeto 			}
24168fe96085Stim szeto 			(void) sscanf(ouiProp, "%2X%2X%2X",
24178fe96085Stim szeto 			    &oui[0], &oui[1], &oui[2]);
24188fe96085Stim szeto 
24198fe96085Stim szeto 			diskLu->companyId = 0;
24208fe96085Stim szeto 			diskLu->companyId += oui[0] << 16;
24218fe96085Stim szeto 			diskLu->companyId += oui[1] << 8;
24228fe96085Stim szeto 			diskLu->companyId += oui[2];
24238fe96085Stim szeto 			diskLu->companyIdValid = B_TRUE;
24248fe96085Stim szeto 			break;
24258fe96085Stim szeto 		case STMF_LU_PROP_GUID:
24268fe96085Stim szeto 			if (strlen(propVal) != LU_ASCII_GUID_SIZE) {
24278fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
24288fe96085Stim szeto 			}
24298fe96085Stim szeto 
24308fe96085Stim szeto 			if ((strlcpy(guidProp, propVal, sizeof (guidProp))) >=
24318fe96085Stim szeto 			    sizeof (guidProp)) {
24328fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24338fe96085Stim szeto 			}
24348fe96085Stim szeto 
24358fe96085Stim szeto 			if (checkHexUpper(guidProp) != 0) {
24368fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24378fe96085Stim szeto 			}
24388fe96085Stim szeto 
24398fe96085Stim szeto 			(void) sscanf(guidProp,
24408fe96085Stim szeto 			    "%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X",
24418fe96085Stim szeto 			    &guid[0], &guid[1], &guid[2], &guid[3], &guid[4],
24428fe96085Stim szeto 			    &guid[5], &guid[6], &guid[7], &guid[8], &guid[9],
24438fe96085Stim szeto 			    &guid[10], &guid[11], &guid[12], &guid[13],
24448fe96085Stim szeto 			    &guid[14], &guid[15]);
24458fe96085Stim szeto 			for (i = 0; i < sizeof (diskLu->luGuid); i++) {
24468fe96085Stim szeto 				diskLu->luGuid[i] = guid[i];
24478fe96085Stim szeto 			}
24488fe96085Stim szeto 			diskLu->luGuidValid = B_TRUE;
24498fe96085Stim szeto 			break;
24508fe96085Stim szeto 		case STMF_LU_PROP_FILENAME:
24518fe96085Stim szeto 			if ((strlcpy(diskLu->luDataFileName, propVal,
24528fe96085Stim szeto 			    sizeof (diskLu->luDataFileName))) >=
24538fe96085Stim szeto 			    sizeof (diskLu->luDataFileName)) {
24548fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
24558fe96085Stim szeto 			}
24568fe96085Stim szeto 			diskLu->luDataFileNameValid = B_TRUE;
24578fe96085Stim szeto 			break;
24588fe96085Stim szeto 		case STMF_LU_PROP_META_FILENAME:
24598fe96085Stim szeto 			if ((strlcpy(diskLu->luMetaFileName, propVal,
24608fe96085Stim szeto 			    sizeof (diskLu->luMetaFileName))) >=
24618fe96085Stim szeto 			    sizeof (diskLu->luMetaFileName)) {
24628fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
24638fe96085Stim szeto 			}
24648fe96085Stim szeto 			diskLu->luMetaFileNameValid = B_TRUE;
24658fe96085Stim szeto 			break;
24668fe96085Stim szeto 		case STMF_LU_PROP_PID:
24678fe96085Stim szeto 			if ((propSize = strlen(propVal)) >
24688fe96085Stim szeto 			    sizeof (diskLu->pid)) {
24698fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
24708fe96085Stim szeto 			}
24718fe96085Stim szeto 			(void) strncpy(diskLu->pid, propVal, propSize);
24728fe96085Stim szeto 			diskLu->pidValid = B_TRUE;
24738fe96085Stim szeto 			break;
24748fe96085Stim szeto 		case STMF_LU_PROP_SERIAL_NUM:
24758fe96085Stim szeto 			if ((propSize = strlen(propVal)) >
24768fe96085Stim szeto 			    (sizeof (diskLu->serialNum) - 1)) {
24778fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
24788fe96085Stim szeto 			}
24798fe96085Stim szeto 			(void) strncpy(diskLu->serialNum, propVal, propSize);
24808fe96085Stim szeto 			diskLu->serialNumValid = B_TRUE;
24818fe96085Stim szeto 			break;
24828fe96085Stim szeto 		case STMF_LU_PROP_SIZE:
24838fe96085Stim szeto 			if ((niceStrToNum(propVal, &diskLu->luSize) != 0)) {
24848fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24858fe96085Stim szeto 			}
24868fe96085Stim szeto 			diskLu->luSizeValid = B_TRUE;
24878fe96085Stim szeto 			break;
24888fe96085Stim szeto 		case STMF_LU_PROP_VID:
24898fe96085Stim szeto 			if ((propSize = strlen(propVal)) >
24908fe96085Stim szeto 			    sizeof (diskLu->vid)) {
24918fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
24928fe96085Stim szeto 			}
24938fe96085Stim szeto 			(void) strncpy(diskLu->vid, propVal, propSize);
24948fe96085Stim szeto 			diskLu->vidValid = B_TRUE;
24958fe96085Stim szeto 			break;
24968fe96085Stim szeto 		case STMF_LU_PROP_WRITE_PROTECT:
24978fe96085Stim szeto 			if (strcasecmp(propVal, "TRUE") == 0) {
24988fe96085Stim szeto 				diskLu->writeProtectEnable = B_TRUE;
24998fe96085Stim szeto 			} else if (strcasecmp(propVal, "FALSE") == 0) {
25008fe96085Stim szeto 				diskLu->writeProtectEnable = B_FALSE;
25018fe96085Stim szeto 			} else {
25028fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
25038fe96085Stim szeto 			}
25048fe96085Stim szeto 			diskLu->writeProtectEnableValid = B_TRUE;
25058fe96085Stim szeto 			break;
25068fe96085Stim szeto 		case STMF_LU_PROP_WRITE_CACHE_DISABLE:
25078fe96085Stim szeto 			if (strcasecmp(propVal, "TRUE") == 0) {
25088fe96085Stim szeto 				diskLu->writebackCacheDisable = B_TRUE;
25098fe96085Stim szeto 			} else if (strcasecmp(propVal, "FALSE") == 0) {
25108fe96085Stim szeto 				diskLu->writebackCacheDisable = B_FALSE;
25118fe96085Stim szeto 			} else {
25128fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
25138fe96085Stim szeto 			}
25148fe96085Stim szeto 			diskLu->writebackCacheDisableValid = B_TRUE;
25158fe96085Stim szeto 			break;
25168fe96085Stim szeto 		default:
25178fe96085Stim szeto 			ret = STMF_ERROR_NO_PROP;
25188fe96085Stim szeto 			break;
25198fe96085Stim szeto 	}
25208fe96085Stim szeto 	return (ret);
25218fe96085Stim szeto }
25228fe96085Stim szeto 
25238fe96085Stim szeto static int
25248fe96085Stim szeto checkHexUpper(char *buf)
25258fe96085Stim szeto {
25268fe96085Stim szeto 	int i;
25278fe96085Stim szeto 
25288fe96085Stim szeto 	for (i = 0; i < strlen(buf); i++) {
25298fe96085Stim szeto 		if (isxdigit(buf[i])) {
25308fe96085Stim szeto 			buf[i] = toupper(buf[i]);
25318fe96085Stim szeto 			continue;
25328fe96085Stim szeto 		}
25338fe96085Stim szeto 		return (-1);
25348fe96085Stim szeto 	}
25358fe96085Stim szeto 
25368fe96085Stim szeto 	return (0);
25378fe96085Stim szeto }
25388fe96085Stim szeto 
25398fe96085Stim szeto /*
25408fe96085Stim szeto  * Given a numeric suffix, convert the value into a number of bits that the
25418fe96085Stim szeto  * resulting value must be shifted.
25428fe96085Stim szeto  * Code lifted from libzfs_util.c
25438fe96085Stim szeto  */
25448fe96085Stim szeto static int
25458fe96085Stim szeto strToShift(const char *buf)
25468fe96085Stim szeto {
25478fe96085Stim szeto 	const char *ends = "BKMGTPE";
25488fe96085Stim szeto 	int i;
25498fe96085Stim szeto 
25508fe96085Stim szeto 	if (buf[0] == '\0')
25518fe96085Stim szeto 		return (0);
25528fe96085Stim szeto 
25538fe96085Stim szeto 	for (i = 0; i < strlen(ends); i++) {
25548fe96085Stim szeto 		if (toupper(buf[0]) == ends[i])
25558fe96085Stim szeto 			return (10*i);
25568fe96085Stim szeto 	}
25578fe96085Stim szeto 
25588fe96085Stim szeto 	return (-1);
25598fe96085Stim szeto }
25608fe96085Stim szeto 
25618fe96085Stim szeto int
25628fe96085Stim szeto stmfFreeLuResource(luResource hdl)
25638fe96085Stim szeto {
25648fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
25658fe96085Stim szeto 	if (hdl == NULL) {
25668fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
25678fe96085Stim szeto 	}
25688fe96085Stim szeto 
25698fe96085Stim szeto 	luResourceImpl *hdlImpl = hdl;
25708fe96085Stim szeto 	free(hdlImpl->resource);
25718fe96085Stim szeto 	free(hdlImpl);
25728fe96085Stim szeto 	return (ret);
25738fe96085Stim szeto }
25748fe96085Stim szeto 
25758fe96085Stim szeto /*
25768fe96085Stim szeto  * Convert a string of the form '100G' into a real number. Used when setting
25778fe96085Stim szeto  * the size of a logical unit.
25788fe96085Stim szeto  * Code lifted from libzfs_util.c
25798fe96085Stim szeto  */
25808fe96085Stim szeto static int
25818fe96085Stim szeto niceStrToNum(const char *value, uint64_t *num)
25828fe96085Stim szeto {
25838fe96085Stim szeto 	char *end;
25848fe96085Stim szeto 	int shift;
25858fe96085Stim szeto 
25868fe96085Stim szeto 	*num = 0;
25878fe96085Stim szeto 
25888fe96085Stim szeto 	/* Check to see if this looks like a number.  */
25898fe96085Stim szeto 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
25908fe96085Stim szeto 		return (-1);
25918fe96085Stim szeto 	}
25928fe96085Stim szeto 
25938fe96085Stim szeto 	/* Rely on stroull() to process the numeric portion.  */
25948fe96085Stim szeto 	errno = 0;
25958fe96085Stim szeto 	*num = strtoull(value, &end, 10);
25968fe96085Stim szeto 
25978fe96085Stim szeto 	/*
25988fe96085Stim szeto 	 * Check for ERANGE, which indicates that the value is too large to fit
25998fe96085Stim szeto 	 * in a 64-bit value.
26008fe96085Stim szeto 	 */
26018fe96085Stim szeto 	if (errno == ERANGE) {
26028fe96085Stim szeto 		return (-1);
26038fe96085Stim szeto 	}
26048fe96085Stim szeto 
26058fe96085Stim szeto 	/*
26068fe96085Stim szeto 	 * If we have a decimal value, then do the computation with floating
26078fe96085Stim szeto 	 * point arithmetic.  Otherwise, use standard arithmetic.
26088fe96085Stim szeto 	 */
26098fe96085Stim szeto 	if (*end == '.') {
26108fe96085Stim szeto 		double fval = strtod(value, &end);
26118fe96085Stim szeto 
26128fe96085Stim szeto 		if ((shift = strToShift(end)) == -1) {
26138fe96085Stim szeto 			return (-1);
26148fe96085Stim szeto 		}
26158fe96085Stim szeto 
26168fe96085Stim szeto 		fval *= pow(2, shift);
26178fe96085Stim szeto 
26188fe96085Stim szeto 		if (fval > UINT64_MAX) {
26198fe96085Stim szeto 			return (-1);
26208fe96085Stim szeto 		}
26218fe96085Stim szeto 
26228fe96085Stim szeto 		*num = (uint64_t)fval;
26238fe96085Stim szeto 	} else {
26248fe96085Stim szeto 		if ((shift = strToShift(end)) == -1) {
26258fe96085Stim szeto 			return (-1);
26268fe96085Stim szeto 		}
26278fe96085Stim szeto 
26288fe96085Stim szeto 		/* Check for overflow */
26298fe96085Stim szeto 		if (shift >= 64 || (*num << shift) >> shift != *num) {
26308fe96085Stim szeto 			return (-1);
26318fe96085Stim szeto 		}
26328fe96085Stim szeto 
26338fe96085Stim szeto 		*num <<= shift;
26348fe96085Stim szeto 	}
26358fe96085Stim szeto 
26368fe96085Stim szeto 	return (0);
26378fe96085Stim szeto }
26388fe96085Stim szeto 
26398fe96085Stim szeto /*
2640fcf3ce44SJohn Forte  * stmfCreateTargetGroup
2641fcf3ce44SJohn Forte  *
2642fcf3ce44SJohn Forte  * Purpose: Create a local port group
2643fcf3ce44SJohn Forte  *
2644fcf3ce44SJohn Forte  * targetGroupName - name of local port group to create
2645fcf3ce44SJohn Forte  */
2646fcf3ce44SJohn Forte int
2647fcf3ce44SJohn Forte stmfCreateTargetGroup(stmfGroupName *targetGroupName)
2648fcf3ce44SJohn Forte {
2649fcf3ce44SJohn Forte 	int ret;
2650fcf3ce44SJohn Forte 	int fd;
2651fcf3ce44SJohn Forte 
2652fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
2653fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
2654fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName))) {
2655fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2656fcf3ce44SJohn Forte 	}
2657fcf3ce44SJohn Forte 
2658fcf3ce44SJohn Forte 	/* Check to ensure service exists */
2659fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
2660fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
2661fcf3ce44SJohn Forte 	}
2662fcf3ce44SJohn Forte 
2663fcf3ce44SJohn Forte 	/* call init */
2664fcf3ce44SJohn Forte 	ret = initializeConfig();
2665fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2666fcf3ce44SJohn Forte 		return (ret);
2667fcf3ce44SJohn Forte 	}
2668fcf3ce44SJohn Forte 
2669fcf3ce44SJohn Forte 	/*
2670fcf3ce44SJohn Forte 	 * Open control node for stmf
2671fcf3ce44SJohn Forte 	 */
2672fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
2673fcf3ce44SJohn Forte 		return (ret);
2674fcf3ce44SJohn Forte 
2675fcf3ce44SJohn Forte 	/*
2676fcf3ce44SJohn Forte 	 * Add the group to the driver
2677fcf3ce44SJohn Forte 	 */
2678fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_TARGET_GROUP,
2679fcf3ce44SJohn Forte 	    targetGroupName)) != STMF_STATUS_SUCCESS) {
2680fcf3ce44SJohn Forte 		goto done;
2681fcf3ce44SJohn Forte 	}
2682fcf3ce44SJohn Forte 
26838fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
26848fe96085Stim szeto 		goto done;
26858fe96085Stim szeto 	}
26868fe96085Stim szeto 
2687fcf3ce44SJohn Forte 	/*
2688fcf3ce44SJohn Forte 	 * If the add to the driver was successful, add it to the persistent
2689fcf3ce44SJohn Forte 	 * store.
2690fcf3ce44SJohn Forte 	 */
2691fcf3ce44SJohn Forte 	ret = psCreateTargetGroup((char *)targetGroupName);
2692fcf3ce44SJohn Forte 	switch (ret) {
2693fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
2694fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
2695fcf3ce44SJohn Forte 			break;
2696fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
2697fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
2698fcf3ce44SJohn Forte 			break;
2699fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
2700fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
2701fcf3ce44SJohn Forte 			break;
2702fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2703fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
2704fcf3ce44SJohn Forte 			break;
2705fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
2706fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
2707fcf3ce44SJohn Forte 			break;
2708fcf3ce44SJohn Forte 		default:
2709fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
2710fcf3ce44SJohn Forte 			    "stmfCreateTargetGroup:psCreateTargetGroup"
2711fcf3ce44SJohn Forte 			    ":error(%d)", ret);
2712fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
2713fcf3ce44SJohn Forte 			break;
2714fcf3ce44SJohn Forte 	}
2715fcf3ce44SJohn Forte 
2716fcf3ce44SJohn Forte done:
2717fcf3ce44SJohn Forte 	(void) close(fd);
2718fcf3ce44SJohn Forte 	return (ret);
2719fcf3ce44SJohn Forte }
2720fcf3ce44SJohn Forte 
2721fcf3ce44SJohn Forte /*
2722fcf3ce44SJohn Forte  * stmfDeleteHostGroup
2723fcf3ce44SJohn Forte  *
2724fcf3ce44SJohn Forte  * Purpose: Delete an initiator or local port group
2725fcf3ce44SJohn Forte  *
2726fcf3ce44SJohn Forte  * hostGroupName - group to delete
2727fcf3ce44SJohn Forte  */
2728fcf3ce44SJohn Forte int
2729fcf3ce44SJohn Forte stmfDeleteHostGroup(stmfGroupName *hostGroupName)
2730fcf3ce44SJohn Forte {
2731fcf3ce44SJohn Forte 	int ret;
2732fcf3ce44SJohn Forte 	int fd;
2733fcf3ce44SJohn Forte 
2734fcf3ce44SJohn Forte 	if (hostGroupName == NULL) {
2735fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2736fcf3ce44SJohn Forte 	}
2737fcf3ce44SJohn Forte 
2738fcf3ce44SJohn Forte 	/* Check to ensure service exists */
2739fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
2740fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
2741fcf3ce44SJohn Forte 	}
2742fcf3ce44SJohn Forte 
2743fcf3ce44SJohn Forte 	/* call init */
2744fcf3ce44SJohn Forte 	ret = initializeConfig();
2745fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2746fcf3ce44SJohn Forte 		return (ret);
2747fcf3ce44SJohn Forte 	}
2748fcf3ce44SJohn Forte 
2749fcf3ce44SJohn Forte 	/*
2750fcf3ce44SJohn Forte 	 * Open control node for stmf
2751fcf3ce44SJohn Forte 	 */
2752fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
2753fcf3ce44SJohn Forte 		return (ret);
2754fcf3ce44SJohn Forte 
2755fcf3ce44SJohn Forte 	/*
2756fcf3ce44SJohn Forte 	 * Remove the group from the driver
2757fcf3ce44SJohn Forte 	 */
2758fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_REMOVE_HOST_GROUP,
2759fcf3ce44SJohn Forte 	    hostGroupName)) != STMF_STATUS_SUCCESS) {
2760fcf3ce44SJohn Forte 		goto done;
2761fcf3ce44SJohn Forte 	}
2762fcf3ce44SJohn Forte 
27638fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
27648fe96085Stim szeto 		goto done;
27658fe96085Stim szeto 	}
27668fe96085Stim szeto 
2767fcf3ce44SJohn Forte 	/*
2768fcf3ce44SJohn Forte 	 * If the remove from the driver was successful, remove it from the
2769fcf3ce44SJohn Forte 	 * persistent store.
2770fcf3ce44SJohn Forte 	 */
2771fcf3ce44SJohn Forte 	ret = psDeleteHostGroup((char *)hostGroupName);
2772fcf3ce44SJohn Forte 	switch (ret) {
2773fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
2774fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
2775fcf3ce44SJohn Forte 			break;
2776fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
2777fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
2778fcf3ce44SJohn Forte 			break;
2779fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
2780fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
2781fcf3ce44SJohn Forte 			break;
2782fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2783fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
2784fcf3ce44SJohn Forte 			break;
2785fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
2786fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
2787fcf3ce44SJohn Forte 			break;
2788fcf3ce44SJohn Forte 		default:
2789fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
2790fcf3ce44SJohn Forte 			    "stmfDeleteHostGroup:psDeleteHostGroup:error(%d)",
2791fcf3ce44SJohn Forte 			    ret);
2792fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
2793fcf3ce44SJohn Forte 			break;
2794fcf3ce44SJohn Forte 	}
2795fcf3ce44SJohn Forte 
2796fcf3ce44SJohn Forte done:
2797fcf3ce44SJohn Forte 	(void) close(fd);
2798fcf3ce44SJohn Forte 	return (ret);
2799fcf3ce44SJohn Forte }
2800fcf3ce44SJohn Forte 
2801fcf3ce44SJohn Forte /*
2802fcf3ce44SJohn Forte  * stmfDeleteTargetGroup
2803fcf3ce44SJohn Forte  *
2804fcf3ce44SJohn Forte  * Purpose: Delete an initiator or local port group
2805fcf3ce44SJohn Forte  *
2806fcf3ce44SJohn Forte  * targetGroupName - group to delete
2807fcf3ce44SJohn Forte  */
2808fcf3ce44SJohn Forte int
2809fcf3ce44SJohn Forte stmfDeleteTargetGroup(stmfGroupName *targetGroupName)
2810fcf3ce44SJohn Forte {
2811fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
2812fcf3ce44SJohn Forte 	int fd;
2813fcf3ce44SJohn Forte 
2814fcf3ce44SJohn Forte 	if (targetGroupName == NULL) {
2815fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2816fcf3ce44SJohn Forte 	}
2817fcf3ce44SJohn Forte 
2818fcf3ce44SJohn Forte 	/* Check to ensure service exists */
2819fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
2820fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
2821fcf3ce44SJohn Forte 	}
2822fcf3ce44SJohn Forte 
2823fcf3ce44SJohn Forte 	/* call init */
2824fcf3ce44SJohn Forte 	ret = initializeConfig();
2825fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2826fcf3ce44SJohn Forte 		return (ret);
2827fcf3ce44SJohn Forte 	}
2828fcf3ce44SJohn Forte 
2829fcf3ce44SJohn Forte 	/*
2830fcf3ce44SJohn Forte 	 * Open control node for stmf
2831fcf3ce44SJohn Forte 	 */
2832fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
2833fcf3ce44SJohn Forte 		return (ret);
2834fcf3ce44SJohn Forte 
2835fcf3ce44SJohn Forte 	/*
2836fcf3ce44SJohn Forte 	 * Remove the group from the driver
2837fcf3ce44SJohn Forte 	 */
2838fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_REMOVE_TARGET_GROUP,
2839fcf3ce44SJohn Forte 	    targetGroupName)) != STMF_STATUS_SUCCESS) {
2840fcf3ce44SJohn Forte 		goto done;
2841fcf3ce44SJohn Forte 	}
2842fcf3ce44SJohn Forte 
28438fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
28448fe96085Stim szeto 		goto done;
28458fe96085Stim szeto 	}
28468fe96085Stim szeto 
2847fcf3ce44SJohn Forte 	/*
2848fcf3ce44SJohn Forte 	 * If the remove from the driver was successful, remove it from the
2849fcf3ce44SJohn Forte 	 * persistent store.
2850fcf3ce44SJohn Forte 	 */
2851fcf3ce44SJohn Forte 	ret = psDeleteTargetGroup((char *)targetGroupName);
2852fcf3ce44SJohn Forte 	switch (ret) {
2853fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
2854fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
2855fcf3ce44SJohn Forte 			break;
2856fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
2857fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
2858fcf3ce44SJohn Forte 			break;
2859fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
2860fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
2861fcf3ce44SJohn Forte 			break;
2862fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2863fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
2864fcf3ce44SJohn Forte 			break;
2865fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
2866fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
2867fcf3ce44SJohn Forte 			break;
2868fcf3ce44SJohn Forte 		default:
2869fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
2870fcf3ce44SJohn Forte 			    "stmfDeleteTargetGroup:psDeleteTargetGroup"
2871fcf3ce44SJohn Forte 			    ":error(%d)", ret);
2872fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
2873fcf3ce44SJohn Forte 			break;
2874fcf3ce44SJohn Forte 	}
2875fcf3ce44SJohn Forte 
2876fcf3ce44SJohn Forte done:
2877fcf3ce44SJohn Forte 	(void) close(fd);
2878fcf3ce44SJohn Forte 	return (ret);
2879fcf3ce44SJohn Forte }
2880fcf3ce44SJohn Forte 
2881fcf3ce44SJohn Forte /*
2882fcf3ce44SJohn Forte  * stmfDevidFromIscsiName
2883fcf3ce44SJohn Forte  *
2884fcf3ce44SJohn Forte  * Purpose: convert an iSCSI name to an stmf devid
2885fcf3ce44SJohn Forte  *
2886fcf3ce44SJohn Forte  * iscsiName - unicode nul terminated utf-8 encoded iSCSI name
2887fcf3ce44SJohn Forte  * devid - on success, contains the converted iscsi name
2888fcf3ce44SJohn Forte  */
2889fcf3ce44SJohn Forte int
2890fcf3ce44SJohn Forte stmfDevidFromIscsiName(char *iscsiName, stmfDevid *devid)
2891fcf3ce44SJohn Forte {
2892fcf3ce44SJohn Forte 	if (devid == NULL || iscsiName == NULL)
2893fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2894fcf3ce44SJohn Forte 
2895fcf3ce44SJohn Forte 	bzero(devid, sizeof (stmfDevid));
2896fcf3ce44SJohn Forte 
2897fcf3ce44SJohn Forte 	/* Validate size of target */
2898fcf3ce44SJohn Forte 	if ((devid->identLength = strlen(iscsiName)) > MAX_ISCSI_NAME ||
2899fcf3ce44SJohn Forte 	    devid->identLength < strlen(EUI) ||
2900fcf3ce44SJohn Forte 	    devid->identLength < strlen(IQN)) {
2901fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2902fcf3ce44SJohn Forte 	}
2903fcf3ce44SJohn Forte 
2904fcf3ce44SJohn Forte 	if ((strncmp(iscsiName, EUI, strlen(EUI)) != 0) &&
2905fcf3ce44SJohn Forte 	    strncmp(iscsiName, IQN, strlen(IQN)) != 0) {
2906fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2907fcf3ce44SJohn Forte 	}
2908fcf3ce44SJohn Forte 
2909fcf3ce44SJohn Forte 	/* copy UTF-8 bytes to ident */
2910fcf3ce44SJohn Forte 	bcopy(iscsiName, devid->ident, devid->identLength);
2911fcf3ce44SJohn Forte 
2912fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
2913fcf3ce44SJohn Forte }
2914fcf3ce44SJohn Forte 
2915fcf3ce44SJohn Forte /*
2916fcf3ce44SJohn Forte  * stmfDevidFromWwn
2917fcf3ce44SJohn Forte  *
2918fcf3ce44SJohn Forte  * Purpose: convert a WWN to an stmf devid
2919fcf3ce44SJohn Forte  *
2920fcf3ce44SJohn Forte  * wwn - 8-byte wwn identifier
2921fcf3ce44SJohn Forte  * devid - on success, contains the converted wwn
2922fcf3ce44SJohn Forte  */
2923fcf3ce44SJohn Forte int
2924fcf3ce44SJohn Forte stmfDevidFromWwn(uchar_t *wwn, stmfDevid *devid)
2925fcf3ce44SJohn Forte {
2926fcf3ce44SJohn Forte 	if (wwn == NULL || devid == NULL)
2927fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2928fcf3ce44SJohn Forte 
2929fcf3ce44SJohn Forte 	bzero(devid, sizeof (stmfDevid));
2930fcf3ce44SJohn Forte 
2931fcf3ce44SJohn Forte 	/* Copy eui prefix */
2932fcf3ce44SJohn Forte 	(void) bcopy(WWN, devid->ident, strlen(WWN));
2933fcf3ce44SJohn Forte 
2934fcf3ce44SJohn Forte 	/* Convert to ASCII uppercase hexadecimal string */
2935fcf3ce44SJohn Forte 	(void) snprintf((char *)&devid->ident[strlen(WWN)],
2936fcf3ce44SJohn Forte 	    sizeof (devid->ident), "%02X%02X%02X%02X%02X%02X%02X%02X",
2937fcf3ce44SJohn Forte 	    wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
2938fcf3ce44SJohn Forte 
2939fcf3ce44SJohn Forte 	devid->identLength = strlen((char *)devid->ident);
2940fcf3ce44SJohn Forte 
2941fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
2942fcf3ce44SJohn Forte }
2943fcf3ce44SJohn Forte 
2944fcf3ce44SJohn Forte /*
2945fcf3ce44SJohn Forte  * stmfFreeMemory
2946fcf3ce44SJohn Forte  *
2947fcf3ce44SJohn Forte  * Purpose: Free memory allocated by this library
2948fcf3ce44SJohn Forte  *
2949fcf3ce44SJohn Forte  * memory - previously allocated pointer of memory managed by library
2950fcf3ce44SJohn Forte  */
2951fcf3ce44SJohn Forte void
2952fcf3ce44SJohn Forte stmfFreeMemory(void *memory)
2953fcf3ce44SJohn Forte {
2954fcf3ce44SJohn Forte 	free(memory);
2955fcf3ce44SJohn Forte }
2956fcf3ce44SJohn Forte 
2957fcf3ce44SJohn Forte /*
29588fe96085Stim szeto  * get host group, target group list from stmf
2959fcf3ce44SJohn Forte  *
29608fe96085Stim szeto  * groupType - HOST_GROUP, TARGET_GROUP
2961fcf3ce44SJohn Forte  */
29628fe96085Stim szeto static int
29638fe96085Stim szeto groupListIoctl(stmfGroupList **groupList, int groupType)
2964fcf3ce44SJohn Forte {
2965fcf3ce44SJohn Forte 	int ret;
29668fe96085Stim szeto 	int fd;
29678fe96085Stim szeto 	int ioctlRet;
29688fe96085Stim szeto 	int i;
29698fe96085Stim szeto 	int cmd;
29708fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
29718fe96085Stim szeto 	/* framework group list */
29728fe96085Stim szeto 	stmf_group_name_t *iGroupList = NULL;
29738fe96085Stim szeto 	uint32_t groupListSize;
2974fcf3ce44SJohn Forte 
29758fe96085Stim szeto 	if (groupList == NULL) {
2976fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2977fcf3ce44SJohn Forte 	}
2978fcf3ce44SJohn Forte 
29798fe96085Stim szeto 	if (groupType == HOST_GROUP) {
29808fe96085Stim szeto 		cmd = STMF_IOCTL_GET_HG_LIST;
29818fe96085Stim szeto 	} else if (groupType == TARGET_GROUP) {
29828fe96085Stim szeto 		cmd = STMF_IOCTL_GET_TG_LIST;
29838fe96085Stim szeto 	} else {
29848fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
29858fe96085Stim szeto 	}
29868fe96085Stim szeto 
29878fe96085Stim szeto 	/* call init */
29888fe96085Stim szeto 	ret = initializeConfig();
29898fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
29908fe96085Stim szeto 		return (ret);
29918fe96085Stim szeto 	}
29928fe96085Stim szeto 
29938fe96085Stim szeto 	/*
29948fe96085Stim szeto 	 * Open control node for stmf
29958fe96085Stim szeto 	 */
29968fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
29978fe96085Stim szeto 		return (ret);
29988fe96085Stim szeto 
29998fe96085Stim szeto 	/*
30008fe96085Stim szeto 	 * Allocate ioctl input buffer
30018fe96085Stim szeto 	 */
30028fe96085Stim szeto 	groupListSize = ALLOC_GROUP;
30038fe96085Stim szeto 	groupListSize = groupListSize * (sizeof (stmf_group_name_t));
30048fe96085Stim szeto 	iGroupList = (stmf_group_name_t *)calloc(1, groupListSize);
30058fe96085Stim szeto 	if (iGroupList == NULL) {
30068fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
30078fe96085Stim szeto 		goto done;
30088fe96085Stim szeto 	}
30098fe96085Stim szeto 
30108fe96085Stim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
30118fe96085Stim szeto 	/*
30128fe96085Stim szeto 	 * Issue ioctl to get the group list
30138fe96085Stim szeto 	 */
30148fe96085Stim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
30158fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = groupListSize;
30168fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupList;
30178fe96085Stim szeto 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
30188fe96085Stim szeto 	if (ioctlRet != 0) {
30198fe96085Stim szeto 		switch (errno) {
30208fe96085Stim szeto 			case EBUSY:
30218fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
30228fe96085Stim szeto 				break;
30238fe96085Stim szeto 			case EPERM:
30248fe96085Stim szeto 			case EACCES:
30258fe96085Stim szeto 				ret = STMF_ERROR_PERM;
30268fe96085Stim szeto 				break;
30278fe96085Stim szeto 			default:
30288fe96085Stim szeto 				syslog(LOG_DEBUG,
30298fe96085Stim szeto 				    "groupListIoctl:ioctl errno(%d)",
30308fe96085Stim szeto 				    errno);
30318fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
30328fe96085Stim szeto 				break;
30338fe96085Stim szeto 		}
30348fe96085Stim szeto 		goto done;
30358fe96085Stim szeto 	}
30368fe96085Stim szeto 	/*
30378fe96085Stim szeto 	 * Check whether input buffer was large enough
30388fe96085Stim szeto 	 */
30398fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_GROUP) {
30408fe96085Stim szeto 		groupListSize = stmfIoctl.stmf_obuf_max_nentries *
30418fe96085Stim szeto 		    sizeof (stmf_group_name_t);
30428fe96085Stim szeto 		iGroupList = realloc(iGroupList, groupListSize);
30438fe96085Stim szeto 		if (iGroupList == NULL) {
30448fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
30458fe96085Stim szeto 			goto done;
30468fe96085Stim szeto 		}
30478fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = groupListSize;
30488fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupList;
30498fe96085Stim szeto 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
30508fe96085Stim szeto 		if (ioctlRet != 0) {
30518fe96085Stim szeto 			switch (errno) {
30528fe96085Stim szeto 				case EBUSY:
30538fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
30548fe96085Stim szeto 					break;
30558fe96085Stim szeto 				case EPERM:
30568fe96085Stim szeto 				case EACCES:
30578fe96085Stim szeto 					ret = STMF_ERROR_PERM;
30588fe96085Stim szeto 					break;
30598fe96085Stim szeto 				default:
30608fe96085Stim szeto 					syslog(LOG_DEBUG,
30618fe96085Stim szeto 					    "groupListIoctl:ioctl errno(%d)",
30628fe96085Stim szeto 					    errno);
30638fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
30648fe96085Stim szeto 					break;
30658fe96085Stim szeto 			}
30668fe96085Stim szeto 			goto done;
30678fe96085Stim szeto 		}
30688fe96085Stim szeto 	}
30698fe96085Stim szeto 
30708fe96085Stim szeto 	/* allocate and copy to caller's buffer */
30718fe96085Stim szeto 	*groupList = (stmfGroupList *)calloc(1, sizeof (stmfGroupList) *
30728fe96085Stim szeto 	    stmfIoctl.stmf_obuf_nentries);
30738fe96085Stim szeto 	if (*groupList == NULL) {
30748fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
30758fe96085Stim szeto 		goto done;
30768fe96085Stim szeto 	}
30778fe96085Stim szeto 	(*groupList)->cnt = stmfIoctl.stmf_obuf_nentries;
30788fe96085Stim szeto 	for (i = 0; i < stmfIoctl.stmf_obuf_nentries; i++) {
30798fe96085Stim szeto 		bcopy(iGroupList->name, (*groupList)->name[i],
30808fe96085Stim szeto 		    sizeof (stmfGroupName));
30818fe96085Stim szeto 		iGroupList++;
30828fe96085Stim szeto 	}
30838fe96085Stim szeto 
30848fe96085Stim szeto done:
30858fe96085Stim szeto 	free(iGroupList);
30868fe96085Stim szeto 	(void) close(fd);
30878fe96085Stim szeto 	return (ret);
30888fe96085Stim szeto }
30898fe96085Stim szeto 
30908fe96085Stim szeto /*
30918fe96085Stim szeto  * get host group members, target group members from stmf
30928fe96085Stim szeto  *
30938fe96085Stim szeto  * groupProps - allocated on success
30948fe96085Stim szeto  *
30958fe96085Stim szeto  * groupType - HOST_GROUP, TARGET_GROUP
30968fe96085Stim szeto  */
30978fe96085Stim szeto static int
30988fe96085Stim szeto groupMemberListIoctl(stmfGroupName *groupName, stmfGroupProperties **groupProps,
30998fe96085Stim szeto     int groupType)
31008fe96085Stim szeto {
31018fe96085Stim szeto 	int ret;
31028fe96085Stim szeto 	int fd;
31038fe96085Stim szeto 	int ioctlRet;
31048fe96085Stim szeto 	int i;
31058fe96085Stim szeto 	int cmd;
31068fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
31078fe96085Stim szeto 	/* framework group list */
31088fe96085Stim szeto 	stmf_group_name_t iGroupName;
31098fe96085Stim szeto 	stmf_ge_ident_t *iGroupMembers;
31108fe96085Stim szeto 	uint32_t groupListSize;
31118fe96085Stim szeto 
31128fe96085Stim szeto 	if (groupName == NULL) {
31138fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
31148fe96085Stim szeto 	}
31158fe96085Stim szeto 
31168fe96085Stim szeto 	if (groupType == HOST_GROUP) {
31178fe96085Stim szeto 		cmd = STMF_IOCTL_GET_HG_ENTRIES;
31188fe96085Stim szeto 	} else if (groupType == TARGET_GROUP) {
31198fe96085Stim szeto 		cmd = STMF_IOCTL_GET_TG_ENTRIES;
31208fe96085Stim szeto 	} else {
31218fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
31228fe96085Stim szeto 	}
31238fe96085Stim szeto 
31248fe96085Stim szeto 	/* call init */
31258fe96085Stim szeto 	ret = initializeConfig();
31268fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
31278fe96085Stim szeto 		return (ret);
31288fe96085Stim szeto 	}
31298fe96085Stim szeto 
31308fe96085Stim szeto 	/*
31318fe96085Stim szeto 	 * Open control node for stmf
31328fe96085Stim szeto 	 */
31338fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
31348fe96085Stim szeto 		return (ret);
31358fe96085Stim szeto 
31368fe96085Stim szeto 	bzero(&iGroupName, sizeof (iGroupName));
31378fe96085Stim szeto 
31388fe96085Stim szeto 	bcopy(groupName, &iGroupName.name, strlen((char *)groupName));
31398fe96085Stim szeto 
31408fe96085Stim szeto 	iGroupName.name_size = strlen((char *)groupName);
31418fe96085Stim szeto 
31428fe96085Stim szeto 	/*
31438fe96085Stim szeto 	 * Allocate ioctl input buffer
31448fe96085Stim szeto 	 */
31458fe96085Stim szeto 	groupListSize = ALLOC_GRP_MEMBER;
31468fe96085Stim szeto 	groupListSize = groupListSize * (sizeof (stmf_ge_ident_t));
31478fe96085Stim szeto 	iGroupMembers = (stmf_ge_ident_t *)calloc(1, groupListSize);
31488fe96085Stim szeto 	if (iGroupMembers == NULL) {
31498fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
31508fe96085Stim szeto 		goto done;
31518fe96085Stim szeto 	}
31528fe96085Stim szeto 
31538fe96085Stim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
31548fe96085Stim szeto 	/*
31558fe96085Stim szeto 	 * Issue ioctl to get the group list
31568fe96085Stim szeto 	 */
31578fe96085Stim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
31588fe96085Stim szeto 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&iGroupName;
31598fe96085Stim szeto 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_group_name_t);
31608fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = groupListSize;
31618fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupMembers;
31628fe96085Stim szeto 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
31638fe96085Stim szeto 	if (ioctlRet != 0) {
31648fe96085Stim szeto 		switch (errno) {
31658fe96085Stim szeto 			case EBUSY:
31668fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
31678fe96085Stim szeto 				break;
31688fe96085Stim szeto 			case EPERM:
31698fe96085Stim szeto 			case EACCES:
31708fe96085Stim szeto 				ret = STMF_ERROR_PERM;
31718fe96085Stim szeto 				break;
31728fe96085Stim szeto 			default:
31738fe96085Stim szeto 				syslog(LOG_DEBUG,
31748fe96085Stim szeto 				    "groupListIoctl:ioctl errno(%d)",
31758fe96085Stim szeto 				    errno);
31768fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
31778fe96085Stim szeto 				break;
31788fe96085Stim szeto 		}
31798fe96085Stim szeto 		goto done;
31808fe96085Stim szeto 	}
31818fe96085Stim szeto 	/*
31828fe96085Stim szeto 	 * Check whether input buffer was large enough
31838fe96085Stim szeto 	 */
31848fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_GRP_MEMBER) {
31858fe96085Stim szeto 		groupListSize = stmfIoctl.stmf_obuf_max_nentries *
31868fe96085Stim szeto 		    sizeof (stmf_ge_ident_t);
31878fe96085Stim szeto 		iGroupMembers = realloc(iGroupMembers, groupListSize);
31888fe96085Stim szeto 		if (iGroupMembers == NULL) {
31898fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
31908fe96085Stim szeto 			goto done;
31918fe96085Stim szeto 		}
31928fe96085Stim szeto 		stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&iGroupName;
31938fe96085Stim szeto 		stmfIoctl.stmf_ibuf_size = sizeof (stmf_group_name_t);
31948fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = groupListSize;
31958fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupMembers;
31968fe96085Stim szeto 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
31978fe96085Stim szeto 		if (ioctlRet != 0) {
31988fe96085Stim szeto 			switch (errno) {
31998fe96085Stim szeto 				case EBUSY:
32008fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
32018fe96085Stim szeto 					break;
32028fe96085Stim szeto 				case EPERM:
32038fe96085Stim szeto 				case EACCES:
32048fe96085Stim szeto 					ret = STMF_ERROR_PERM;
32058fe96085Stim szeto 					break;
32068fe96085Stim szeto 				default:
32078fe96085Stim szeto 					syslog(LOG_DEBUG,
32088fe96085Stim szeto 					    "groupListIoctl:ioctl errno(%d)",
32098fe96085Stim szeto 					    errno);
32108fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
32118fe96085Stim szeto 					break;
32128fe96085Stim szeto 			}
32138fe96085Stim szeto 			goto done;
32148fe96085Stim szeto 		}
32158fe96085Stim szeto 	}
32168fe96085Stim szeto 
32178fe96085Stim szeto 	/* allocate and copy to caller's buffer */
32188fe96085Stim szeto 	*groupProps = (stmfGroupProperties *)calloc(1,
32198fe96085Stim szeto 	    sizeof (stmfGroupProperties) * stmfIoctl.stmf_obuf_nentries);
32208fe96085Stim szeto 	if (*groupProps == NULL) {
32218fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
32228fe96085Stim szeto 		goto done;
32238fe96085Stim szeto 	}
32248fe96085Stim szeto 	(*groupProps)->cnt = stmfIoctl.stmf_obuf_nentries;
32258fe96085Stim szeto 	for (i = 0; i < stmfIoctl.stmf_obuf_nentries; i++) {
32268fe96085Stim szeto 		(*groupProps)->name[i].identLength =
32278fe96085Stim szeto 		    iGroupMembers->ident_size;
32288fe96085Stim szeto 		bcopy(iGroupMembers->ident, (*groupProps)->name[i].ident,
32298fe96085Stim szeto 		    iGroupMembers->ident_size);
32308fe96085Stim szeto 		iGroupMembers++;
32318fe96085Stim szeto 	}
32328fe96085Stim szeto 
32338fe96085Stim szeto done:
32348fe96085Stim szeto 	free(iGroupMembers);
32358fe96085Stim szeto 	(void) close(fd);
32368fe96085Stim szeto 	return (ret);
32378fe96085Stim szeto }
32388fe96085Stim szeto 
32398fe96085Stim szeto /*
32408fe96085Stim szeto  * Purpose: access persistent config data for host groups and target groups
32418fe96085Stim szeto  */
32428fe96085Stim szeto static int
32438fe96085Stim szeto iLoadGroupFromPs(stmfGroupList **groupList, int type)
32448fe96085Stim szeto {
32458fe96085Stim szeto 	int ret;
32468fe96085Stim szeto 
32478fe96085Stim szeto 	if (groupList == NULL) {
32488fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
32498fe96085Stim szeto 	}
32508fe96085Stim szeto 
32518fe96085Stim szeto 	if (type == HOST_GROUP) {
32528fe96085Stim szeto 		ret = psGetHostGroupList(groupList);
32538fe96085Stim szeto 	} else if (type == TARGET_GROUP) {
32548fe96085Stim szeto 		ret = psGetTargetGroupList(groupList);
32558fe96085Stim szeto 	} else {
32568fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
32578fe96085Stim szeto 	}
3258fcf3ce44SJohn Forte 	switch (ret) {
3259fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3260fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3261fcf3ce44SJohn Forte 			break;
3262fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3263fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3264fcf3ce44SJohn Forte 			break;
3265fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3266fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3267fcf3ce44SJohn Forte 			break;
3268fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3269fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3270fcf3ce44SJohn Forte 			break;
3271fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3272fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3273fcf3ce44SJohn Forte 			break;
3274fcf3ce44SJohn Forte 		default:
3275fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3276fcf3ce44SJohn Forte 			    "stmfGetHostGroupList:psGetHostGroupList:error(%d)",
3277fcf3ce44SJohn Forte 			    ret);
3278fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3279fcf3ce44SJohn Forte 			break;
3280fcf3ce44SJohn Forte 	}
3281fcf3ce44SJohn Forte 
3282fcf3ce44SJohn Forte 	return (ret);
3283fcf3ce44SJohn Forte }
3284fcf3ce44SJohn Forte 
3285fcf3ce44SJohn Forte /*
32868fe96085Stim szeto  * stmfGetHostGroupList
3287fcf3ce44SJohn Forte  *
32888fe96085Stim szeto  * Purpose: Retrieves the list of initiator group oids
3289fcf3ce44SJohn Forte  *
32908fe96085Stim szeto  * hostGroupList - pointer to pointer to hostGroupList structure
32918fe96085Stim szeto  *                 on success, this contains the host group list.
3292fcf3ce44SJohn Forte  */
3293fcf3ce44SJohn Forte int
32948fe96085Stim szeto stmfGetHostGroupList(stmfGroupList **hostGroupList)
3295fcf3ce44SJohn Forte {
32968fe96085Stim szeto 	int ret = STMF_STATUS_ERROR;
3297fcf3ce44SJohn Forte 
32988fe96085Stim szeto 	if (hostGroupList == NULL) {
3299fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3300fcf3ce44SJohn Forte 	}
3301fcf3ce44SJohn Forte 
33028fe96085Stim szeto 	ret = groupListIoctl(hostGroupList, HOST_GROUP);
33038fe96085Stim szeto 	return (ret);
33048fe96085Stim szeto }
33058fe96085Stim szeto 
33068fe96085Stim szeto 
33078fe96085Stim szeto /*
33088fe96085Stim szeto  * Purpose: access persistent config data for host groups and target groups
33098fe96085Stim szeto  */
33108fe96085Stim szeto static int
33118fe96085Stim szeto iLoadGroupMembersFromPs(stmfGroupName *groupName,
33128fe96085Stim szeto     stmfGroupProperties **groupProp, int type)
33138fe96085Stim szeto {
33148fe96085Stim szeto 	int ret;
33158fe96085Stim szeto 
33168fe96085Stim szeto 	if (groupName == NULL) {
33178fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
33188fe96085Stim szeto 	}
33198fe96085Stim szeto 
33208fe96085Stim szeto 	if (type == HOST_GROUP) {
3321fcf3ce44SJohn Forte 		ret = psGetHostGroupMemberList((char *)groupName, groupProp);
33228fe96085Stim szeto 	} else if (type == TARGET_GROUP) {
33238fe96085Stim szeto 		ret = psGetTargetGroupMemberList((char *)groupName, groupProp);
33248fe96085Stim szeto 	} else {
33258fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
33268fe96085Stim szeto 	}
3327fcf3ce44SJohn Forte 	switch (ret) {
3328fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3329fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3330fcf3ce44SJohn Forte 			break;
3331fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3332fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3333fcf3ce44SJohn Forte 			break;
3334fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3335fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3336fcf3ce44SJohn Forte 			break;
3337fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3338fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3339fcf3ce44SJohn Forte 			break;
3340fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3341fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3342fcf3ce44SJohn Forte 			break;
3343fcf3ce44SJohn Forte 		default:
3344fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
33458fe96085Stim szeto 			    "iLoadGroupMembersFromPs:psGetHostGroupList:"
33468fe96085Stim szeto 			    "error(%d)", ret);
3347fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3348fcf3ce44SJohn Forte 			break;
3349fcf3ce44SJohn Forte 	}
3350fcf3ce44SJohn Forte 
3351fcf3ce44SJohn Forte 	return (ret);
3352fcf3ce44SJohn Forte }
3353fcf3ce44SJohn Forte 
3354fcf3ce44SJohn Forte /*
33558fe96085Stim szeto  * stmfGetHostGroupMembers
33568fe96085Stim szeto  *
33578fe96085Stim szeto  * Purpose: Retrieves the group properties for a host group
33588fe96085Stim szeto  *
33598fe96085Stim szeto  * groupName - name of group for which to retrieve host group members.
33608fe96085Stim szeto  * groupProp - pointer to pointer to stmfGroupProperties structure
33618fe96085Stim szeto  *             on success, this contains the list of group members.
33628fe96085Stim szeto  */
33638fe96085Stim szeto int
33648fe96085Stim szeto stmfGetHostGroupMembers(stmfGroupName *groupName,
33658fe96085Stim szeto     stmfGroupProperties **groupProp)
33668fe96085Stim szeto {
33678fe96085Stim szeto 	int ret;
33688fe96085Stim szeto 
33698fe96085Stim szeto 	if (groupName == NULL || groupProp == NULL) {
33708fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
33718fe96085Stim szeto 	}
33728fe96085Stim szeto 
33738fe96085Stim szeto 	ret = groupMemberListIoctl(groupName, groupProp, HOST_GROUP);
33748fe96085Stim szeto 
33758fe96085Stim szeto 	return (ret);
33768fe96085Stim szeto }
33778fe96085Stim szeto 
33788fe96085Stim szeto /*
3379fcf3ce44SJohn Forte  * stmfGetProviderData
3380fcf3ce44SJohn Forte  *
3381fcf3ce44SJohn Forte  * Purpose: Get provider data list
3382fcf3ce44SJohn Forte  *
3383fcf3ce44SJohn Forte  * providerName - name of provider for which to retrieve the data
3384fcf3ce44SJohn Forte  * nvl - pointer to nvlist_t pointer which will contain the nvlist data
3385fcf3ce44SJohn Forte  *       retrieved.
3386fcf3ce44SJohn Forte  * providerType - type of provider for which to retrieve data.
3387fcf3ce44SJohn Forte  *		    STMF_LU_PROVIDER_TYPE
3388fcf3ce44SJohn Forte  *		    STMF_PORT_PROVIDER_TYPE
3389fcf3ce44SJohn Forte  */
3390fcf3ce44SJohn Forte int
3391fcf3ce44SJohn Forte stmfGetProviderData(char *providerName, nvlist_t **nvl, int providerType)
3392fcf3ce44SJohn Forte {
3393fcf3ce44SJohn Forte 	return (stmfGetProviderDataProt(providerName, nvl, providerType,
3394fcf3ce44SJohn Forte 	    NULL));
3395fcf3ce44SJohn Forte }
3396fcf3ce44SJohn Forte 
3397fcf3ce44SJohn Forte /*
3398fcf3ce44SJohn Forte  * stmfGetProviderDataProt
3399fcf3ce44SJohn Forte  *
3400fcf3ce44SJohn Forte  * Purpose: Get provider data list with token
3401fcf3ce44SJohn Forte  *
3402fcf3ce44SJohn Forte  * providerName - name of provider for which to retrieve the data
3403fcf3ce44SJohn Forte  * nvl - pointer to nvlist_t pointer which will contain the nvlist data
3404fcf3ce44SJohn Forte  *       retrieved.
3405fcf3ce44SJohn Forte  * providerType - type of provider for which to retrieve data.
3406fcf3ce44SJohn Forte  *		    STMF_LU_PROVIDER_TYPE
3407fcf3ce44SJohn Forte  *		    STMF_PORT_PROVIDER_TYPE
3408fcf3ce44SJohn Forte  * setToken - Returns the stale data token
3409fcf3ce44SJohn Forte  */
3410fcf3ce44SJohn Forte int
3411fcf3ce44SJohn Forte stmfGetProviderDataProt(char *providerName, nvlist_t **nvl, int providerType,
3412fcf3ce44SJohn Forte     uint64_t *setToken)
3413fcf3ce44SJohn Forte {
3414fcf3ce44SJohn Forte 	int ret;
3415fcf3ce44SJohn Forte 
3416fcf3ce44SJohn Forte 	if (providerName == NULL || nvl == NULL) {
3417fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3418fcf3ce44SJohn Forte 	}
3419fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
3420fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
3421fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3422fcf3ce44SJohn Forte 	}
3423fcf3ce44SJohn Forte 	/* call init */
3424fcf3ce44SJohn Forte 	ret = initializeConfig();
3425fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3426fcf3ce44SJohn Forte 		return (ret);
3427fcf3ce44SJohn Forte 	}
34288fe96085Stim szeto 	return (getProviderData(providerName, nvl, providerType, setToken));
3429fcf3ce44SJohn Forte }
3430fcf3ce44SJohn Forte 
3431fcf3ce44SJohn Forte /*
3432fcf3ce44SJohn Forte  * stmfGetProviderDataList
3433fcf3ce44SJohn Forte  *
3434fcf3ce44SJohn Forte  * Purpose: Get the list of providers currently persisting data
3435fcf3ce44SJohn Forte  *
3436fcf3ce44SJohn Forte  * providerList - pointer to pointer to an stmfProviderList structure allocated
3437fcf3ce44SJohn Forte  *                by the caller. Will contain the list of providers on success.
3438fcf3ce44SJohn Forte  */
3439fcf3ce44SJohn Forte int
3440fcf3ce44SJohn Forte stmfGetProviderDataList(stmfProviderList **providerList)
3441fcf3ce44SJohn Forte {
3442fcf3ce44SJohn Forte 	int ret;
3443fcf3ce44SJohn Forte 
3444fcf3ce44SJohn Forte 	ret = psGetProviderDataList(providerList);
3445fcf3ce44SJohn Forte 	switch (ret) {
3446fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3447fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3448fcf3ce44SJohn Forte 			break;
3449fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3450fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3451fcf3ce44SJohn Forte 			break;
3452fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3453fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3454fcf3ce44SJohn Forte 			break;
3455fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3456fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3457fcf3ce44SJohn Forte 			break;
3458fcf3ce44SJohn Forte 		default:
3459fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3460fcf3ce44SJohn Forte 			    "stmfGetProviderDataList:psGetProviderDataList"
3461fcf3ce44SJohn Forte 			    ":error(%d)", ret);
3462fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3463fcf3ce44SJohn Forte 			break;
3464fcf3ce44SJohn Forte 	}
3465fcf3ce44SJohn Forte 
3466fcf3ce44SJohn Forte 	return (ret);
3467fcf3ce44SJohn Forte }
3468fcf3ce44SJohn Forte 
3469fcf3ce44SJohn Forte 
3470fcf3ce44SJohn Forte /*
3471fcf3ce44SJohn Forte  * stmfGetSessionList
3472fcf3ce44SJohn Forte  *
3473fcf3ce44SJohn Forte  * Purpose: Retrieves the session list for a target (devid)
3474fcf3ce44SJohn Forte  *
3475fcf3ce44SJohn Forte  * devid - devid of target for which to retrieve session information.
3476fcf3ce44SJohn Forte  * sessionList - pointer to pointer to stmfSessionList structure
3477fcf3ce44SJohn Forte  *             on success, this contains the list of initiator sessions.
3478fcf3ce44SJohn Forte  */
3479fcf3ce44SJohn Forte int
3480fcf3ce44SJohn Forte stmfGetSessionList(stmfDevid *devid, stmfSessionList **sessionList)
3481fcf3ce44SJohn Forte {
3482fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
3483fcf3ce44SJohn Forte 	int fd;
3484fcf3ce44SJohn Forte 	int ioctlRet;
3485fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_SESSION_LIST;
3486fcf3ce44SJohn Forte 	int i;
3487fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
3488fcf3ce44SJohn Forte 	slist_scsi_session_t *fSessionList;
3489fcf3ce44SJohn Forte 	uint8_t ident[260];
3490fcf3ce44SJohn Forte 	uint32_t fSessionListSize;
3491fcf3ce44SJohn Forte 
3492fcf3ce44SJohn Forte 	if (sessionList == NULL || devid == NULL) {
3493fcf3ce44SJohn Forte 		ret = STMF_ERROR_INVALID_ARG;
3494fcf3ce44SJohn Forte 	}
3495fcf3ce44SJohn Forte 
3496fcf3ce44SJohn Forte 	/* call init */
3497fcf3ce44SJohn Forte 	ret = initializeConfig();
3498fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3499fcf3ce44SJohn Forte 		return (ret);
3500fcf3ce44SJohn Forte 	}
3501fcf3ce44SJohn Forte 
3502fcf3ce44SJohn Forte 	/*
3503fcf3ce44SJohn Forte 	 * Open control node for stmf
3504fcf3ce44SJohn Forte 	 */
3505fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3506fcf3ce44SJohn Forte 		return (ret);
3507fcf3ce44SJohn Forte 
3508fcf3ce44SJohn Forte 	/*
3509fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
3510fcf3ce44SJohn Forte 	 */
35118fe96085Stim szeto 	fSessionListSize = ALLOC_SESSION;
3512fcf3ce44SJohn Forte 	fSessionListSize = fSessionListSize * (sizeof (slist_scsi_session_t));
3513fcf3ce44SJohn Forte 	fSessionList = (slist_scsi_session_t *)calloc(1, fSessionListSize);
3514fcf3ce44SJohn Forte 	if (fSessionList == NULL) {
3515fcf3ce44SJohn Forte 		return (STMF_ERROR_NOMEM);
3516fcf3ce44SJohn Forte 	}
3517fcf3ce44SJohn Forte 
3518fcf3ce44SJohn Forte 	ident[IDENT_LENGTH_BYTE] = devid->identLength;
3519fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &ident[IDENT_LENGTH_BYTE + 1],
3520fcf3ce44SJohn Forte 	    devid->identLength);
3521fcf3ce44SJohn Forte 
3522fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
3523fcf3ce44SJohn Forte 	/*
3524fcf3ce44SJohn Forte 	 * Issue ioctl to get the session list
3525fcf3ce44SJohn Forte 	 */
3526fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
3527fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ident;
3528fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ident);
3529fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fSessionListSize;
3530fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fSessionList;
3531fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
3532fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
3533fcf3ce44SJohn Forte 		switch (errno) {
3534fcf3ce44SJohn Forte 			case EBUSY:
3535fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
3536fcf3ce44SJohn Forte 				break;
35378fe96085Stim szeto 			case EPERM:
3538fcf3ce44SJohn Forte 			case EACCES:
3539fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
3540fcf3ce44SJohn Forte 				break;
3541fcf3ce44SJohn Forte 			default:
3542fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
3543fcf3ce44SJohn Forte 				    "stmfGetSessionList:ioctl errno(%d)",
3544fcf3ce44SJohn Forte 				    errno);
3545fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
3546fcf3ce44SJohn Forte 				break;
3547fcf3ce44SJohn Forte 		}
3548fcf3ce44SJohn Forte 		goto done;
3549fcf3ce44SJohn Forte 	}
3550fcf3ce44SJohn Forte 	/*
3551fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
3552fcf3ce44SJohn Forte 	 */
35538fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_SESSION) {
3554fcf3ce44SJohn Forte 		fSessionListSize = stmfIoctl.stmf_obuf_max_nentries *
3555fcf3ce44SJohn Forte 		    sizeof (slist_scsi_session_t);
3556fcf3ce44SJohn Forte 		fSessionList = realloc(fSessionList, fSessionListSize);
3557fcf3ce44SJohn Forte 		if (fSessionList == NULL) {
3558fcf3ce44SJohn Forte 			return (STMF_ERROR_NOMEM);
3559fcf3ce44SJohn Forte 		}
3560fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fSessionListSize;
3561fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fSessionList;
3562fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
3563fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
3564fcf3ce44SJohn Forte 			switch (errno) {
3565fcf3ce44SJohn Forte 				case EBUSY:
3566fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
3567fcf3ce44SJohn Forte 					break;
35688fe96085Stim szeto 				case EPERM:
3569fcf3ce44SJohn Forte 				case EACCES:
3570fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
3571fcf3ce44SJohn Forte 					break;
3572fcf3ce44SJohn Forte 				default:
3573fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
3574fcf3ce44SJohn Forte 					    "stmfGetSessionList:ioctl "
3575fcf3ce44SJohn Forte 					    "errno(%d)", errno);
3576fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
3577fcf3ce44SJohn Forte 					break;
3578fcf3ce44SJohn Forte 			}
3579fcf3ce44SJohn Forte 			goto done;
3580fcf3ce44SJohn Forte 		}
3581fcf3ce44SJohn Forte 	}
3582fcf3ce44SJohn Forte 
3583fcf3ce44SJohn Forte 	/*
3584fcf3ce44SJohn Forte 	 * allocate caller's buffer with the final size
3585fcf3ce44SJohn Forte 	 */
3586fcf3ce44SJohn Forte 	*sessionList = (stmfSessionList *)calloc(1, sizeof (stmfSessionList) +
3587fcf3ce44SJohn Forte 	    stmfIoctl.stmf_obuf_max_nentries * sizeof (stmfSession));
3588fcf3ce44SJohn Forte 	if (*sessionList == NULL) {
3589fcf3ce44SJohn Forte 		ret = STMF_ERROR_NOMEM;
3590fcf3ce44SJohn Forte 		free(sessionList);
3591fcf3ce44SJohn Forte 		goto done;
3592fcf3ce44SJohn Forte 	}
3593fcf3ce44SJohn Forte 
3594fcf3ce44SJohn Forte 	(*sessionList)->cnt = stmfIoctl.stmf_obuf_max_nentries;
3595fcf3ce44SJohn Forte 
3596fcf3ce44SJohn Forte 	/*
3597fcf3ce44SJohn Forte 	 * copy session info to caller's buffer
3598fcf3ce44SJohn Forte 	 */
3599fcf3ce44SJohn Forte 	for (i = 0; i < (*sessionList)->cnt; i++) {
3600fcf3ce44SJohn Forte 		(*sessionList)->session[i].initiator.identLength =
3601fcf3ce44SJohn Forte 		    fSessionList->initiator[IDENT_LENGTH_BYTE];
3602fcf3ce44SJohn Forte 		bcopy(&(fSessionList->initiator[IDENT_LENGTH_BYTE + 1]),
3603fcf3ce44SJohn Forte 		    (*sessionList)->session[i].initiator.ident,
3604fcf3ce44SJohn Forte 		    STMF_IDENT_LENGTH);
3605fcf3ce44SJohn Forte 		bcopy(&(fSessionList->alias),
3606fcf3ce44SJohn Forte 		    &((*sessionList)->session[i].alias),
3607fcf3ce44SJohn Forte 		    sizeof ((*sessionList)->session[i].alias));
3608fcf3ce44SJohn Forte 		bcopy(&(fSessionList++->creation_time),
3609fcf3ce44SJohn Forte 		    &((*sessionList)->session[i].creationTime),
3610fcf3ce44SJohn Forte 		    sizeof (time_t));
3611fcf3ce44SJohn Forte 	}
3612fcf3ce44SJohn Forte done:
3613fcf3ce44SJohn Forte 	(void) close(fd);
3614fcf3ce44SJohn Forte 	return (ret);
3615fcf3ce44SJohn Forte }
3616fcf3ce44SJohn Forte 
3617fcf3ce44SJohn Forte /*
3618fcf3ce44SJohn Forte  * stmfGetTargetGroupList
3619fcf3ce44SJohn Forte  *
3620fcf3ce44SJohn Forte  * Purpose: Retrieves the list of target groups
3621fcf3ce44SJohn Forte  *
3622fcf3ce44SJohn Forte  * targetGroupList - pointer to a pointer to an stmfGroupList structure. On
3623fcf3ce44SJohn Forte  *		     success, it contains the list of target groups.
3624fcf3ce44SJohn Forte  */
3625fcf3ce44SJohn Forte int
3626fcf3ce44SJohn Forte stmfGetTargetGroupList(stmfGroupList **targetGroupList)
3627fcf3ce44SJohn Forte {
3628fcf3ce44SJohn Forte 	int ret;
3629fcf3ce44SJohn Forte 
3630fcf3ce44SJohn Forte 	if (targetGroupList == NULL) {
3631fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3632fcf3ce44SJohn Forte 	}
3633fcf3ce44SJohn Forte 
36348fe96085Stim szeto 	ret = groupListIoctl(targetGroupList, TARGET_GROUP);
3635fcf3ce44SJohn Forte 	return (ret);
3636fcf3ce44SJohn Forte }
3637fcf3ce44SJohn Forte 
3638fcf3ce44SJohn Forte /*
3639fcf3ce44SJohn Forte  * stmfGetTargetGroupMembers
3640fcf3ce44SJohn Forte  *
3641fcf3ce44SJohn Forte  * Purpose: Retrieves the group members for a target group
3642fcf3ce44SJohn Forte  *
3643fcf3ce44SJohn Forte  * groupName - name of target group for which to retrieve members.
3644fcf3ce44SJohn Forte  * groupProp - pointer to pointer to stmfGroupProperties structure
3645fcf3ce44SJohn Forte  *             on success, this contains the list of group members.
3646fcf3ce44SJohn Forte  */
3647fcf3ce44SJohn Forte int
3648fcf3ce44SJohn Forte stmfGetTargetGroupMembers(stmfGroupName *groupName,
3649fcf3ce44SJohn Forte     stmfGroupProperties **groupProp)
3650fcf3ce44SJohn Forte {
3651fcf3ce44SJohn Forte 	int ret;
3652fcf3ce44SJohn Forte 
3653fcf3ce44SJohn Forte 	if (groupName == NULL || groupProp == NULL) {
3654fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3655fcf3ce44SJohn Forte 	}
3656fcf3ce44SJohn Forte 
36578fe96085Stim szeto 	ret = groupMemberListIoctl(groupName, groupProp, TARGET_GROUP);
3658fcf3ce44SJohn Forte 
3659fcf3ce44SJohn Forte 	return (ret);
3660fcf3ce44SJohn Forte }
3661fcf3ce44SJohn Forte 
3662fcf3ce44SJohn Forte /*
3663fcf3ce44SJohn Forte  * stmfGetTargetList
3664fcf3ce44SJohn Forte  *
3665fcf3ce44SJohn Forte  * Purpose: Retrieves the list of target ports
3666fcf3ce44SJohn Forte  *
3667fcf3ce44SJohn Forte  * targetList - pointer to a pointer to an stmfDevidList structure.
3668fcf3ce44SJohn Forte  *		    On success, it contains the list of local ports (target).
3669fcf3ce44SJohn Forte  */
3670fcf3ce44SJohn Forte int
3671fcf3ce44SJohn Forte stmfGetTargetList(stmfDevidList **targetList)
3672fcf3ce44SJohn Forte {
3673fcf3ce44SJohn Forte 	int ret;
3674fcf3ce44SJohn Forte 	int fd;
3675fcf3ce44SJohn Forte 	int ioctlRet;
3676fcf3ce44SJohn Forte 	int i;
3677fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
3678fcf3ce44SJohn Forte 	/* framework target port list */
36798fe96085Stim szeto 	slist_target_port_t *fTargetList, *fTargetListP = NULL;
3680fcf3ce44SJohn Forte 	uint32_t fTargetListSize;
3681fcf3ce44SJohn Forte 
3682fcf3ce44SJohn Forte 	if (targetList == NULL) {
3683fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3684fcf3ce44SJohn Forte 	}
3685fcf3ce44SJohn Forte 
3686fcf3ce44SJohn Forte 	/* call init */
3687fcf3ce44SJohn Forte 	ret = initializeConfig();
3688fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3689fcf3ce44SJohn Forte 		return (ret);
3690fcf3ce44SJohn Forte 	}
3691fcf3ce44SJohn Forte 
3692fcf3ce44SJohn Forte 	/*
3693fcf3ce44SJohn Forte 	 * Open control node for stmf
3694fcf3ce44SJohn Forte 	 */
3695fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3696fcf3ce44SJohn Forte 		return (ret);
3697fcf3ce44SJohn Forte 
3698fcf3ce44SJohn Forte 	/*
3699fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
3700fcf3ce44SJohn Forte 	 */
37018fe96085Stim szeto 	fTargetListSize = ALLOC_TARGET_PORT * sizeof (slist_target_port_t);
37023e7352aeSJohn Forte 	fTargetListP = fTargetList =
37033e7352aeSJohn Forte 	    (slist_target_port_t *)calloc(1, fTargetListSize);
3704fcf3ce44SJohn Forte 	if (fTargetList == NULL) {
37058fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
3706fcf3ce44SJohn Forte 		goto done;
3707fcf3ce44SJohn Forte 	}
3708fcf3ce44SJohn Forte 
3709fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
3710fcf3ce44SJohn Forte 	/*
37113e7352aeSJohn Forte 	 * Issue ioctl to retrieve target list
3712fcf3ce44SJohn Forte 	 */
3713fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
3714fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fTargetListSize;
3715fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fTargetList;
3716fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_TARGET_PORT_LIST, &stmfIoctl);
3717fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
3718fcf3ce44SJohn Forte 		switch (errno) {
3719fcf3ce44SJohn Forte 			case EBUSY:
3720fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
3721fcf3ce44SJohn Forte 				break;
37228fe96085Stim szeto 			case EPERM:
3723fcf3ce44SJohn Forte 			case EACCES:
3724fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
3725fcf3ce44SJohn Forte 				break;
3726fcf3ce44SJohn Forte 			default:
3727fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
3728fcf3ce44SJohn Forte 				    "stmfGetTargetList:ioctl errno(%d)", errno);
3729fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
3730fcf3ce44SJohn Forte 				break;
3731fcf3ce44SJohn Forte 		}
3732fcf3ce44SJohn Forte 		goto done;
3733fcf3ce44SJohn Forte 	}
3734fcf3ce44SJohn Forte 	/*
3735fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
3736fcf3ce44SJohn Forte 	 */
37378fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_TARGET_PORT) {
3738fcf3ce44SJohn Forte 		fTargetListSize = stmfIoctl.stmf_obuf_max_nentries *
373976602b8dSJohn Forte 		    sizeof (slist_target_port_t);
37403e7352aeSJohn Forte 		fTargetListP = fTargetList =
37413e7352aeSJohn Forte 		    realloc(fTargetList, fTargetListSize);
3742fcf3ce44SJohn Forte 		if (fTargetList == NULL) {
37438fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
37448fe96085Stim szeto 			goto done;
3745fcf3ce44SJohn Forte 		}
3746fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fTargetListSize;
3747fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fTargetList;
3748fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, STMF_IOCTL_TARGET_PORT_LIST,
3749fcf3ce44SJohn Forte 		    &stmfIoctl);
3750fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
3751fcf3ce44SJohn Forte 			switch (errno) {
3752fcf3ce44SJohn Forte 				case EBUSY:
3753fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
3754fcf3ce44SJohn Forte 					break;
37558fe96085Stim szeto 				case EPERM:
3756fcf3ce44SJohn Forte 				case EACCES:
3757fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
3758fcf3ce44SJohn Forte 					break;
3759fcf3ce44SJohn Forte 				default:
3760fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
3761fcf3ce44SJohn Forte 					    "stmfGetTargetList:ioctl errno(%d)",
3762fcf3ce44SJohn Forte 					    errno);
3763fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
3764fcf3ce44SJohn Forte 					break;
3765fcf3ce44SJohn Forte 			}
3766fcf3ce44SJohn Forte 			goto done;
3767fcf3ce44SJohn Forte 		}
3768fcf3ce44SJohn Forte 	}
3769fcf3ce44SJohn Forte 
3770fcf3ce44SJohn Forte 	*targetList = (stmfDevidList *)calloc(1,
3771fcf3ce44SJohn Forte 	    stmfIoctl.stmf_obuf_max_nentries * sizeof (stmfDevid) +
3772fcf3ce44SJohn Forte 	    sizeof (stmfDevidList));
37738fe96085Stim szeto 	if (*targetList == NULL) {
37748fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
37758fe96085Stim szeto 		goto done;
37768fe96085Stim szeto 	}
3777fcf3ce44SJohn Forte 
3778fcf3ce44SJohn Forte 	(*targetList)->cnt = stmfIoctl.stmf_obuf_max_nentries;
3779fcf3ce44SJohn Forte 	for (i = 0; i < stmfIoctl.stmf_obuf_max_nentries; i++, fTargetList++) {
3780fcf3ce44SJohn Forte 		(*targetList)->devid[i].identLength =
3781fcf3ce44SJohn Forte 		    fTargetList->target[IDENT_LENGTH_BYTE];
3782fcf3ce44SJohn Forte 		bcopy(&fTargetList->target[IDENT_LENGTH_BYTE + 1],
3783fcf3ce44SJohn Forte 		    &(*targetList)->devid[i].ident,
3784fcf3ce44SJohn Forte 		    fTargetList->target[IDENT_LENGTH_BYTE]);
3785fcf3ce44SJohn Forte 	}
3786fcf3ce44SJohn Forte 
3787fcf3ce44SJohn Forte done:
3788fcf3ce44SJohn Forte 	(void) close(fd);
37893e7352aeSJohn Forte 	free(fTargetListP);
3790fcf3ce44SJohn Forte 	return (ret);
3791fcf3ce44SJohn Forte }
3792fcf3ce44SJohn Forte 
3793fcf3ce44SJohn Forte /*
3794fcf3ce44SJohn Forte  * stmfGetTargetProperties
3795fcf3ce44SJohn Forte  *
3796fcf3ce44SJohn Forte  * Purpose:  Retrieves the properties for a logical unit
3797fcf3ce44SJohn Forte  *
3798fcf3ce44SJohn Forte  * devid - devid of the target for which to retrieve properties
3799fcf3ce44SJohn Forte  * targetProps - pointer to an stmfTargetProperties structure.
3800fcf3ce44SJohn Forte  *		On success, it contains the target properties for
3801fcf3ce44SJohn Forte  *		the specified devid.
3802fcf3ce44SJohn Forte  */
3803fcf3ce44SJohn Forte int
3804fcf3ce44SJohn Forte stmfGetTargetProperties(stmfDevid *devid, stmfTargetProperties *targetProps)
3805fcf3ce44SJohn Forte {
3806fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
3807fcf3ce44SJohn Forte 	int fd;
3808fcf3ce44SJohn Forte 	int ioctlRet;
3809fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
3810fcf3ce44SJohn Forte 	sioc_target_port_props_t targetProperties;
3811fcf3ce44SJohn Forte 
3812fcf3ce44SJohn Forte 	if (devid == NULL || targetProps == NULL) {
3813fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3814fcf3ce44SJohn Forte 	}
3815fcf3ce44SJohn Forte 
3816fcf3ce44SJohn Forte 	/* call init */
3817fcf3ce44SJohn Forte 	ret = initializeConfig();
3818fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3819fcf3ce44SJohn Forte 		return (ret);
3820fcf3ce44SJohn Forte 	}
3821fcf3ce44SJohn Forte 
3822fcf3ce44SJohn Forte 	/*
3823fcf3ce44SJohn Forte 	 * Open control node for stmf
3824fcf3ce44SJohn Forte 	 */
3825fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3826fcf3ce44SJohn Forte 		return (ret);
3827fcf3ce44SJohn Forte 
3828fcf3ce44SJohn Forte 	targetProperties.tgt_id[IDENT_LENGTH_BYTE] = devid->identLength;
3829fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetProperties.tgt_id[IDENT_LENGTH_BYTE + 1],
3830fcf3ce44SJohn Forte 	    devid->identLength);
3831fcf3ce44SJohn Forte 
3832fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
3833fcf3ce44SJohn Forte 	/*
3834fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
3835fcf3ce44SJohn Forte 	 */
3836fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
3837fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (targetProperties.tgt_id);
3838fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&targetProperties.tgt_id;
3839fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&targetProperties;
3840fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (targetProperties);
3841fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_GET_TARGET_PORT_PROPERTIES,
3842fcf3ce44SJohn Forte 	    &stmfIoctl);
3843fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
3844fcf3ce44SJohn Forte 		switch (errno) {
3845fcf3ce44SJohn Forte 			case EBUSY:
3846fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
3847fcf3ce44SJohn Forte 				break;
38488fe96085Stim szeto 			case EPERM:
3849fcf3ce44SJohn Forte 			case EACCES:
3850fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
3851fcf3ce44SJohn Forte 				break;
3852fcf3ce44SJohn Forte 			case ENOENT:
3853fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
3854fcf3ce44SJohn Forte 				break;
3855fcf3ce44SJohn Forte 			default:
3856fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
3857fcf3ce44SJohn Forte 				    "stmfGetTargetProperties:ioctl errno(%d)",
3858fcf3ce44SJohn Forte 				    errno);
3859fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
3860fcf3ce44SJohn Forte 				break;
3861fcf3ce44SJohn Forte 		}
3862fcf3ce44SJohn Forte 		goto done;
3863fcf3ce44SJohn Forte 	}
3864fcf3ce44SJohn Forte 
3865fcf3ce44SJohn Forte 	bcopy(targetProperties.tgt_provider_name, targetProps->providerName,
3866fcf3ce44SJohn Forte 	    sizeof (targetProperties.tgt_provider_name));
3867fcf3ce44SJohn Forte 	if (targetProperties.tgt_state == STMF_STATE_ONLINE) {
3868fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_ONLINE;
3869fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_OFFLINE) {
3870fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_OFFLINE;
3871fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_ONLINING) {
3872fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_ONLINING;
3873fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_OFFLINING) {
3874fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_OFFLINING;
3875fcf3ce44SJohn Forte 	}
3876fcf3ce44SJohn Forte 	bcopy(targetProperties.tgt_alias, targetProps->alias,
3877fcf3ce44SJohn Forte 	    sizeof (targetProps->alias));
3878fcf3ce44SJohn Forte done:
3879fcf3ce44SJohn Forte 	(void) close(fd);
3880fcf3ce44SJohn Forte 	return (ret);
3881fcf3ce44SJohn Forte }
3882fcf3ce44SJohn Forte 
3883fcf3ce44SJohn Forte /*
3884fcf3ce44SJohn Forte  * stmfGetLogicalUnitList
3885fcf3ce44SJohn Forte  *
3886fcf3ce44SJohn Forte  * Purpose: Retrieves list of logical unit Object IDs
3887fcf3ce44SJohn Forte  *
3888fcf3ce44SJohn Forte  * luList - pointer to a pointer to a stmfGuidList structure. On success,
3889fcf3ce44SJohn Forte  *          it contains the list of logical unit guids.
3890fcf3ce44SJohn Forte  *
3891fcf3ce44SJohn Forte  */
3892fcf3ce44SJohn Forte int
3893fcf3ce44SJohn Forte stmfGetLogicalUnitList(stmfGuidList **luList)
3894fcf3ce44SJohn Forte {
3895fcf3ce44SJohn Forte 	int ret;
3896fcf3ce44SJohn Forte 	int fd;
3897fcf3ce44SJohn Forte 	int ioctlRet;
3898fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_LU_LIST;
38998fe96085Stim szeto 	int i;
3900fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
3901fcf3ce44SJohn Forte 	slist_lu_t *fLuList;
3902fcf3ce44SJohn Forte 	uint32_t fLuListSize;
39038fe96085Stim szeto 	uint32_t listCnt;
3904fcf3ce44SJohn Forte 
3905fcf3ce44SJohn Forte 	if (luList == NULL) {
3906fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3907fcf3ce44SJohn Forte 	}
3908fcf3ce44SJohn Forte 
3909fcf3ce44SJohn Forte 	/* call init */
3910fcf3ce44SJohn Forte 	ret = initializeConfig();
3911fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3912fcf3ce44SJohn Forte 		return (ret);
3913fcf3ce44SJohn Forte 	}
3914fcf3ce44SJohn Forte 
3915fcf3ce44SJohn Forte 	/*
3916fcf3ce44SJohn Forte 	 * Open control node for stmf
3917fcf3ce44SJohn Forte 	 */
3918fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3919fcf3ce44SJohn Forte 		return (ret);
3920fcf3ce44SJohn Forte 
3921fcf3ce44SJohn Forte 	/*
3922fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
3923fcf3ce44SJohn Forte 	 */
39248fe96085Stim szeto 	fLuListSize = ALLOC_LU;
3925fcf3ce44SJohn Forte 	fLuListSize = fLuListSize * (sizeof (slist_lu_t));
3926fcf3ce44SJohn Forte 	fLuList = (slist_lu_t *)calloc(1, fLuListSize);
3927fcf3ce44SJohn Forte 	if (fLuList == NULL) {
39288fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
39298fe96085Stim szeto 		goto done;
3930fcf3ce44SJohn Forte 	}
3931fcf3ce44SJohn Forte 
3932fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
3933fcf3ce44SJohn Forte 	/*
3934fcf3ce44SJohn Forte 	 * Issue ioctl to get the LU list
3935fcf3ce44SJohn Forte 	 */
3936fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
3937fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fLuListSize;
3938fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fLuList;
3939fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
3940fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
3941fcf3ce44SJohn Forte 		switch (errno) {
3942fcf3ce44SJohn Forte 			case EBUSY:
3943fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
3944fcf3ce44SJohn Forte 				break;
39458fe96085Stim szeto 			case EPERM:
3946fcf3ce44SJohn Forte 			case EACCES:
3947fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
3948fcf3ce44SJohn Forte 				break;
3949fcf3ce44SJohn Forte 			default:
3950fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
3951fcf3ce44SJohn Forte 				    "stmfGetLogicalUnitList:ioctl errno(%d)",
3952fcf3ce44SJohn Forte 				    errno);
3953fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
3954fcf3ce44SJohn Forte 				break;
3955fcf3ce44SJohn Forte 		}
3956fcf3ce44SJohn Forte 		goto done;
3957fcf3ce44SJohn Forte 	}
3958fcf3ce44SJohn Forte 	/*
3959fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
3960fcf3ce44SJohn Forte 	 */
39618fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_LU) {
3962fcf3ce44SJohn Forte 		fLuListSize = stmfIoctl.stmf_obuf_max_nentries *
3963fcf3ce44SJohn Forte 		    sizeof (slist_lu_t);
39648fe96085Stim szeto 		free(fLuList);
39658fe96085Stim szeto 		fLuList = (slist_lu_t *)calloc(1, fLuListSize);
3966fcf3ce44SJohn Forte 		if (fLuList == NULL) {
39678fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
39688fe96085Stim szeto 			goto done;
3969fcf3ce44SJohn Forte 		}
3970fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fLuListSize;
3971fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fLuList;
3972fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
3973fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
3974fcf3ce44SJohn Forte 			switch (errno) {
3975fcf3ce44SJohn Forte 				case EBUSY:
3976fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
3977fcf3ce44SJohn Forte 					break;
39788fe96085Stim szeto 				case EPERM:
3979fcf3ce44SJohn Forte 				case EACCES:
3980fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
3981fcf3ce44SJohn Forte 					break;
3982fcf3ce44SJohn Forte 				default:
3983fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
3984fcf3ce44SJohn Forte 					    "stmfGetLogicalUnitList:"
3985fcf3ce44SJohn Forte 					    "ioctl errno(%d)", errno);
3986fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
3987fcf3ce44SJohn Forte 					break;
3988fcf3ce44SJohn Forte 			}
3989fcf3ce44SJohn Forte 			goto done;
3990fcf3ce44SJohn Forte 		}
3991fcf3ce44SJohn Forte 	}
3992fcf3ce44SJohn Forte 
3993fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3994fcf3ce44SJohn Forte 		goto done;
3995fcf3ce44SJohn Forte 	}
3996fcf3ce44SJohn Forte 
39978fe96085Stim szeto 	listCnt = stmfIoctl.stmf_obuf_nentries;
3998fcf3ce44SJohn Forte 
3999fcf3ce44SJohn Forte 	/*
4000fcf3ce44SJohn Forte 	 * allocate caller's buffer with the final size
4001fcf3ce44SJohn Forte 	 */
4002fcf3ce44SJohn Forte 	*luList = (stmfGuidList *)calloc(1, sizeof (stmfGuidList) +
40038fe96085Stim szeto 	    listCnt * sizeof (stmfGuid));
4004fcf3ce44SJohn Forte 	if (*luList == NULL) {
4005fcf3ce44SJohn Forte 		ret = STMF_ERROR_NOMEM;
4006fcf3ce44SJohn Forte 		goto done;
4007fcf3ce44SJohn Forte 	}
4008fcf3ce44SJohn Forte 
40098fe96085Stim szeto 	(*luList)->cnt = listCnt;
40108fe96085Stim szeto 
40118fe96085Stim szeto 	/* copy to caller's buffer */
40128fe96085Stim szeto 	for (i = 0; i < listCnt; i++) {
40138fe96085Stim szeto 		bcopy(&fLuList[i].lu_guid, (*luList)->guid[i].guid,
4014fcf3ce44SJohn Forte 		    sizeof (stmfGuid));
4015fcf3ce44SJohn Forte 	}
4016fcf3ce44SJohn Forte 
40178fe96085Stim szeto 	/*
40188fe96085Stim szeto 	 * sort the list. This gives a consistent view across gets
40198fe96085Stim szeto 	 */
40208fe96085Stim szeto 	qsort((void *)&((*luList)->guid[0]), (*luList)->cnt,
40218fe96085Stim szeto 	    sizeof (stmfGuid), guidCompare);
4022fcf3ce44SJohn Forte 
4023fcf3ce44SJohn Forte done:
4024fcf3ce44SJohn Forte 	(void) close(fd);
4025fcf3ce44SJohn Forte 	/*
4026fcf3ce44SJohn Forte 	 * free internal buffers
4027fcf3ce44SJohn Forte 	 */
4028fcf3ce44SJohn Forte 	free(fLuList);
4029fcf3ce44SJohn Forte 	return (ret);
4030fcf3ce44SJohn Forte }
4031fcf3ce44SJohn Forte 
4032fcf3ce44SJohn Forte /*
4033fcf3ce44SJohn Forte  * stmfGetLogicalUnitProperties
4034fcf3ce44SJohn Forte  *
4035fcf3ce44SJohn Forte  * Purpose:  Retrieves the properties for a logical unit
4036fcf3ce44SJohn Forte  *
4037fcf3ce44SJohn Forte  * lu - guid of the logical unit for which to retrieve properties
4038fcf3ce44SJohn Forte  * stmfLuProps - pointer to an stmfLogicalUnitProperties structure. On success,
4039fcf3ce44SJohn Forte  *               it contains the logical unit properties for the specified guid.
4040fcf3ce44SJohn Forte  */
4041fcf3ce44SJohn Forte int
4042fcf3ce44SJohn Forte stmfGetLogicalUnitProperties(stmfGuid *lu, stmfLogicalUnitProperties *luProps)
4043fcf3ce44SJohn Forte {
4044fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4045fcf3ce44SJohn Forte 	int stmfRet;
4046fcf3ce44SJohn Forte 	int fd;
4047fcf3ce44SJohn Forte 	int ioctlRet;
4048fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_GET_LU_PROPERTIES;
4049fcf3ce44SJohn Forte 	stmfViewEntryList *viewEntryList = NULL;
4050fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4051fcf3ce44SJohn Forte 	sioc_lu_props_t fLuProps;
4052fcf3ce44SJohn Forte 
40538fe96085Stim szeto 	if (lu == NULL || luProps == NULL) {
40548fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
4055fcf3ce44SJohn Forte 	}
4056fcf3ce44SJohn Forte 
4057fcf3ce44SJohn Forte 	bzero(luProps, sizeof (stmfLogicalUnitProperties));
4058fcf3ce44SJohn Forte 
4059fcf3ce44SJohn Forte 	/* call init */
4060fcf3ce44SJohn Forte 	ret = initializeConfig();
4061fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4062fcf3ce44SJohn Forte 		return (ret);
4063fcf3ce44SJohn Forte 	}
4064fcf3ce44SJohn Forte 
4065fcf3ce44SJohn Forte 	/*
4066fcf3ce44SJohn Forte 	 * Open control node for stmf
4067fcf3ce44SJohn Forte 	 */
4068fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
4069fcf3ce44SJohn Forte 		return (ret);
4070fcf3ce44SJohn Forte 
4071fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4072fcf3ce44SJohn Forte 	/*
4073fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
4074fcf3ce44SJohn Forte 	 */
4075fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4076fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGuid);
4077fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)lu;
4078fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&fLuProps;
4079fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (fLuProps);
4080fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4081fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4082fcf3ce44SJohn Forte 		switch (errno) {
4083fcf3ce44SJohn Forte 			case EBUSY:
4084fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4085fcf3ce44SJohn Forte 				break;
40868fe96085Stim szeto 			case EPERM:
4087fcf3ce44SJohn Forte 			case EACCES:
4088fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4089fcf3ce44SJohn Forte 				break;
4090fcf3ce44SJohn Forte 			case ENOENT:
4091fcf3ce44SJohn Forte 				stmfRet = stmfGetViewEntryList(lu,
4092fcf3ce44SJohn Forte 				    &viewEntryList);
4093fcf3ce44SJohn Forte 				if (stmfRet == STMF_STATUS_SUCCESS) {
4094fcf3ce44SJohn Forte 					luProps->status =
4095fcf3ce44SJohn Forte 					    STMF_LOGICAL_UNIT_UNREGISTERED;
4096fcf3ce44SJohn Forte 					if (viewEntryList->cnt > 0) {
4097fcf3ce44SJohn Forte 						ret = STMF_STATUS_SUCCESS;
4098fcf3ce44SJohn Forte 					} else {
4099fcf3ce44SJohn Forte 						ret = STMF_ERROR_NOT_FOUND;
4100fcf3ce44SJohn Forte 					}
4101fcf3ce44SJohn Forte 				} else {
4102fcf3ce44SJohn Forte 					ret = STMF_ERROR_NOT_FOUND;
4103fcf3ce44SJohn Forte 				}
4104fcf3ce44SJohn Forte 				stmfFreeMemory(viewEntryList);
4105fcf3ce44SJohn Forte 				break;
4106fcf3ce44SJohn Forte 			default:
4107fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4108fcf3ce44SJohn Forte 				    "stmfGetLogicalUnit:ioctl errno(%d)",
4109fcf3ce44SJohn Forte 				    errno);
4110fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4111fcf3ce44SJohn Forte 				break;
4112fcf3ce44SJohn Forte 		}
4113fcf3ce44SJohn Forte 		goto done;
4114fcf3ce44SJohn Forte 	}
4115fcf3ce44SJohn Forte 
4116fcf3ce44SJohn Forte 	bcopy(fLuProps.lu_provider_name, luProps->providerName,
4117fcf3ce44SJohn Forte 	    sizeof (fLuProps.lu_provider_name));
4118fcf3ce44SJohn Forte 	if (fLuProps.lu_state == STMF_STATE_ONLINE) {
4119fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_ONLINE;
4120fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_OFFLINE) {
4121fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_OFFLINE;
4122fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_ONLINING) {
4123fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_ONLINING;
4124fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_OFFLINING) {
4125fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_OFFLINING;
4126fcf3ce44SJohn Forte 	}
4127fcf3ce44SJohn Forte 	bcopy(fLuProps.lu_alias, luProps->alias, sizeof (luProps->alias));
4128fcf3ce44SJohn Forte done:
4129fcf3ce44SJohn Forte 	(void) close(fd);
4130fcf3ce44SJohn Forte 	return (ret);
4131fcf3ce44SJohn Forte }
4132fcf3ce44SJohn Forte 
4133fcf3ce44SJohn Forte /*
4134fcf3ce44SJohn Forte  * stmfGetState
4135fcf3ce44SJohn Forte  *
4136fcf3ce44SJohn Forte  * Purpose: retrieve the current state of the stmf module
4137fcf3ce44SJohn Forte  *
4138fcf3ce44SJohn Forte  * state - pointer to stmfState structure allocated by the caller
4139fcf3ce44SJohn Forte  *         On success, contains the state of stmf
4140fcf3ce44SJohn Forte  */
4141fcf3ce44SJohn Forte int
4142fcf3ce44SJohn Forte stmfGetState(stmfState *state)
4143fcf3ce44SJohn Forte {
4144fcf3ce44SJohn Forte 	int ret;
4145fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
4146fcf3ce44SJohn Forte 
4147fcf3ce44SJohn Forte 	if (state == NULL) {
4148fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4149fcf3ce44SJohn Forte 	}
4150fcf3ce44SJohn Forte 
4151fcf3ce44SJohn Forte 	ret = getStmfState(&iState);
4152fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4153fcf3ce44SJohn Forte 		return (ret);
4154fcf3ce44SJohn Forte 	}
4155fcf3ce44SJohn Forte 	switch (iState.state) {
4156fcf3ce44SJohn Forte 		case STMF_STATE_ONLINE:
4157fcf3ce44SJohn Forte 			state->operationalState =
4158fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_ONLINE;
4159fcf3ce44SJohn Forte 			break;
4160fcf3ce44SJohn Forte 		case STMF_STATE_OFFLINE:
4161fcf3ce44SJohn Forte 			state->operationalState =
4162fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_OFFLINE;
4163fcf3ce44SJohn Forte 			break;
4164fcf3ce44SJohn Forte 		case STMF_STATE_ONLINING:
4165fcf3ce44SJohn Forte 			state->operationalState =
4166fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_ONLINING;
4167fcf3ce44SJohn Forte 			break;
4168fcf3ce44SJohn Forte 		case STMF_STATE_OFFLINING:
4169fcf3ce44SJohn Forte 			state->operationalState =
4170fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_OFFLINING;
4171fcf3ce44SJohn Forte 			break;
4172fcf3ce44SJohn Forte 		default:
4173fcf3ce44SJohn Forte 			state->operationalState =
4174fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_UNKNOWN;
4175fcf3ce44SJohn Forte 			break;
4176fcf3ce44SJohn Forte 	}
4177fcf3ce44SJohn Forte 	switch (iState.config_state) {
4178fcf3ce44SJohn Forte 		case STMF_CONFIG_NONE:
4179fcf3ce44SJohn Forte 			state->configState = STMF_CONFIG_STATE_NONE;
4180fcf3ce44SJohn Forte 			break;
4181fcf3ce44SJohn Forte 		case STMF_CONFIG_INIT:
4182fcf3ce44SJohn Forte 			state->configState = STMF_CONFIG_STATE_INIT;
4183fcf3ce44SJohn Forte 			break;
4184fcf3ce44SJohn Forte 		case STMF_CONFIG_INIT_DONE:
4185fcf3ce44SJohn Forte 			state->configState =
4186fcf3ce44SJohn Forte 			    STMF_CONFIG_STATE_INIT_DONE;
4187fcf3ce44SJohn Forte 			break;
4188fcf3ce44SJohn Forte 		default:
4189fcf3ce44SJohn Forte 			state->configState =
4190fcf3ce44SJohn Forte 			    STMF_CONFIG_STATE_UNKNOWN;
4191fcf3ce44SJohn Forte 			break;
4192fcf3ce44SJohn Forte 	}
4193fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
4194fcf3ce44SJohn Forte }
4195fcf3ce44SJohn Forte 
4196fcf3ce44SJohn Forte /*
4197fcf3ce44SJohn Forte  * stmfGetViewEntryList
4198fcf3ce44SJohn Forte  *
4199fcf3ce44SJohn Forte  * Purpose: Retrieves the list of view entries for the specified
4200fcf3ce44SJohn Forte  *          logical unit.
4201fcf3ce44SJohn Forte  *
4202fcf3ce44SJohn Forte  * lu - the guid of the logical unit for which to retrieve the view entry list
4203fcf3ce44SJohn Forte  * viewEntryList - a pointer to a pointer to a stmfViewEntryList structure. On
4204fcf3ce44SJohn Forte  *                 success, contains the list of view entries.
4205fcf3ce44SJohn Forte  */
4206fcf3ce44SJohn Forte int
4207fcf3ce44SJohn Forte stmfGetViewEntryList(stmfGuid *lu, stmfViewEntryList **viewEntryList)
4208fcf3ce44SJohn Forte {
4209fcf3ce44SJohn Forte 	int ret;
42108fe96085Stim szeto 	int fd;
42118fe96085Stim szeto 	int ioctlRet;
42128fe96085Stim szeto 	int cmd = STMF_IOCTL_LU_VE_LIST;
42138fe96085Stim szeto 	int i;
42148fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
42158fe96085Stim szeto 	stmf_view_op_entry_t *fVeList;
42168fe96085Stim szeto 	uint32_t fVeListSize;
42178fe96085Stim szeto 	uint32_t listCnt;
4218fcf3ce44SJohn Forte 
4219fcf3ce44SJohn Forte 	if (lu == NULL || viewEntryList == NULL) {
4220fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4221fcf3ce44SJohn Forte 	}
4222fcf3ce44SJohn Forte 
42238fe96085Stim szeto 	/* call init */
42248fe96085Stim szeto 	ret = initializeConfig();
42258fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
42268fe96085Stim szeto 		return (ret);
42278fe96085Stim szeto 	}
42288fe96085Stim szeto 
42298fe96085Stim szeto 	/*
42308fe96085Stim szeto 	 * Open control node for stmf
42318fe96085Stim szeto 	 */
42328fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
42338fe96085Stim szeto 		return (ret);
42348fe96085Stim szeto 
42358fe96085Stim szeto 	/*
42368fe96085Stim szeto 	 * Allocate ioctl input buffer
42378fe96085Stim szeto 	 */
42388fe96085Stim szeto 	fVeListSize = ALLOC_VE;
42398fe96085Stim szeto 	fVeListSize = fVeListSize * (sizeof (stmf_view_op_entry_t));
42408fe96085Stim szeto 	fVeList = (stmf_view_op_entry_t *)calloc(1, fVeListSize);
42418fe96085Stim szeto 	if (fVeList == NULL) {
42428fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
42438fe96085Stim szeto 		goto done;
42448fe96085Stim szeto 	}
42458fe96085Stim szeto 
42468fe96085Stim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
42478fe96085Stim szeto 	/*
42488fe96085Stim szeto 	 * Issue ioctl to get the LU list
42498fe96085Stim szeto 	 */
42508fe96085Stim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
42518fe96085Stim szeto 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)lu;
42528fe96085Stim szeto 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGuid);
42538fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = fVeListSize;
42548fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fVeList;
42558fe96085Stim szeto 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
42568fe96085Stim szeto 	if (ioctlRet != 0) {
42578fe96085Stim szeto 		switch (errno) {
42588fe96085Stim szeto 			case EBUSY:
4259fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4260fcf3ce44SJohn Forte 				break;
42618fe96085Stim szeto 			case EPERM:
42628fe96085Stim szeto 			case EACCES:
42638fe96085Stim szeto 				ret = STMF_ERROR_PERM;
4264fcf3ce44SJohn Forte 				break;
4265fcf3ce44SJohn Forte 			default:
4266fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
42678fe96085Stim szeto 				    "stmfGetViewEntryList:ioctl errno(%d)",
42688fe96085Stim szeto 				    errno);
4269fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4270fcf3ce44SJohn Forte 				break;
4271fcf3ce44SJohn Forte 		}
42728fe96085Stim szeto 		goto done;
42738fe96085Stim szeto 	}
42748fe96085Stim szeto 	/*
42758fe96085Stim szeto 	 * Check whether input buffer was large enough
42768fe96085Stim szeto 	 */
42778fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_VE) {
42788fe96085Stim szeto 		bzero(&stmfIoctl, sizeof (stmfIoctl));
42798fe96085Stim szeto 		fVeListSize = stmfIoctl.stmf_obuf_max_nentries *
42808fe96085Stim szeto 		    sizeof (stmf_view_op_entry_t);
42818fe96085Stim szeto 		free(fVeList);
42828fe96085Stim szeto 		fVeList = (stmf_view_op_entry_t *)calloc(1, fVeListSize);
42838fe96085Stim szeto 		if (fVeList == NULL) {
42848fe96085Stim szeto 			return (STMF_ERROR_NOMEM);
42858fe96085Stim szeto 		}
42868fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = fVeListSize;
42878fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fVeList;
42888fe96085Stim szeto 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
42898fe96085Stim szeto 		if (ioctlRet != 0) {
42908fe96085Stim szeto 			switch (errno) {
42918fe96085Stim szeto 				case EBUSY:
42928fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
42938fe96085Stim szeto 					break;
42948fe96085Stim szeto 				case EPERM:
42958fe96085Stim szeto 				case EACCES:
42968fe96085Stim szeto 					ret = STMF_ERROR_PERM;
42978fe96085Stim szeto 					break;
42988fe96085Stim szeto 				default:
42998fe96085Stim szeto 					syslog(LOG_DEBUG,
43008fe96085Stim szeto 					    "stmfGetLogicalUnitList:"
43018fe96085Stim szeto 					    "ioctl errno(%d)", errno);
43028fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
43038fe96085Stim szeto 					break;
43048fe96085Stim szeto 			}
43058fe96085Stim szeto 			goto done;
43068fe96085Stim szeto 		}
43078fe96085Stim szeto 	}
4308fcf3ce44SJohn Forte 
43098fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
43108fe96085Stim szeto 		goto done;
43118fe96085Stim szeto 	}
43128fe96085Stim szeto 
43138fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_nentries == 0) {
43148fe96085Stim szeto 		ret = STMF_ERROR_NOT_FOUND;
43158fe96085Stim szeto 		goto done;
43168fe96085Stim szeto 	}
43178fe96085Stim szeto 
43188fe96085Stim szeto 	listCnt = stmfIoctl.stmf_obuf_nentries;
43198fe96085Stim szeto 
43208fe96085Stim szeto 	/*
43218fe96085Stim szeto 	 * allocate caller's buffer with the final size
43228fe96085Stim szeto 	 */
43238fe96085Stim szeto 	*viewEntryList = (stmfViewEntryList *)calloc(1,
43248fe96085Stim szeto 	    sizeof (stmfViewEntryList) + listCnt * sizeof (stmfViewEntry));
43258fe96085Stim szeto 	if (*viewEntryList == NULL) {
43268fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
43278fe96085Stim szeto 		goto done;
43288fe96085Stim szeto 	}
43298fe96085Stim szeto 
43308fe96085Stim szeto 	(*viewEntryList)->cnt = listCnt;
43318fe96085Stim szeto 
43328fe96085Stim szeto 	/* copy to caller's buffer */
43338fe96085Stim szeto 	for (i = 0; i < listCnt; i++) {
43348fe96085Stim szeto 		(*viewEntryList)->ve[i].veIndexValid = B_TRUE;
43358fe96085Stim szeto 		(*viewEntryList)->ve[i].veIndex = fVeList[i].ve_ndx;
43368fe96085Stim szeto 		if (fVeList[i].ve_all_hosts == 1) {
43378fe96085Stim szeto 			(*viewEntryList)->ve[i].allHosts = B_TRUE;
43388fe96085Stim szeto 		} else {
43398fe96085Stim szeto 			bcopy(fVeList[i].ve_host_group.name,
43408fe96085Stim szeto 			    (*viewEntryList)->ve[i].hostGroup,
43418fe96085Stim szeto 			    fVeList[i].ve_host_group.name_size);
43428fe96085Stim szeto 		}
43438fe96085Stim szeto 		if (fVeList[i].ve_all_targets == 1) {
43448fe96085Stim szeto 			(*viewEntryList)->ve[i].allTargets = B_TRUE;
43458fe96085Stim szeto 		} else {
43468fe96085Stim szeto 			bcopy(fVeList[i].ve_target_group.name,
43478fe96085Stim szeto 			    (*viewEntryList)->ve[i].targetGroup,
43488fe96085Stim szeto 			    fVeList[i].ve_target_group.name_size);
43498fe96085Stim szeto 		}
43508fe96085Stim szeto 		bcopy(fVeList[i].ve_lu_nbr, (*viewEntryList)->ve[i].luNbr,
43518fe96085Stim szeto 		    sizeof ((*viewEntryList)->ve[i].luNbr));
43528fe96085Stim szeto 		(*viewEntryList)->ve[i].luNbrValid = B_TRUE;
43538fe96085Stim szeto 	}
43548fe96085Stim szeto 
43558fe96085Stim szeto 	/*
43568fe96085Stim szeto 	 * sort the list. This gives a consistent view across gets
43578fe96085Stim szeto 	 */
43588fe96085Stim szeto 	qsort((void *)&((*viewEntryList)->ve[0]), (*viewEntryList)->cnt,
43598fe96085Stim szeto 	    sizeof (stmfViewEntry), viewEntryCompare);
43608fe96085Stim szeto 
43618fe96085Stim szeto done:
43628fe96085Stim szeto 	(void) close(fd);
43638fe96085Stim szeto 	/*
43648fe96085Stim szeto 	 * free internal buffers
43658fe96085Stim szeto 	 */
43668fe96085Stim szeto 	free(fVeList);
4367fcf3ce44SJohn Forte 	return (ret);
4368fcf3ce44SJohn Forte }
4369fcf3ce44SJohn Forte 
43708fe96085Stim szeto 
4371fcf3ce44SJohn Forte /*
4372fcf3ce44SJohn Forte  * loadHostGroups
4373fcf3ce44SJohn Forte  *
4374fcf3ce44SJohn Forte  * Purpose - issues the ioctl to load the host groups into stmf
4375fcf3ce44SJohn Forte  *
4376fcf3ce44SJohn Forte  * fd - file descriptor for the control node of stmf.
4377fcf3ce44SJohn Forte  * groupList - populated host group list
4378fcf3ce44SJohn Forte  */
4379fcf3ce44SJohn Forte static int
4380fcf3ce44SJohn Forte loadHostGroups(int fd, stmfGroupList *groupList)
4381fcf3ce44SJohn Forte {
4382fcf3ce44SJohn Forte 	int i, j;
4383fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4384fcf3ce44SJohn Forte 	stmfGroupProperties *groupProps = NULL;
4385fcf3ce44SJohn Forte 
4386fcf3ce44SJohn Forte 	for (i = 0; i < groupList->cnt; i++) {
4387fcf3ce44SJohn Forte 		if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_HOST_GROUP,
4388fcf3ce44SJohn Forte 		    &(groupList->name[i]))) != STMF_STATUS_SUCCESS) {
4389fcf3ce44SJohn Forte 			goto out;
4390fcf3ce44SJohn Forte 		}
43918fe96085Stim szeto 		ret = iLoadGroupMembersFromPs(&(groupList->name[i]),
43928fe96085Stim szeto 		    &groupProps, HOST_GROUP);
4393fcf3ce44SJohn Forte 		for (j = 0; j < groupProps->cnt; j++) {
4394fcf3ce44SJohn Forte 			if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_HG_ENTRY,
4395fcf3ce44SJohn Forte 			    &(groupList->name[i]), &(groupProps->name[j])))
4396fcf3ce44SJohn Forte 			    != STMF_STATUS_SUCCESS) {
4397fcf3ce44SJohn Forte 				goto out;
4398fcf3ce44SJohn Forte 			}
4399fcf3ce44SJohn Forte 		}
4400fcf3ce44SJohn Forte 	}
4401fcf3ce44SJohn Forte 
4402fcf3ce44SJohn Forte 
4403fcf3ce44SJohn Forte out:
4404fcf3ce44SJohn Forte 	stmfFreeMemory(groupProps);
4405fcf3ce44SJohn Forte 	return (ret);
4406fcf3ce44SJohn Forte }
4407fcf3ce44SJohn Forte 
4408fcf3ce44SJohn Forte /*
4409fcf3ce44SJohn Forte  * loadTargetGroups
4410fcf3ce44SJohn Forte  *
4411fcf3ce44SJohn Forte  * Purpose - issues the ioctl to load the target groups into stmf
4412fcf3ce44SJohn Forte  *
4413fcf3ce44SJohn Forte  * fd - file descriptor for the control node of stmf.
4414fcf3ce44SJohn Forte  * groupList - populated target group list.
4415fcf3ce44SJohn Forte  */
4416fcf3ce44SJohn Forte static int
4417fcf3ce44SJohn Forte loadTargetGroups(int fd, stmfGroupList *groupList)
4418fcf3ce44SJohn Forte {
4419fcf3ce44SJohn Forte 	int i, j;
4420fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4421fcf3ce44SJohn Forte 	stmfGroupProperties *groupProps = NULL;
4422fcf3ce44SJohn Forte 
4423fcf3ce44SJohn Forte 	for (i = 0; i < groupList->cnt; i++) {
4424fcf3ce44SJohn Forte 		if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_TARGET_GROUP,
4425fcf3ce44SJohn Forte 		    &(groupList->name[i]))) != STMF_STATUS_SUCCESS) {
4426fcf3ce44SJohn Forte 			goto out;
4427fcf3ce44SJohn Forte 		}
44288fe96085Stim szeto 		ret = iLoadGroupMembersFromPs(&(groupList->name[i]),
44298fe96085Stim szeto 		    &groupProps, TARGET_GROUP);
4430fcf3ce44SJohn Forte 		for (j = 0; j < groupProps->cnt; j++) {
4431fcf3ce44SJohn Forte 			if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_TG_ENTRY,
4432fcf3ce44SJohn Forte 			    &(groupList->name[i]), &(groupProps->name[j])))
4433fcf3ce44SJohn Forte 			    != STMF_STATUS_SUCCESS) {
4434fcf3ce44SJohn Forte 				goto out;
4435fcf3ce44SJohn Forte 			}
4436fcf3ce44SJohn Forte 		}
4437fcf3ce44SJohn Forte 	}
4438fcf3ce44SJohn Forte 
4439fcf3ce44SJohn Forte 
4440fcf3ce44SJohn Forte out:
4441fcf3ce44SJohn Forte 	stmfFreeMemory(groupProps);
4442fcf3ce44SJohn Forte 	return (ret);
4443fcf3ce44SJohn Forte }
4444fcf3ce44SJohn Forte 
4445fcf3ce44SJohn Forte 
4446fcf3ce44SJohn Forte /*
4447fcf3ce44SJohn Forte  * loadStore
4448fcf3ce44SJohn Forte  *
4449fcf3ce44SJohn Forte  * Purpose: Load the configuration data from the store
4450fcf3ce44SJohn Forte  *
4451fcf3ce44SJohn Forte  * First load the host groups and target groups, then the view entries
4452fcf3ce44SJohn Forte  * and finally the provider data
4453fcf3ce44SJohn Forte  *
4454fcf3ce44SJohn Forte  * fd - file descriptor of control node for stmf.
4455fcf3ce44SJohn Forte  */
4456fcf3ce44SJohn Forte static int
4457fcf3ce44SJohn Forte loadStore(int fd)
4458fcf3ce44SJohn Forte {
4459fcf3ce44SJohn Forte 	int ret;
4460fcf3ce44SJohn Forte 	int i, j;
4461fcf3ce44SJohn Forte 	stmfGroupList *groupList = NULL;
4462fcf3ce44SJohn Forte 	stmfGuidList *guidList = NULL;
4463fcf3ce44SJohn Forte 	stmfViewEntryList *viewEntryList = NULL;
4464fcf3ce44SJohn Forte 	stmfProviderList *providerList = NULL;
4465fcf3ce44SJohn Forte 	int providerType;
4466fcf3ce44SJohn Forte 	nvlist_t *nvl = NULL;
4467fcf3ce44SJohn Forte 
4468fcf3ce44SJohn Forte 
4469fcf3ce44SJohn Forte 
4470fcf3ce44SJohn Forte 	/* load host groups */
44718fe96085Stim szeto 	ret = iLoadGroupFromPs(&groupList, HOST_GROUP);
4472fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4473fcf3ce44SJohn Forte 		return (ret);
4474fcf3ce44SJohn Forte 	}
4475fcf3ce44SJohn Forte 	ret = loadHostGroups(fd, groupList);
4476fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4477fcf3ce44SJohn Forte 		goto out;
4478fcf3ce44SJohn Forte 	}
4479fcf3ce44SJohn Forte 
4480fcf3ce44SJohn Forte 	stmfFreeMemory(groupList);
4481fcf3ce44SJohn Forte 	groupList = NULL;
4482fcf3ce44SJohn Forte 
4483fcf3ce44SJohn Forte 	/* load target groups */
44848fe96085Stim szeto 	ret = iLoadGroupFromPs(&groupList, TARGET_GROUP);
4485fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4486fcf3ce44SJohn Forte 		goto out;
4487fcf3ce44SJohn Forte 	}
4488fcf3ce44SJohn Forte 	ret = loadTargetGroups(fd, groupList);
4489fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4490fcf3ce44SJohn Forte 		goto out;
4491fcf3ce44SJohn Forte 	}
4492fcf3ce44SJohn Forte 
4493fcf3ce44SJohn Forte 	stmfFreeMemory(groupList);
4494fcf3ce44SJohn Forte 	groupList = NULL;
4495fcf3ce44SJohn Forte 
4496fcf3ce44SJohn Forte 	/* Get the guid list */
4497fcf3ce44SJohn Forte 	ret = psGetLogicalUnitList(&guidList);
4498fcf3ce44SJohn Forte 	switch (ret) {
4499fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
4500fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
4501fcf3ce44SJohn Forte 			break;
4502fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
4503fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
4504fcf3ce44SJohn Forte 			break;
4505fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
4506fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
4507fcf3ce44SJohn Forte 			break;
4508fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4509fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
4510fcf3ce44SJohn Forte 			break;
4511fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
4512fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
4513fcf3ce44SJohn Forte 			break;
4514fcf3ce44SJohn Forte 		default:
4515fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
4516fcf3ce44SJohn Forte 			break;
4517fcf3ce44SJohn Forte 	}
4518fcf3ce44SJohn Forte 
4519fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4520fcf3ce44SJohn Forte 		goto out;
4521fcf3ce44SJohn Forte 	}
4522fcf3ce44SJohn Forte 
4523fcf3ce44SJohn Forte 	/*
4524fcf3ce44SJohn Forte 	 * We have the guid list, now get the corresponding
4525fcf3ce44SJohn Forte 	 * view entries for each guid
4526fcf3ce44SJohn Forte 	 */
4527fcf3ce44SJohn Forte 	for (i = 0; i < guidList->cnt; i++) {
4528fcf3ce44SJohn Forte 		ret = psGetViewEntryList(&guidList->guid[i], &viewEntryList);
4529fcf3ce44SJohn Forte 		switch (ret) {
4530fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
4531fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
4532fcf3ce44SJohn Forte 				break;
4533fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
4534fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4535fcf3ce44SJohn Forte 				break;
4536fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
4537fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4538fcf3ce44SJohn Forte 				break;
4539fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4540fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
4541fcf3ce44SJohn Forte 				break;
4542fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
4543fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
4544fcf3ce44SJohn Forte 				break;
4545fcf3ce44SJohn Forte 			default:
4546fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4547fcf3ce44SJohn Forte 				break;
4548fcf3ce44SJohn Forte 		}
4549fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
4550fcf3ce44SJohn Forte 			goto out;
4551fcf3ce44SJohn Forte 		}
4552fcf3ce44SJohn Forte 		for (j = 0; j < viewEntryList->cnt; j++) {
4553fcf3ce44SJohn Forte 			ret = addViewEntryIoctl(fd, &guidList->guid[i],
4554fcf3ce44SJohn Forte 			    &viewEntryList->ve[j]);
4555fcf3ce44SJohn Forte 			if (ret != STMF_STATUS_SUCCESS) {
4556fcf3ce44SJohn Forte 				goto out;
4557fcf3ce44SJohn Forte 			}
4558fcf3ce44SJohn Forte 		}
4559fcf3ce44SJohn Forte 	}
4560fcf3ce44SJohn Forte 
4561fcf3ce44SJohn Forte 	/* get the list of providers that have data */
4562fcf3ce44SJohn Forte 	ret = psGetProviderDataList(&providerList);
4563fcf3ce44SJohn Forte 	switch (ret) {
4564fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
4565fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
4566fcf3ce44SJohn Forte 			break;
4567fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
4568fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
4569fcf3ce44SJohn Forte 			break;
4570fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
4571fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
4572fcf3ce44SJohn Forte 			break;
4573fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4574fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
4575fcf3ce44SJohn Forte 			break;
4576fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
4577fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
4578fcf3ce44SJohn Forte 			break;
4579fcf3ce44SJohn Forte 		default:
4580fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
4581fcf3ce44SJohn Forte 			break;
4582fcf3ce44SJohn Forte 	}
4583fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4584fcf3ce44SJohn Forte 		goto out;
4585fcf3ce44SJohn Forte 	}
4586fcf3ce44SJohn Forte 
4587fcf3ce44SJohn Forte 	for (i = 0; i < providerList->cnt; i++) {
4588fcf3ce44SJohn Forte 		providerType = providerList->provider[i].providerType;
4589fcf3ce44SJohn Forte 		ret = psGetProviderData(providerList->provider[i].name,
4590fcf3ce44SJohn Forte 		    &nvl, providerType, NULL);
4591fcf3ce44SJohn Forte 		switch (ret) {
4592fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
4593fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
4594fcf3ce44SJohn Forte 				break;
4595fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
4596fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4597fcf3ce44SJohn Forte 				break;
4598fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
4599fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4600fcf3ce44SJohn Forte 				break;
4601fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4602fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
4603fcf3ce44SJohn Forte 				break;
4604fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
4605fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
4606fcf3ce44SJohn Forte 				break;
4607fcf3ce44SJohn Forte 			default:
4608fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4609fcf3ce44SJohn Forte 				break;
4610fcf3ce44SJohn Forte 		}
4611fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
4612fcf3ce44SJohn Forte 			goto out;
4613fcf3ce44SJohn Forte 		}
4614fcf3ce44SJohn Forte 
4615fcf3ce44SJohn Forte 		/* call setProviderData */
4616fcf3ce44SJohn Forte 		ret = setProviderData(fd, providerList->provider[i].name, nvl,
46178fe96085Stim szeto 		    providerType, NULL);
4618fcf3ce44SJohn Forte 		switch (ret) {
4619fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
4620fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
4621fcf3ce44SJohn Forte 				break;
4622fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
4623fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4624fcf3ce44SJohn Forte 				break;
4625fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
4626fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4627fcf3ce44SJohn Forte 				break;
4628fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4629fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
4630fcf3ce44SJohn Forte 				break;
4631fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
4632fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
4633fcf3ce44SJohn Forte 				break;
4634fcf3ce44SJohn Forte 			default:
4635fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4636fcf3ce44SJohn Forte 				break;
4637fcf3ce44SJohn Forte 		}
4638fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
4639fcf3ce44SJohn Forte 			goto out;
4640fcf3ce44SJohn Forte 		}
4641fcf3ce44SJohn Forte 
4642fcf3ce44SJohn Forte 		nvlist_free(nvl);
4643fcf3ce44SJohn Forte 		nvl = NULL;
4644fcf3ce44SJohn Forte 	}
4645fcf3ce44SJohn Forte out:
4646fcf3ce44SJohn Forte 	if (groupList != NULL) {
4647fcf3ce44SJohn Forte 		free(groupList);
4648fcf3ce44SJohn Forte 	}
4649fcf3ce44SJohn Forte 	if (guidList != NULL) {
4650fcf3ce44SJohn Forte 		free(guidList);
4651fcf3ce44SJohn Forte 	}
4652fcf3ce44SJohn Forte 	if (viewEntryList != NULL) {
4653fcf3ce44SJohn Forte 		free(viewEntryList);
4654fcf3ce44SJohn Forte 	}
4655fcf3ce44SJohn Forte 	if (nvl != NULL) {
4656fcf3ce44SJohn Forte 		nvlist_free(nvl);
4657fcf3ce44SJohn Forte 	}
4658fcf3ce44SJohn Forte 	return (ret);
4659fcf3ce44SJohn Forte }
4660fcf3ce44SJohn Forte 
4661fcf3ce44SJohn Forte /*
4662fcf3ce44SJohn Forte  * stmfLoadConfig
4663fcf3ce44SJohn Forte  *
4664fcf3ce44SJohn Forte  * Purpose - load the configuration data from smf into stmf
4665fcf3ce44SJohn Forte  *
4666fcf3ce44SJohn Forte  */
4667fcf3ce44SJohn Forte int
4668fcf3ce44SJohn Forte stmfLoadConfig(void)
4669fcf3ce44SJohn Forte {
46708fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
4671fcf3ce44SJohn Forte 	int fd;
4672fcf3ce44SJohn Forte 	stmf_state_desc_t stmfStateSet;
4673fcf3ce44SJohn Forte 	stmfState state;
4674fcf3ce44SJohn Forte 
46758fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
46768fe96085Stim szeto 		stmfStateSet.state = STMF_STATE_OFFLINE;
46778fe96085Stim szeto 		stmfStateSet.config_state = STMF_CONFIG_INIT;
46788fe96085Stim szeto 		if ((ret = openStmf(OPEN_EXCL_STMF, &fd))
46798fe96085Stim szeto 		    != STMF_STATUS_SUCCESS) {
46808fe96085Stim szeto 			return (ret);
46818fe96085Stim szeto 		}
46828fe96085Stim szeto 		ret = setStmfState(fd, &stmfStateSet, STMF_SERVICE_TYPE);
46838fe96085Stim szeto 		if (ret != STMF_STATUS_SUCCESS) {
46848fe96085Stim szeto 			goto done;
46858fe96085Stim szeto 		}
46868fe96085Stim szeto 		stmfStateSet.config_state = STMF_CONFIG_INIT_DONE;
46878fe96085Stim szeto 		goto done;
46888fe96085Stim szeto 	}
4689fcf3ce44SJohn Forte 
4690fcf3ce44SJohn Forte 	/* Check to ensure service exists */
4691fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
4692fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
4693fcf3ce44SJohn Forte 	}
4694fcf3ce44SJohn Forte 
4695fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
4696fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
4697fcf3ce44SJohn Forte 		if (state.operationalState != STMF_SERVICE_STATE_OFFLINE) {
4698fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_ONLINE);
4699fcf3ce44SJohn Forte 		}
4700fcf3ce44SJohn Forte 	} else {
4701fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
4702fcf3ce44SJohn Forte 	}
4703fcf3ce44SJohn Forte 
4704fcf3ce44SJohn Forte 
4705fcf3ce44SJohn Forte 	stmfStateSet.state = STMF_STATE_OFFLINE;
4706fcf3ce44SJohn Forte 	stmfStateSet.config_state = STMF_CONFIG_INIT;
4707fcf3ce44SJohn Forte 
4708fcf3ce44SJohn Forte 	/*
4709fcf3ce44SJohn Forte 	 * Open control node for stmf
4710fcf3ce44SJohn Forte 	 */
4711fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
4712fcf3ce44SJohn Forte 		return (ret);
4713fcf3ce44SJohn Forte 
4714fcf3ce44SJohn Forte 	ret = setStmfState(fd, &stmfStateSet, STMF_SERVICE_TYPE);
4715fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4716fcf3ce44SJohn Forte 		goto done;
4717fcf3ce44SJohn Forte 	}
4718fcf3ce44SJohn Forte 
4719fcf3ce44SJohn Forte 	/* Load the persistent configuration data */
4720fcf3ce44SJohn Forte 	ret = loadStore(fd);
4721fcf3ce44SJohn Forte 	if (ret != 0) {
4722fcf3ce44SJohn Forte 		goto done;
4723fcf3ce44SJohn Forte 	}
4724fcf3ce44SJohn Forte 
4725fcf3ce44SJohn Forte 	stmfStateSet.state = STMF_STATE_OFFLINE;
4726fcf3ce44SJohn Forte 	stmfStateSet.config_state = STMF_CONFIG_INIT_DONE;
4727fcf3ce44SJohn Forte 
4728fcf3ce44SJohn Forte done:
4729fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
4730fcf3ce44SJohn Forte 		ret = setStmfState(fd, &stmfStateSet, STMF_SERVICE_TYPE);
4731fcf3ce44SJohn Forte 	}
4732fcf3ce44SJohn Forte 	(void) close(fd);
4733fcf3ce44SJohn Forte 	return (ret);
4734fcf3ce44SJohn Forte }
4735fcf3ce44SJohn Forte 
47368fe96085Stim szeto 
4737fcf3ce44SJohn Forte /*
4738fcf3ce44SJohn Forte  * getStmfState
4739fcf3ce44SJohn Forte  *
4740fcf3ce44SJohn Forte  * stmfState - pointer to stmf_state_desc_t structure. Will contain the state
4741fcf3ce44SJohn Forte  *             information of the stmf service on success.
4742fcf3ce44SJohn Forte  */
4743fcf3ce44SJohn Forte static int
4744fcf3ce44SJohn Forte getStmfState(stmf_state_desc_t *stmfState)
4745fcf3ce44SJohn Forte {
4746fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4747fcf3ce44SJohn Forte 	int fd;
4748fcf3ce44SJohn Forte 	int ioctlRet;
4749fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4750fcf3ce44SJohn Forte 
4751fcf3ce44SJohn Forte 	/*
4752fcf3ce44SJohn Forte 	 * Open control node for stmf
4753fcf3ce44SJohn Forte 	 */
4754fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
4755fcf3ce44SJohn Forte 		return (ret);
4756fcf3ce44SJohn Forte 
4757fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4758fcf3ce44SJohn Forte 	/*
4759fcf3ce44SJohn Forte 	 * Issue ioctl to get the stmf state
4760fcf3ce44SJohn Forte 	 */
4761fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4762fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_state_desc_t);
4763fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)stmfState;
4764fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (stmf_state_desc_t);
4765fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)stmfState;
4766fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_GET_STMF_STATE, &stmfIoctl);
4767fcf3ce44SJohn Forte 
4768fcf3ce44SJohn Forte 	(void) close(fd);
4769fcf3ce44SJohn Forte 
4770fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4771fcf3ce44SJohn Forte 		switch (errno) {
4772fcf3ce44SJohn Forte 			case EBUSY:
4773fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4774fcf3ce44SJohn Forte 				break;
4775fcf3ce44SJohn Forte 			case EPERM:
4776fcf3ce44SJohn Forte 			case EACCES:
4777fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4778fcf3ce44SJohn Forte 				break;
4779fcf3ce44SJohn Forte 			default:
4780fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4781fcf3ce44SJohn Forte 				    "getStmfState:ioctl errno(%d)", errno);
4782fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4783fcf3ce44SJohn Forte 				break;
4784fcf3ce44SJohn Forte 		}
4785fcf3ce44SJohn Forte 	}
4786fcf3ce44SJohn Forte 	return (ret);
4787fcf3ce44SJohn Forte }
4788fcf3ce44SJohn Forte 
4789fcf3ce44SJohn Forte 
4790fcf3ce44SJohn Forte /*
4791fcf3ce44SJohn Forte  * setStmfState
4792fcf3ce44SJohn Forte  *
4793fcf3ce44SJohn Forte  * stmfState - pointer to caller set state structure
4794fcf3ce44SJohn Forte  * objectType - one of:
4795fcf3ce44SJohn Forte  *		LOGICAL_UNIT_TYPE
4796fcf3ce44SJohn Forte  *		TARGET_TYPE
4797fcf3ce44SJohn Forte  *		STMF_SERVICE_TYPE
4798fcf3ce44SJohn Forte  */
4799fcf3ce44SJohn Forte static int
4800fcf3ce44SJohn Forte setStmfState(int fd, stmf_state_desc_t *stmfState, int objectType)
4801fcf3ce44SJohn Forte {
4802fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4803fcf3ce44SJohn Forte 	int ioctlRet;
4804fcf3ce44SJohn Forte 	int cmd;
4805fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4806fcf3ce44SJohn Forte 
4807fcf3ce44SJohn Forte 	switch (objectType) {
4808fcf3ce44SJohn Forte 		case LOGICAL_UNIT_TYPE:
4809fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_LU_STATE;
4810fcf3ce44SJohn Forte 			break;
4811fcf3ce44SJohn Forte 		case TARGET_TYPE:
4812fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_TARGET_PORT_STATE;
4813fcf3ce44SJohn Forte 			break;
4814fcf3ce44SJohn Forte 		case STMF_SERVICE_TYPE:
4815fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_STMF_STATE;
4816fcf3ce44SJohn Forte 			break;
4817fcf3ce44SJohn Forte 		default:
4818fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
4819fcf3ce44SJohn Forte 			goto done;
4820fcf3ce44SJohn Forte 	}
4821fcf3ce44SJohn Forte 
4822fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4823fcf3ce44SJohn Forte 	/*
4824fcf3ce44SJohn Forte 	 * Issue ioctl to set the stmf state
4825fcf3ce44SJohn Forte 	 */
4826fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4827fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_state_desc_t);
4828fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)stmfState;
4829fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4830fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4831fcf3ce44SJohn Forte 		switch (errno) {
4832fcf3ce44SJohn Forte 			case EBUSY:
4833fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4834fcf3ce44SJohn Forte 				break;
48358fe96085Stim szeto 			case EPERM:
4836fcf3ce44SJohn Forte 			case EACCES:
4837fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4838fcf3ce44SJohn Forte 				break;
4839fcf3ce44SJohn Forte 			case ENOENT:
4840fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4841fcf3ce44SJohn Forte 				break;
4842fcf3ce44SJohn Forte 			default:
4843fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4844fcf3ce44SJohn Forte 				    "setStmfState:ioctl errno(%d)", errno);
4845fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4846fcf3ce44SJohn Forte 				break;
4847fcf3ce44SJohn Forte 		}
4848fcf3ce44SJohn Forte 	}
4849fcf3ce44SJohn Forte done:
4850fcf3ce44SJohn Forte 	return (ret);
4851fcf3ce44SJohn Forte }
4852fcf3ce44SJohn Forte 
4853fcf3ce44SJohn Forte /*
4854fcf3ce44SJohn Forte  * stmfOnline
4855fcf3ce44SJohn Forte  *
4856fcf3ce44SJohn Forte  * Purpose: Online stmf service
4857fcf3ce44SJohn Forte  *
4858fcf3ce44SJohn Forte  */
4859fcf3ce44SJohn Forte int
4860fcf3ce44SJohn Forte stmfOnline(void)
4861fcf3ce44SJohn Forte {
4862fcf3ce44SJohn Forte 	int ret;
4863fcf3ce44SJohn Forte 	int fd;
4864fcf3ce44SJohn Forte 	stmfState state;
4865fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
4866fcf3ce44SJohn Forte 
4867fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
4868fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
4869fcf3ce44SJohn Forte 		if (state.operationalState == STMF_SERVICE_STATE_ONLINE) {
4870fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_ONLINE);
4871fcf3ce44SJohn Forte 		}
4872fcf3ce44SJohn Forte 	} else {
4873fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
4874fcf3ce44SJohn Forte 	}
4875fcf3ce44SJohn Forte 	iState.state = STMF_STATE_ONLINE;
4876fcf3ce44SJohn Forte 	iState.config_state = STMF_CONFIG_NONE;
4877fcf3ce44SJohn Forte 	/*
4878fcf3ce44SJohn Forte 	 * Open control node for stmf
4879fcf3ce44SJohn Forte 	 * to make call to setStmfState()
4880fcf3ce44SJohn Forte 	 */
4881fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
4882fcf3ce44SJohn Forte 		return (ret);
4883fcf3ce44SJohn Forte 	ret = setStmfState(fd, &iState, STMF_SERVICE_TYPE);
4884fcf3ce44SJohn Forte 	(void) close(fd);
4885fcf3ce44SJohn Forte 	return (ret);
4886fcf3ce44SJohn Forte }
4887fcf3ce44SJohn Forte 
4888fcf3ce44SJohn Forte /*
4889fcf3ce44SJohn Forte  * stmfOffline
4890fcf3ce44SJohn Forte  *
4891fcf3ce44SJohn Forte  * Purpose: Offline stmf service
4892fcf3ce44SJohn Forte  *
4893fcf3ce44SJohn Forte  */
4894fcf3ce44SJohn Forte int
4895fcf3ce44SJohn Forte stmfOffline(void)
4896fcf3ce44SJohn Forte {
4897fcf3ce44SJohn Forte 	int ret;
4898fcf3ce44SJohn Forte 	int fd;
4899fcf3ce44SJohn Forte 	stmfState state;
4900fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
4901fcf3ce44SJohn Forte 
4902fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
4903fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
4904fcf3ce44SJohn Forte 		if (state.operationalState == STMF_SERVICE_STATE_OFFLINE) {
4905fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_OFFLINE);
4906fcf3ce44SJohn Forte 		}
4907fcf3ce44SJohn Forte 	} else {
4908fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
4909fcf3ce44SJohn Forte 	}
4910fcf3ce44SJohn Forte 	iState.state = STMF_STATE_OFFLINE;
4911fcf3ce44SJohn Forte 	iState.config_state = STMF_CONFIG_NONE;
4912fcf3ce44SJohn Forte 
4913fcf3ce44SJohn Forte 	/*
4914fcf3ce44SJohn Forte 	 * Open control node for stmf
4915fcf3ce44SJohn Forte 	 * to make call to setStmfState()
4916fcf3ce44SJohn Forte 	 */
4917fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
4918fcf3ce44SJohn Forte 		return (ret);
4919fcf3ce44SJohn Forte 	ret = setStmfState(fd, &iState, STMF_SERVICE_TYPE);
4920fcf3ce44SJohn Forte 	(void) close(fd);
4921fcf3ce44SJohn Forte 	return (ret);
4922fcf3ce44SJohn Forte }
4923fcf3ce44SJohn Forte 
4924fcf3ce44SJohn Forte 
4925fcf3ce44SJohn Forte /*
4926fcf3ce44SJohn Forte  * stmfOfflineTarget
4927fcf3ce44SJohn Forte  *
4928fcf3ce44SJohn Forte  * Purpose: Change state of target to offline
4929fcf3ce44SJohn Forte  *
4930fcf3ce44SJohn Forte  * devid - devid of the target to offline
4931fcf3ce44SJohn Forte  */
4932fcf3ce44SJohn Forte int
4933fcf3ce44SJohn Forte stmfOfflineTarget(stmfDevid *devid)
4934fcf3ce44SJohn Forte {
4935fcf3ce44SJohn Forte 	stmf_state_desc_t targetState;
4936fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4937fcf3ce44SJohn Forte 	int fd;
4938fcf3ce44SJohn Forte 
4939fcf3ce44SJohn Forte 	if (devid == NULL) {
4940fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4941fcf3ce44SJohn Forte 	}
4942fcf3ce44SJohn Forte 	bzero(&targetState, sizeof (targetState));
4943fcf3ce44SJohn Forte 
4944fcf3ce44SJohn Forte 	targetState.state = STMF_STATE_OFFLINE;
4945fcf3ce44SJohn Forte 	targetState.ident[IDENT_LENGTH_BYTE] = devid->identLength;
4946fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetState.ident[IDENT_LENGTH_BYTE + 1],
4947fcf3ce44SJohn Forte 	    devid->identLength);
4948fcf3ce44SJohn Forte 	/*
4949fcf3ce44SJohn Forte 	 * Open control node for stmf
4950fcf3ce44SJohn Forte 	 * to make call to setStmfState()
4951fcf3ce44SJohn Forte 	 */
4952fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
4953fcf3ce44SJohn Forte 		return (ret);
4954fcf3ce44SJohn Forte 	ret = setStmfState(fd, &targetState, TARGET_TYPE);
4955fcf3ce44SJohn Forte 	(void) close(fd);
4956fcf3ce44SJohn Forte 	return (ret);
4957fcf3ce44SJohn Forte }
4958fcf3ce44SJohn Forte 
4959fcf3ce44SJohn Forte /*
4960fcf3ce44SJohn Forte  * stmfOfflineLogicalUnit
4961fcf3ce44SJohn Forte  *
4962fcf3ce44SJohn Forte  * Purpose: Change state of logical unit to offline
4963fcf3ce44SJohn Forte  *
4964fcf3ce44SJohn Forte  * lu - guid of the logical unit to offline
4965fcf3ce44SJohn Forte  */
4966fcf3ce44SJohn Forte int
4967fcf3ce44SJohn Forte stmfOfflineLogicalUnit(stmfGuid *lu)
4968fcf3ce44SJohn Forte {
4969fcf3ce44SJohn Forte 	stmf_state_desc_t luState;
4970fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4971fcf3ce44SJohn Forte 	int fd;
4972fcf3ce44SJohn Forte 
4973fcf3ce44SJohn Forte 	if (lu == NULL) {
4974fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4975fcf3ce44SJohn Forte 	}
4976fcf3ce44SJohn Forte 
4977fcf3ce44SJohn Forte 	bzero(&luState, sizeof (luState));
4978fcf3ce44SJohn Forte 
4979fcf3ce44SJohn Forte 	luState.state = STMF_STATE_OFFLINE;
4980fcf3ce44SJohn Forte 	bcopy(lu, &luState.ident, sizeof (stmfGuid));
4981fcf3ce44SJohn Forte 	/*
4982fcf3ce44SJohn Forte 	 * Open control node for stmf
4983fcf3ce44SJohn Forte 	 * to make call to setStmfState()
4984fcf3ce44SJohn Forte 	 */
4985fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
4986fcf3ce44SJohn Forte 		return (ret);
4987fcf3ce44SJohn Forte 	ret = setStmfState(fd, &luState, LOGICAL_UNIT_TYPE);
4988fcf3ce44SJohn Forte 	(void) close(fd);
4989fcf3ce44SJohn Forte 	return (ret);
4990fcf3ce44SJohn Forte }
4991fcf3ce44SJohn Forte 
4992fcf3ce44SJohn Forte /*
4993fcf3ce44SJohn Forte  * stmfOnlineTarget
4994fcf3ce44SJohn Forte  *
4995fcf3ce44SJohn Forte  * Purpose: Change state of target to online
4996fcf3ce44SJohn Forte  *
4997fcf3ce44SJohn Forte  * devid - devid of the target to online
4998fcf3ce44SJohn Forte  */
4999fcf3ce44SJohn Forte int
5000fcf3ce44SJohn Forte stmfOnlineTarget(stmfDevid *devid)
5001fcf3ce44SJohn Forte {
5002fcf3ce44SJohn Forte 	stmf_state_desc_t targetState;
5003fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5004fcf3ce44SJohn Forte 	int fd;
5005fcf3ce44SJohn Forte 
5006fcf3ce44SJohn Forte 	if (devid == NULL) {
5007fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5008fcf3ce44SJohn Forte 	}
5009fcf3ce44SJohn Forte 	bzero(&targetState, sizeof (targetState));
5010fcf3ce44SJohn Forte 
5011fcf3ce44SJohn Forte 	targetState.state = STMF_STATE_ONLINE;
5012fcf3ce44SJohn Forte 	targetState.ident[IDENT_LENGTH_BYTE] = devid->identLength;
5013fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetState.ident[IDENT_LENGTH_BYTE + 1],
5014fcf3ce44SJohn Forte 	    devid->identLength);
5015fcf3ce44SJohn Forte 	/*
5016fcf3ce44SJohn Forte 	 * Open control node for stmf
5017fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5018fcf3ce44SJohn Forte 	 */
5019fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5020fcf3ce44SJohn Forte 		return (ret);
5021fcf3ce44SJohn Forte 	ret = setStmfState(fd, &targetState, TARGET_TYPE);
5022fcf3ce44SJohn Forte 	(void) close(fd);
5023fcf3ce44SJohn Forte 	return (ret);
5024fcf3ce44SJohn Forte }
5025fcf3ce44SJohn Forte 
5026fcf3ce44SJohn Forte /*
5027fcf3ce44SJohn Forte  * stmfOnlineLogicalUnit
5028fcf3ce44SJohn Forte  *
5029fcf3ce44SJohn Forte  * Purpose: Change state of logical unit to online
5030fcf3ce44SJohn Forte  *
5031fcf3ce44SJohn Forte  * lu - guid of the logical unit to online
5032fcf3ce44SJohn Forte  */
5033fcf3ce44SJohn Forte int
5034fcf3ce44SJohn Forte stmfOnlineLogicalUnit(stmfGuid *lu)
5035fcf3ce44SJohn Forte {
5036fcf3ce44SJohn Forte 	stmf_state_desc_t luState;
5037fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5038fcf3ce44SJohn Forte 	int fd;
5039fcf3ce44SJohn Forte 
5040fcf3ce44SJohn Forte 	if (lu == NULL) {
5041fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5042fcf3ce44SJohn Forte 	}
5043fcf3ce44SJohn Forte 
5044fcf3ce44SJohn Forte 	bzero(&luState, sizeof (luState));
5045fcf3ce44SJohn Forte 
5046fcf3ce44SJohn Forte 	luState.state = STMF_STATE_ONLINE;
5047fcf3ce44SJohn Forte 	bcopy(lu, &luState.ident, sizeof (stmfGuid));
5048fcf3ce44SJohn Forte 	/*
5049fcf3ce44SJohn Forte 	 * Open control node for stmf
5050fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5051fcf3ce44SJohn Forte 	 */
5052fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5053fcf3ce44SJohn Forte 		return (ret);
5054fcf3ce44SJohn Forte 	ret = setStmfState(fd, &luState, LOGICAL_UNIT_TYPE);
5055fcf3ce44SJohn Forte 	(void) close(fd);
5056fcf3ce44SJohn Forte 	return (ret);
5057fcf3ce44SJohn Forte }
5058fcf3ce44SJohn Forte 
5059fcf3ce44SJohn Forte /*
5060fcf3ce44SJohn Forte  * stmfRemoveFromHostGroup
5061fcf3ce44SJohn Forte  *
5062fcf3ce44SJohn Forte  * Purpose: Removes an initiator from an initiator group
5063fcf3ce44SJohn Forte  *
5064fcf3ce44SJohn Forte  * hostGroupName - name of an initiator group
5065fcf3ce44SJohn Forte  * hostName - name of host group member to remove
5066fcf3ce44SJohn Forte  */
5067fcf3ce44SJohn Forte int
5068fcf3ce44SJohn Forte stmfRemoveFromHostGroup(stmfGroupName *hostGroupName, stmfDevid *hostName)
5069fcf3ce44SJohn Forte {
5070fcf3ce44SJohn Forte 	int ret;
5071fcf3ce44SJohn Forte 	int fd;
5072fcf3ce44SJohn Forte 
5073fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
5074fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
5075fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || hostName == NULL) {
5076fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5077fcf3ce44SJohn Forte 	}
5078fcf3ce44SJohn Forte 
5079fcf3ce44SJohn Forte 	/* call init */
5080fcf3ce44SJohn Forte 	ret = initializeConfig();
5081fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5082fcf3ce44SJohn Forte 		return (ret);
5083fcf3ce44SJohn Forte 	}
5084fcf3ce44SJohn Forte 
5085fcf3ce44SJohn Forte 	/*
5086fcf3ce44SJohn Forte 	 * Open control node for stmf
5087fcf3ce44SJohn Forte 	 */
5088fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5089fcf3ce44SJohn Forte 		return (ret);
5090fcf3ce44SJohn Forte 
5091fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_REMOVE_HG_ENTRY,
5092fcf3ce44SJohn Forte 	    hostGroupName, hostName)) != STMF_STATUS_SUCCESS) {
5093fcf3ce44SJohn Forte 		goto done;
5094fcf3ce44SJohn Forte 	}
5095fcf3ce44SJohn Forte 
50968fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
50978fe96085Stim szeto 		goto done;
50988fe96085Stim szeto 	}
50998fe96085Stim szeto 
5100fcf3ce44SJohn Forte 	ret = psRemoveHostGroupMember((char *)hostGroupName,
5101fcf3ce44SJohn Forte 	    (char *)hostName->ident);
5102fcf3ce44SJohn Forte 	switch (ret) {
5103fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5104fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5105fcf3ce44SJohn Forte 			break;
5106fcf3ce44SJohn Forte 		case STMF_PS_ERROR_MEMBER_NOT_FOUND:
5107fcf3ce44SJohn Forte 			ret = STMF_ERROR_MEMBER_NOT_FOUND;
5108fcf3ce44SJohn Forte 			break;
5109fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
5110fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
5111fcf3ce44SJohn Forte 			break;
5112fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5113fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5114fcf3ce44SJohn Forte 			break;
5115fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5116fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5117fcf3ce44SJohn Forte 			break;
5118fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5119fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5120fcf3ce44SJohn Forte 			break;
5121fcf3ce44SJohn Forte 		default:
5122fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
5123fcf3ce44SJohn Forte 			    "stmfRemoveFromHostGroup"
5124fcf3ce44SJohn Forte 			    "psRemoveHostGroupMember:error(%d)", ret);
5125fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5126fcf3ce44SJohn Forte 			break;
5127fcf3ce44SJohn Forte 	}
5128fcf3ce44SJohn Forte 
5129fcf3ce44SJohn Forte done:
5130fcf3ce44SJohn Forte 	(void) close(fd);
5131fcf3ce44SJohn Forte 	return (ret);
5132fcf3ce44SJohn Forte }
5133fcf3ce44SJohn Forte 
5134fcf3ce44SJohn Forte /*
5135fcf3ce44SJohn Forte  * stmfRemoveFromTargetGroup
5136fcf3ce44SJohn Forte  *
5137fcf3ce44SJohn Forte  * Purpose: Removes a local port from a local port group
5138fcf3ce44SJohn Forte  *
5139fcf3ce44SJohn Forte  * targetGroupName - name of a target group
5140fcf3ce44SJohn Forte  * targetName - name of target to remove
5141fcf3ce44SJohn Forte  */
5142fcf3ce44SJohn Forte int
5143fcf3ce44SJohn Forte stmfRemoveFromTargetGroup(stmfGroupName *targetGroupName, stmfDevid *targetName)
5144fcf3ce44SJohn Forte {
5145fcf3ce44SJohn Forte 	int ret;
5146fcf3ce44SJohn Forte 	int fd;
5147fcf3ce44SJohn Forte 
5148fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
5149fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
5150fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || targetName == NULL) {
5151fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5152fcf3ce44SJohn Forte 	}
5153fcf3ce44SJohn Forte 
5154fcf3ce44SJohn Forte 	/* call init */
5155fcf3ce44SJohn Forte 	ret = initializeConfig();
5156fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5157fcf3ce44SJohn Forte 		return (ret);
5158fcf3ce44SJohn Forte 	}
5159fcf3ce44SJohn Forte 
5160fcf3ce44SJohn Forte 	/*
5161fcf3ce44SJohn Forte 	 * Open control node for stmf
5162fcf3ce44SJohn Forte 	 */
5163fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5164fcf3ce44SJohn Forte 		return (ret);
5165fcf3ce44SJohn Forte 
5166fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_REMOVE_TG_ENTRY,
5167fcf3ce44SJohn Forte 	    targetGroupName, targetName)) != STMF_STATUS_SUCCESS) {
5168fcf3ce44SJohn Forte 		goto done;
5169fcf3ce44SJohn Forte 	}
5170fcf3ce44SJohn Forte 
51718fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
51728fe96085Stim szeto 		goto done;
51738fe96085Stim szeto 	}
51748fe96085Stim szeto 
5175fcf3ce44SJohn Forte 	ret = psRemoveTargetGroupMember((char *)targetGroupName,
5176fcf3ce44SJohn Forte 	    (char *)targetName->ident);
5177fcf3ce44SJohn Forte 	switch (ret) {
5178fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5179fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5180fcf3ce44SJohn Forte 			break;
5181fcf3ce44SJohn Forte 		case STMF_PS_ERROR_MEMBER_NOT_FOUND:
5182fcf3ce44SJohn Forte 			ret = STMF_ERROR_MEMBER_NOT_FOUND;
5183fcf3ce44SJohn Forte 			break;
5184fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
5185fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
5186fcf3ce44SJohn Forte 			break;
5187fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5188fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5189fcf3ce44SJohn Forte 			break;
5190fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5191fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5192fcf3ce44SJohn Forte 			break;
5193fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5194fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5195fcf3ce44SJohn Forte 			break;
5196fcf3ce44SJohn Forte 		default:
5197fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
5198fcf3ce44SJohn Forte 			    "stmfRemoveFromTargetGroup"
5199fcf3ce44SJohn Forte 			    "psRemoveTargetGroupMember:error(%d)", ret);
5200fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5201fcf3ce44SJohn Forte 			break;
5202fcf3ce44SJohn Forte 	}
5203fcf3ce44SJohn Forte 
5204fcf3ce44SJohn Forte done:
5205fcf3ce44SJohn Forte 	(void) close(fd);
5206fcf3ce44SJohn Forte 	return (ret);
5207fcf3ce44SJohn Forte }
5208fcf3ce44SJohn Forte 
5209fcf3ce44SJohn Forte /*
5210fcf3ce44SJohn Forte  * stmfRemoveViewEntry
5211fcf3ce44SJohn Forte  *
5212fcf3ce44SJohn Forte  * Purpose: Removes a view entry from a logical unit
5213fcf3ce44SJohn Forte  *
5214fcf3ce44SJohn Forte  * lu - guid of lu for which view entry is being removed
5215fcf3ce44SJohn Forte  * viewEntryIndex - index of view entry to remove
5216fcf3ce44SJohn Forte  *
5217fcf3ce44SJohn Forte  */
5218fcf3ce44SJohn Forte int
5219fcf3ce44SJohn Forte stmfRemoveViewEntry(stmfGuid *lu, uint32_t viewEntryIndex)
5220fcf3ce44SJohn Forte {
5221fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5222fcf3ce44SJohn Forte 	int fd;
5223fcf3ce44SJohn Forte 	int ioctlRet;
5224fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
5225fcf3ce44SJohn Forte 	stmf_view_op_entry_t ioctlViewEntry;
5226fcf3ce44SJohn Forte 
5227fcf3ce44SJohn Forte 	if (lu == NULL) {
5228fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5229fcf3ce44SJohn Forte 	}
5230fcf3ce44SJohn Forte 
5231fcf3ce44SJohn Forte 	/* call init */
5232fcf3ce44SJohn Forte 	ret = initializeConfig();
5233fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5234fcf3ce44SJohn Forte 		return (ret);
5235fcf3ce44SJohn Forte 	}
5236fcf3ce44SJohn Forte 
5237fcf3ce44SJohn Forte 	/*
5238fcf3ce44SJohn Forte 	 * Open control node for stmf
5239fcf3ce44SJohn Forte 	 */
5240fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5241fcf3ce44SJohn Forte 		return (ret);
5242fcf3ce44SJohn Forte 
5243fcf3ce44SJohn Forte 	bzero(&ioctlViewEntry, sizeof (ioctlViewEntry));
5244fcf3ce44SJohn Forte 	ioctlViewEntry.ve_ndx_valid = B_TRUE;
5245fcf3ce44SJohn Forte 	ioctlViewEntry.ve_ndx = viewEntryIndex;
5246fcf3ce44SJohn Forte 	bcopy(lu, &ioctlViewEntry.ve_guid, sizeof (stmfGuid));
5247fcf3ce44SJohn Forte 
5248fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
5249fcf3ce44SJohn Forte 	/*
5250fcf3ce44SJohn Forte 	 * Issue ioctl to add to the view entry
5251fcf3ce44SJohn Forte 	 */
5252fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
5253fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ioctlViewEntry);
5254fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ioctlViewEntry;
5255fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_REMOVE_VIEW_ENTRY, &stmfIoctl);
5256fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
5257fcf3ce44SJohn Forte 		switch (errno) {
5258fcf3ce44SJohn Forte 			case EBUSY:
5259fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
5260fcf3ce44SJohn Forte 				break;
52618fe96085Stim szeto 			case EPERM:
52628fe96085Stim szeto 				ret = STMF_ERROR_PERM;
52638fe96085Stim szeto 				break;
5264fcf3ce44SJohn Forte 			case EACCES:
5265fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
5266fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
5267fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
5268fcf3ce44SJohn Forte 						break;
5269fcf3ce44SJohn Forte 					default:
5270fcf3ce44SJohn Forte 						ret = STMF_ERROR_PERM;
5271fcf3ce44SJohn Forte 						break;
5272fcf3ce44SJohn Forte 				}
5273fcf3ce44SJohn Forte 				break;
5274fcf3ce44SJohn Forte 			case ENODEV:
5275fcf3ce44SJohn Forte 			case ENOENT:
5276fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
5277fcf3ce44SJohn Forte 				break;
5278fcf3ce44SJohn Forte 			default:
5279fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
5280fcf3ce44SJohn Forte 				    "stmfRemoveViewEntry:ioctl errno(%d)",
5281fcf3ce44SJohn Forte 				    errno);
5282fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
5283fcf3ce44SJohn Forte 				break;
5284fcf3ce44SJohn Forte 		}
5285fcf3ce44SJohn Forte 		goto done;
5286fcf3ce44SJohn Forte 	}
5287fcf3ce44SJohn Forte 
52888fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
52898fe96085Stim szeto 		goto done;
52908fe96085Stim szeto 	}
52918fe96085Stim szeto 
5292fcf3ce44SJohn Forte 	ret = psRemoveViewEntry(lu, viewEntryIndex);
5293fcf3ce44SJohn Forte 	switch (ret) {
5294fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5295fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5296fcf3ce44SJohn Forte 			break;
5297fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
5298fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
5299fcf3ce44SJohn Forte 			break;
5300fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5301fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5302fcf3ce44SJohn Forte 			break;
5303fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5304fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5305fcf3ce44SJohn Forte 			break;
5306fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5307fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5308fcf3ce44SJohn Forte 			break;
5309fcf3ce44SJohn Forte 		default:
5310fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
5311fcf3ce44SJohn Forte 			    "stmfRemoveViewEntry" "psRemoveViewEntry:error(%d)",
5312fcf3ce44SJohn Forte 			    ret);
5313fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5314fcf3ce44SJohn Forte 			break;
5315fcf3ce44SJohn Forte 	}
5316fcf3ce44SJohn Forte 
5317fcf3ce44SJohn Forte done:
5318fcf3ce44SJohn Forte 	(void) close(fd);
5319fcf3ce44SJohn Forte 	return (ret);
5320fcf3ce44SJohn Forte }
5321fcf3ce44SJohn Forte 
5322fcf3ce44SJohn Forte /*
5323fcf3ce44SJohn Forte  * stmfSetProviderData
5324fcf3ce44SJohn Forte  *
5325fcf3ce44SJohn Forte  * Purpose: set the provider data
5326fcf3ce44SJohn Forte  *
5327fcf3ce44SJohn Forte  * providerName - unique name of provider
5328fcf3ce44SJohn Forte  * nvl - nvlist to set
5329fcf3ce44SJohn Forte  * providerType - type of provider for which to set data
5330fcf3ce44SJohn Forte  *		STMF_LU_PROVIDER_TYPE
5331fcf3ce44SJohn Forte  *		STMF_PORT_PROVIDER_TYPE
5332fcf3ce44SJohn Forte  */
5333fcf3ce44SJohn Forte int
5334fcf3ce44SJohn Forte stmfSetProviderData(char *providerName, nvlist_t *nvl, int providerType)
5335fcf3ce44SJohn Forte {
5336fcf3ce44SJohn Forte 	return (stmfSetProviderDataProt(providerName, nvl, providerType,
5337fcf3ce44SJohn Forte 	    NULL));
5338fcf3ce44SJohn Forte }
5339fcf3ce44SJohn Forte 
5340fcf3ce44SJohn Forte /*
5341fcf3ce44SJohn Forte  * stmfSetProviderDataProt
5342fcf3ce44SJohn Forte  *
5343fcf3ce44SJohn Forte  * Purpose: set the provider data
5344fcf3ce44SJohn Forte  *
5345fcf3ce44SJohn Forte  * providerName - unique name of provider
5346fcf3ce44SJohn Forte  * nvl - nvlist to set
5347fcf3ce44SJohn Forte  * providerType - type of provider for which to set data
5348fcf3ce44SJohn Forte  *		STMF_LU_PROVIDER_TYPE
5349fcf3ce44SJohn Forte  *		STMF_PORT_PROVIDER_TYPE
5350fcf3ce44SJohn Forte  * setToken - Stale data token returned in the stmfGetProviderDataProt()
5351fcf3ce44SJohn Forte  *	      call or NULL.
5352fcf3ce44SJohn Forte  */
5353fcf3ce44SJohn Forte int
5354fcf3ce44SJohn Forte stmfSetProviderDataProt(char *providerName, nvlist_t *nvl, int providerType,
5355fcf3ce44SJohn Forte     uint64_t *setToken)
5356fcf3ce44SJohn Forte {
5357fcf3ce44SJohn Forte 	int ret;
5358fcf3ce44SJohn Forte 	int fd;
5359fcf3ce44SJohn Forte 
5360fcf3ce44SJohn Forte 	if (providerName == NULL || nvl == NULL) {
5361fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5362fcf3ce44SJohn Forte 	}
5363fcf3ce44SJohn Forte 
5364fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
5365fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
5366fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5367fcf3ce44SJohn Forte 	}
5368fcf3ce44SJohn Forte 
5369fcf3ce44SJohn Forte 	/* call init */
5370fcf3ce44SJohn Forte 	ret = initializeConfig();
5371fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5372fcf3ce44SJohn Forte 		return (ret);
5373fcf3ce44SJohn Forte 	}
5374fcf3ce44SJohn Forte 
5375fcf3ce44SJohn Forte 	/*
5376fcf3ce44SJohn Forte 	 * Open control node for stmf
5377fcf3ce44SJohn Forte 	 */
5378fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5379fcf3ce44SJohn Forte 		return (ret);
5380fcf3ce44SJohn Forte 
53818fe96085Stim szeto 	ret = setProviderData(fd, providerName, nvl, providerType, setToken);
5382fcf3ce44SJohn Forte 
5383fcf3ce44SJohn Forte 	(void) close(fd);
5384fcf3ce44SJohn Forte 
5385fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5386fcf3ce44SJohn Forte 		goto done;
5387fcf3ce44SJohn Forte 	}
5388fcf3ce44SJohn Forte 
53898fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
53908fe96085Stim szeto 		goto done;
53918fe96085Stim szeto 	}
53928fe96085Stim szeto 
5393fcf3ce44SJohn Forte 	/* setting driver provider data successful. Now persist it */
53948fe96085Stim szeto 	ret = psSetProviderData(providerName, nvl, providerType, NULL);
5395fcf3ce44SJohn Forte 	switch (ret) {
5396fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5397fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5398fcf3ce44SJohn Forte 			break;
5399fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
5400fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
5401fcf3ce44SJohn Forte 			break;
5402fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5403fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5404fcf3ce44SJohn Forte 			break;
5405fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5406fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5407fcf3ce44SJohn Forte 			break;
5408fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5409fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5410fcf3ce44SJohn Forte 			break;
5411fcf3ce44SJohn Forte 		case STMF_PS_ERROR_PROV_DATA_STALE:
5412fcf3ce44SJohn Forte 			ret = STMF_ERROR_PROV_DATA_STALE;
5413fcf3ce44SJohn Forte 			break;
5414fcf3ce44SJohn Forte 		default:
5415fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
5416fcf3ce44SJohn Forte 			    "stmfSetProviderData"
5417fcf3ce44SJohn Forte 			    "psSetProviderData:error(%d)", ret);
5418fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5419fcf3ce44SJohn Forte 			break;
5420fcf3ce44SJohn Forte 	}
5421fcf3ce44SJohn Forte 
5422fcf3ce44SJohn Forte done:
5423fcf3ce44SJohn Forte 	return (ret);
5424fcf3ce44SJohn Forte }
5425fcf3ce44SJohn Forte 
5426fcf3ce44SJohn Forte /*
54278fe96085Stim szeto  * getProviderData
54288fe96085Stim szeto  *
54298fe96085Stim szeto  * Purpose: set the provider data from stmf
54308fe96085Stim szeto  *
54318fe96085Stim szeto  * providerName - unique name of provider
54328fe96085Stim szeto  * nvl - nvlist to load/retrieve
54338fe96085Stim szeto  * providerType - logical unit or port provider
54348fe96085Stim szeto  * setToken - returned stale data token
54358fe96085Stim szeto  */
54368fe96085Stim szeto int
54378fe96085Stim szeto getProviderData(char *providerName, nvlist_t **nvl, int providerType,
54388fe96085Stim szeto     uint64_t *setToken)
54398fe96085Stim szeto {
54408fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
54418fe96085Stim szeto 	int fd;
54428fe96085Stim szeto 	int ioctlRet;
54438fe96085Stim szeto 	size_t nvlistSize = ALLOC_PP_DATA_SIZE;
54448fe96085Stim szeto 	int retryCnt = 0;
54458fe96085Stim szeto 	int retryCntMax = MAX_PROVIDER_RETRY;
54468fe96085Stim szeto 	stmf_ppioctl_data_t ppi = {0}, *ppi_out = NULL;
54478fe96085Stim szeto 	boolean_t retry = B_TRUE;
54488fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
54498fe96085Stim szeto 
54508fe96085Stim szeto 	if (providerName == NULL) {
54518fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
54528fe96085Stim szeto 	}
54538fe96085Stim szeto 
54548fe96085Stim szeto 	/*
54558fe96085Stim szeto 	 * Open control node for stmf
54568fe96085Stim szeto 	 */
54578fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
54588fe96085Stim szeto 		return (ret);
54598fe96085Stim szeto 
54608fe96085Stim szeto 	/* set provider name and provider type */
54618fe96085Stim szeto 	if (strlcpy(ppi.ppi_name, providerName,
54628fe96085Stim szeto 	    sizeof (ppi.ppi_name)) >=
54638fe96085Stim szeto 	    sizeof (ppi.ppi_name)) {
54648fe96085Stim szeto 		ret = STMF_ERROR_INVALID_ARG;
54658fe96085Stim szeto 		goto done;
54668fe96085Stim szeto 	}
54678fe96085Stim szeto 	switch (providerType) {
54688fe96085Stim szeto 		case STMF_LU_PROVIDER_TYPE:
54698fe96085Stim szeto 			ppi.ppi_lu_provider = 1;
54708fe96085Stim szeto 			break;
54718fe96085Stim szeto 		case STMF_PORT_PROVIDER_TYPE:
54728fe96085Stim szeto 			ppi.ppi_port_provider = 1;
54738fe96085Stim szeto 			break;
54748fe96085Stim szeto 		default:
54758fe96085Stim szeto 			ret = STMF_ERROR_INVALID_ARG;
54768fe96085Stim szeto 			goto done;
54778fe96085Stim szeto 	}
54788fe96085Stim szeto 
54798fe96085Stim szeto 	do {
54808fe96085Stim szeto 		/* allocate memory for ioctl */
54818fe96085Stim szeto 		ppi_out = (stmf_ppioctl_data_t *)calloc(1, nvlistSize +
54828fe96085Stim szeto 		    sizeof (stmf_ppioctl_data_t));
54838fe96085Stim szeto 		if (ppi_out == NULL) {
54848fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
54858fe96085Stim szeto 			goto done;
54868fe96085Stim szeto 
54878fe96085Stim szeto 		}
54888fe96085Stim szeto 
54898fe96085Stim szeto 		/* set the size of the ioctl data to allocated buffer */
54908fe96085Stim szeto 		ppi.ppi_data_size = nvlistSize;
54918fe96085Stim szeto 
54928fe96085Stim szeto 		bzero(&stmfIoctl, sizeof (stmfIoctl));
54938fe96085Stim szeto 
54948fe96085Stim szeto 		stmfIoctl.stmf_version = STMF_VERSION_1;
54958fe96085Stim szeto 		stmfIoctl.stmf_ibuf_size = sizeof (stmf_ppioctl_data_t);
54968fe96085Stim szeto 		stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ppi;
54978fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = sizeof (stmf_ppioctl_data_t) +
54988fe96085Stim szeto 		    nvlistSize;
54998fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)ppi_out;
55008fe96085Stim szeto 		ioctlRet = ioctl(fd, STMF_IOCTL_GET_PP_DATA, &stmfIoctl);
55018fe96085Stim szeto 		if (ioctlRet != 0) {
55028fe96085Stim szeto 			switch (errno) {
55038fe96085Stim szeto 				case EBUSY:
55048fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
55058fe96085Stim szeto 					break;
55068fe96085Stim szeto 				case EPERM:
55078fe96085Stim szeto 				case EACCES:
55088fe96085Stim szeto 					ret = STMF_ERROR_PERM;
55098fe96085Stim szeto 					break;
55108fe96085Stim szeto 				case EINVAL:
55118fe96085Stim szeto 					if (stmfIoctl.stmf_error ==
55128fe96085Stim szeto 					    STMF_IOCERR_INSUFFICIENT_BUF) {
55138fe96085Stim szeto 						nvlistSize =
55148fe96085Stim szeto 						    ppi_out->ppi_data_size;
55158fe96085Stim szeto 						free(ppi_out);
55168fe96085Stim szeto 						ppi_out = NULL;
55178fe96085Stim szeto 						if (retryCnt++ > retryCntMax) {
55188fe96085Stim szeto 							retry = B_FALSE;
55198fe96085Stim szeto 							ret = STMF_ERROR_BUSY;
55208fe96085Stim szeto 						} else {
55218fe96085Stim szeto 							ret =
55228fe96085Stim szeto 							    STMF_STATUS_SUCCESS;
55238fe96085Stim szeto 						}
55248fe96085Stim szeto 					} else {
55258fe96085Stim szeto 						syslog(LOG_DEBUG,
55268fe96085Stim szeto 						    "getProviderData:ioctl"
55278fe96085Stim szeto 						    "unable to retrieve "
55288fe96085Stim szeto 						    "nvlist");
55298fe96085Stim szeto 						ret = STMF_STATUS_ERROR;
55308fe96085Stim szeto 					}
55318fe96085Stim szeto 					break;
55328fe96085Stim szeto 				case ENOENT:
55338fe96085Stim szeto 					ret = STMF_ERROR_NOT_FOUND;
55348fe96085Stim szeto 					break;
55358fe96085Stim szeto 				default:
55368fe96085Stim szeto 					syslog(LOG_DEBUG,
55378fe96085Stim szeto 					    "getProviderData:ioctl errno(%d)",
55388fe96085Stim szeto 					    errno);
55398fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
55408fe96085Stim szeto 					break;
55418fe96085Stim szeto 			}
55428fe96085Stim szeto 			if (ret != STMF_STATUS_SUCCESS)
55438fe96085Stim szeto 				goto done;
55448fe96085Stim szeto 		}
55458fe96085Stim szeto 	} while (retry && stmfIoctl.stmf_error == STMF_IOCERR_INSUFFICIENT_BUF);
55468fe96085Stim szeto 
55478fe96085Stim szeto 	if ((ret = nvlist_unpack((char *)ppi_out->ppi_data,
55488fe96085Stim szeto 	    ppi_out->ppi_data_size, nvl, 0)) != 0) {
55498fe96085Stim szeto 		ret = STMF_STATUS_ERROR;
55508fe96085Stim szeto 		goto done;
55518fe96085Stim szeto 	}
55528fe96085Stim szeto 
55538fe96085Stim szeto 	/* caller has asked for new token */
55548fe96085Stim szeto 	if (setToken) {
55558fe96085Stim szeto 		*setToken = ppi_out->ppi_token;
55568fe96085Stim szeto 	}
55578fe96085Stim szeto done:
55588fe96085Stim szeto 	free(ppi_out);
55598fe96085Stim szeto 	(void) close(fd);
55608fe96085Stim szeto 	return (ret);
55618fe96085Stim szeto }
55628fe96085Stim szeto 
55638fe96085Stim szeto /*
5564fcf3ce44SJohn Forte  * setProviderData
5565fcf3ce44SJohn Forte  *
55668fe96085Stim szeto  * Purpose: set the provider data in stmf
5567fcf3ce44SJohn Forte  *
5568fcf3ce44SJohn Forte  * providerName - unique name of provider
5569fcf3ce44SJohn Forte  * nvl - nvlist to set
5570fcf3ce44SJohn Forte  * providerType - logical unit or port provider
55718fe96085Stim szeto  * setToken - stale data token to check if not NULL
5572fcf3ce44SJohn Forte  */
5573fcf3ce44SJohn Forte static int
55748fe96085Stim szeto setProviderData(int fd, char *providerName, nvlist_t *nvl, int providerType,
55758fe96085Stim szeto     uint64_t *setToken)
5576fcf3ce44SJohn Forte {
5577fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5578fcf3ce44SJohn Forte 	int ioctlRet;
5579fcf3ce44SJohn Forte 	size_t nvlistEncodedSize;
5580fcf3ce44SJohn Forte 	stmf_ppioctl_data_t *ppi = NULL;
55818fe96085Stim szeto 	uint64_t outToken;
5582fcf3ce44SJohn Forte 	char *allocatedNvBuffer;
5583fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
5584fcf3ce44SJohn Forte 
5585fcf3ce44SJohn Forte 	if (providerName == NULL) {
5586fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5587fcf3ce44SJohn Forte 	}
5588fcf3ce44SJohn Forte 
5589fcf3ce44SJohn Forte 	/* get size of encoded nvlist */
5590fcf3ce44SJohn Forte 	if (nvlist_size(nvl, &nvlistEncodedSize, NV_ENCODE_XDR) != 0) {
5591fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
5592fcf3ce44SJohn Forte 	}
5593fcf3ce44SJohn Forte 
5594fcf3ce44SJohn Forte 	/* allocate memory for ioctl */
5595fcf3ce44SJohn Forte 	ppi = (stmf_ppioctl_data_t *)calloc(1, nvlistEncodedSize +
5596fcf3ce44SJohn Forte 	    sizeof (stmf_ppioctl_data_t));
5597fcf3ce44SJohn Forte 	if (ppi == NULL) {
5598fcf3ce44SJohn Forte 		return (STMF_ERROR_NOMEM);
5599fcf3ce44SJohn Forte 	}
5600fcf3ce44SJohn Forte 
56018fe96085Stim szeto 	if (setToken) {
56028fe96085Stim szeto 		ppi->ppi_token_valid = 1;
56038fe96085Stim szeto 		ppi->ppi_token = *setToken;
56048fe96085Stim szeto 	}
56058fe96085Stim szeto 
5606fcf3ce44SJohn Forte 	allocatedNvBuffer = (char *)&ppi->ppi_data;
5607fcf3ce44SJohn Forte 	if (nvlist_pack(nvl, &allocatedNvBuffer, &nvlistEncodedSize,
5608fcf3ce44SJohn Forte 	    NV_ENCODE_XDR, 0) != 0) {
5609fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
5610fcf3ce44SJohn Forte 	}
5611fcf3ce44SJohn Forte 
5612fcf3ce44SJohn Forte 	/* set provider name and provider type */
5613fcf3ce44SJohn Forte 	(void) strncpy(ppi->ppi_name, providerName, sizeof (ppi->ppi_name));
5614fcf3ce44SJohn Forte 	switch (providerType) {
5615fcf3ce44SJohn Forte 		case STMF_LU_PROVIDER_TYPE:
5616fcf3ce44SJohn Forte 			ppi->ppi_lu_provider = 1;
5617fcf3ce44SJohn Forte 			break;
5618fcf3ce44SJohn Forte 		case STMF_PORT_PROVIDER_TYPE:
5619fcf3ce44SJohn Forte 			ppi->ppi_port_provider = 1;
5620fcf3ce44SJohn Forte 			break;
5621fcf3ce44SJohn Forte 		default:
5622fcf3ce44SJohn Forte 			return (STMF_ERROR_INVALID_ARG);
5623fcf3ce44SJohn Forte 	}
5624fcf3ce44SJohn Forte 
5625fcf3ce44SJohn Forte 	/* set the size of the ioctl data to packed data size */
5626fcf3ce44SJohn Forte 	ppi->ppi_data_size = nvlistEncodedSize;
5627fcf3ce44SJohn Forte 
5628fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
5629fcf3ce44SJohn Forte 
5630fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
5631fcf3ce44SJohn Forte 	/*
5632fcf3ce44SJohn Forte 	 * Subtracting 8 from the size as that is the size of the last member
5633fcf3ce44SJohn Forte 	 * of the structure where the packed data resides
5634fcf3ce44SJohn Forte 	 */
5635fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = nvlistEncodedSize +
5636fcf3ce44SJohn Forte 	    sizeof (stmf_ppioctl_data_t) - 8;
5637fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)ppi;
56388fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = sizeof (uint64_t);
56398fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&outToken;
5640fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_LOAD_PP_DATA, &stmfIoctl);
5641fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
5642fcf3ce44SJohn Forte 		switch (errno) {
5643fcf3ce44SJohn Forte 			case EBUSY:
5644fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
5645fcf3ce44SJohn Forte 				break;
56468fe96085Stim szeto 			case EPERM:
5647fcf3ce44SJohn Forte 			case EACCES:
5648fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
5649fcf3ce44SJohn Forte 				break;
56508fe96085Stim szeto 			case EINVAL:
56518fe96085Stim szeto 				if (stmfIoctl.stmf_error ==
56528fe96085Stim szeto 				    STMF_IOCERR_PPD_UPDATED) {
56538fe96085Stim szeto 					ret = STMF_ERROR_PROV_DATA_STALE;
56548fe96085Stim szeto 				} else {
56558fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
56568fe96085Stim szeto 				}
56578fe96085Stim szeto 				break;
5658fcf3ce44SJohn Forte 			default:
5659fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
5660fcf3ce44SJohn Forte 				    "setProviderData:ioctl errno(%d)", errno);
5661fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
5662fcf3ce44SJohn Forte 				break;
5663fcf3ce44SJohn Forte 		}
5664fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS)
5665fcf3ce44SJohn Forte 			goto done;
5666fcf3ce44SJohn Forte 	}
5667fcf3ce44SJohn Forte 
56688fe96085Stim szeto 	/* caller has asked for new token */
56698fe96085Stim szeto 	if (setToken) {
56708fe96085Stim szeto 		*setToken = outToken;
56718fe96085Stim szeto 	}
5672fcf3ce44SJohn Forte done:
5673fcf3ce44SJohn Forte 	free(ppi);
5674fcf3ce44SJohn Forte 	return (ret);
5675fcf3ce44SJohn Forte }
56768fe96085Stim szeto 
56778fe96085Stim szeto /*
56788fe96085Stim szeto  * set the persistence method in the library only or library and service
56798fe96085Stim szeto  */
56808fe96085Stim szeto int
56818fe96085Stim szeto stmfSetPersistMethod(uint8_t persistType, boolean_t serviceSet)
56828fe96085Stim szeto {
56838fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
56848fe96085Stim szeto 	int oldPersist;
56858fe96085Stim szeto 
56868fe96085Stim szeto 	(void) pthread_mutex_lock(&persistenceTypeLock);
56878fe96085Stim szeto 	oldPersist = iPersistType;
56888fe96085Stim szeto 	if (persistType == STMF_PERSIST_NONE ||
56898fe96085Stim szeto 	    persistType == STMF_PERSIST_SMF) {
56908fe96085Stim szeto 		iLibSetPersist = B_TRUE;
56918fe96085Stim szeto 		iPersistType = persistType;
56928fe96085Stim szeto 	} else {
56938fe96085Stim szeto 		(void) pthread_mutex_unlock(&persistenceTypeLock);
56948fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
56958fe96085Stim szeto 	}
56968fe96085Stim szeto 	/* Is this for this library open or in SMF */
56978fe96085Stim szeto 	if (serviceSet == B_TRUE) {
56988fe96085Stim szeto 		ret = psSetServicePersist(persistType);
56998fe96085Stim szeto 		if (ret != STMF_PS_SUCCESS) {
57008fe96085Stim szeto 			ret = STMF_ERROR_PERSIST_TYPE;
57018fe96085Stim szeto 			/* Set to old value */
57028fe96085Stim szeto 			iPersistType = oldPersist;
57038fe96085Stim szeto 		}
57048fe96085Stim szeto 	}
57058fe96085Stim szeto 	(void) pthread_mutex_unlock(&persistenceTypeLock);
57068fe96085Stim szeto 
57078fe96085Stim szeto 	return (ret);
57088fe96085Stim szeto }
57098fe96085Stim szeto 
57108fe96085Stim szeto /*
57118fe96085Stim szeto  * Only returns internal state for persist. If unset, goes to ps. If that
57128fe96085Stim szeto  * fails, returns default setting
57138fe96085Stim szeto  */
57148fe96085Stim szeto static uint8_t
57158fe96085Stim szeto iGetPersistMethod()
57168fe96085Stim szeto {
57178fe96085Stim szeto 
57188fe96085Stim szeto 	uint8_t persistType = 0;
57198fe96085Stim szeto 
57208fe96085Stim szeto 	(void) pthread_mutex_lock(&persistenceTypeLock);
57218fe96085Stim szeto 	if (iLibSetPersist) {
57228fe96085Stim szeto 		persistType = iPersistType;
57238fe96085Stim szeto 	} else {
57248fe96085Stim szeto 		int ret;
57258fe96085Stim szeto 		ret = psGetServicePersist(&persistType);
57268fe96085Stim szeto 		if (ret != STMF_PS_SUCCESS) {
57278fe96085Stim szeto 			/* set to default */
57288fe96085Stim szeto 			persistType = STMF_DEFAULT_PERSIST;
57298fe96085Stim szeto 		}
57308fe96085Stim szeto 	}
57318fe96085Stim szeto 	(void) pthread_mutex_unlock(&persistenceTypeLock);
57328fe96085Stim szeto 	return (persistType);
57338fe96085Stim szeto }
57348fe96085Stim szeto 
57358fe96085Stim szeto /*
57368fe96085Stim szeto  * Returns either library state or persistent config state depending on
57378fe96085Stim szeto  * serviceState
57388fe96085Stim szeto  */
57398fe96085Stim szeto int
57408fe96085Stim szeto stmfGetPersistMethod(uint8_t *persistType, boolean_t serviceState)
57418fe96085Stim szeto {
57428fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
57438fe96085Stim szeto 
57448fe96085Stim szeto 	if (persistType == NULL) {
57458fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
57468fe96085Stim szeto 	}
57478fe96085Stim szeto 	if (serviceState) {
57488fe96085Stim szeto 		ret = psGetServicePersist(persistType);
57498fe96085Stim szeto 		if (ret != STMF_PS_SUCCESS) {
57508fe96085Stim szeto 			ret = STMF_ERROR_PERSIST_TYPE;
57518fe96085Stim szeto 		}
57528fe96085Stim szeto 	} else {
57538fe96085Stim szeto 		(void) pthread_mutex_lock(&persistenceTypeLock);
57548fe96085Stim szeto 		if (iLibSetPersist) {
57558fe96085Stim szeto 			*persistType = iPersistType;
57568fe96085Stim szeto 		} else {
57578fe96085Stim szeto 			*persistType = STMF_DEFAULT_PERSIST;
57588fe96085Stim szeto 		}
57598fe96085Stim szeto 		(void) pthread_mutex_unlock(&persistenceTypeLock);
57608fe96085Stim szeto 	}
57618fe96085Stim szeto 
57628fe96085Stim szeto 	return (ret);
57638fe96085Stim szeto }
5764