xref: /titanic_51/usr/src/lib/libstmf/common/stmf.c (revision 640428ae0ffb68033a9bf38dcc271e17d13933cb)
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:
3455c8cac22Stim szeto 				switch (stmfIoctl.stmf_error) {
3465c8cac22Stim szeto 					case STMF_IOCERR_TG_NEED_TG_OFFLINE:
3475c8cac22Stim szeto 						ret = STMF_ERROR_TG_ONLINE;
3485c8cac22Stim szeto 						break;
3495c8cac22Stim szeto 					default:
350fcf3ce44SJohn Forte 						ret = STMF_ERROR_BUSY;
351fcf3ce44SJohn Forte 						break;
3525c8cac22Stim szeto 				}
3535c8cac22Stim 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;
10812f624233SNattuvetty Bhavyan 	int luMgmtUrlLen = 0;
10828fe96085Stim szeto 	int sluBufSize = 0;
10838fe96085Stim szeto 	int bufOffset = 0;
10848fe96085Stim szeto 	int fd = 0;
10858fe96085Stim szeto 	int ioctlRet;
10868fe96085Stim szeto 	int savedErrno;
10878fe96085Stim szeto 	stmfGuid guid;
10888fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
10898fe96085Stim szeto 
10908fe96085Stim szeto 	sbd_create_and_reg_lu_t *sbdLu = NULL;
10918fe96085Stim szeto 
10928fe96085Stim szeto 	/*
10938fe96085Stim szeto 	 * Open control node for sbd
10948fe96085Stim szeto 	 */
10958fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
10968fe96085Stim szeto 		return (ret);
10978fe96085Stim szeto 
10988fe96085Stim szeto 	/* data file name must be specified */
10998fe96085Stim szeto 	if (disk->luDataFileNameValid) {
11008fe96085Stim szeto 		dataFileNameLen = strlen(disk->luDataFileName);
11018fe96085Stim szeto 	} else {
11028fe96085Stim szeto 		(void) close(fd);
11038fe96085Stim szeto 		return (STMF_ERROR_MISSING_PROP_VAL);
11048fe96085Stim szeto 	}
11058fe96085Stim szeto 
11068fe96085Stim szeto 	sluBufSize += dataFileNameLen + 1;
11078fe96085Stim szeto 
11088fe96085Stim szeto 	if (disk->luMetaFileNameValid) {
11098fe96085Stim szeto 		metaFileNameLen = strlen(disk->luMetaFileName);
11108fe96085Stim szeto 		sluBufSize += metaFileNameLen + 1;
11118fe96085Stim szeto 	}
11128fe96085Stim szeto 
11138fe96085Stim szeto 	serialNumLen = strlen(disk->serialNum);
11148fe96085Stim szeto 	sluBufSize += serialNumLen;
11158fe96085Stim szeto 
11168fe96085Stim szeto 	if (disk->luAliasValid) {
11178fe96085Stim szeto 		luAliasLen = strlen(disk->luAlias);
11188fe96085Stim szeto 		sluBufSize += luAliasLen + 1;
11198fe96085Stim szeto 	}
11208fe96085Stim szeto 
11212f624233SNattuvetty Bhavyan 	if (disk->luMgmtUrlValid) {
11222f624233SNattuvetty Bhavyan 		luMgmtUrlLen = strlen(disk->luMgmtUrl);
11232f624233SNattuvetty Bhavyan 		sluBufSize += luMgmtUrlLen + 1;
11242f624233SNattuvetty Bhavyan 	}
11252f624233SNattuvetty Bhavyan 
11268fe96085Stim szeto 	/*
11278fe96085Stim szeto 	 * 8 is the size of the buffer set aside for
11288fe96085Stim szeto 	 * concatenation of variable length fields
11298fe96085Stim szeto 	 */
11308fe96085Stim szeto 	sbdLu = (sbd_create_and_reg_lu_t *)calloc(1,
11318fe96085Stim szeto 	    sizeof (sbd_create_and_reg_lu_t) + sluBufSize - 8);
11328fe96085Stim szeto 	if (sbdLu == NULL) {
11338fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
11348fe96085Stim szeto 	}
11358fe96085Stim szeto 
11368fe96085Stim szeto 	sbdLu->slu_struct_size = sizeof (sbd_create_and_reg_lu_t) +
11378fe96085Stim szeto 	    sluBufSize - 8;
11388fe96085Stim szeto 
11398fe96085Stim szeto 	if (metaFileNameLen) {
11408fe96085Stim szeto 		sbdLu->slu_meta_fname_valid = 1;
11418fe96085Stim szeto 		sbdLu->slu_meta_fname_off = bufOffset;
11428fe96085Stim szeto 		bcopy(disk->luMetaFileName, &(sbdLu->slu_buf[bufOffset]),
11438fe96085Stim szeto 		    metaFileNameLen + 1);
11448fe96085Stim szeto 		bufOffset += metaFileNameLen + 1;
11458fe96085Stim szeto 	}
11468fe96085Stim szeto 
11478fe96085Stim szeto 	bcopy(disk->luDataFileName, &(sbdLu->slu_buf[bufOffset]),
11488fe96085Stim szeto 	    dataFileNameLen + 1);
11498fe96085Stim szeto 	sbdLu->slu_data_fname_off = bufOffset;
11508fe96085Stim szeto 	bufOffset += dataFileNameLen + 1;
11518fe96085Stim szeto 
11528fe96085Stim szeto 	/* currently, serial # is not passed null terminated to the driver */
11538fe96085Stim szeto 	if (disk->serialNumValid) {
11548fe96085Stim szeto 		sbdLu->slu_serial_valid = 1;
11558fe96085Stim szeto 		sbdLu->slu_serial_off = bufOffset;
11568fe96085Stim szeto 		sbdLu->slu_serial_size = serialNumLen;
11578fe96085Stim szeto 		bcopy(disk->serialNum, &(sbdLu->slu_buf[bufOffset]),
11588fe96085Stim szeto 		    serialNumLen);
11598fe96085Stim szeto 		bufOffset += serialNumLen;
11608fe96085Stim szeto 	}
11618fe96085Stim szeto 
11628fe96085Stim szeto 	if (disk->luAliasValid) {
11638fe96085Stim szeto 		sbdLu->slu_alias_valid = 1;
11648fe96085Stim szeto 		sbdLu->slu_alias_off = bufOffset;
11658fe96085Stim szeto 		bcopy(disk->luAlias, &(sbdLu->slu_buf[bufOffset]),
11668fe96085Stim szeto 		    luAliasLen + 1);
11678fe96085Stim szeto 		bufOffset += luAliasLen + 1;
11688fe96085Stim szeto 	}
11698fe96085Stim szeto 
11702f624233SNattuvetty Bhavyan 	if (disk->luMgmtUrlValid) {
11712f624233SNattuvetty Bhavyan 		sbdLu->slu_mgmt_url_valid = 1;
11722f624233SNattuvetty Bhavyan 		sbdLu->slu_mgmt_url_off = bufOffset;
11732f624233SNattuvetty Bhavyan 		bcopy(disk->luMgmtUrl, &(sbdLu->slu_buf[bufOffset]),
11742f624233SNattuvetty Bhavyan 		    luMgmtUrlLen + 1);
11752f624233SNattuvetty Bhavyan 		bufOffset += luMgmtUrlLen + 1;
11762f624233SNattuvetty Bhavyan 	}
11772f624233SNattuvetty Bhavyan 
11788fe96085Stim szeto 	if (disk->luSizeValid) {
11798fe96085Stim szeto 		sbdLu->slu_lu_size_valid = 1;
11808fe96085Stim szeto 		sbdLu->slu_lu_size = disk->luSize;
11818fe96085Stim szeto 	}
11828fe96085Stim szeto 
11838fe96085Stim szeto 	if (disk->luGuidValid) {
11848fe96085Stim szeto 		sbdLu->slu_guid_valid = 1;
11858fe96085Stim szeto 		bcopy(disk->luGuid, sbdLu->slu_guid, sizeof (disk->luGuid));
11868fe96085Stim szeto 	}
11878fe96085Stim szeto 
11888fe96085Stim szeto 	if (disk->vidValid) {
11898fe96085Stim szeto 		sbdLu->slu_vid_valid = 1;
11908fe96085Stim szeto 		bcopy(disk->vid, sbdLu->slu_vid, sizeof (disk->vid));
11918fe96085Stim szeto 	}
11928fe96085Stim szeto 
11938fe96085Stim szeto 	if (disk->pidValid) {
11948fe96085Stim szeto 		sbdLu->slu_pid_valid = 1;
11958fe96085Stim szeto 		bcopy(disk->pid, sbdLu->slu_pid, sizeof (disk->pid));
11968fe96085Stim szeto 	}
11978fe96085Stim szeto 
11988fe96085Stim szeto 	if (disk->revValid) {
11998fe96085Stim szeto 		sbdLu->slu_rev_valid = 1;
12008fe96085Stim szeto 		bcopy(disk->rev, sbdLu->slu_rev, sizeof (disk->rev));
12018fe96085Stim szeto 	}
12028fe96085Stim szeto 
12038fe96085Stim szeto 	if (disk->companyIdValid) {
12048fe96085Stim szeto 		sbdLu->slu_company_id_valid = 1;
12058fe96085Stim szeto 		sbdLu->slu_company_id = disk->companyId;
12068fe96085Stim szeto 	}
12078fe96085Stim szeto 
12088fe96085Stim szeto 	if (disk->blkSizeValid) {
12098fe96085Stim szeto 		sbdLu->slu_blksize_valid = 1;
12108fe96085Stim szeto 		sbdLu->slu_blksize = disk->blkSize;
12118fe96085Stim szeto 	}
12128fe96085Stim szeto 
12138fe96085Stim szeto 	if (disk->writeProtectEnableValid) {
12148fe96085Stim szeto 		if (disk->writeProtectEnable) {
12158fe96085Stim szeto 			sbdLu->slu_write_protected = 1;
12168fe96085Stim szeto 		}
12178fe96085Stim szeto 	}
12188fe96085Stim szeto 
12198fe96085Stim szeto 	if (disk->writebackCacheDisableValid) {
12208fe96085Stim szeto 		sbdLu->slu_writeback_cache_disable_valid = 1;
12218fe96085Stim szeto 		if (disk->writebackCacheDisable) {
12228fe96085Stim szeto 			sbdLu->slu_writeback_cache_disable = 1;
12238fe96085Stim szeto 		}
12248fe96085Stim szeto 	}
12258fe96085Stim szeto 
12268fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
12278fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdLu->slu_struct_size;
12288fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdLu;
12298fe96085Stim szeto 	sbdIoctl.stmf_obuf_size = sbdLu->slu_struct_size;
12308fe96085Stim szeto 	sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdLu;
12318fe96085Stim szeto 
12328fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_CREATE_AND_REGISTER_LU, &sbdIoctl);
12338fe96085Stim szeto 	if (ioctlRet != 0) {
12348fe96085Stim szeto 		savedErrno = errno;
12358fe96085Stim szeto 		switch (savedErrno) {
12368fe96085Stim szeto 			case EBUSY:
12378fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
12388fe96085Stim szeto 				break;
12398fe96085Stim szeto 			case EPERM:
12408fe96085Stim szeto 			case EACCES:
12418fe96085Stim szeto 				ret = STMF_ERROR_PERM;
12428fe96085Stim szeto 				break;
12438fe96085Stim szeto 			default:
12448fe96085Stim szeto 				diskError(sbdIoctl.stmf_error, &ret);
12458fe96085Stim szeto 				if (ret == STMF_STATUS_ERROR) {
12468fe96085Stim szeto 					syslog(LOG_DEBUG,
12478fe96085Stim szeto 					"createDiskLu:ioctl "
12488fe96085Stim szeto 					"error(%d) (%d) (%d)", ioctlRet,
12498fe96085Stim szeto 					    sbdIoctl.stmf_error, savedErrno);
12508fe96085Stim szeto 				}
12518fe96085Stim szeto 				break;
12528fe96085Stim szeto 		}
12538fe96085Stim szeto 	}
12548fe96085Stim szeto 
12558fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
12568fe96085Stim szeto 		goto done;
12578fe96085Stim szeto 	}
12588fe96085Stim szeto 
12598fe96085Stim szeto 	/*
12608fe96085Stim szeto 	 * on success, copy the resulting guid into the caller's guid if not
12618fe96085Stim szeto 	 * NULL
12628fe96085Stim szeto 	 */
12638fe96085Stim szeto 	if (createdGuid) {
12648fe96085Stim szeto 		bcopy(sbdLu->slu_guid, createdGuid->guid,
12658fe96085Stim szeto 		    sizeof (sbdLu->slu_guid));
12668fe96085Stim szeto 	}
12678fe96085Stim szeto 
12688fe96085Stim szeto 	bcopy(sbdLu->slu_guid, guid.guid, sizeof (sbdLu->slu_guid));
12698fe96085Stim szeto 	if (disk->luMetaFileNameValid) {
12708fe96085Stim szeto 		ret = addGuidToDiskStore(&guid, disk->luMetaFileName);
12718fe96085Stim szeto 	} else {
12728fe96085Stim szeto 		ret = addGuidToDiskStore(&guid, disk->luDataFileName);
12738fe96085Stim szeto 	}
12748fe96085Stim szeto done:
12758fe96085Stim szeto 	free(sbdLu);
12768fe96085Stim szeto 	(void) close(fd);
12778fe96085Stim szeto 	return (ret);
12788fe96085Stim szeto }
12798fe96085Stim szeto 
12808fe96085Stim szeto 
12818fe96085Stim szeto /*
12828fe96085Stim szeto  * stmfImportLu
12838fe96085Stim szeto  *
12848fe96085Stim szeto  * Purpose: Import a previously created logical unit
12858fe96085Stim szeto  *
12868fe96085Stim szeto  * dType - Type of logical unit
12878fe96085Stim szeto  *         Can be: STMF_DISK
12888fe96085Stim szeto  *
12898fe96085Stim szeto  * luGuid - If non-NULL, on success, contains the guid of the imported logical
12908fe96085Stim szeto  *	    unit
12918fe96085Stim szeto  *
12928fe96085Stim szeto  * fname - A file name where the metadata resides
12938fe96085Stim szeto  *
12948fe96085Stim szeto  */
12958fe96085Stim szeto int
12968fe96085Stim szeto stmfImportLu(uint16_t dType, char *fname, stmfGuid *luGuid)
12978fe96085Stim szeto {
12988fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
12998fe96085Stim szeto 
13008fe96085Stim szeto 	if (dType == STMF_DISK) {
13018fe96085Stim szeto 		ret = importDiskLu(fname, luGuid);
13028fe96085Stim szeto 	} else {
13038fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
13048fe96085Stim szeto 	}
13058fe96085Stim szeto 
13068fe96085Stim szeto 	return (ret);
13078fe96085Stim szeto }
13088fe96085Stim szeto 
13098fe96085Stim szeto /*
13108fe96085Stim szeto  * importDiskLu
13118fe96085Stim szeto  *
13128fe96085Stim szeto  * filename - filename to import
13138fe96085Stim szeto  * createdGuid - if not NULL, on success contains the imported guid
13148fe96085Stim szeto  *
13158fe96085Stim szeto  */
13168fe96085Stim szeto static int
13178fe96085Stim szeto importDiskLu(char *fname, stmfGuid *createdGuid)
13188fe96085Stim szeto {
13198fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
13208fe96085Stim szeto 	int fd = 0;
13218fe96085Stim szeto 	int ioctlRet;
13228fe96085Stim szeto 	int savedErrno;
13238fe96085Stim szeto 	int metaFileNameLen;
13248fe96085Stim szeto 	stmfGuid iGuid;
13258fe96085Stim szeto 	int iluBufSize = 0;
13268fe96085Stim szeto 	sbd_import_lu_t *sbdLu = NULL;
13278fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
13288fe96085Stim szeto 
13298fe96085Stim szeto 	if (fname == NULL) {
13308fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
13318fe96085Stim szeto 	}
13328fe96085Stim szeto 
13338fe96085Stim szeto 	/*
13348fe96085Stim szeto 	 * Open control node for sbd
13358fe96085Stim szeto 	 */
13368fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
13378fe96085Stim szeto 		return (ret);
13388fe96085Stim szeto 
13398fe96085Stim szeto 	metaFileNameLen = strlen(fname);
13408fe96085Stim szeto 	iluBufSize += metaFileNameLen + 1;
13418fe96085Stim szeto 
13428fe96085Stim szeto 	/*
13438fe96085Stim szeto 	 * 8 is the size of the buffer set aside for
13448fe96085Stim szeto 	 * concatenation of variable length fields
13458fe96085Stim szeto 	 */
13468fe96085Stim szeto 	sbdLu = (sbd_import_lu_t *)calloc(1,
13478fe96085Stim szeto 	    sizeof (sbd_import_lu_t) + iluBufSize - 8);
13488fe96085Stim szeto 	if (sbdLu == NULL) {
13498fe96085Stim szeto 		(void) close(fd);
13508fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
13518fe96085Stim szeto 	}
13528fe96085Stim szeto 
13538fe96085Stim szeto 	/*
13548fe96085Stim szeto 	 * Accept either a data file or meta data file.
13558fe96085Stim szeto 	 * sbd will do the right thing here either way.
13568fe96085Stim szeto 	 * i.e. if it's a data file, it assumes that the
13578fe96085Stim szeto 	 * meta data is shared with the data.
13588fe96085Stim szeto 	 */
13598fe96085Stim szeto 	(void) strncpy(sbdLu->ilu_meta_fname, fname, metaFileNameLen);
13608fe96085Stim szeto 
13618fe96085Stim szeto 	sbdLu->ilu_struct_size = sizeof (sbd_import_lu_t) + iluBufSize - 8;
13628fe96085Stim szeto 
13638fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
13648fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdLu->ilu_struct_size;
13658fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdLu;
13668fe96085Stim szeto 	sbdIoctl.stmf_obuf_size = sbdLu->ilu_struct_size;
13678fe96085Stim szeto 	sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdLu;
13688fe96085Stim szeto 
13698fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_IMPORT_LU, &sbdIoctl);
13708fe96085Stim szeto 	if (ioctlRet != 0) {
13718fe96085Stim szeto 		savedErrno = errno;
13728fe96085Stim szeto 		switch (savedErrno) {
13738fe96085Stim szeto 			case EBUSY:
13748fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
13758fe96085Stim szeto 				break;
13768fe96085Stim szeto 			case EPERM:
13778fe96085Stim szeto 			case EACCES:
13788fe96085Stim szeto 				ret = STMF_ERROR_PERM;
13798fe96085Stim szeto 				break;
13808fe96085Stim szeto 			default:
13818fe96085Stim szeto 				diskError(sbdIoctl.stmf_error, &ret);
13828fe96085Stim szeto 				if (ret == STMF_STATUS_ERROR) {
13838fe96085Stim szeto 					syslog(LOG_DEBUG,
13848fe96085Stim szeto 					"importDiskLu:ioctl "
13858fe96085Stim szeto 					"error(%d) (%d) (%d)", ioctlRet,
13868fe96085Stim szeto 					    sbdIoctl.stmf_error, savedErrno);
13878fe96085Stim szeto 				}
13888fe96085Stim szeto 				break;
13898fe96085Stim szeto 		}
13908fe96085Stim szeto 	}
13918fe96085Stim szeto 
13928fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
13938fe96085Stim szeto 		goto done;
13948fe96085Stim szeto 	}
13958fe96085Stim szeto 
13968fe96085Stim szeto 	/*
13978fe96085Stim szeto 	 * on success, copy the resulting guid into the caller's guid if not
13988fe96085Stim szeto 	 * NULL and add it to the persistent store for sbd
13998fe96085Stim szeto 	 */
14008fe96085Stim szeto 	if (createdGuid) {
14018fe96085Stim szeto 		bcopy(sbdLu->ilu_ret_guid, createdGuid->guid,
14028fe96085Stim szeto 		    sizeof (sbdLu->ilu_ret_guid));
14038fe96085Stim szeto 		ret = addGuidToDiskStore(createdGuid, fname);
14048fe96085Stim szeto 	} else {
14058fe96085Stim szeto 		bcopy(sbdLu->ilu_ret_guid, iGuid.guid,
14068fe96085Stim szeto 		    sizeof (sbdLu->ilu_ret_guid));
14078fe96085Stim szeto 		ret = addGuidToDiskStore(&iGuid, fname);
14088fe96085Stim szeto 	}
14098fe96085Stim szeto done:
14108fe96085Stim szeto 	free(sbdLu);
14118fe96085Stim szeto 	(void) close(fd);
14128fe96085Stim szeto 	return (ret);
14138fe96085Stim szeto }
14148fe96085Stim szeto 
14158fe96085Stim szeto /*
14168fe96085Stim szeto  * diskError
14178fe96085Stim szeto  *
14188fe96085Stim szeto  * Purpose: Translate sbd driver error
14198fe96085Stim szeto  */
14208fe96085Stim szeto static void
14218fe96085Stim szeto diskError(uint32_t stmfError, int *ret)
14228fe96085Stim szeto {
14238fe96085Stim szeto 	switch (stmfError) {
14248fe96085Stim szeto 		case SBD_RET_META_CREATION_FAILED:
14258fe96085Stim szeto 		case SBD_RET_ZFS_META_CREATE_FAILED:
14268fe96085Stim szeto 			*ret = STMF_ERROR_META_CREATION;
14278fe96085Stim szeto 			break;
14288fe96085Stim szeto 		case SBD_RET_INVALID_BLKSIZE:
14298fe96085Stim szeto 			*ret = STMF_ERROR_INVALID_BLKSIZE;
14308fe96085Stim szeto 			break;
14318fe96085Stim szeto 		case SBD_RET_FILE_ALREADY_REGISTERED:
14328fe96085Stim szeto 			*ret = STMF_ERROR_FILE_IN_USE;
14338fe96085Stim szeto 			break;
14348fe96085Stim szeto 		case SBD_RET_GUID_ALREADY_REGISTERED:
14358fe96085Stim szeto 			*ret = STMF_ERROR_GUID_IN_USE;
14368fe96085Stim szeto 			break;
14378fe96085Stim szeto 		case SBD_RET_META_PATH_NOT_ABSOLUTE:
14388fe96085Stim szeto 		case SBD_RET_META_FILE_LOOKUP_FAILED:
14398fe96085Stim szeto 		case SBD_RET_META_FILE_OPEN_FAILED:
14408fe96085Stim szeto 		case SBD_RET_META_FILE_GETATTR_FAILED:
14418fe96085Stim szeto 		case SBD_RET_NO_META:
14428fe96085Stim szeto 			*ret = STMF_ERROR_META_FILE_NAME;
14438fe96085Stim szeto 			break;
14448fe96085Stim szeto 		case SBD_RET_DATA_PATH_NOT_ABSOLUTE:
14458fe96085Stim szeto 		case SBD_RET_DATA_FILE_LOOKUP_FAILED:
14468fe96085Stim szeto 		case SBD_RET_DATA_FILE_OPEN_FAILED:
14478fe96085Stim szeto 		case SBD_RET_DATA_FILE_GETATTR_FAILED:
14488fe96085Stim szeto 			*ret = STMF_ERROR_DATA_FILE_NAME;
14498fe96085Stim szeto 			break;
14508fe96085Stim szeto 		case SBD_RET_FILE_SIZE_ERROR:
14518fe96085Stim szeto 			*ret = STMF_ERROR_FILE_SIZE_INVALID;
14528fe96085Stim szeto 			break;
14538fe96085Stim szeto 		case SBD_RET_SIZE_OUT_OF_RANGE:
14548fe96085Stim szeto 			*ret = STMF_ERROR_SIZE_OUT_OF_RANGE;
14558fe96085Stim szeto 			break;
14568fe96085Stim szeto 		case SBD_RET_LU_BUSY:
14578fe96085Stim szeto 			*ret = STMF_ERROR_LU_BUSY;
14588fe96085Stim szeto 			break;
14598fe96085Stim szeto 		case SBD_RET_WRITE_CACHE_SET_FAILED:
14608fe96085Stim szeto 			*ret = STMF_ERROR_WRITE_CACHE_SET;
14618fe96085Stim szeto 			break;
14628fe96085Stim szeto 		default:
14638fe96085Stim szeto 			*ret = STMF_STATUS_ERROR;
14648fe96085Stim szeto 			break;
14658fe96085Stim szeto 	}
14668fe96085Stim szeto }
14678fe96085Stim szeto 
14688fe96085Stim szeto /*
14698fe96085Stim szeto  * Creates a logical unit resource of type STMF_DISK.
14708fe96085Stim szeto  *
14718fe96085Stim szeto  * No defaults should be set here as all defaults are derived from the
14728fe96085Stim szeto  * driver's default settings.
14738fe96085Stim szeto  */
14748fe96085Stim szeto static int
14758fe96085Stim szeto createDiskResource(luResourceImpl *hdl)
14768fe96085Stim szeto {
14778fe96085Stim szeto 	hdl->type = STMF_DISK;
14788fe96085Stim szeto 
14798fe96085Stim szeto 	hdl->resource = calloc(1, sizeof (diskResource));
14808fe96085Stim szeto 	if (hdl->resource == NULL) {
14818fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
14828fe96085Stim szeto 	}
14838fe96085Stim szeto 
14848fe96085Stim szeto 	return (STMF_STATUS_SUCCESS);
14858fe96085Stim szeto }
14868fe96085Stim szeto 
14878fe96085Stim szeto /*
14888fe96085Stim szeto  * stmfDeleteLu
14898fe96085Stim szeto  *
14908fe96085Stim szeto  * Purpose: Delete a logical unit
14918fe96085Stim szeto  *
14928fe96085Stim szeto  * hdl - handle to logical unit resource created via stmfCreateLuResource
14938fe96085Stim szeto  *
14948fe96085Stim szeto  * luGuid - If non-NULL, on success, contains the guid of the created logical
14958fe96085Stim szeto  *	    unit
14968fe96085Stim szeto  */
14978fe96085Stim szeto int
14988fe96085Stim szeto stmfDeleteLu(stmfGuid *luGuid)
14998fe96085Stim szeto {
15008fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
15018fe96085Stim szeto 	stmfLogicalUnitProperties luProps;
15028fe96085Stim szeto 
15038fe96085Stim szeto 	if (luGuid == NULL) {
15048fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
15058fe96085Stim szeto 	}
15068fe96085Stim szeto 
15078fe96085Stim szeto 	/* Check logical unit provider name to call correct dtype function */
15088fe96085Stim szeto 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
15098fe96085Stim szeto 	    != STMF_STATUS_SUCCESS) {
15108fe96085Stim szeto 		return (ret);
15118fe96085Stim szeto 	} else {
15128fe96085Stim szeto 		if (strcmp(luProps.providerName, "sbd") == 0) {
15138fe96085Stim szeto 			ret = deleteDiskLu(luGuid);
15148fe96085Stim szeto 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
15158fe96085Stim szeto 			return (STMF_ERROR_NOT_FOUND);
15168fe96085Stim szeto 		} else {
15178fe96085Stim szeto 			return (STMF_ERROR_INVALID_ARG);
15188fe96085Stim szeto 		}
15198fe96085Stim szeto 	}
15208fe96085Stim szeto 
15218fe96085Stim szeto 	return (ret);
15228fe96085Stim szeto }
15238fe96085Stim szeto 
15248fe96085Stim szeto static int
15258fe96085Stim szeto deleteDiskLu(stmfGuid *luGuid)
15268fe96085Stim szeto {
15278fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
15288fe96085Stim szeto 	int fd;
15298fe96085Stim szeto 	int savedErrno;
15308fe96085Stim szeto 	int ioctlRet;
15318fe96085Stim szeto 	sbd_delete_lu_t deleteLu = {0};
15328fe96085Stim szeto 
15338fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
15348fe96085Stim szeto 
15358fe96085Stim szeto 	/*
15368fe96085Stim szeto 	 * Open control node for sbd
15378fe96085Stim szeto 	 */
15388fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
15398fe96085Stim szeto 		return (ret);
15408fe96085Stim szeto 
15418fe96085Stim szeto 	ret = removeGuidFromDiskStore(luGuid);
15428fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
15438fe96085Stim szeto 		goto done;
15448fe96085Stim szeto 	}
15458fe96085Stim szeto 
15468fe96085Stim szeto 	bcopy(luGuid, deleteLu.dlu_guid, sizeof (deleteLu.dlu_guid));
15478fe96085Stim szeto 	deleteLu.dlu_by_guid = 1;
15488fe96085Stim szeto 
15498fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
15508fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sizeof (deleteLu);
15518fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)&deleteLu;
15528fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_DELETE_LU, &sbdIoctl);
15538fe96085Stim szeto 	if (ioctlRet != 0) {
15548fe96085Stim szeto 		savedErrno = errno;
15558fe96085Stim szeto 		switch (savedErrno) {
15568fe96085Stim szeto 			case EBUSY:
15578fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
15588fe96085Stim szeto 				break;
15598fe96085Stim szeto 			case EPERM:
15608fe96085Stim szeto 			case EACCES:
15618fe96085Stim szeto 				ret = STMF_ERROR_PERM;
15628fe96085Stim szeto 				break;
15638fe96085Stim szeto 			case ENOENT:
15648fe96085Stim szeto 				ret = STMF_ERROR_NOT_FOUND;
15658fe96085Stim szeto 				break;
15668fe96085Stim szeto 			default:
15678fe96085Stim szeto 				syslog(LOG_DEBUG,
15688fe96085Stim szeto 				    "deleteDiskLu:ioctl error(%d) (%d) (%d)",
15698fe96085Stim szeto 				    ioctlRet, sbdIoctl.stmf_error, savedErrno);
15708fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
15718fe96085Stim szeto 				break;
15728fe96085Stim szeto 		}
15738fe96085Stim szeto 	}
15748fe96085Stim szeto 
15758fe96085Stim szeto done:
15768fe96085Stim szeto 	(void) close(fd);
15778fe96085Stim szeto 	return (ret);
15788fe96085Stim szeto }
15798fe96085Stim szeto 
15808fe96085Stim szeto /*
15818fe96085Stim szeto  * stmfModifyLu
15828fe96085Stim szeto  *
15838fe96085Stim szeto  * Purpose: Modify properties of a logical unit
15848fe96085Stim szeto  *
15858fe96085Stim szeto  * luGuid - guid of registered logical unit
15868fe96085Stim szeto  * prop - property to modify
15878fe96085Stim szeto  * propVal - property value to set
15888fe96085Stim szeto  *
15898fe96085Stim szeto  */
15908fe96085Stim szeto int
15918fe96085Stim szeto stmfModifyLu(stmfGuid *luGuid, uint32_t prop, const char *propVal)
15928fe96085Stim szeto {
15938fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
15948fe96085Stim szeto 	stmfLogicalUnitProperties luProps;
15958fe96085Stim szeto 
15968fe96085Stim szeto 	if (luGuid == NULL) {
15978fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
15988fe96085Stim szeto 	}
15998fe96085Stim szeto 
16008fe96085Stim szeto 	/* Check logical unit provider name to call correct dtype function */
16018fe96085Stim szeto 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
16028fe96085Stim szeto 	    != STMF_STATUS_SUCCESS) {
16038fe96085Stim szeto 		return (ret);
16048fe96085Stim szeto 	} else {
16058fe96085Stim szeto 		if (strcmp(luProps.providerName, "sbd") == 0) {
16068fe96085Stim szeto 			ret = modifyDiskLuProp(luGuid, NULL, prop, propVal);
16078fe96085Stim szeto 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
16088fe96085Stim szeto 			return (STMF_ERROR_NOT_FOUND);
16098fe96085Stim szeto 		} else {
16108fe96085Stim szeto 			return (STMF_ERROR_INVALID_ARG);
16118fe96085Stim szeto 		}
16128fe96085Stim szeto 	}
16138fe96085Stim szeto 
16148fe96085Stim szeto 	return (ret);
16158fe96085Stim szeto }
16168fe96085Stim szeto 
16178fe96085Stim szeto /*
16188fe96085Stim szeto  * stmfModifyLuByFname
16198fe96085Stim szeto  *
16208fe96085Stim szeto  * Purpose: Modify a device by filename. Device does not need to be registered.
16218fe96085Stim szeto  *
16228fe96085Stim szeto  * dType - type of device to modify
16238fe96085Stim szeto  *         STMF_DISK
16248fe96085Stim szeto  *
16258fe96085Stim szeto  * fname - filename or meta filename
16268fe96085Stim szeto  * prop - valid property identifier
16278fe96085Stim szeto  * propVal - property value
16288fe96085Stim szeto  *
16298fe96085Stim szeto  */
16308fe96085Stim szeto int
16318fe96085Stim szeto stmfModifyLuByFname(uint16_t dType, const char *fname, uint32_t prop,
16328fe96085Stim szeto     const char *propVal)
16338fe96085Stim szeto {
16348fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
16358fe96085Stim szeto 	if (fname == NULL) {
16368fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
16378fe96085Stim szeto 	}
16388fe96085Stim szeto 
16398fe96085Stim szeto 	if (dType == STMF_DISK) {
16408fe96085Stim szeto 		ret = modifyDiskLuProp(NULL, fname, prop, propVal);
16418fe96085Stim szeto 	} else {
16428fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
16438fe96085Stim szeto 	}
16448fe96085Stim szeto 
16458fe96085Stim szeto 	return (ret);
16468fe96085Stim szeto }
16478fe96085Stim szeto 
16488fe96085Stim szeto static int
16498fe96085Stim szeto modifyDiskLuProp(stmfGuid *luGuid, const char *fname, uint32_t prop,
16508fe96085Stim szeto     const char *propVal)
16518fe96085Stim szeto {
16528fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
16538fe96085Stim szeto 	luResource hdl = NULL;
16548fe96085Stim szeto 	luResourceImpl *luPropsHdl;
16558fe96085Stim szeto 
16568fe96085Stim szeto 	ret = stmfCreateLuResource(STMF_DISK, &hdl);
16578fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
16588fe96085Stim szeto 		return (ret);
16598fe96085Stim szeto 	}
16608fe96085Stim szeto 	ret = validateModifyDiskProp(prop);
16618fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
16628fe96085Stim szeto 		(void) stmfFreeLuResource(hdl);
16638fe96085Stim szeto 		return (STMF_ERROR_INVALID_PROP);
16648fe96085Stim szeto 	}
16658fe96085Stim szeto 	ret = stmfSetLuProp(hdl, prop, propVal);
16668fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
16678fe96085Stim szeto 		(void) stmfFreeLuResource(hdl);
16688fe96085Stim szeto 		return (ret);
16698fe96085Stim szeto 	}
16708fe96085Stim szeto 	luPropsHdl = hdl;
16718fe96085Stim szeto 	ret = modifyDiskLu((diskResource *)luPropsHdl->resource, luGuid, fname);
16728fe96085Stim szeto 	(void) stmfFreeLuResource(hdl);
16738fe96085Stim szeto 	return (ret);
16748fe96085Stim szeto }
16758fe96085Stim szeto 
16768fe96085Stim szeto static int
16778fe96085Stim szeto validateModifyDiskProp(uint32_t prop)
16788fe96085Stim szeto {
16798fe96085Stim szeto 	switch (prop) {
16808fe96085Stim szeto 		case STMF_LU_PROP_ALIAS:
16818fe96085Stim szeto 		case STMF_LU_PROP_SIZE:
16822f624233SNattuvetty Bhavyan 		case STMF_LU_PROP_MGMT_URL:
16838fe96085Stim szeto 		case STMF_LU_PROP_WRITE_PROTECT:
16848fe96085Stim szeto 		case STMF_LU_PROP_WRITE_CACHE_DISABLE:
16858fe96085Stim szeto 			return (STMF_STATUS_SUCCESS);
16868fe96085Stim szeto 			break;
16878fe96085Stim szeto 		default:
16888fe96085Stim szeto 			return (STMF_STATUS_ERROR);
16898fe96085Stim szeto 			break;
16908fe96085Stim szeto 	}
16918fe96085Stim szeto }
16928fe96085Stim szeto 
16938fe96085Stim szeto static int
16948fe96085Stim szeto modifyDiskLu(diskResource *disk, stmfGuid *luGuid, const char *fname)
16958fe96085Stim szeto {
16968fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
16978fe96085Stim szeto 	int luAliasLen = 0;
16982f624233SNattuvetty Bhavyan 	int luMgmtUrlLen = 0;
16998fe96085Stim szeto 	int mluBufSize = 0;
17008fe96085Stim szeto 	int bufOffset = 0;
17018fe96085Stim szeto 	int fd = 0;
17028fe96085Stim szeto 	int ioctlRet;
17038fe96085Stim szeto 	int savedErrno;
17048fe96085Stim szeto 	int fnameSize = 0;
17058fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
17068fe96085Stim szeto 
17078fe96085Stim szeto 	sbd_modify_lu_t *sbdLu = NULL;
17088fe96085Stim szeto 
17098fe96085Stim szeto 	if (luGuid == NULL && fname == NULL) {
17108fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
17118fe96085Stim szeto 	}
17128fe96085Stim szeto 
17138fe96085Stim szeto 	if (fname) {
17148fe96085Stim szeto 		fnameSize = strlen(fname) + 1;
17158fe96085Stim szeto 		mluBufSize += fnameSize;
17168fe96085Stim szeto 	}
17178fe96085Stim szeto 
17188fe96085Stim szeto 	/*
17198fe96085Stim szeto 	 * Open control node for sbd
17208fe96085Stim szeto 	 */
17218fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
17228fe96085Stim szeto 		return (ret);
17238fe96085Stim szeto 
17248fe96085Stim szeto 	if (disk->luAliasValid) {
17258fe96085Stim szeto 		luAliasLen = strlen(disk->luAlias);
17268fe96085Stim szeto 		mluBufSize += luAliasLen + 1;
17278fe96085Stim szeto 	}
17288fe96085Stim szeto 
17292f624233SNattuvetty Bhavyan 	if (disk->luMgmtUrlValid) {
17302f624233SNattuvetty Bhavyan 		luMgmtUrlLen = strlen(disk->luMgmtUrl);
17312f624233SNattuvetty Bhavyan 		mluBufSize += luMgmtUrlLen + 1;
17322f624233SNattuvetty Bhavyan 	}
17332f624233SNattuvetty Bhavyan 
17348fe96085Stim szeto 	/*
17358fe96085Stim szeto 	 * 8 is the size of the buffer set aside for
17368fe96085Stim szeto 	 * concatenation of variable length fields
17378fe96085Stim szeto 	 */
17388fe96085Stim szeto 	sbdLu = (sbd_modify_lu_t *)calloc(1,
17398fe96085Stim szeto 	    sizeof (sbd_modify_lu_t) + mluBufSize - 8 + fnameSize);
17408fe96085Stim szeto 	if (sbdLu == NULL) {
17418fe96085Stim szeto 		(void) close(fd);
17428fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
17438fe96085Stim szeto 	}
17448fe96085Stim szeto 
17458fe96085Stim szeto 	sbdLu->mlu_struct_size = sizeof (sbd_modify_lu_t) +
17468fe96085Stim szeto 	    mluBufSize - 8 + fnameSize;
17478fe96085Stim szeto 
17488fe96085Stim szeto 	if (disk->luAliasValid) {
17498fe96085Stim szeto 		sbdLu->mlu_alias_valid = 1;
17508fe96085Stim szeto 		sbdLu->mlu_alias_off = bufOffset;
17518fe96085Stim szeto 		bcopy(disk->luAlias, &(sbdLu->mlu_buf[bufOffset]),
17528fe96085Stim szeto 		    luAliasLen + 1);
17538fe96085Stim szeto 		bufOffset += luAliasLen + 1;
17548fe96085Stim szeto 	}
17558fe96085Stim szeto 
17562f624233SNattuvetty Bhavyan 	if (disk->luMgmtUrlValid) {
17572f624233SNattuvetty Bhavyan 		sbdLu->mlu_mgmt_url_valid = 1;
17582f624233SNattuvetty Bhavyan 		sbdLu->mlu_mgmt_url_off = bufOffset;
17592f624233SNattuvetty Bhavyan 		bcopy(disk->luMgmtUrl, &(sbdLu->mlu_buf[bufOffset]),
17602f624233SNattuvetty Bhavyan 		    luMgmtUrlLen + 1);
17612f624233SNattuvetty Bhavyan 		bufOffset += luMgmtUrlLen + 1;
17622f624233SNattuvetty Bhavyan 	}
17632f624233SNattuvetty Bhavyan 
17648fe96085Stim szeto 	if (disk->luSizeValid) {
17658fe96085Stim szeto 		sbdLu->mlu_lu_size_valid = 1;
17668fe96085Stim szeto 		sbdLu->mlu_lu_size = disk->luSize;
17678fe96085Stim szeto 	}
17688fe96085Stim szeto 
17698fe96085Stim szeto 	if (disk->writeProtectEnableValid) {
17708fe96085Stim szeto 		sbdLu->mlu_write_protected_valid = 1;
17718fe96085Stim szeto 		if (disk->writeProtectEnable) {
17728fe96085Stim szeto 			sbdLu->mlu_write_protected = 1;
17738fe96085Stim szeto 		}
17748fe96085Stim szeto 	}
17758fe96085Stim szeto 
17768fe96085Stim szeto 	if (disk->writebackCacheDisableValid) {
17778fe96085Stim szeto 		sbdLu->mlu_writeback_cache_disable_valid = 1;
17788fe96085Stim szeto 		if (disk->writebackCacheDisable) {
17798fe96085Stim szeto 			sbdLu->mlu_writeback_cache_disable = 1;
17808fe96085Stim szeto 		}
17818fe96085Stim szeto 	}
17828fe96085Stim szeto 
17838fe96085Stim szeto 	if (luGuid) {
17848fe96085Stim szeto 		bcopy(luGuid, sbdLu->mlu_input_guid, sizeof (stmfGuid));
17858fe96085Stim szeto 		sbdLu->mlu_by_guid = 1;
17868fe96085Stim szeto 	} else {
17878fe96085Stim szeto 		sbdLu->mlu_fname_off = bufOffset;
17888fe96085Stim szeto 		bcopy(fname, &(sbdLu->mlu_buf[bufOffset]), fnameSize + 1);
17898fe96085Stim szeto 		sbdLu->mlu_by_fname = 1;
17908fe96085Stim szeto 	}
17918fe96085Stim szeto 
17928fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
17938fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdLu->mlu_struct_size;
17948fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdLu;
17958fe96085Stim szeto 
17968fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_MODIFY_LU, &sbdIoctl);
17978fe96085Stim szeto 	if (ioctlRet != 0) {
17988fe96085Stim szeto 		savedErrno = errno;
17998fe96085Stim szeto 		switch (savedErrno) {
18008fe96085Stim szeto 			case EBUSY:
18018fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
18028fe96085Stim szeto 				break;
18038fe96085Stim szeto 			case EPERM:
18048fe96085Stim szeto 			case EACCES:
18058fe96085Stim szeto 				ret = STMF_ERROR_PERM;
18068fe96085Stim szeto 				break;
18078fe96085Stim szeto 			default:
18088fe96085Stim szeto 				diskError(sbdIoctl.stmf_error, &ret);
18098fe96085Stim szeto 				if (ret == STMF_STATUS_ERROR) {
18108fe96085Stim szeto 					syslog(LOG_DEBUG,
18118fe96085Stim szeto 					"modifyDiskLu:ioctl "
18128fe96085Stim szeto 					"error(%d) (%d) (%d)", ioctlRet,
18138fe96085Stim szeto 					    sbdIoctl.stmf_error, savedErrno);
18148fe96085Stim szeto 				}
18158fe96085Stim szeto 				break;
18168fe96085Stim szeto 		}
18178fe96085Stim szeto 	}
18188fe96085Stim szeto 
18198fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
18208fe96085Stim szeto 		goto done;
18218fe96085Stim szeto 	}
18228fe96085Stim szeto 
18238fe96085Stim szeto done:
18248fe96085Stim szeto 	free(sbdLu);
18258fe96085Stim szeto 	(void) close(fd);
18268fe96085Stim szeto 	return (ret);
18278fe96085Stim szeto }
18288fe96085Stim szeto 
18298fe96085Stim szeto /*
18308fe96085Stim szeto  * removeGuidFromDiskStore
18318fe96085Stim szeto  *
18328fe96085Stim szeto  * Purpose: delete a logical unit from the sbd provider data
18338fe96085Stim szeto  */
18348fe96085Stim szeto static int
18358fe96085Stim szeto removeGuidFromDiskStore(stmfGuid *guid)
18368fe96085Stim szeto {
18378fe96085Stim szeto 	return (persistDiskGuid(guid, NULL, B_FALSE));
18388fe96085Stim szeto }
18398fe96085Stim szeto 
18408fe96085Stim szeto 
18418fe96085Stim szeto /*
18428fe96085Stim szeto  * addGuidToDiskStore
18438fe96085Stim szeto  *
18448fe96085Stim szeto  * Purpose: add a logical unit to the sbd provider data
18458fe96085Stim szeto  */
18468fe96085Stim szeto static int
18478fe96085Stim szeto addGuidToDiskStore(stmfGuid *guid, char *filename)
18488fe96085Stim szeto {
18498fe96085Stim szeto 	return (persistDiskGuid(guid, filename, B_TRUE));
18508fe96085Stim szeto }
18518fe96085Stim szeto 
18528fe96085Stim szeto 
18538fe96085Stim szeto /*
18548fe96085Stim szeto  * persistDiskGuid
18558fe96085Stim szeto  *
18568fe96085Stim szeto  * Purpose: Persist or unpersist a guid for the sbd provider data
18578fe96085Stim szeto  *
18588fe96085Stim szeto  */
18598fe96085Stim szeto static int
18608fe96085Stim szeto persistDiskGuid(stmfGuid *guid, char *filename, boolean_t persist)
18618fe96085Stim szeto {
18628fe96085Stim szeto 	char	    guidAsciiBuf[LU_ASCII_GUID_SIZE + 1] = {0};
18638fe96085Stim szeto 	nvlist_t    *nvl = NULL;
18648fe96085Stim szeto 
18658fe96085Stim szeto 	uint64_t    setToken;
18668fe96085Stim szeto 	boolean_t   retryGetProviderData = B_FALSE;
18678fe96085Stim szeto 	boolean_t   newData = B_FALSE;
18688fe96085Stim szeto 	int	    ret = STMF_STATUS_SUCCESS;
18698fe96085Stim szeto 	int	    retryCnt = 0;
18708fe96085Stim szeto 	int	    stmfRet;
18718fe96085Stim szeto 
18728fe96085Stim szeto 	/* if we're persisting a guid, there must be a filename */
18738fe96085Stim szeto 	if (persist && !filename) {
18748fe96085Stim szeto 		return (1);
18758fe96085Stim szeto 	}
18768fe96085Stim szeto 
18778fe96085Stim szeto 	/* guid is stored in lowercase ascii hex */
18788fe96085Stim szeto 	(void) snprintf(guidAsciiBuf, sizeof (guidAsciiBuf),
18798fe96085Stim szeto 	    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
18808fe96085Stim szeto 	    "%02x%02x%02x%02x%02x%02x",
18818fe96085Stim szeto 	    guid->guid[0], guid->guid[1], guid->guid[2], guid->guid[3],
18828fe96085Stim szeto 	    guid->guid[4], guid->guid[5], guid->guid[6], guid->guid[7],
18838fe96085Stim szeto 	    guid->guid[8], guid->guid[9], guid->guid[10], guid->guid[11],
18848fe96085Stim szeto 	    guid->guid[12], guid->guid[13], guid->guid[14], guid->guid[15]);
18858fe96085Stim szeto 
18868fe96085Stim szeto 
18878fe96085Stim szeto 	do {
18888fe96085Stim szeto 		retryGetProviderData = B_FALSE;
18898fe96085Stim szeto 		stmfRet = stmfGetProviderDataProt("sbd", &nvl,
18908fe96085Stim szeto 		    STMF_LU_PROVIDER_TYPE, &setToken);
18918fe96085Stim szeto 		if (stmfRet != STMF_STATUS_SUCCESS) {
18928fe96085Stim szeto 			if (persist && stmfRet == STMF_ERROR_NOT_FOUND) {
18938fe96085Stim szeto 				ret = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
18948fe96085Stim szeto 				if (ret != 0) {
18958fe96085Stim szeto 					syslog(LOG_DEBUG,
18968fe96085Stim szeto 					    "unpersistGuid:nvlist_alloc(%d)",
18978fe96085Stim szeto 					    ret);
18988fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
18998fe96085Stim szeto 					goto done;
19008fe96085Stim szeto 				}
19018fe96085Stim szeto 				newData = B_TRUE;
19028fe96085Stim szeto 			} else {
19038fe96085Stim szeto 				ret = stmfRet;
19048fe96085Stim szeto 				goto done;
19058fe96085Stim szeto 			}
19068fe96085Stim szeto 		}
19078fe96085Stim szeto 		if (persist) {
19088fe96085Stim szeto 			ret = nvlist_add_string(nvl, guidAsciiBuf, filename);
19098fe96085Stim szeto 		} else {
19108fe96085Stim szeto 			ret = nvlist_remove(nvl, guidAsciiBuf,
19118fe96085Stim szeto 			    DATA_TYPE_STRING);
19128fe96085Stim szeto 			if (ret == ENOENT) {
19138fe96085Stim szeto 				ret = 0;
19148fe96085Stim szeto 			}
19158fe96085Stim szeto 		}
19168fe96085Stim szeto 		if (ret == 0) {
19178fe96085Stim szeto 			if (newData) {
19188fe96085Stim szeto 				stmfRet = stmfSetProviderDataProt("sbd", nvl,
19198fe96085Stim szeto 				    STMF_LU_PROVIDER_TYPE, NULL);
19208fe96085Stim szeto 			} else {
19218fe96085Stim szeto 				stmfRet = stmfSetProviderDataProt("sbd", nvl,
19228fe96085Stim szeto 				    STMF_LU_PROVIDER_TYPE, &setToken);
19238fe96085Stim szeto 			}
19248fe96085Stim szeto 			if (stmfRet != STMF_STATUS_SUCCESS) {
19258fe96085Stim szeto 				if (stmfRet == STMF_ERROR_BUSY) {
19268fe96085Stim szeto 					/* get/set failed, try again */
19278fe96085Stim szeto 					retryGetProviderData = B_TRUE;
19288fe96085Stim szeto 					if (retryCnt++ > MAX_PROVIDER_RETRY) {
19298fe96085Stim szeto 						ret = stmfRet;
19308fe96085Stim szeto 						break;
19318fe96085Stim szeto 					}
19328fe96085Stim szeto 					continue;
19338fe96085Stim szeto 				} else if (stmfRet ==
19348fe96085Stim szeto 				    STMF_ERROR_PROV_DATA_STALE) {
19358fe96085Stim szeto 					/* update failed, try again */
19368fe96085Stim szeto 					nvlist_free(nvl);
19378fe96085Stim szeto 					nvl = NULL;
19388fe96085Stim szeto 					retryGetProviderData = B_TRUE;
19398fe96085Stim szeto 					if (retryCnt++ > MAX_PROVIDER_RETRY) {
19408fe96085Stim szeto 						ret = stmfRet;
19418fe96085Stim szeto 						break;
19428fe96085Stim szeto 					}
19438fe96085Stim szeto 					continue;
19448fe96085Stim szeto 				} else {
19458fe96085Stim szeto 					syslog(LOG_DEBUG,
19468fe96085Stim szeto 					    "unpersistGuid:error(%x)", stmfRet);
19478fe96085Stim szeto 					ret = stmfRet;
19488fe96085Stim szeto 				}
19498fe96085Stim szeto 				break;
19508fe96085Stim szeto 			}
19518fe96085Stim szeto 		} else {
19528fe96085Stim szeto 			syslog(LOG_DEBUG,
19538fe96085Stim szeto 			    "unpersistGuid:error nvlist_add/remove(%d)",
19548fe96085Stim szeto 			    ret);
19558fe96085Stim szeto 			ret = STMF_STATUS_ERROR;
19568fe96085Stim szeto 		}
19578fe96085Stim szeto 	} while (retryGetProviderData);
19588fe96085Stim szeto 
19598fe96085Stim szeto done:
19608fe96085Stim szeto 	nvlist_free(nvl);
19618fe96085Stim szeto 	return (ret);
19628fe96085Stim szeto }
19638fe96085Stim szeto 
19648fe96085Stim szeto 
19658fe96085Stim szeto /*
19668fe96085Stim szeto  * stmfGetLuProp
19678fe96085Stim szeto  *
19688fe96085Stim szeto  * Purpose: Get current value for a resource property
19698fe96085Stim szeto  *
19708fe96085Stim szeto  * hdl - luResource from a previous call to stmfCreateLuResource
19718fe96085Stim szeto  *
19728fe96085Stim szeto  * resourceProp - a valid resource property type
19738fe96085Stim szeto  *
19748fe96085Stim szeto  * propVal - void pointer to a pointer of the value to be retrieved
19758fe96085Stim szeto  */
19768fe96085Stim szeto int
19778fe96085Stim szeto stmfGetLuProp(luResource hdl, uint32_t prop, char *propVal, size_t *propLen)
19788fe96085Stim szeto {
19798fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
19808fe96085Stim szeto 	luResourceImpl *luPropsHdl = hdl;
19818fe96085Stim szeto 	if (hdl == NULL || propLen == NULL || propVal == NULL) {
19828fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
19838fe96085Stim szeto 	}
19848fe96085Stim szeto 
19858fe96085Stim szeto 	if (luPropsHdl->type == STMF_DISK) {
19868fe96085Stim szeto 		ret = getDiskProp(luPropsHdl, prop, propVal, propLen);
19878fe96085Stim szeto 	} else {
19888fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
19898fe96085Stim szeto 	}
19908fe96085Stim szeto 
19918fe96085Stim szeto 	return (ret);
19928fe96085Stim szeto }
19938fe96085Stim szeto 
19948fe96085Stim szeto /*
19958fe96085Stim szeto  * stmfGetLuResource
19968fe96085Stim szeto  *
19978fe96085Stim szeto  * Purpose: Get a logical unit resource handle for a given logical unit.
19988fe96085Stim szeto  *
19998fe96085Stim szeto  * hdl - pointer to luResource
20008fe96085Stim szeto  */
20018fe96085Stim szeto int
20028fe96085Stim szeto stmfGetLuResource(stmfGuid *luGuid, luResource *hdl)
20038fe96085Stim szeto {
20048fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
20058fe96085Stim szeto 	stmfLogicalUnitProperties luProps;
20068fe96085Stim szeto 
20078fe96085Stim szeto 
20088fe96085Stim szeto 	/* Check logical unit provider name to call correct dtype function */
20098fe96085Stim szeto 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
20108fe96085Stim szeto 	    != STMF_STATUS_SUCCESS) {
20118fe96085Stim szeto 		return (ret);
20128fe96085Stim szeto 	} else {
20138fe96085Stim szeto 		if (strcmp(luProps.providerName, "sbd") == 0) {
20148fe96085Stim szeto 			ret = getDiskAllProps(luGuid, hdl);
20158fe96085Stim szeto 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
20168fe96085Stim szeto 			return (STMF_ERROR_NOT_FOUND);
20178fe96085Stim szeto 		} else {
20188fe96085Stim szeto 			return (STMF_ERROR_INVALID_ARG);
20198fe96085Stim szeto 		}
20208fe96085Stim szeto 	}
20218fe96085Stim szeto 
20228fe96085Stim szeto 	return (ret);
20238fe96085Stim szeto }
20248fe96085Stim szeto 
20258fe96085Stim szeto /*
20268fe96085Stim szeto  * getDiskAllProps
20278fe96085Stim szeto  *
20288fe96085Stim szeto  * Purpose: load all disk properties from sbd driver
20298fe96085Stim szeto  *
20308fe96085Stim szeto  * luGuid - guid of disk device for which properties are to be retrieved
20318fe96085Stim szeto  * hdl - allocated luResource into which properties are to be copied
20328fe96085Stim szeto  *
20338fe96085Stim szeto  */
20348fe96085Stim szeto static int
20358fe96085Stim szeto getDiskAllProps(stmfGuid *luGuid, luResource *hdl)
20368fe96085Stim szeto {
20378fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
20388fe96085Stim szeto 	int fd;
20398fe96085Stim szeto 	sbd_lu_props_t *sbdProps;
20408fe96085Stim szeto 	int ioctlRet;
20418fe96085Stim szeto 	int savedErrno;
20428fe96085Stim szeto 	int sbdPropsSize = sizeof (*sbdProps) + MAX_SBD_PROPS;
20438fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
20448fe96085Stim szeto 
20458fe96085Stim szeto 	/*
20468fe96085Stim szeto 	 * Open control node for sbd
20478fe96085Stim szeto 	 */
20488fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
20498fe96085Stim szeto 		return (ret);
20508fe96085Stim szeto 
20518fe96085Stim szeto 
20528fe96085Stim szeto 	*hdl = calloc(1, sizeof (luResourceImpl));
20538fe96085Stim szeto 	if (*hdl == NULL) {
20548fe96085Stim szeto 		(void) close(fd);
20558fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
20568fe96085Stim szeto 	}
20578fe96085Stim szeto 
20588fe96085Stim szeto 	sbdProps = calloc(1, sbdPropsSize);
20598fe96085Stim szeto 	if (sbdProps == NULL) {
20608fe96085Stim szeto 		free(*hdl);
20618fe96085Stim szeto 		(void) close(fd);
20628fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
20638fe96085Stim szeto 	}
20648fe96085Stim szeto 
20658fe96085Stim szeto 	ret = createDiskResource((luResourceImpl *)*hdl);
20668fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
20678fe96085Stim szeto 		free(*hdl);
20688fe96085Stim szeto 		(void) close(fd);
20698fe96085Stim szeto 		return (ret);
20708fe96085Stim szeto 	}
20718fe96085Stim szeto 
20728fe96085Stim szeto 	sbdProps->slp_input_guid = 1;
20738fe96085Stim szeto 	bcopy(luGuid, sbdProps->slp_guid, sizeof (sbdProps->slp_guid));
20748fe96085Stim szeto 
20758fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
20768fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdPropsSize;
20778fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdProps;
20788fe96085Stim szeto 	sbdIoctl.stmf_obuf_size = sbdPropsSize;
20798fe96085Stim szeto 	sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdProps;
20808fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_GET_LU_PROPS, &sbdIoctl);
20818fe96085Stim szeto 	if (ioctlRet != 0) {
20828fe96085Stim szeto 		savedErrno = errno;
20838fe96085Stim szeto 		switch (savedErrno) {
20848fe96085Stim szeto 			case EBUSY:
20858fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
20868fe96085Stim szeto 				break;
20878fe96085Stim szeto 			case EPERM:
20888fe96085Stim szeto 			case EACCES:
20898fe96085Stim szeto 				ret = STMF_ERROR_PERM;
20908fe96085Stim szeto 				break;
20918fe96085Stim szeto 			case ENOENT:
20928fe96085Stim szeto 				ret = STMF_ERROR_NOT_FOUND;
20938fe96085Stim szeto 				break;
20948fe96085Stim szeto 			default:
20958fe96085Stim szeto 				syslog(LOG_DEBUG,
20968fe96085Stim szeto 				    "getDiskAllProps:ioctl error(%d) (%d) (%d)",
20978fe96085Stim szeto 				    ioctlRet, sbdIoctl.stmf_error, savedErrno);
20988fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
20998fe96085Stim szeto 				break;
21008fe96085Stim szeto 		}
21018fe96085Stim szeto 	}
21028fe96085Stim szeto 
21038fe96085Stim szeto 	if (ret == STMF_STATUS_SUCCESS) {
21048fe96085Stim szeto 		ret = loadDiskPropsFromDriver((luResourceImpl *)*hdl, sbdProps);
21058fe96085Stim szeto 	}
21068fe96085Stim szeto 
21078fe96085Stim szeto 	(void) close(fd);
21088fe96085Stim szeto 	return (ret);
21098fe96085Stim szeto }
21108fe96085Stim szeto 
21118fe96085Stim szeto /*
21128fe96085Stim szeto  * loadDiskPropsFromDriver
21138fe96085Stim szeto  *
21148fe96085Stim szeto  * Purpose: Retrieve all disk type properties from sbd driver
21158fe96085Stim szeto  *
21168fe96085Stim szeto  * hdl - Allocated luResourceImpl
21178fe96085Stim szeto  * sbdProps - sbd_lu_props_t structure returned from sbd driver
21188fe96085Stim szeto  *
21198fe96085Stim szeto  */
21208fe96085Stim szeto static int
21218fe96085Stim szeto loadDiskPropsFromDriver(luResourceImpl *hdl, sbd_lu_props_t *sbdProps)
21228fe96085Stim szeto {
21238fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
21248fe96085Stim szeto 	diskResource *diskLu = hdl->resource;
21258fe96085Stim szeto 	/* copy guid */
21268fe96085Stim szeto 	diskLu->luGuidValid = B_TRUE;
21278fe96085Stim szeto 	bcopy(sbdProps->slp_guid, diskLu->luGuid, sizeof (sbdProps->slp_guid));
21288fe96085Stim szeto 
21298fe96085Stim szeto 	if (sbdProps->slp_separate_meta && sbdProps->slp_meta_fname_valid) {
21308fe96085Stim szeto 		diskLu->luMetaFileNameValid = B_TRUE;
21318fe96085Stim szeto 		if (strlcpy(diskLu->luMetaFileName,
21328fe96085Stim szeto 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_meta_fname_off]),
21338fe96085Stim szeto 		    sizeof (diskLu->luMetaFileName)) >=
21348fe96085Stim szeto 		    sizeof (diskLu->luMetaFileName)) {
21358fe96085Stim szeto 			return (STMF_STATUS_ERROR);
21368fe96085Stim szeto 		}
21378fe96085Stim szeto 	}
21388fe96085Stim szeto 
21398fe96085Stim szeto 	if (sbdProps->slp_data_fname_valid) {
21408fe96085Stim szeto 		diskLu->luDataFileNameValid = B_TRUE;
21418fe96085Stim szeto 		if (strlcpy(diskLu->luDataFileName,
21428fe96085Stim szeto 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_data_fname_off]),
21438fe96085Stim szeto 		    sizeof (diskLu->luDataFileName)) >=
21448fe96085Stim szeto 		    sizeof (diskLu->luDataFileName)) {
21458fe96085Stim szeto 			return (STMF_STATUS_ERROR);
21468fe96085Stim szeto 		}
21478fe96085Stim szeto 	}
21488fe96085Stim szeto 
21498fe96085Stim szeto 	if (sbdProps->slp_serial_valid) {
21508fe96085Stim szeto 		diskLu->serialNumValid = B_TRUE;
21518fe96085Stim szeto 		bcopy(&(sbdProps->slp_buf[sbdProps->slp_serial_off]),
21528fe96085Stim szeto 		    diskLu->serialNum, sbdProps->slp_serial_size);
21538fe96085Stim szeto 	}
21548fe96085Stim szeto 
21552f624233SNattuvetty Bhavyan 	if (sbdProps->slp_mgmt_url_valid) {
21562f624233SNattuvetty Bhavyan 		diskLu->luMgmtUrlValid = B_TRUE;
21572f624233SNattuvetty Bhavyan 		if (strlcpy(diskLu->luMgmtUrl,
21582f624233SNattuvetty Bhavyan 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_mgmt_url_off]),
21592f624233SNattuvetty Bhavyan 		    sizeof (diskLu->luMgmtUrl)) >=
21602f624233SNattuvetty Bhavyan 		    sizeof (diskLu->luMgmtUrl)) {
21612f624233SNattuvetty Bhavyan 			return (STMF_STATUS_ERROR);
21622f624233SNattuvetty Bhavyan 		}
21632f624233SNattuvetty Bhavyan 	}
21642f624233SNattuvetty Bhavyan 
21658fe96085Stim szeto 	if (sbdProps->slp_alias_valid) {
21668fe96085Stim szeto 		diskLu->luAliasValid = B_TRUE;
21678fe96085Stim szeto 		if (strlcpy(diskLu->luAlias,
21688fe96085Stim szeto 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_alias_off]),
21698fe96085Stim szeto 		    sizeof (diskLu->luAlias)) >=
21708fe96085Stim szeto 		    sizeof (diskLu->luAlias)) {
21718fe96085Stim szeto 			return (STMF_STATUS_ERROR);
21728fe96085Stim szeto 		}
21738fe96085Stim szeto 	} else { /* set alias to data filename if not set */
21748fe96085Stim szeto 		if (sbdProps->slp_data_fname_valid) {
21758fe96085Stim szeto 			diskLu->luAliasValid = B_TRUE;
21768fe96085Stim szeto 			if (strlcpy(diskLu->luAlias,
21778fe96085Stim szeto 			    (char *)&(sbdProps->slp_buf[
21788fe96085Stim szeto 			    sbdProps->slp_data_fname_off]),
21798fe96085Stim szeto 			    sizeof (diskLu->luAlias)) >=
21808fe96085Stim szeto 			    sizeof (diskLu->luAlias)) {
21818fe96085Stim szeto 				return (STMF_STATUS_ERROR);
21828fe96085Stim szeto 			}
21838fe96085Stim szeto 		}
21848fe96085Stim szeto 	}
21858fe96085Stim szeto 
21868fe96085Stim szeto 	diskLu->vidValid = B_TRUE;
21878fe96085Stim szeto 	bcopy(sbdProps->slp_vid, diskLu->vid, sizeof (diskLu->vid));
21888fe96085Stim szeto 
21898fe96085Stim szeto 	diskLu->pidValid = B_TRUE;
21908fe96085Stim szeto 	bcopy(sbdProps->slp_pid, diskLu->pid, sizeof (diskLu->pid));
21918fe96085Stim szeto 
21928fe96085Stim szeto 	diskLu->revValid = B_TRUE;
21938fe96085Stim szeto 	bcopy(sbdProps->slp_rev, diskLu->rev, sizeof (diskLu->rev));
21948fe96085Stim szeto 
21958fe96085Stim szeto 	diskLu->writeProtectEnableValid = B_TRUE;
21968fe96085Stim szeto 	if (sbdProps->slp_write_protected) {
21978fe96085Stim szeto 		diskLu->writeProtectEnable = B_TRUE;
21988fe96085Stim szeto 	}
21998fe96085Stim szeto 
22008fe96085Stim szeto 	diskLu->writebackCacheDisableValid = B_TRUE;
22018fe96085Stim szeto 	if (sbdProps->slp_writeback_cache_disable_cur) {
22028fe96085Stim szeto 		diskLu->writebackCacheDisable = B_TRUE;
22038fe96085Stim szeto 	}
22048fe96085Stim szeto 
22058fe96085Stim szeto 	diskLu->blkSizeValid = B_TRUE;
22068fe96085Stim szeto 	diskLu->blkSize = sbdProps->slp_blksize;
22078fe96085Stim szeto 
22088fe96085Stim szeto 	diskLu->luSizeValid = B_TRUE;
22098fe96085Stim szeto 	diskLu->luSize = sbdProps->slp_lu_size;
22108fe96085Stim szeto 
22118fe96085Stim szeto 	return (ret);
22128fe96085Stim szeto }
22138fe96085Stim szeto 
22148fe96085Stim szeto 
22158fe96085Stim szeto /*
22168fe96085Stim szeto  * stmfSetLuProp
22178fe96085Stim szeto  *
22188fe96085Stim szeto  * Purpose: set a property on an luResource
22198fe96085Stim szeto  *
22208fe96085Stim szeto  * hdl - allocated luResource
22218fe96085Stim szeto  * prop - property identifier
22228fe96085Stim szeto  * propVal - property value to be set
22238fe96085Stim szeto  */
22248fe96085Stim szeto int
22258fe96085Stim szeto stmfSetLuProp(luResource hdl, uint32_t prop, const char *propVal)
22268fe96085Stim szeto {
22278fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
22288fe96085Stim szeto 	luResourceImpl *luPropsHdl = hdl;
22298fe96085Stim szeto 	if (hdl == NULL) {
22308fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
22318fe96085Stim szeto 	}
22328fe96085Stim szeto 
22338fe96085Stim szeto 	if (luPropsHdl->type == STMF_DISK) {
22348fe96085Stim szeto 		ret = setDiskProp(luPropsHdl, prop, propVal);
22358fe96085Stim szeto 	} else {
22368fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
22378fe96085Stim szeto 	}
22388fe96085Stim szeto 
22398fe96085Stim szeto 	return (ret);
22408fe96085Stim szeto }
22418fe96085Stim szeto 
22428fe96085Stim szeto /*
22438fe96085Stim szeto  * getDiskProp
22448fe96085Stim szeto  *
22458fe96085Stim szeto  * Purpose: retrieve a given property from a logical unit resource of type disk
22468fe96085Stim szeto  *
22478fe96085Stim szeto  * hdl - allocated luResourceImpl
22488fe96085Stim szeto  * prop - property identifier
22498fe96085Stim szeto  * propVal - pointer to character to contain the retrieved property value
22508fe96085Stim szeto  * propLen - On input this is the length of propVal. On failure, it contains the
22518fe96085Stim szeto  *           number of bytes required for propVal
22528fe96085Stim szeto  */
22538fe96085Stim szeto static int
22548fe96085Stim szeto getDiskProp(luResourceImpl *hdl, uint32_t prop, char *propVal, size_t *propLen)
22558fe96085Stim szeto {
22568fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
22578fe96085Stim szeto 	diskResource *diskLu = hdl->resource;
22588fe96085Stim szeto 	size_t reqLen;
22598fe96085Stim szeto 
22608fe96085Stim szeto 	switch (prop) {
22618fe96085Stim szeto 		case STMF_LU_PROP_BLOCK_SIZE:
22628fe96085Stim szeto 			if (diskLu->blkSizeValid == B_FALSE) {
22638fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
22648fe96085Stim szeto 			}
22658fe96085Stim szeto 			reqLen = snprintf(propVal, *propLen, "%llu",
22668fe96085Stim szeto 			    (u_longlong_t)diskLu->blkSize);
22678fe96085Stim szeto 			if (reqLen >= *propLen) {
22688fe96085Stim szeto 				*propLen = reqLen + 1;
22698fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
22708fe96085Stim szeto 			}
22718fe96085Stim szeto 			break;
22728fe96085Stim szeto 		case STMF_LU_PROP_FILENAME:
22738fe96085Stim szeto 			if (diskLu->luDataFileNameValid == B_FALSE) {
22748fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
22758fe96085Stim szeto 			}
22768fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->luDataFileName,
22778fe96085Stim szeto 			    *propLen)) >= *propLen) {
22788fe96085Stim szeto 				*propLen = reqLen + 1;
22798fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
22808fe96085Stim szeto 			}
22818fe96085Stim szeto 			break;
22828fe96085Stim szeto 		case STMF_LU_PROP_META_FILENAME:
22838fe96085Stim szeto 			if (diskLu->luMetaFileNameValid == B_FALSE) {
22848fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
22858fe96085Stim szeto 			}
22868fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->luMetaFileName,
22878fe96085Stim szeto 			    *propLen)) >= *propLen) {
22888fe96085Stim szeto 				*propLen = reqLen + 1;
22898fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
22908fe96085Stim szeto 			}
22918fe96085Stim szeto 			break;
22922f624233SNattuvetty Bhavyan 		case STMF_LU_PROP_MGMT_URL:
22932f624233SNattuvetty Bhavyan 			if (diskLu->luMgmtUrlValid == B_FALSE) {
22942f624233SNattuvetty Bhavyan 				return (STMF_ERROR_NO_PROP);
22952f624233SNattuvetty Bhavyan 			}
22962f624233SNattuvetty Bhavyan 			if ((reqLen = strlcpy(propVal, diskLu->luMgmtUrl,
22972f624233SNattuvetty Bhavyan 			    *propLen)) >= *propLen) {
22982f624233SNattuvetty Bhavyan 				*propLen = reqLen + 1;
22992f624233SNattuvetty Bhavyan 				return (STMF_ERROR_INVALID_ARG);
23002f624233SNattuvetty Bhavyan 			}
23012f624233SNattuvetty Bhavyan 			break;
23028fe96085Stim szeto 		case STMF_LU_PROP_GUID:
23038fe96085Stim szeto 			if (diskLu->luGuidValid == B_FALSE) {
23048fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
23058fe96085Stim szeto 			}
23068fe96085Stim szeto 			reqLen = snprintf(propVal, *propLen,
23078fe96085Stim szeto 			    "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X"
23088fe96085Stim szeto 			    "%02X%02X%02X%02X",
23098fe96085Stim szeto 			    diskLu->luGuid[0], diskLu->luGuid[1],
23108fe96085Stim szeto 			    diskLu->luGuid[2], diskLu->luGuid[3],
23118fe96085Stim szeto 			    diskLu->luGuid[4], diskLu->luGuid[5],
23128fe96085Stim szeto 			    diskLu->luGuid[6], diskLu->luGuid[7],
23138fe96085Stim szeto 			    diskLu->luGuid[8], diskLu->luGuid[9],
23148fe96085Stim szeto 			    diskLu->luGuid[10], diskLu->luGuid[11],
23158fe96085Stim szeto 			    diskLu->luGuid[12], diskLu->luGuid[13],
23168fe96085Stim szeto 			    diskLu->luGuid[14], diskLu->luGuid[15]);
23178fe96085Stim szeto 			if (reqLen >= *propLen) {
23188fe96085Stim szeto 				*propLen = reqLen + 1;
23198fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
23208fe96085Stim szeto 			}
23218fe96085Stim szeto 			break;
23228fe96085Stim szeto 		case STMF_LU_PROP_SERIAL_NUM:
23238fe96085Stim szeto 			if (diskLu->serialNumValid == B_FALSE) {
23248fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
23258fe96085Stim szeto 			}
23268fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->serialNum,
23278fe96085Stim szeto 			    *propLen)) >= *propLen) {
23288fe96085Stim szeto 				*propLen = reqLen + 1;
23298fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
23308fe96085Stim szeto 			}
23318fe96085Stim szeto 			break;
23328fe96085Stim szeto 		case STMF_LU_PROP_SIZE:
23338fe96085Stim szeto 			if (diskLu->luSizeValid == B_FALSE) {
23348fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
23358fe96085Stim szeto 			}
23368fe96085Stim szeto 			(void) snprintf(propVal, *propLen, "%llu",
23378fe96085Stim szeto 			    (u_longlong_t)diskLu->luSize);
23388fe96085Stim szeto 			break;
23398fe96085Stim szeto 		case STMF_LU_PROP_ALIAS:
23408fe96085Stim szeto 			if (diskLu->luAliasValid == B_FALSE) {
23418fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
23428fe96085Stim szeto 			}
23438fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->luAlias,
23448fe96085Stim szeto 			    *propLen)) >= *propLen) {
23458fe96085Stim szeto 				*propLen = reqLen + 1;
23468fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
23478fe96085Stim szeto 			}
23488fe96085Stim szeto 			break;
23498fe96085Stim szeto 		case STMF_LU_PROP_VID:
23508fe96085Stim szeto 			if (diskLu->vidValid == B_FALSE) {
23518fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
23528fe96085Stim szeto 			}
23538fe96085Stim szeto 			if (*propLen <= sizeof (diskLu->vid)) {
23548fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
23558fe96085Stim szeto 			}
23568fe96085Stim szeto 			bcopy(diskLu->vid, propVal, sizeof (diskLu->vid));
23578fe96085Stim szeto 			propVal[sizeof (diskLu->vid)] = 0;
23588fe96085Stim szeto 			break;
23598fe96085Stim szeto 		case STMF_LU_PROP_PID:
23608fe96085Stim szeto 			if (diskLu->pidValid == B_FALSE) {
23618fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
23628fe96085Stim szeto 			}
23638fe96085Stim szeto 			if (*propLen <= sizeof (diskLu->pid)) {
23648fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
23658fe96085Stim szeto 			}
23668fe96085Stim szeto 			bcopy(diskLu->pid, propVal, sizeof (diskLu->pid));
23678fe96085Stim szeto 			propVal[sizeof (diskLu->pid)] = 0;
23688fe96085Stim szeto 			break;
23698fe96085Stim szeto 		case STMF_LU_PROP_WRITE_PROTECT:
23708fe96085Stim szeto 			if (diskLu->writeProtectEnableValid == B_FALSE) {
23718fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
23728fe96085Stim szeto 			}
23738fe96085Stim szeto 			if (diskLu->writeProtectEnable) {
23748fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "true",
23758fe96085Stim szeto 				    *propLen)) >= *propLen) {
23768fe96085Stim szeto 					*propLen = reqLen + 1;
23778fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
23788fe96085Stim szeto 				}
23798fe96085Stim szeto 			} else {
23808fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "false",
23818fe96085Stim szeto 				    *propLen)) >= *propLen) {
23828fe96085Stim szeto 					*propLen = reqLen + 1;
23838fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
23848fe96085Stim szeto 				}
23858fe96085Stim szeto 			}
23868fe96085Stim szeto 			break;
23878fe96085Stim szeto 		case STMF_LU_PROP_WRITE_CACHE_DISABLE:
23888fe96085Stim szeto 			if (diskLu->writebackCacheDisableValid == B_FALSE) {
23898fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
23908fe96085Stim szeto 			}
23918fe96085Stim szeto 			if (diskLu->writebackCacheDisable) {
23928fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "true",
23938fe96085Stim szeto 				    *propLen)) >= *propLen) {
23948fe96085Stim szeto 					*propLen = reqLen + 1;
23958fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
23968fe96085Stim szeto 				}
23978fe96085Stim szeto 			} else {
23988fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "false",
23998fe96085Stim szeto 				    *propLen)) >= *propLen) {
24008fe96085Stim szeto 					*propLen = reqLen + 1;
24018fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
24028fe96085Stim szeto 				}
24038fe96085Stim szeto 			}
24048fe96085Stim szeto 			break;
24058fe96085Stim szeto 		default:
24068fe96085Stim szeto 			ret = STMF_ERROR_NO_PROP;
24078fe96085Stim szeto 			break;
24088fe96085Stim szeto 	}
24098fe96085Stim szeto 
24108fe96085Stim szeto 	return (ret);
24118fe96085Stim szeto }
24128fe96085Stim szeto 
24138fe96085Stim szeto /*
24148fe96085Stim szeto  * setDiskProp
24158fe96085Stim szeto  *
24168fe96085Stim szeto  * Purpose: set properties for resource of type disk
24178fe96085Stim szeto  *
24188fe96085Stim szeto  * hdl - allocated luResourceImpl
24198fe96085Stim szeto  * resourceProp - valid resource identifier
24208fe96085Stim szeto  * propVal - valid resource value
24218fe96085Stim szeto  */
24228fe96085Stim szeto static int
24238fe96085Stim szeto setDiskProp(luResourceImpl *hdl, uint32_t resourceProp, const char *propVal)
24248fe96085Stim szeto {
24258fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
24268fe96085Stim szeto 	int i;
24278fe96085Stim szeto 	diskResource *diskLu = hdl->resource;
24288fe96085Stim szeto 	unsigned long long numericProp = 0;
24298fe96085Stim szeto 	char guidProp[LU_ASCII_GUID_SIZE + 1];
24308fe96085Stim szeto 	char ouiProp[OUI_ASCII_SIZE + 1];
24318fe96085Stim szeto 	unsigned int oui[OUI_SIZE];
24328fe96085Stim szeto 	unsigned int guid[LU_GUID_SIZE];
24338fe96085Stim szeto 	int propSize;
24348fe96085Stim szeto 
24358fe96085Stim szeto 
24368fe96085Stim szeto 	if (propVal == NULL) {
24378fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
24388fe96085Stim szeto 	}
24398fe96085Stim szeto 
24408fe96085Stim szeto 	switch (resourceProp) {
24418fe96085Stim szeto 		case STMF_LU_PROP_ALIAS:
24428fe96085Stim szeto 			if (strlcpy(diskLu->luAlias, propVal,
24438fe96085Stim szeto 			    sizeof (diskLu->luAlias)) >=
24448fe96085Stim szeto 			    sizeof (diskLu->luAlias)) {
24458fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
24468fe96085Stim szeto 			}
24478fe96085Stim szeto 			diskLu->luAliasValid = B_TRUE;
24488fe96085Stim szeto 			break;
24498fe96085Stim szeto 		case STMF_LU_PROP_BLOCK_SIZE:
24508fe96085Stim szeto 			(void) sscanf(propVal, "%llu", &numericProp);
24518fe96085Stim szeto 			if (numericProp > UINT16_MAX) {
24528fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
24538fe96085Stim szeto 			}
24548fe96085Stim szeto 			diskLu->blkSize = numericProp;
24558fe96085Stim szeto 			diskLu->blkSizeValid = B_TRUE;
24568fe96085Stim szeto 			break;
24578fe96085Stim szeto 		case STMF_LU_PROP_COMPANY_ID:
24588fe96085Stim szeto 			if ((strlcpy(ouiProp, propVal, sizeof (ouiProp))) >=
24598fe96085Stim szeto 			    sizeof (ouiProp)) {
24608fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24618fe96085Stim szeto 			}
24628fe96085Stim szeto 			if (checkHexUpper(ouiProp) != 0) {
24638fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24648fe96085Stim szeto 			}
24658fe96085Stim szeto 			(void) sscanf(ouiProp, "%2X%2X%2X",
24668fe96085Stim szeto 			    &oui[0], &oui[1], &oui[2]);
24678fe96085Stim szeto 
24688fe96085Stim szeto 			diskLu->companyId = 0;
24698fe96085Stim szeto 			diskLu->companyId += oui[0] << 16;
24708fe96085Stim szeto 			diskLu->companyId += oui[1] << 8;
24718fe96085Stim szeto 			diskLu->companyId += oui[2];
24728fe96085Stim szeto 			diskLu->companyIdValid = B_TRUE;
24738fe96085Stim szeto 			break;
24748fe96085Stim szeto 		case STMF_LU_PROP_GUID:
24758fe96085Stim szeto 			if (strlen(propVal) != LU_ASCII_GUID_SIZE) {
24768fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
24778fe96085Stim szeto 			}
24788fe96085Stim szeto 
24798fe96085Stim szeto 			if ((strlcpy(guidProp, propVal, sizeof (guidProp))) >=
24808fe96085Stim szeto 			    sizeof (guidProp)) {
24818fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24828fe96085Stim szeto 			}
24838fe96085Stim szeto 
24848fe96085Stim szeto 			if (checkHexUpper(guidProp) != 0) {
24858fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24868fe96085Stim szeto 			}
24878fe96085Stim szeto 
24888fe96085Stim szeto 			(void) sscanf(guidProp,
24898fe96085Stim szeto 			    "%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X",
24908fe96085Stim szeto 			    &guid[0], &guid[1], &guid[2], &guid[3], &guid[4],
24918fe96085Stim szeto 			    &guid[5], &guid[6], &guid[7], &guid[8], &guid[9],
24928fe96085Stim szeto 			    &guid[10], &guid[11], &guid[12], &guid[13],
24938fe96085Stim szeto 			    &guid[14], &guid[15]);
24948fe96085Stim szeto 			for (i = 0; i < sizeof (diskLu->luGuid); i++) {
24958fe96085Stim szeto 				diskLu->luGuid[i] = guid[i];
24968fe96085Stim szeto 			}
24978fe96085Stim szeto 			diskLu->luGuidValid = B_TRUE;
24988fe96085Stim szeto 			break;
24998fe96085Stim szeto 		case STMF_LU_PROP_FILENAME:
25008fe96085Stim szeto 			if ((strlcpy(diskLu->luDataFileName, propVal,
25018fe96085Stim szeto 			    sizeof (diskLu->luDataFileName))) >=
25028fe96085Stim szeto 			    sizeof (diskLu->luDataFileName)) {
25038fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
25048fe96085Stim szeto 			}
25058fe96085Stim szeto 			diskLu->luDataFileNameValid = B_TRUE;
25068fe96085Stim szeto 			break;
25078fe96085Stim szeto 		case STMF_LU_PROP_META_FILENAME:
25088fe96085Stim szeto 			if ((strlcpy(diskLu->luMetaFileName, propVal,
25098fe96085Stim szeto 			    sizeof (diskLu->luMetaFileName))) >=
25108fe96085Stim szeto 			    sizeof (diskLu->luMetaFileName)) {
25118fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
25128fe96085Stim szeto 			}
25138fe96085Stim szeto 			diskLu->luMetaFileNameValid = B_TRUE;
25148fe96085Stim szeto 			break;
25152f624233SNattuvetty Bhavyan 		case STMF_LU_PROP_MGMT_URL:
25162f624233SNattuvetty Bhavyan 			if ((strlcpy(diskLu->luMgmtUrl, propVal,
25172f624233SNattuvetty Bhavyan 			    sizeof (diskLu->luMgmtUrl))) >=
25182f624233SNattuvetty Bhavyan 			    sizeof (diskLu->luMgmtUrl)) {
25192f624233SNattuvetty Bhavyan 				return (STMF_ERROR_INVALID_PROPSIZE);
25202f624233SNattuvetty Bhavyan 			}
25212f624233SNattuvetty Bhavyan 			diskLu->luMgmtUrlValid = B_TRUE;
25222f624233SNattuvetty Bhavyan 			break;
25238fe96085Stim szeto 		case STMF_LU_PROP_PID:
25248fe96085Stim szeto 			if ((propSize = strlen(propVal)) >
25258fe96085Stim szeto 			    sizeof (diskLu->pid)) {
25268fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
25278fe96085Stim szeto 			}
25288fe96085Stim szeto 			(void) strncpy(diskLu->pid, propVal, propSize);
25298fe96085Stim szeto 			diskLu->pidValid = B_TRUE;
25308fe96085Stim szeto 			break;
25318fe96085Stim szeto 		case STMF_LU_PROP_SERIAL_NUM:
25328fe96085Stim szeto 			if ((propSize = strlen(propVal)) >
25338fe96085Stim szeto 			    (sizeof (diskLu->serialNum) - 1)) {
25348fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
25358fe96085Stim szeto 			}
25368fe96085Stim szeto 			(void) strncpy(diskLu->serialNum, propVal, propSize);
25378fe96085Stim szeto 			diskLu->serialNumValid = B_TRUE;
25388fe96085Stim szeto 			break;
25398fe96085Stim szeto 		case STMF_LU_PROP_SIZE:
25408fe96085Stim szeto 			if ((niceStrToNum(propVal, &diskLu->luSize) != 0)) {
25418fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
25428fe96085Stim szeto 			}
25438fe96085Stim szeto 			diskLu->luSizeValid = B_TRUE;
25448fe96085Stim szeto 			break;
25458fe96085Stim szeto 		case STMF_LU_PROP_VID:
25468fe96085Stim szeto 			if ((propSize = strlen(propVal)) >
25478fe96085Stim szeto 			    sizeof (diskLu->vid)) {
25488fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
25498fe96085Stim szeto 			}
25508fe96085Stim szeto 			(void) strncpy(diskLu->vid, propVal, propSize);
25518fe96085Stim szeto 			diskLu->vidValid = B_TRUE;
25528fe96085Stim szeto 			break;
25538fe96085Stim szeto 		case STMF_LU_PROP_WRITE_PROTECT:
25548fe96085Stim szeto 			if (strcasecmp(propVal, "TRUE") == 0) {
25558fe96085Stim szeto 				diskLu->writeProtectEnable = B_TRUE;
25568fe96085Stim szeto 			} else if (strcasecmp(propVal, "FALSE") == 0) {
25578fe96085Stim szeto 				diskLu->writeProtectEnable = B_FALSE;
25588fe96085Stim szeto 			} else {
25598fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
25608fe96085Stim szeto 			}
25618fe96085Stim szeto 			diskLu->writeProtectEnableValid = B_TRUE;
25628fe96085Stim szeto 			break;
25638fe96085Stim szeto 		case STMF_LU_PROP_WRITE_CACHE_DISABLE:
25648fe96085Stim szeto 			if (strcasecmp(propVal, "TRUE") == 0) {
25658fe96085Stim szeto 				diskLu->writebackCacheDisable = B_TRUE;
25668fe96085Stim szeto 			} else if (strcasecmp(propVal, "FALSE") == 0) {
25678fe96085Stim szeto 				diskLu->writebackCacheDisable = B_FALSE;
25688fe96085Stim szeto 			} else {
25698fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
25708fe96085Stim szeto 			}
25718fe96085Stim szeto 			diskLu->writebackCacheDisableValid = B_TRUE;
25728fe96085Stim szeto 			break;
25738fe96085Stim szeto 		default:
25748fe96085Stim szeto 			ret = STMF_ERROR_NO_PROP;
25758fe96085Stim szeto 			break;
25768fe96085Stim szeto 	}
25778fe96085Stim szeto 	return (ret);
25788fe96085Stim szeto }
25798fe96085Stim szeto 
25808fe96085Stim szeto static int
25818fe96085Stim szeto checkHexUpper(char *buf)
25828fe96085Stim szeto {
25838fe96085Stim szeto 	int i;
25848fe96085Stim szeto 
25858fe96085Stim szeto 	for (i = 0; i < strlen(buf); i++) {
25868fe96085Stim szeto 		if (isxdigit(buf[i])) {
25878fe96085Stim szeto 			buf[i] = toupper(buf[i]);
25888fe96085Stim szeto 			continue;
25898fe96085Stim szeto 		}
25908fe96085Stim szeto 		return (-1);
25918fe96085Stim szeto 	}
25928fe96085Stim szeto 
25938fe96085Stim szeto 	return (0);
25948fe96085Stim szeto }
25958fe96085Stim szeto 
25968fe96085Stim szeto /*
25978fe96085Stim szeto  * Given a numeric suffix, convert the value into a number of bits that the
25988fe96085Stim szeto  * resulting value must be shifted.
25998fe96085Stim szeto  * Code lifted from libzfs_util.c
26008fe96085Stim szeto  */
26018fe96085Stim szeto static int
26028fe96085Stim szeto strToShift(const char *buf)
26038fe96085Stim szeto {
26048fe96085Stim szeto 	const char *ends = "BKMGTPE";
26058fe96085Stim szeto 	int i;
26068fe96085Stim szeto 
26078fe96085Stim szeto 	if (buf[0] == '\0')
26088fe96085Stim szeto 		return (0);
26098fe96085Stim szeto 
26108fe96085Stim szeto 	for (i = 0; i < strlen(ends); i++) {
26118fe96085Stim szeto 		if (toupper(buf[0]) == ends[i])
26128fe96085Stim szeto 			return (10*i);
26138fe96085Stim szeto 	}
26148fe96085Stim szeto 
26158fe96085Stim szeto 	return (-1);
26168fe96085Stim szeto }
26178fe96085Stim szeto 
26188fe96085Stim szeto int
26198fe96085Stim szeto stmfFreeLuResource(luResource hdl)
26208fe96085Stim szeto {
26218fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
26228fe96085Stim szeto 	if (hdl == NULL) {
26238fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
26248fe96085Stim szeto 	}
26258fe96085Stim szeto 
26268fe96085Stim szeto 	luResourceImpl *hdlImpl = hdl;
26278fe96085Stim szeto 	free(hdlImpl->resource);
26288fe96085Stim szeto 	free(hdlImpl);
26298fe96085Stim szeto 	return (ret);
26308fe96085Stim szeto }
26318fe96085Stim szeto 
26328fe96085Stim szeto /*
26338fe96085Stim szeto  * Convert a string of the form '100G' into a real number. Used when setting
26348fe96085Stim szeto  * the size of a logical unit.
26358fe96085Stim szeto  * Code lifted from libzfs_util.c
26368fe96085Stim szeto  */
26378fe96085Stim szeto static int
26388fe96085Stim szeto niceStrToNum(const char *value, uint64_t *num)
26398fe96085Stim szeto {
26408fe96085Stim szeto 	char *end;
26418fe96085Stim szeto 	int shift;
26428fe96085Stim szeto 
26438fe96085Stim szeto 	*num = 0;
26448fe96085Stim szeto 
26458fe96085Stim szeto 	/* Check to see if this looks like a number.  */
26468fe96085Stim szeto 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
26478fe96085Stim szeto 		return (-1);
26488fe96085Stim szeto 	}
26498fe96085Stim szeto 
26508fe96085Stim szeto 	/* Rely on stroull() to process the numeric portion.  */
26518fe96085Stim szeto 	errno = 0;
26528fe96085Stim szeto 	*num = strtoull(value, &end, 10);
26538fe96085Stim szeto 
26548fe96085Stim szeto 	/*
26558fe96085Stim szeto 	 * Check for ERANGE, which indicates that the value is too large to fit
26568fe96085Stim szeto 	 * in a 64-bit value.
26578fe96085Stim szeto 	 */
26588fe96085Stim szeto 	if (errno == ERANGE) {
26598fe96085Stim szeto 		return (-1);
26608fe96085Stim szeto 	}
26618fe96085Stim szeto 
26628fe96085Stim szeto 	/*
26638fe96085Stim szeto 	 * If we have a decimal value, then do the computation with floating
26648fe96085Stim szeto 	 * point arithmetic.  Otherwise, use standard arithmetic.
26658fe96085Stim szeto 	 */
26668fe96085Stim szeto 	if (*end == '.') {
26678fe96085Stim szeto 		double fval = strtod(value, &end);
26688fe96085Stim szeto 
26698fe96085Stim szeto 		if ((shift = strToShift(end)) == -1) {
26708fe96085Stim szeto 			return (-1);
26718fe96085Stim szeto 		}
26728fe96085Stim szeto 
26738fe96085Stim szeto 		fval *= pow(2, shift);
26748fe96085Stim szeto 
26758fe96085Stim szeto 		if (fval > UINT64_MAX) {
26768fe96085Stim szeto 			return (-1);
26778fe96085Stim szeto 		}
26788fe96085Stim szeto 
26798fe96085Stim szeto 		*num = (uint64_t)fval;
26808fe96085Stim szeto 	} else {
26818fe96085Stim szeto 		if ((shift = strToShift(end)) == -1) {
26828fe96085Stim szeto 			return (-1);
26838fe96085Stim szeto 		}
26848fe96085Stim szeto 
26858fe96085Stim szeto 		/* Check for overflow */
26868fe96085Stim szeto 		if (shift >= 64 || (*num << shift) >> shift != *num) {
26878fe96085Stim szeto 			return (-1);
26888fe96085Stim szeto 		}
26898fe96085Stim szeto 
26908fe96085Stim szeto 		*num <<= shift;
26918fe96085Stim szeto 	}
26928fe96085Stim szeto 
26938fe96085Stim szeto 	return (0);
26948fe96085Stim szeto }
26958fe96085Stim szeto 
26968fe96085Stim szeto /*
2697fcf3ce44SJohn Forte  * stmfCreateTargetGroup
2698fcf3ce44SJohn Forte  *
2699fcf3ce44SJohn Forte  * Purpose: Create a local port group
2700fcf3ce44SJohn Forte  *
2701fcf3ce44SJohn Forte  * targetGroupName - name of local port group to create
2702fcf3ce44SJohn Forte  */
2703fcf3ce44SJohn Forte int
2704fcf3ce44SJohn Forte stmfCreateTargetGroup(stmfGroupName *targetGroupName)
2705fcf3ce44SJohn Forte {
2706fcf3ce44SJohn Forte 	int ret;
2707fcf3ce44SJohn Forte 	int fd;
2708fcf3ce44SJohn Forte 
2709fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
2710fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
2711fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName))) {
2712fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2713fcf3ce44SJohn Forte 	}
2714fcf3ce44SJohn Forte 
2715fcf3ce44SJohn Forte 	/* Check to ensure service exists */
2716fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
2717fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
2718fcf3ce44SJohn Forte 	}
2719fcf3ce44SJohn Forte 
2720fcf3ce44SJohn Forte 	/* call init */
2721fcf3ce44SJohn Forte 	ret = initializeConfig();
2722fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2723fcf3ce44SJohn Forte 		return (ret);
2724fcf3ce44SJohn Forte 	}
2725fcf3ce44SJohn Forte 
2726fcf3ce44SJohn Forte 	/*
2727fcf3ce44SJohn Forte 	 * Open control node for stmf
2728fcf3ce44SJohn Forte 	 */
2729fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
2730fcf3ce44SJohn Forte 		return (ret);
2731fcf3ce44SJohn Forte 
2732fcf3ce44SJohn Forte 	/*
2733fcf3ce44SJohn Forte 	 * Add the group to the driver
2734fcf3ce44SJohn Forte 	 */
2735fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_TARGET_GROUP,
2736fcf3ce44SJohn Forte 	    targetGroupName)) != STMF_STATUS_SUCCESS) {
2737fcf3ce44SJohn Forte 		goto done;
2738fcf3ce44SJohn Forte 	}
2739fcf3ce44SJohn Forte 
27408fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
27418fe96085Stim szeto 		goto done;
27428fe96085Stim szeto 	}
27438fe96085Stim szeto 
2744fcf3ce44SJohn Forte 	/*
2745fcf3ce44SJohn Forte 	 * If the add to the driver was successful, add it to the persistent
2746fcf3ce44SJohn Forte 	 * store.
2747fcf3ce44SJohn Forte 	 */
2748fcf3ce44SJohn Forte 	ret = psCreateTargetGroup((char *)targetGroupName);
2749fcf3ce44SJohn Forte 	switch (ret) {
2750fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
2751fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
2752fcf3ce44SJohn Forte 			break;
2753fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
2754fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
2755fcf3ce44SJohn Forte 			break;
2756fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
2757fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
2758fcf3ce44SJohn Forte 			break;
2759fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2760fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
2761fcf3ce44SJohn Forte 			break;
2762fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
2763fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
2764fcf3ce44SJohn Forte 			break;
2765fcf3ce44SJohn Forte 		default:
2766fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
2767fcf3ce44SJohn Forte 			    "stmfCreateTargetGroup:psCreateTargetGroup"
2768fcf3ce44SJohn Forte 			    ":error(%d)", ret);
2769fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
2770fcf3ce44SJohn Forte 			break;
2771fcf3ce44SJohn Forte 	}
2772fcf3ce44SJohn Forte 
2773fcf3ce44SJohn Forte done:
2774fcf3ce44SJohn Forte 	(void) close(fd);
2775fcf3ce44SJohn Forte 	return (ret);
2776fcf3ce44SJohn Forte }
2777fcf3ce44SJohn Forte 
2778fcf3ce44SJohn Forte /*
2779fcf3ce44SJohn Forte  * stmfDeleteHostGroup
2780fcf3ce44SJohn Forte  *
2781fcf3ce44SJohn Forte  * Purpose: Delete an initiator or local port group
2782fcf3ce44SJohn Forte  *
2783fcf3ce44SJohn Forte  * hostGroupName - group to delete
2784fcf3ce44SJohn Forte  */
2785fcf3ce44SJohn Forte int
2786fcf3ce44SJohn Forte stmfDeleteHostGroup(stmfGroupName *hostGroupName)
2787fcf3ce44SJohn Forte {
2788fcf3ce44SJohn Forte 	int ret;
2789fcf3ce44SJohn Forte 	int fd;
2790fcf3ce44SJohn Forte 
2791fcf3ce44SJohn Forte 	if (hostGroupName == NULL) {
2792fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2793fcf3ce44SJohn Forte 	}
2794fcf3ce44SJohn Forte 
2795fcf3ce44SJohn Forte 	/* Check to ensure service exists */
2796fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
2797fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
2798fcf3ce44SJohn Forte 	}
2799fcf3ce44SJohn Forte 
2800fcf3ce44SJohn Forte 	/* call init */
2801fcf3ce44SJohn Forte 	ret = initializeConfig();
2802fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2803fcf3ce44SJohn Forte 		return (ret);
2804fcf3ce44SJohn Forte 	}
2805fcf3ce44SJohn Forte 
2806fcf3ce44SJohn Forte 	/*
2807fcf3ce44SJohn Forte 	 * Open control node for stmf
2808fcf3ce44SJohn Forte 	 */
2809fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
2810fcf3ce44SJohn Forte 		return (ret);
2811fcf3ce44SJohn Forte 
2812fcf3ce44SJohn Forte 	/*
2813fcf3ce44SJohn Forte 	 * Remove the group from the driver
2814fcf3ce44SJohn Forte 	 */
2815fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_REMOVE_HOST_GROUP,
2816fcf3ce44SJohn Forte 	    hostGroupName)) != STMF_STATUS_SUCCESS) {
2817fcf3ce44SJohn Forte 		goto done;
2818fcf3ce44SJohn Forte 	}
2819fcf3ce44SJohn Forte 
28208fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
28218fe96085Stim szeto 		goto done;
28228fe96085Stim szeto 	}
28238fe96085Stim szeto 
2824fcf3ce44SJohn Forte 	/*
2825fcf3ce44SJohn Forte 	 * If the remove from the driver was successful, remove it from the
2826fcf3ce44SJohn Forte 	 * persistent store.
2827fcf3ce44SJohn Forte 	 */
2828fcf3ce44SJohn Forte 	ret = psDeleteHostGroup((char *)hostGroupName);
2829fcf3ce44SJohn Forte 	switch (ret) {
2830fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
2831fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
2832fcf3ce44SJohn Forte 			break;
2833fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
2834fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
2835fcf3ce44SJohn Forte 			break;
2836fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
2837fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
2838fcf3ce44SJohn Forte 			break;
2839fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2840fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
2841fcf3ce44SJohn Forte 			break;
2842fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
2843fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
2844fcf3ce44SJohn Forte 			break;
2845fcf3ce44SJohn Forte 		default:
2846fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
2847fcf3ce44SJohn Forte 			    "stmfDeleteHostGroup:psDeleteHostGroup:error(%d)",
2848fcf3ce44SJohn Forte 			    ret);
2849fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
2850fcf3ce44SJohn Forte 			break;
2851fcf3ce44SJohn Forte 	}
2852fcf3ce44SJohn Forte 
2853fcf3ce44SJohn Forte done:
2854fcf3ce44SJohn Forte 	(void) close(fd);
2855fcf3ce44SJohn Forte 	return (ret);
2856fcf3ce44SJohn Forte }
2857fcf3ce44SJohn Forte 
2858fcf3ce44SJohn Forte /*
2859fcf3ce44SJohn Forte  * stmfDeleteTargetGroup
2860fcf3ce44SJohn Forte  *
2861fcf3ce44SJohn Forte  * Purpose: Delete an initiator or local port group
2862fcf3ce44SJohn Forte  *
2863fcf3ce44SJohn Forte  * targetGroupName - group to delete
2864fcf3ce44SJohn Forte  */
2865fcf3ce44SJohn Forte int
2866fcf3ce44SJohn Forte stmfDeleteTargetGroup(stmfGroupName *targetGroupName)
2867fcf3ce44SJohn Forte {
2868fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
2869fcf3ce44SJohn Forte 	int fd;
2870fcf3ce44SJohn Forte 
2871fcf3ce44SJohn Forte 	if (targetGroupName == NULL) {
2872fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2873fcf3ce44SJohn Forte 	}
2874fcf3ce44SJohn Forte 
2875fcf3ce44SJohn Forte 	/* Check to ensure service exists */
2876fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
2877fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
2878fcf3ce44SJohn Forte 	}
2879fcf3ce44SJohn Forte 
2880fcf3ce44SJohn Forte 	/* call init */
2881fcf3ce44SJohn Forte 	ret = initializeConfig();
2882fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2883fcf3ce44SJohn Forte 		return (ret);
2884fcf3ce44SJohn Forte 	}
2885fcf3ce44SJohn Forte 
2886fcf3ce44SJohn Forte 	/*
2887fcf3ce44SJohn Forte 	 * Open control node for stmf
2888fcf3ce44SJohn Forte 	 */
2889fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
2890fcf3ce44SJohn Forte 		return (ret);
2891fcf3ce44SJohn Forte 
2892fcf3ce44SJohn Forte 	/*
2893fcf3ce44SJohn Forte 	 * Remove the group from the driver
2894fcf3ce44SJohn Forte 	 */
2895fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_REMOVE_TARGET_GROUP,
2896fcf3ce44SJohn Forte 	    targetGroupName)) != STMF_STATUS_SUCCESS) {
2897fcf3ce44SJohn Forte 		goto done;
2898fcf3ce44SJohn Forte 	}
2899fcf3ce44SJohn Forte 
29008fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
29018fe96085Stim szeto 		goto done;
29028fe96085Stim szeto 	}
29038fe96085Stim szeto 
2904fcf3ce44SJohn Forte 	/*
2905fcf3ce44SJohn Forte 	 * If the remove from the driver was successful, remove it from the
2906fcf3ce44SJohn Forte 	 * persistent store.
2907fcf3ce44SJohn Forte 	 */
2908fcf3ce44SJohn Forte 	ret = psDeleteTargetGroup((char *)targetGroupName);
2909fcf3ce44SJohn Forte 	switch (ret) {
2910fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
2911fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
2912fcf3ce44SJohn Forte 			break;
2913fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
2914fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
2915fcf3ce44SJohn Forte 			break;
2916fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
2917fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
2918fcf3ce44SJohn Forte 			break;
2919fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2920fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
2921fcf3ce44SJohn Forte 			break;
2922fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
2923fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
2924fcf3ce44SJohn Forte 			break;
2925fcf3ce44SJohn Forte 		default:
2926fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
2927fcf3ce44SJohn Forte 			    "stmfDeleteTargetGroup:psDeleteTargetGroup"
2928fcf3ce44SJohn Forte 			    ":error(%d)", ret);
2929fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
2930fcf3ce44SJohn Forte 			break;
2931fcf3ce44SJohn Forte 	}
2932fcf3ce44SJohn Forte 
2933fcf3ce44SJohn Forte done:
2934fcf3ce44SJohn Forte 	(void) close(fd);
2935fcf3ce44SJohn Forte 	return (ret);
2936fcf3ce44SJohn Forte }
2937fcf3ce44SJohn Forte 
2938fcf3ce44SJohn Forte /*
2939fcf3ce44SJohn Forte  * stmfDevidFromIscsiName
2940fcf3ce44SJohn Forte  *
2941fcf3ce44SJohn Forte  * Purpose: convert an iSCSI name to an stmf devid
2942fcf3ce44SJohn Forte  *
2943fcf3ce44SJohn Forte  * iscsiName - unicode nul terminated utf-8 encoded iSCSI name
2944fcf3ce44SJohn Forte  * devid - on success, contains the converted iscsi name
2945fcf3ce44SJohn Forte  */
2946fcf3ce44SJohn Forte int
2947fcf3ce44SJohn Forte stmfDevidFromIscsiName(char *iscsiName, stmfDevid *devid)
2948fcf3ce44SJohn Forte {
2949fcf3ce44SJohn Forte 	if (devid == NULL || iscsiName == NULL)
2950fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2951fcf3ce44SJohn Forte 
2952fcf3ce44SJohn Forte 	bzero(devid, sizeof (stmfDevid));
2953fcf3ce44SJohn Forte 
2954fcf3ce44SJohn Forte 	/* Validate size of target */
2955fcf3ce44SJohn Forte 	if ((devid->identLength = strlen(iscsiName)) > MAX_ISCSI_NAME ||
2956fcf3ce44SJohn Forte 	    devid->identLength < strlen(EUI) ||
2957fcf3ce44SJohn Forte 	    devid->identLength < strlen(IQN)) {
2958fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2959fcf3ce44SJohn Forte 	}
2960fcf3ce44SJohn Forte 
2961fcf3ce44SJohn Forte 	if ((strncmp(iscsiName, EUI, strlen(EUI)) != 0) &&
2962fcf3ce44SJohn Forte 	    strncmp(iscsiName, IQN, strlen(IQN)) != 0) {
2963fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2964fcf3ce44SJohn Forte 	}
2965fcf3ce44SJohn Forte 
2966fcf3ce44SJohn Forte 	/* copy UTF-8 bytes to ident */
2967fcf3ce44SJohn Forte 	bcopy(iscsiName, devid->ident, devid->identLength);
2968fcf3ce44SJohn Forte 
2969fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
2970fcf3ce44SJohn Forte }
2971fcf3ce44SJohn Forte 
2972fcf3ce44SJohn Forte /*
2973fcf3ce44SJohn Forte  * stmfDevidFromWwn
2974fcf3ce44SJohn Forte  *
2975fcf3ce44SJohn Forte  * Purpose: convert a WWN to an stmf devid
2976fcf3ce44SJohn Forte  *
2977fcf3ce44SJohn Forte  * wwn - 8-byte wwn identifier
2978fcf3ce44SJohn Forte  * devid - on success, contains the converted wwn
2979fcf3ce44SJohn Forte  */
2980fcf3ce44SJohn Forte int
2981fcf3ce44SJohn Forte stmfDevidFromWwn(uchar_t *wwn, stmfDevid *devid)
2982fcf3ce44SJohn Forte {
2983fcf3ce44SJohn Forte 	if (wwn == NULL || devid == NULL)
2984fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2985fcf3ce44SJohn Forte 
2986fcf3ce44SJohn Forte 	bzero(devid, sizeof (stmfDevid));
2987fcf3ce44SJohn Forte 
2988fcf3ce44SJohn Forte 	/* Copy eui prefix */
2989fcf3ce44SJohn Forte 	(void) bcopy(WWN, devid->ident, strlen(WWN));
2990fcf3ce44SJohn Forte 
2991fcf3ce44SJohn Forte 	/* Convert to ASCII uppercase hexadecimal string */
2992fcf3ce44SJohn Forte 	(void) snprintf((char *)&devid->ident[strlen(WWN)],
2993fcf3ce44SJohn Forte 	    sizeof (devid->ident), "%02X%02X%02X%02X%02X%02X%02X%02X",
2994fcf3ce44SJohn Forte 	    wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
2995fcf3ce44SJohn Forte 
2996fcf3ce44SJohn Forte 	devid->identLength = strlen((char *)devid->ident);
2997fcf3ce44SJohn Forte 
2998fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
2999fcf3ce44SJohn Forte }
3000fcf3ce44SJohn Forte 
3001fcf3ce44SJohn Forte /*
3002fcf3ce44SJohn Forte  * stmfFreeMemory
3003fcf3ce44SJohn Forte  *
3004fcf3ce44SJohn Forte  * Purpose: Free memory allocated by this library
3005fcf3ce44SJohn Forte  *
3006fcf3ce44SJohn Forte  * memory - previously allocated pointer of memory managed by library
3007fcf3ce44SJohn Forte  */
3008fcf3ce44SJohn Forte void
3009fcf3ce44SJohn Forte stmfFreeMemory(void *memory)
3010fcf3ce44SJohn Forte {
3011fcf3ce44SJohn Forte 	free(memory);
3012fcf3ce44SJohn Forte }
3013fcf3ce44SJohn Forte 
3014fcf3ce44SJohn Forte /*
30158fe96085Stim szeto  * get host group, target group list from stmf
3016fcf3ce44SJohn Forte  *
30178fe96085Stim szeto  * groupType - HOST_GROUP, TARGET_GROUP
3018fcf3ce44SJohn Forte  */
30198fe96085Stim szeto static int
30208fe96085Stim szeto groupListIoctl(stmfGroupList **groupList, int groupType)
3021fcf3ce44SJohn Forte {
3022fcf3ce44SJohn Forte 	int ret;
30238fe96085Stim szeto 	int fd;
30248fe96085Stim szeto 	int ioctlRet;
30258fe96085Stim szeto 	int i;
30268fe96085Stim szeto 	int cmd;
30278fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
30288fe96085Stim szeto 	/* framework group list */
30298fe96085Stim szeto 	stmf_group_name_t *iGroupList = NULL;
30308fe96085Stim szeto 	uint32_t groupListSize;
3031fcf3ce44SJohn Forte 
30328fe96085Stim szeto 	if (groupList == NULL) {
3033fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3034fcf3ce44SJohn Forte 	}
3035fcf3ce44SJohn Forte 
30368fe96085Stim szeto 	if (groupType == HOST_GROUP) {
30378fe96085Stim szeto 		cmd = STMF_IOCTL_GET_HG_LIST;
30388fe96085Stim szeto 	} else if (groupType == TARGET_GROUP) {
30398fe96085Stim szeto 		cmd = STMF_IOCTL_GET_TG_LIST;
30408fe96085Stim szeto 	} else {
30418fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
30428fe96085Stim szeto 	}
30438fe96085Stim szeto 
30448fe96085Stim szeto 	/* call init */
30458fe96085Stim szeto 	ret = initializeConfig();
30468fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
30478fe96085Stim szeto 		return (ret);
30488fe96085Stim szeto 	}
30498fe96085Stim szeto 
30508fe96085Stim szeto 	/*
30518fe96085Stim szeto 	 * Open control node for stmf
30528fe96085Stim szeto 	 */
30538fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
30548fe96085Stim szeto 		return (ret);
30558fe96085Stim szeto 
30568fe96085Stim szeto 	/*
30578fe96085Stim szeto 	 * Allocate ioctl input buffer
30588fe96085Stim szeto 	 */
30598fe96085Stim szeto 	groupListSize = ALLOC_GROUP;
30608fe96085Stim szeto 	groupListSize = groupListSize * (sizeof (stmf_group_name_t));
30618fe96085Stim szeto 	iGroupList = (stmf_group_name_t *)calloc(1, groupListSize);
30628fe96085Stim szeto 	if (iGroupList == NULL) {
30638fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
30648fe96085Stim szeto 		goto done;
30658fe96085Stim szeto 	}
30668fe96085Stim szeto 
30678fe96085Stim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
30688fe96085Stim szeto 	/*
30698fe96085Stim szeto 	 * Issue ioctl to get the group list
30708fe96085Stim szeto 	 */
30718fe96085Stim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
30728fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = groupListSize;
30738fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupList;
30748fe96085Stim szeto 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
30758fe96085Stim szeto 	if (ioctlRet != 0) {
30768fe96085Stim szeto 		switch (errno) {
30778fe96085Stim szeto 			case EBUSY:
30788fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
30798fe96085Stim szeto 				break;
30808fe96085Stim szeto 			case EPERM:
30818fe96085Stim szeto 			case EACCES:
30828fe96085Stim szeto 				ret = STMF_ERROR_PERM;
30838fe96085Stim szeto 				break;
30848fe96085Stim szeto 			default:
30858fe96085Stim szeto 				syslog(LOG_DEBUG,
30868fe96085Stim szeto 				    "groupListIoctl:ioctl errno(%d)",
30878fe96085Stim szeto 				    errno);
30888fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
30898fe96085Stim szeto 				break;
30908fe96085Stim szeto 		}
30918fe96085Stim szeto 		goto done;
30928fe96085Stim szeto 	}
30938fe96085Stim szeto 	/*
30948fe96085Stim szeto 	 * Check whether input buffer was large enough
30958fe96085Stim szeto 	 */
30968fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_GROUP) {
30978fe96085Stim szeto 		groupListSize = stmfIoctl.stmf_obuf_max_nentries *
30988fe96085Stim szeto 		    sizeof (stmf_group_name_t);
30998fe96085Stim szeto 		iGroupList = realloc(iGroupList, groupListSize);
31008fe96085Stim szeto 		if (iGroupList == NULL) {
31018fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
31028fe96085Stim szeto 			goto done;
31038fe96085Stim szeto 		}
31048fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = groupListSize;
31058fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupList;
31068fe96085Stim szeto 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
31078fe96085Stim szeto 		if (ioctlRet != 0) {
31088fe96085Stim szeto 			switch (errno) {
31098fe96085Stim szeto 				case EBUSY:
31108fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
31118fe96085Stim szeto 					break;
31128fe96085Stim szeto 				case EPERM:
31138fe96085Stim szeto 				case EACCES:
31148fe96085Stim szeto 					ret = STMF_ERROR_PERM;
31158fe96085Stim szeto 					break;
31168fe96085Stim szeto 				default:
31178fe96085Stim szeto 					syslog(LOG_DEBUG,
31188fe96085Stim szeto 					    "groupListIoctl:ioctl errno(%d)",
31198fe96085Stim szeto 					    errno);
31208fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
31218fe96085Stim szeto 					break;
31228fe96085Stim szeto 			}
31238fe96085Stim szeto 			goto done;
31248fe96085Stim szeto 		}
31258fe96085Stim szeto 	}
31268fe96085Stim szeto 
31278fe96085Stim szeto 	/* allocate and copy to caller's buffer */
31284f2997b2Ssrivijitha dugganapalli 	*groupList = (stmfGroupList *)calloc(1, sizeof (stmfGroupList) +
31294f2997b2Ssrivijitha dugganapalli 	    sizeof (stmfGroupName) * stmfIoctl.stmf_obuf_nentries);
31308fe96085Stim szeto 	if (*groupList == NULL) {
31318fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
31328fe96085Stim szeto 		goto done;
31338fe96085Stim szeto 	}
31348fe96085Stim szeto 	(*groupList)->cnt = stmfIoctl.stmf_obuf_nentries;
31358fe96085Stim szeto 	for (i = 0; i < stmfIoctl.stmf_obuf_nentries; i++) {
31362f624233SNattuvetty Bhavyan 		bcopy(iGroupList[i].name, (*groupList)->name[i],
31378fe96085Stim szeto 		    sizeof (stmfGroupName));
31388fe96085Stim szeto 	}
31398fe96085Stim szeto 
31408fe96085Stim szeto done:
31418fe96085Stim szeto 	free(iGroupList);
31428fe96085Stim szeto 	(void) close(fd);
31438fe96085Stim szeto 	return (ret);
31448fe96085Stim szeto }
31458fe96085Stim szeto 
31468fe96085Stim szeto /*
31478fe96085Stim szeto  * get host group members, target group members from stmf
31488fe96085Stim szeto  *
31498fe96085Stim szeto  * groupProps - allocated on success
31508fe96085Stim szeto  *
31518fe96085Stim szeto  * groupType - HOST_GROUP, TARGET_GROUP
31528fe96085Stim szeto  */
31538fe96085Stim szeto static int
31548fe96085Stim szeto groupMemberListIoctl(stmfGroupName *groupName, stmfGroupProperties **groupProps,
31558fe96085Stim szeto     int groupType)
31568fe96085Stim szeto {
31578fe96085Stim szeto 	int ret;
31588fe96085Stim szeto 	int fd;
31598fe96085Stim szeto 	int ioctlRet;
31608fe96085Stim szeto 	int i;
31618fe96085Stim szeto 	int cmd;
31628fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
31638fe96085Stim szeto 	/* framework group list */
31648fe96085Stim szeto 	stmf_group_name_t iGroupName;
31658fe96085Stim szeto 	stmf_ge_ident_t *iGroupMembers;
31668fe96085Stim szeto 	uint32_t groupListSize;
31678fe96085Stim szeto 
31688fe96085Stim szeto 	if (groupName == NULL) {
31698fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
31708fe96085Stim szeto 	}
31718fe96085Stim szeto 
31728fe96085Stim szeto 	if (groupType == HOST_GROUP) {
31738fe96085Stim szeto 		cmd = STMF_IOCTL_GET_HG_ENTRIES;
31748fe96085Stim szeto 	} else if (groupType == TARGET_GROUP) {
31758fe96085Stim szeto 		cmd = STMF_IOCTL_GET_TG_ENTRIES;
31768fe96085Stim szeto 	} else {
31778fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
31788fe96085Stim szeto 	}
31798fe96085Stim szeto 
31808fe96085Stim szeto 	/* call init */
31818fe96085Stim szeto 	ret = initializeConfig();
31828fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
31838fe96085Stim szeto 		return (ret);
31848fe96085Stim szeto 	}
31858fe96085Stim szeto 
31868fe96085Stim szeto 	/*
31878fe96085Stim szeto 	 * Open control node for stmf
31888fe96085Stim szeto 	 */
31898fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
31908fe96085Stim szeto 		return (ret);
31918fe96085Stim szeto 
31928fe96085Stim szeto 	bzero(&iGroupName, sizeof (iGroupName));
31938fe96085Stim szeto 
31948fe96085Stim szeto 	bcopy(groupName, &iGroupName.name, strlen((char *)groupName));
31958fe96085Stim szeto 
31968fe96085Stim szeto 	iGroupName.name_size = strlen((char *)groupName);
31978fe96085Stim szeto 
31988fe96085Stim szeto 	/*
31998fe96085Stim szeto 	 * Allocate ioctl input buffer
32008fe96085Stim szeto 	 */
32018fe96085Stim szeto 	groupListSize = ALLOC_GRP_MEMBER;
32028fe96085Stim szeto 	groupListSize = groupListSize * (sizeof (stmf_ge_ident_t));
32038fe96085Stim szeto 	iGroupMembers = (stmf_ge_ident_t *)calloc(1, groupListSize);
32048fe96085Stim szeto 	if (iGroupMembers == NULL) {
32058fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
32068fe96085Stim szeto 		goto done;
32078fe96085Stim szeto 	}
32088fe96085Stim szeto 
32098fe96085Stim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
32108fe96085Stim szeto 	/*
32118fe96085Stim szeto 	 * Issue ioctl to get the group list
32128fe96085Stim szeto 	 */
32138fe96085Stim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
32148fe96085Stim szeto 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&iGroupName;
32158fe96085Stim szeto 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_group_name_t);
32168fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = groupListSize;
32178fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupMembers;
32188fe96085Stim szeto 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
32198fe96085Stim szeto 	if (ioctlRet != 0) {
32208fe96085Stim szeto 		switch (errno) {
32218fe96085Stim szeto 			case EBUSY:
32228fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
32238fe96085Stim szeto 				break;
32248fe96085Stim szeto 			case EPERM:
32258fe96085Stim szeto 			case EACCES:
32268fe96085Stim szeto 				ret = STMF_ERROR_PERM;
32278fe96085Stim szeto 				break;
32288fe96085Stim szeto 			default:
32298fe96085Stim szeto 				syslog(LOG_DEBUG,
32308fe96085Stim szeto 				    "groupListIoctl:ioctl errno(%d)",
32318fe96085Stim szeto 				    errno);
32328fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
32338fe96085Stim szeto 				break;
32348fe96085Stim szeto 		}
32358fe96085Stim szeto 		goto done;
32368fe96085Stim szeto 	}
32378fe96085Stim szeto 	/*
32388fe96085Stim szeto 	 * Check whether input buffer was large enough
32398fe96085Stim szeto 	 */
32408fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_GRP_MEMBER) {
32418fe96085Stim szeto 		groupListSize = stmfIoctl.stmf_obuf_max_nentries *
32428fe96085Stim szeto 		    sizeof (stmf_ge_ident_t);
32438fe96085Stim szeto 		iGroupMembers = realloc(iGroupMembers, groupListSize);
32448fe96085Stim szeto 		if (iGroupMembers == NULL) {
32458fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
32468fe96085Stim szeto 			goto done;
32478fe96085Stim szeto 		}
32488fe96085Stim szeto 		stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&iGroupName;
32498fe96085Stim szeto 		stmfIoctl.stmf_ibuf_size = sizeof (stmf_group_name_t);
32508fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = groupListSize;
32518fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupMembers;
32528fe96085Stim szeto 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
32538fe96085Stim szeto 		if (ioctlRet != 0) {
32548fe96085Stim szeto 			switch (errno) {
32558fe96085Stim szeto 				case EBUSY:
32568fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
32578fe96085Stim szeto 					break;
32588fe96085Stim szeto 				case EPERM:
32598fe96085Stim szeto 				case EACCES:
32608fe96085Stim szeto 					ret = STMF_ERROR_PERM;
32618fe96085Stim szeto 					break;
32628fe96085Stim szeto 				default:
32638fe96085Stim szeto 					syslog(LOG_DEBUG,
32648fe96085Stim szeto 					    "groupListIoctl:ioctl errno(%d)",
32658fe96085Stim szeto 					    errno);
32668fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
32678fe96085Stim szeto 					break;
32688fe96085Stim szeto 			}
32698fe96085Stim szeto 			goto done;
32708fe96085Stim szeto 		}
32718fe96085Stim szeto 	}
32728fe96085Stim szeto 
32738fe96085Stim szeto 	/* allocate and copy to caller's buffer */
32748fe96085Stim szeto 	*groupProps = (stmfGroupProperties *)calloc(1,
32754f2997b2Ssrivijitha dugganapalli 	    sizeof (stmfGroupProperties) +
32764f2997b2Ssrivijitha dugganapalli 	    sizeof (stmfDevid) * stmfIoctl.stmf_obuf_nentries);
32778fe96085Stim szeto 	if (*groupProps == NULL) {
32788fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
32798fe96085Stim szeto 		goto done;
32808fe96085Stim szeto 	}
32818fe96085Stim szeto 	(*groupProps)->cnt = stmfIoctl.stmf_obuf_nentries;
32828fe96085Stim szeto 	for (i = 0; i < stmfIoctl.stmf_obuf_nentries; i++) {
32838fe96085Stim szeto 		(*groupProps)->name[i].identLength =
32842f624233SNattuvetty Bhavyan 		    iGroupMembers[i].ident_size;
32852f624233SNattuvetty Bhavyan 		bcopy(iGroupMembers[i].ident, (*groupProps)->name[i].ident,
32862f624233SNattuvetty Bhavyan 		    iGroupMembers[i].ident_size);
32878fe96085Stim szeto 	}
32888fe96085Stim szeto 
32898fe96085Stim szeto done:
32908fe96085Stim szeto 	free(iGroupMembers);
32918fe96085Stim szeto 	(void) close(fd);
32928fe96085Stim szeto 	return (ret);
32938fe96085Stim szeto }
32948fe96085Stim szeto 
32958fe96085Stim szeto /*
32968fe96085Stim szeto  * Purpose: access persistent config data for host groups and target groups
32978fe96085Stim szeto  */
32988fe96085Stim szeto static int
32998fe96085Stim szeto iLoadGroupFromPs(stmfGroupList **groupList, int type)
33008fe96085Stim szeto {
33018fe96085Stim szeto 	int ret;
33028fe96085Stim szeto 
33038fe96085Stim szeto 	if (groupList == NULL) {
33048fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
33058fe96085Stim szeto 	}
33068fe96085Stim szeto 
33078fe96085Stim szeto 	if (type == HOST_GROUP) {
33088fe96085Stim szeto 		ret = psGetHostGroupList(groupList);
33098fe96085Stim szeto 	} else if (type == TARGET_GROUP) {
33108fe96085Stim szeto 		ret = psGetTargetGroupList(groupList);
33118fe96085Stim szeto 	} else {
33128fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
33138fe96085Stim szeto 	}
3314fcf3ce44SJohn Forte 	switch (ret) {
3315fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3316fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3317fcf3ce44SJohn Forte 			break;
3318fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3319fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3320fcf3ce44SJohn Forte 			break;
3321fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3322fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3323fcf3ce44SJohn Forte 			break;
3324fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3325fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3326fcf3ce44SJohn Forte 			break;
3327fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3328fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3329fcf3ce44SJohn Forte 			break;
3330fcf3ce44SJohn Forte 		default:
3331fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3332fcf3ce44SJohn Forte 			    "stmfGetHostGroupList:psGetHostGroupList:error(%d)",
3333fcf3ce44SJohn Forte 			    ret);
3334fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3335fcf3ce44SJohn Forte 			break;
3336fcf3ce44SJohn Forte 	}
3337fcf3ce44SJohn Forte 
3338fcf3ce44SJohn Forte 	return (ret);
3339fcf3ce44SJohn Forte }
3340fcf3ce44SJohn Forte 
3341fcf3ce44SJohn Forte /*
33428fe96085Stim szeto  * stmfGetHostGroupList
3343fcf3ce44SJohn Forte  *
33448fe96085Stim szeto  * Purpose: Retrieves the list of initiator group oids
3345fcf3ce44SJohn Forte  *
33468fe96085Stim szeto  * hostGroupList - pointer to pointer to hostGroupList structure
33478fe96085Stim szeto  *                 on success, this contains the host group list.
3348fcf3ce44SJohn Forte  */
3349fcf3ce44SJohn Forte int
33508fe96085Stim szeto stmfGetHostGroupList(stmfGroupList **hostGroupList)
3351fcf3ce44SJohn Forte {
33528fe96085Stim szeto 	int ret = STMF_STATUS_ERROR;
3353fcf3ce44SJohn Forte 
33548fe96085Stim szeto 	if (hostGroupList == NULL) {
3355fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3356fcf3ce44SJohn Forte 	}
3357fcf3ce44SJohn Forte 
33588fe96085Stim szeto 	ret = groupListIoctl(hostGroupList, HOST_GROUP);
33598fe96085Stim szeto 	return (ret);
33608fe96085Stim szeto }
33618fe96085Stim szeto 
33628fe96085Stim szeto 
33638fe96085Stim szeto /*
33648fe96085Stim szeto  * Purpose: access persistent config data for host groups and target groups
33658fe96085Stim szeto  */
33668fe96085Stim szeto static int
33678fe96085Stim szeto iLoadGroupMembersFromPs(stmfGroupName *groupName,
33688fe96085Stim szeto     stmfGroupProperties **groupProp, int type)
33698fe96085Stim szeto {
33708fe96085Stim szeto 	int ret;
33718fe96085Stim szeto 
33728fe96085Stim szeto 	if (groupName == NULL) {
33738fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
33748fe96085Stim szeto 	}
33758fe96085Stim szeto 
33768fe96085Stim szeto 	if (type == HOST_GROUP) {
3377fcf3ce44SJohn Forte 		ret = psGetHostGroupMemberList((char *)groupName, groupProp);
33788fe96085Stim szeto 	} else if (type == TARGET_GROUP) {
33798fe96085Stim szeto 		ret = psGetTargetGroupMemberList((char *)groupName, groupProp);
33808fe96085Stim szeto 	} else {
33818fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
33828fe96085Stim szeto 	}
3383fcf3ce44SJohn Forte 	switch (ret) {
3384fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3385fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3386fcf3ce44SJohn Forte 			break;
3387fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3388fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3389fcf3ce44SJohn Forte 			break;
3390fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3391fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3392fcf3ce44SJohn Forte 			break;
3393fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3394fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3395fcf3ce44SJohn Forte 			break;
3396fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3397fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3398fcf3ce44SJohn Forte 			break;
3399fcf3ce44SJohn Forte 		default:
3400fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
34018fe96085Stim szeto 			    "iLoadGroupMembersFromPs:psGetHostGroupList:"
34028fe96085Stim szeto 			    "error(%d)", ret);
3403fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3404fcf3ce44SJohn Forte 			break;
3405fcf3ce44SJohn Forte 	}
3406fcf3ce44SJohn Forte 
3407fcf3ce44SJohn Forte 	return (ret);
3408fcf3ce44SJohn Forte }
3409fcf3ce44SJohn Forte 
3410fcf3ce44SJohn Forte /*
34118fe96085Stim szeto  * stmfGetHostGroupMembers
34128fe96085Stim szeto  *
34138fe96085Stim szeto  * Purpose: Retrieves the group properties for a host group
34148fe96085Stim szeto  *
34158fe96085Stim szeto  * groupName - name of group for which to retrieve host group members.
34168fe96085Stim szeto  * groupProp - pointer to pointer to stmfGroupProperties structure
34178fe96085Stim szeto  *             on success, this contains the list of group members.
34188fe96085Stim szeto  */
34198fe96085Stim szeto int
34208fe96085Stim szeto stmfGetHostGroupMembers(stmfGroupName *groupName,
34218fe96085Stim szeto     stmfGroupProperties **groupProp)
34228fe96085Stim szeto {
34238fe96085Stim szeto 	int ret;
34248fe96085Stim szeto 
34258fe96085Stim szeto 	if (groupName == NULL || groupProp == NULL) {
34268fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
34278fe96085Stim szeto 	}
34288fe96085Stim szeto 
34298fe96085Stim szeto 	ret = groupMemberListIoctl(groupName, groupProp, HOST_GROUP);
34308fe96085Stim szeto 
34318fe96085Stim szeto 	return (ret);
34328fe96085Stim szeto }
34338fe96085Stim szeto 
34348fe96085Stim szeto /*
3435fcf3ce44SJohn Forte  * stmfGetProviderData
3436fcf3ce44SJohn Forte  *
3437fcf3ce44SJohn Forte  * Purpose: Get provider data list
3438fcf3ce44SJohn Forte  *
3439fcf3ce44SJohn Forte  * providerName - name of provider for which to retrieve the data
3440fcf3ce44SJohn Forte  * nvl - pointer to nvlist_t pointer which will contain the nvlist data
3441fcf3ce44SJohn Forte  *       retrieved.
3442fcf3ce44SJohn Forte  * providerType - type of provider for which to retrieve data.
3443fcf3ce44SJohn Forte  *		    STMF_LU_PROVIDER_TYPE
3444fcf3ce44SJohn Forte  *		    STMF_PORT_PROVIDER_TYPE
3445fcf3ce44SJohn Forte  */
3446fcf3ce44SJohn Forte int
3447fcf3ce44SJohn Forte stmfGetProviderData(char *providerName, nvlist_t **nvl, int providerType)
3448fcf3ce44SJohn Forte {
3449fcf3ce44SJohn Forte 	return (stmfGetProviderDataProt(providerName, nvl, providerType,
3450fcf3ce44SJohn Forte 	    NULL));
3451fcf3ce44SJohn Forte }
3452fcf3ce44SJohn Forte 
3453fcf3ce44SJohn Forte /*
3454fcf3ce44SJohn Forte  * stmfGetProviderDataProt
3455fcf3ce44SJohn Forte  *
3456fcf3ce44SJohn Forte  * Purpose: Get provider data list with token
3457fcf3ce44SJohn Forte  *
3458fcf3ce44SJohn Forte  * providerName - name of provider for which to retrieve the data
3459fcf3ce44SJohn Forte  * nvl - pointer to nvlist_t pointer which will contain the nvlist data
3460fcf3ce44SJohn Forte  *       retrieved.
3461fcf3ce44SJohn Forte  * providerType - type of provider for which to retrieve data.
3462fcf3ce44SJohn Forte  *		    STMF_LU_PROVIDER_TYPE
3463fcf3ce44SJohn Forte  *		    STMF_PORT_PROVIDER_TYPE
3464fcf3ce44SJohn Forte  * setToken - Returns the stale data token
3465fcf3ce44SJohn Forte  */
3466fcf3ce44SJohn Forte int
3467fcf3ce44SJohn Forte stmfGetProviderDataProt(char *providerName, nvlist_t **nvl, int providerType,
3468fcf3ce44SJohn Forte     uint64_t *setToken)
3469fcf3ce44SJohn Forte {
3470fcf3ce44SJohn Forte 	int ret;
3471fcf3ce44SJohn Forte 
3472fcf3ce44SJohn Forte 	if (providerName == NULL || nvl == NULL) {
3473fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3474fcf3ce44SJohn Forte 	}
3475fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
3476fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
3477fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3478fcf3ce44SJohn Forte 	}
3479fcf3ce44SJohn Forte 	/* call init */
3480fcf3ce44SJohn Forte 	ret = initializeConfig();
3481fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3482fcf3ce44SJohn Forte 		return (ret);
3483fcf3ce44SJohn Forte 	}
34848fe96085Stim szeto 	return (getProviderData(providerName, nvl, providerType, setToken));
3485fcf3ce44SJohn Forte }
3486fcf3ce44SJohn Forte 
3487fcf3ce44SJohn Forte /*
3488fcf3ce44SJohn Forte  * stmfGetProviderDataList
3489fcf3ce44SJohn Forte  *
3490fcf3ce44SJohn Forte  * Purpose: Get the list of providers currently persisting data
3491fcf3ce44SJohn Forte  *
3492fcf3ce44SJohn Forte  * providerList - pointer to pointer to an stmfProviderList structure allocated
3493fcf3ce44SJohn Forte  *                by the caller. Will contain the list of providers on success.
3494fcf3ce44SJohn Forte  */
3495fcf3ce44SJohn Forte int
3496fcf3ce44SJohn Forte stmfGetProviderDataList(stmfProviderList **providerList)
3497fcf3ce44SJohn Forte {
3498fcf3ce44SJohn Forte 	int ret;
3499fcf3ce44SJohn Forte 
3500fcf3ce44SJohn Forte 	ret = psGetProviderDataList(providerList);
3501fcf3ce44SJohn Forte 	switch (ret) {
3502fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3503fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3504fcf3ce44SJohn Forte 			break;
3505fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3506fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3507fcf3ce44SJohn Forte 			break;
3508fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3509fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3510fcf3ce44SJohn Forte 			break;
3511fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3512fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3513fcf3ce44SJohn Forte 			break;
3514fcf3ce44SJohn Forte 		default:
3515fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3516fcf3ce44SJohn Forte 			    "stmfGetProviderDataList:psGetProviderDataList"
3517fcf3ce44SJohn Forte 			    ":error(%d)", ret);
3518fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3519fcf3ce44SJohn Forte 			break;
3520fcf3ce44SJohn Forte 	}
3521fcf3ce44SJohn Forte 
3522fcf3ce44SJohn Forte 	return (ret);
3523fcf3ce44SJohn Forte }
3524fcf3ce44SJohn Forte 
3525fcf3ce44SJohn Forte 
3526fcf3ce44SJohn Forte /*
3527fcf3ce44SJohn Forte  * stmfGetSessionList
3528fcf3ce44SJohn Forte  *
3529fcf3ce44SJohn Forte  * Purpose: Retrieves the session list for a target (devid)
3530fcf3ce44SJohn Forte  *
3531fcf3ce44SJohn Forte  * devid - devid of target for which to retrieve session information.
3532fcf3ce44SJohn Forte  * sessionList - pointer to pointer to stmfSessionList structure
3533fcf3ce44SJohn Forte  *             on success, this contains the list of initiator sessions.
3534fcf3ce44SJohn Forte  */
3535fcf3ce44SJohn Forte int
3536fcf3ce44SJohn Forte stmfGetSessionList(stmfDevid *devid, stmfSessionList **sessionList)
3537fcf3ce44SJohn Forte {
3538fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
3539fcf3ce44SJohn Forte 	int fd;
3540fcf3ce44SJohn Forte 	int ioctlRet;
3541fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_SESSION_LIST;
3542fcf3ce44SJohn Forte 	int i;
3543fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
354456261083SCharles Ting 	slist_scsi_session_t *fSessionList, *fSessionListP = NULL;
3545fcf3ce44SJohn Forte 	uint8_t ident[260];
3546fcf3ce44SJohn Forte 	uint32_t fSessionListSize;
3547fcf3ce44SJohn Forte 
3548fcf3ce44SJohn Forte 	if (sessionList == NULL || devid == NULL) {
3549fcf3ce44SJohn Forte 		ret = STMF_ERROR_INVALID_ARG;
3550fcf3ce44SJohn Forte 	}
3551fcf3ce44SJohn Forte 
3552fcf3ce44SJohn Forte 	/* call init */
3553fcf3ce44SJohn Forte 	ret = initializeConfig();
3554fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3555fcf3ce44SJohn Forte 		return (ret);
3556fcf3ce44SJohn Forte 	}
3557fcf3ce44SJohn Forte 
3558fcf3ce44SJohn Forte 	/*
3559fcf3ce44SJohn Forte 	 * Open control node for stmf
3560fcf3ce44SJohn Forte 	 */
3561fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3562fcf3ce44SJohn Forte 		return (ret);
3563fcf3ce44SJohn Forte 
3564fcf3ce44SJohn Forte 	/*
3565fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
3566fcf3ce44SJohn Forte 	 */
35678fe96085Stim szeto 	fSessionListSize = ALLOC_SESSION;
3568fcf3ce44SJohn Forte 	fSessionListSize = fSessionListSize * (sizeof (slist_scsi_session_t));
3569fcf3ce44SJohn Forte 	fSessionList = (slist_scsi_session_t *)calloc(1, fSessionListSize);
357056261083SCharles Ting 	fSessionListP = fSessionList;
3571fcf3ce44SJohn Forte 	if (fSessionList == NULL) {
357256261083SCharles Ting 		ret = STMF_ERROR_NOMEM;
357356261083SCharles Ting 		goto done;
3574fcf3ce44SJohn Forte 	}
3575fcf3ce44SJohn Forte 
3576fcf3ce44SJohn Forte 	ident[IDENT_LENGTH_BYTE] = devid->identLength;
3577fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &ident[IDENT_LENGTH_BYTE + 1],
3578fcf3ce44SJohn Forte 	    devid->identLength);
3579fcf3ce44SJohn Forte 
3580fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
3581fcf3ce44SJohn Forte 	/*
3582fcf3ce44SJohn Forte 	 * Issue ioctl to get the session list
3583fcf3ce44SJohn Forte 	 */
3584fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
3585fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ident;
3586fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ident);
3587fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fSessionListSize;
3588fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fSessionList;
3589fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
3590fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
3591fcf3ce44SJohn Forte 		switch (errno) {
3592fcf3ce44SJohn Forte 			case EBUSY:
3593fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
3594fcf3ce44SJohn Forte 				break;
35958fe96085Stim szeto 			case EPERM:
3596fcf3ce44SJohn Forte 			case EACCES:
3597fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
3598fcf3ce44SJohn Forte 				break;
3599fcf3ce44SJohn Forte 			default:
3600fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
3601fcf3ce44SJohn Forte 				    "stmfGetSessionList:ioctl errno(%d)",
3602fcf3ce44SJohn Forte 				    errno);
3603fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
3604fcf3ce44SJohn Forte 				break;
3605fcf3ce44SJohn Forte 		}
3606fcf3ce44SJohn Forte 		goto done;
3607fcf3ce44SJohn Forte 	}
3608fcf3ce44SJohn Forte 	/*
3609fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
3610fcf3ce44SJohn Forte 	 */
36118fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_SESSION) {
3612fcf3ce44SJohn Forte 		fSessionListSize = stmfIoctl.stmf_obuf_max_nentries *
3613fcf3ce44SJohn Forte 		    sizeof (slist_scsi_session_t);
3614fcf3ce44SJohn Forte 		fSessionList = realloc(fSessionList, fSessionListSize);
3615fcf3ce44SJohn Forte 		if (fSessionList == NULL) {
361656261083SCharles Ting 			ret = STMF_ERROR_NOMEM;
361756261083SCharles Ting 			goto done;
3618fcf3ce44SJohn Forte 		}
361956261083SCharles Ting 		fSessionListP = fSessionList;
3620fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fSessionListSize;
3621fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fSessionList;
3622fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
3623fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
3624fcf3ce44SJohn Forte 			switch (errno) {
3625fcf3ce44SJohn Forte 				case EBUSY:
3626fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
3627fcf3ce44SJohn Forte 					break;
36288fe96085Stim szeto 				case EPERM:
3629fcf3ce44SJohn Forte 				case EACCES:
3630fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
3631fcf3ce44SJohn Forte 					break;
3632fcf3ce44SJohn Forte 				default:
3633fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
3634fcf3ce44SJohn Forte 					    "stmfGetSessionList:ioctl "
3635fcf3ce44SJohn Forte 					    "errno(%d)", errno);
3636fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
3637fcf3ce44SJohn Forte 					break;
3638fcf3ce44SJohn Forte 			}
3639fcf3ce44SJohn Forte 			goto done;
3640fcf3ce44SJohn Forte 		}
3641fcf3ce44SJohn Forte 	}
3642fcf3ce44SJohn Forte 
3643fcf3ce44SJohn Forte 	/*
3644fcf3ce44SJohn Forte 	 * allocate caller's buffer with the final size
3645fcf3ce44SJohn Forte 	 */
3646fcf3ce44SJohn Forte 	*sessionList = (stmfSessionList *)calloc(1, sizeof (stmfSessionList) +
3647fcf3ce44SJohn Forte 	    stmfIoctl.stmf_obuf_max_nentries * sizeof (stmfSession));
3648fcf3ce44SJohn Forte 	if (*sessionList == NULL) {
3649fcf3ce44SJohn Forte 		ret = STMF_ERROR_NOMEM;
3650fcf3ce44SJohn Forte 		free(sessionList);
3651fcf3ce44SJohn Forte 		goto done;
3652fcf3ce44SJohn Forte 	}
3653fcf3ce44SJohn Forte 
3654fcf3ce44SJohn Forte 	(*sessionList)->cnt = stmfIoctl.stmf_obuf_max_nentries;
3655fcf3ce44SJohn Forte 
3656fcf3ce44SJohn Forte 	/*
3657fcf3ce44SJohn Forte 	 * copy session info to caller's buffer
3658fcf3ce44SJohn Forte 	 */
3659fcf3ce44SJohn Forte 	for (i = 0; i < (*sessionList)->cnt; i++) {
3660fcf3ce44SJohn Forte 		(*sessionList)->session[i].initiator.identLength =
3661fcf3ce44SJohn Forte 		    fSessionList->initiator[IDENT_LENGTH_BYTE];
3662fcf3ce44SJohn Forte 		bcopy(&(fSessionList->initiator[IDENT_LENGTH_BYTE + 1]),
3663fcf3ce44SJohn Forte 		    (*sessionList)->session[i].initiator.ident,
3664fcf3ce44SJohn Forte 		    STMF_IDENT_LENGTH);
3665fcf3ce44SJohn Forte 		bcopy(&(fSessionList->alias),
3666fcf3ce44SJohn Forte 		    &((*sessionList)->session[i].alias),
3667fcf3ce44SJohn Forte 		    sizeof ((*sessionList)->session[i].alias));
3668fcf3ce44SJohn Forte 		bcopy(&(fSessionList++->creation_time),
3669fcf3ce44SJohn Forte 		    &((*sessionList)->session[i].creationTime),
3670fcf3ce44SJohn Forte 		    sizeof (time_t));
3671fcf3ce44SJohn Forte 	}
3672fcf3ce44SJohn Forte done:
3673fcf3ce44SJohn Forte 	(void) close(fd);
367456261083SCharles Ting 	free(fSessionListP);
3675fcf3ce44SJohn Forte 	return (ret);
3676fcf3ce44SJohn Forte }
3677fcf3ce44SJohn Forte 
3678fcf3ce44SJohn Forte /*
3679fcf3ce44SJohn Forte  * stmfGetTargetGroupList
3680fcf3ce44SJohn Forte  *
3681fcf3ce44SJohn Forte  * Purpose: Retrieves the list of target groups
3682fcf3ce44SJohn Forte  *
3683fcf3ce44SJohn Forte  * targetGroupList - pointer to a pointer to an stmfGroupList structure. On
3684fcf3ce44SJohn Forte  *		     success, it contains the list of target groups.
3685fcf3ce44SJohn Forte  */
3686fcf3ce44SJohn Forte int
3687fcf3ce44SJohn Forte stmfGetTargetGroupList(stmfGroupList **targetGroupList)
3688fcf3ce44SJohn Forte {
3689fcf3ce44SJohn Forte 	int ret;
3690fcf3ce44SJohn Forte 
3691fcf3ce44SJohn Forte 	if (targetGroupList == NULL) {
3692fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3693fcf3ce44SJohn Forte 	}
3694fcf3ce44SJohn Forte 
36958fe96085Stim szeto 	ret = groupListIoctl(targetGroupList, TARGET_GROUP);
3696fcf3ce44SJohn Forte 	return (ret);
3697fcf3ce44SJohn Forte }
3698fcf3ce44SJohn Forte 
3699fcf3ce44SJohn Forte /*
3700fcf3ce44SJohn Forte  * stmfGetTargetGroupMembers
3701fcf3ce44SJohn Forte  *
3702fcf3ce44SJohn Forte  * Purpose: Retrieves the group members for a target group
3703fcf3ce44SJohn Forte  *
3704fcf3ce44SJohn Forte  * groupName - name of target group for which to retrieve members.
3705fcf3ce44SJohn Forte  * groupProp - pointer to pointer to stmfGroupProperties structure
3706fcf3ce44SJohn Forte  *             on success, this contains the list of group members.
3707fcf3ce44SJohn Forte  */
3708fcf3ce44SJohn Forte int
3709fcf3ce44SJohn Forte stmfGetTargetGroupMembers(stmfGroupName *groupName,
3710fcf3ce44SJohn Forte     stmfGroupProperties **groupProp)
3711fcf3ce44SJohn Forte {
3712fcf3ce44SJohn Forte 	int ret;
3713fcf3ce44SJohn Forte 
3714fcf3ce44SJohn Forte 	if (groupName == NULL || groupProp == NULL) {
3715fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3716fcf3ce44SJohn Forte 	}
3717fcf3ce44SJohn Forte 
37188fe96085Stim szeto 	ret = groupMemberListIoctl(groupName, groupProp, TARGET_GROUP);
3719fcf3ce44SJohn Forte 
3720fcf3ce44SJohn Forte 	return (ret);
3721fcf3ce44SJohn Forte }
3722fcf3ce44SJohn Forte 
3723fcf3ce44SJohn Forte /*
3724fcf3ce44SJohn Forte  * stmfGetTargetList
3725fcf3ce44SJohn Forte  *
3726fcf3ce44SJohn Forte  * Purpose: Retrieves the list of target ports
3727fcf3ce44SJohn Forte  *
3728fcf3ce44SJohn Forte  * targetList - pointer to a pointer to an stmfDevidList structure.
3729fcf3ce44SJohn Forte  *		    On success, it contains the list of local ports (target).
3730fcf3ce44SJohn Forte  */
3731fcf3ce44SJohn Forte int
3732fcf3ce44SJohn Forte stmfGetTargetList(stmfDevidList **targetList)
3733fcf3ce44SJohn Forte {
3734fcf3ce44SJohn Forte 	int ret;
3735fcf3ce44SJohn Forte 	int fd;
3736fcf3ce44SJohn Forte 	int ioctlRet;
3737fcf3ce44SJohn Forte 	int i;
3738fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
3739fcf3ce44SJohn Forte 	/* framework target port list */
37408fe96085Stim szeto 	slist_target_port_t *fTargetList, *fTargetListP = NULL;
3741fcf3ce44SJohn Forte 	uint32_t fTargetListSize;
3742fcf3ce44SJohn Forte 
3743fcf3ce44SJohn Forte 	if (targetList == NULL) {
3744fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3745fcf3ce44SJohn Forte 	}
3746fcf3ce44SJohn Forte 
3747fcf3ce44SJohn Forte 	/* call init */
3748fcf3ce44SJohn Forte 	ret = initializeConfig();
3749fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3750fcf3ce44SJohn Forte 		return (ret);
3751fcf3ce44SJohn Forte 	}
3752fcf3ce44SJohn Forte 
3753fcf3ce44SJohn Forte 	/*
3754fcf3ce44SJohn Forte 	 * Open control node for stmf
3755fcf3ce44SJohn Forte 	 */
3756fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3757fcf3ce44SJohn Forte 		return (ret);
3758fcf3ce44SJohn Forte 
3759fcf3ce44SJohn Forte 	/*
3760fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
3761fcf3ce44SJohn Forte 	 */
37628fe96085Stim szeto 	fTargetListSize = ALLOC_TARGET_PORT * sizeof (slist_target_port_t);
37633e7352aeSJohn Forte 	fTargetListP = fTargetList =
37643e7352aeSJohn Forte 	    (slist_target_port_t *)calloc(1, fTargetListSize);
3765fcf3ce44SJohn Forte 	if (fTargetList == NULL) {
37668fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
3767fcf3ce44SJohn Forte 		goto done;
3768fcf3ce44SJohn Forte 	}
3769fcf3ce44SJohn Forte 
3770fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
3771fcf3ce44SJohn Forte 	/*
37723e7352aeSJohn Forte 	 * Issue ioctl to retrieve target list
3773fcf3ce44SJohn Forte 	 */
3774fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
3775fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fTargetListSize;
3776fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fTargetList;
3777fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_TARGET_PORT_LIST, &stmfIoctl);
3778fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
3779fcf3ce44SJohn Forte 		switch (errno) {
3780fcf3ce44SJohn Forte 			case EBUSY:
3781fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
3782fcf3ce44SJohn Forte 				break;
37838fe96085Stim szeto 			case EPERM:
3784fcf3ce44SJohn Forte 			case EACCES:
3785fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
3786fcf3ce44SJohn Forte 				break;
3787fcf3ce44SJohn Forte 			default:
3788fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
3789fcf3ce44SJohn Forte 				    "stmfGetTargetList:ioctl errno(%d)", errno);
3790fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
3791fcf3ce44SJohn Forte 				break;
3792fcf3ce44SJohn Forte 		}
3793fcf3ce44SJohn Forte 		goto done;
3794fcf3ce44SJohn Forte 	}
3795fcf3ce44SJohn Forte 	/*
3796fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
3797fcf3ce44SJohn Forte 	 */
37988fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_TARGET_PORT) {
3799fcf3ce44SJohn Forte 		fTargetListSize = stmfIoctl.stmf_obuf_max_nentries *
380076602b8dSJohn Forte 		    sizeof (slist_target_port_t);
38013e7352aeSJohn Forte 		fTargetListP = fTargetList =
38023e7352aeSJohn Forte 		    realloc(fTargetList, fTargetListSize);
3803fcf3ce44SJohn Forte 		if (fTargetList == NULL) {
38048fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
38058fe96085Stim szeto 			goto done;
3806fcf3ce44SJohn Forte 		}
3807fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fTargetListSize;
3808fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fTargetList;
3809fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, STMF_IOCTL_TARGET_PORT_LIST,
3810fcf3ce44SJohn Forte 		    &stmfIoctl);
3811fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
3812fcf3ce44SJohn Forte 			switch (errno) {
3813fcf3ce44SJohn Forte 				case EBUSY:
3814fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
3815fcf3ce44SJohn Forte 					break;
38168fe96085Stim szeto 				case EPERM:
3817fcf3ce44SJohn Forte 				case EACCES:
3818fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
3819fcf3ce44SJohn Forte 					break;
3820fcf3ce44SJohn Forte 				default:
3821fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
3822fcf3ce44SJohn Forte 					    "stmfGetTargetList:ioctl errno(%d)",
3823fcf3ce44SJohn Forte 					    errno);
3824fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
3825fcf3ce44SJohn Forte 					break;
3826fcf3ce44SJohn Forte 			}
3827fcf3ce44SJohn Forte 			goto done;
3828fcf3ce44SJohn Forte 		}
3829fcf3ce44SJohn Forte 	}
3830fcf3ce44SJohn Forte 
3831fcf3ce44SJohn Forte 	*targetList = (stmfDevidList *)calloc(1,
3832fcf3ce44SJohn Forte 	    stmfIoctl.stmf_obuf_max_nentries * sizeof (stmfDevid) +
3833fcf3ce44SJohn Forte 	    sizeof (stmfDevidList));
38348fe96085Stim szeto 	if (*targetList == NULL) {
38358fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
38368fe96085Stim szeto 		goto done;
38378fe96085Stim szeto 	}
3838fcf3ce44SJohn Forte 
3839fcf3ce44SJohn Forte 	(*targetList)->cnt = stmfIoctl.stmf_obuf_max_nentries;
3840fcf3ce44SJohn Forte 	for (i = 0; i < stmfIoctl.stmf_obuf_max_nentries; i++, fTargetList++) {
3841fcf3ce44SJohn Forte 		(*targetList)->devid[i].identLength =
3842fcf3ce44SJohn Forte 		    fTargetList->target[IDENT_LENGTH_BYTE];
3843fcf3ce44SJohn Forte 		bcopy(&fTargetList->target[IDENT_LENGTH_BYTE + 1],
3844fcf3ce44SJohn Forte 		    &(*targetList)->devid[i].ident,
3845fcf3ce44SJohn Forte 		    fTargetList->target[IDENT_LENGTH_BYTE]);
3846fcf3ce44SJohn Forte 	}
3847fcf3ce44SJohn Forte 
3848fcf3ce44SJohn Forte done:
3849fcf3ce44SJohn Forte 	(void) close(fd);
38503e7352aeSJohn Forte 	free(fTargetListP);
3851fcf3ce44SJohn Forte 	return (ret);
3852fcf3ce44SJohn Forte }
3853fcf3ce44SJohn Forte 
3854fcf3ce44SJohn Forte /*
3855fcf3ce44SJohn Forte  * stmfGetTargetProperties
3856fcf3ce44SJohn Forte  *
3857fcf3ce44SJohn Forte  * Purpose:  Retrieves the properties for a logical unit
3858fcf3ce44SJohn Forte  *
3859fcf3ce44SJohn Forte  * devid - devid of the target for which to retrieve properties
3860fcf3ce44SJohn Forte  * targetProps - pointer to an stmfTargetProperties structure.
3861fcf3ce44SJohn Forte  *		On success, it contains the target properties for
3862fcf3ce44SJohn Forte  *		the specified devid.
3863fcf3ce44SJohn Forte  */
3864fcf3ce44SJohn Forte int
3865fcf3ce44SJohn Forte stmfGetTargetProperties(stmfDevid *devid, stmfTargetProperties *targetProps)
3866fcf3ce44SJohn Forte {
3867fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
3868fcf3ce44SJohn Forte 	int fd;
3869fcf3ce44SJohn Forte 	int ioctlRet;
3870fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
3871fcf3ce44SJohn Forte 	sioc_target_port_props_t targetProperties;
3872fcf3ce44SJohn Forte 
3873fcf3ce44SJohn Forte 	if (devid == NULL || targetProps == NULL) {
3874fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3875fcf3ce44SJohn Forte 	}
3876fcf3ce44SJohn Forte 
3877fcf3ce44SJohn Forte 	/* call init */
3878fcf3ce44SJohn Forte 	ret = initializeConfig();
3879fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3880fcf3ce44SJohn Forte 		return (ret);
3881fcf3ce44SJohn Forte 	}
3882fcf3ce44SJohn Forte 
3883fcf3ce44SJohn Forte 	/*
3884fcf3ce44SJohn Forte 	 * Open control node for stmf
3885fcf3ce44SJohn Forte 	 */
3886fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3887fcf3ce44SJohn Forte 		return (ret);
3888fcf3ce44SJohn Forte 
3889fcf3ce44SJohn Forte 	targetProperties.tgt_id[IDENT_LENGTH_BYTE] = devid->identLength;
3890fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetProperties.tgt_id[IDENT_LENGTH_BYTE + 1],
3891fcf3ce44SJohn Forte 	    devid->identLength);
3892fcf3ce44SJohn Forte 
3893fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
3894fcf3ce44SJohn Forte 	/*
3895fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
3896fcf3ce44SJohn Forte 	 */
3897fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
3898fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (targetProperties.tgt_id);
3899fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&targetProperties.tgt_id;
3900fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&targetProperties;
3901fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (targetProperties);
3902fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_GET_TARGET_PORT_PROPERTIES,
3903fcf3ce44SJohn Forte 	    &stmfIoctl);
3904fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
3905fcf3ce44SJohn Forte 		switch (errno) {
3906fcf3ce44SJohn Forte 			case EBUSY:
3907fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
3908fcf3ce44SJohn Forte 				break;
39098fe96085Stim szeto 			case EPERM:
3910fcf3ce44SJohn Forte 			case EACCES:
3911fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
3912fcf3ce44SJohn Forte 				break;
3913fcf3ce44SJohn Forte 			case ENOENT:
3914fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
3915fcf3ce44SJohn Forte 				break;
3916fcf3ce44SJohn Forte 			default:
3917fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
3918fcf3ce44SJohn Forte 				    "stmfGetTargetProperties:ioctl errno(%d)",
3919fcf3ce44SJohn Forte 				    errno);
3920fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
3921fcf3ce44SJohn Forte 				break;
3922fcf3ce44SJohn Forte 		}
3923fcf3ce44SJohn Forte 		goto done;
3924fcf3ce44SJohn Forte 	}
3925fcf3ce44SJohn Forte 
3926fcf3ce44SJohn Forte 	bcopy(targetProperties.tgt_provider_name, targetProps->providerName,
3927fcf3ce44SJohn Forte 	    sizeof (targetProperties.tgt_provider_name));
3928fcf3ce44SJohn Forte 	if (targetProperties.tgt_state == STMF_STATE_ONLINE) {
3929fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_ONLINE;
3930fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_OFFLINE) {
3931fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_OFFLINE;
3932fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_ONLINING) {
3933fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_ONLINING;
3934fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_OFFLINING) {
3935fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_OFFLINING;
3936fcf3ce44SJohn Forte 	}
3937fcf3ce44SJohn Forte 	bcopy(targetProperties.tgt_alias, targetProps->alias,
3938fcf3ce44SJohn Forte 	    sizeof (targetProps->alias));
3939fcf3ce44SJohn Forte done:
3940fcf3ce44SJohn Forte 	(void) close(fd);
3941fcf3ce44SJohn Forte 	return (ret);
3942fcf3ce44SJohn Forte }
3943fcf3ce44SJohn Forte 
3944fcf3ce44SJohn Forte /*
3945fcf3ce44SJohn Forte  * stmfGetLogicalUnitList
3946fcf3ce44SJohn Forte  *
3947fcf3ce44SJohn Forte  * Purpose: Retrieves list of logical unit Object IDs
3948fcf3ce44SJohn Forte  *
3949fcf3ce44SJohn Forte  * luList - pointer to a pointer to a stmfGuidList structure. On success,
3950fcf3ce44SJohn Forte  *          it contains the list of logical unit guids.
3951fcf3ce44SJohn Forte  *
3952fcf3ce44SJohn Forte  */
3953fcf3ce44SJohn Forte int
3954fcf3ce44SJohn Forte stmfGetLogicalUnitList(stmfGuidList **luList)
3955fcf3ce44SJohn Forte {
3956fcf3ce44SJohn Forte 	int ret;
3957fcf3ce44SJohn Forte 	int fd;
3958fcf3ce44SJohn Forte 	int ioctlRet;
3959fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_LU_LIST;
39608fe96085Stim szeto 	int i;
3961fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
3962fcf3ce44SJohn Forte 	slist_lu_t *fLuList;
3963fcf3ce44SJohn Forte 	uint32_t fLuListSize;
39648fe96085Stim szeto 	uint32_t listCnt;
3965fcf3ce44SJohn Forte 
3966fcf3ce44SJohn Forte 	if (luList == NULL) {
3967fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3968fcf3ce44SJohn Forte 	}
3969fcf3ce44SJohn Forte 
3970fcf3ce44SJohn Forte 	/* call init */
3971fcf3ce44SJohn Forte 	ret = initializeConfig();
3972fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3973fcf3ce44SJohn Forte 		return (ret);
3974fcf3ce44SJohn Forte 	}
3975fcf3ce44SJohn Forte 
3976fcf3ce44SJohn Forte 	/*
3977fcf3ce44SJohn Forte 	 * Open control node for stmf
3978fcf3ce44SJohn Forte 	 */
3979fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3980fcf3ce44SJohn Forte 		return (ret);
3981fcf3ce44SJohn Forte 
3982fcf3ce44SJohn Forte 	/*
3983fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
3984fcf3ce44SJohn Forte 	 */
39858fe96085Stim szeto 	fLuListSize = ALLOC_LU;
3986fcf3ce44SJohn Forte 	fLuListSize = fLuListSize * (sizeof (slist_lu_t));
3987fcf3ce44SJohn Forte 	fLuList = (slist_lu_t *)calloc(1, fLuListSize);
3988fcf3ce44SJohn Forte 	if (fLuList == NULL) {
39898fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
39908fe96085Stim szeto 		goto done;
3991fcf3ce44SJohn Forte 	}
3992fcf3ce44SJohn Forte 
3993fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
3994fcf3ce44SJohn Forte 	/*
3995fcf3ce44SJohn Forte 	 * Issue ioctl to get the LU list
3996fcf3ce44SJohn Forte 	 */
3997fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
3998fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fLuListSize;
3999fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fLuList;
4000fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4001fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4002fcf3ce44SJohn Forte 		switch (errno) {
4003fcf3ce44SJohn Forte 			case EBUSY:
4004fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4005fcf3ce44SJohn Forte 				break;
40068fe96085Stim szeto 			case EPERM:
4007fcf3ce44SJohn Forte 			case EACCES:
4008fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4009fcf3ce44SJohn Forte 				break;
4010fcf3ce44SJohn Forte 			default:
4011fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4012fcf3ce44SJohn Forte 				    "stmfGetLogicalUnitList:ioctl errno(%d)",
4013fcf3ce44SJohn Forte 				    errno);
4014fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4015fcf3ce44SJohn Forte 				break;
4016fcf3ce44SJohn Forte 		}
4017fcf3ce44SJohn Forte 		goto done;
4018fcf3ce44SJohn Forte 	}
4019fcf3ce44SJohn Forte 	/*
4020fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
4021fcf3ce44SJohn Forte 	 */
40228fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_LU) {
4023fcf3ce44SJohn Forte 		fLuListSize = stmfIoctl.stmf_obuf_max_nentries *
4024fcf3ce44SJohn Forte 		    sizeof (slist_lu_t);
40258fe96085Stim szeto 		free(fLuList);
40268fe96085Stim szeto 		fLuList = (slist_lu_t *)calloc(1, fLuListSize);
4027fcf3ce44SJohn Forte 		if (fLuList == NULL) {
40288fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
40298fe96085Stim szeto 			goto done;
4030fcf3ce44SJohn Forte 		}
4031fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fLuListSize;
4032fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fLuList;
4033fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4034fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
4035fcf3ce44SJohn Forte 			switch (errno) {
4036fcf3ce44SJohn Forte 				case EBUSY:
4037fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
4038fcf3ce44SJohn Forte 					break;
40398fe96085Stim szeto 				case EPERM:
4040fcf3ce44SJohn Forte 				case EACCES:
4041fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
4042fcf3ce44SJohn Forte 					break;
4043fcf3ce44SJohn Forte 				default:
4044fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
4045fcf3ce44SJohn Forte 					    "stmfGetLogicalUnitList:"
4046fcf3ce44SJohn Forte 					    "ioctl errno(%d)", errno);
4047fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
4048fcf3ce44SJohn Forte 					break;
4049fcf3ce44SJohn Forte 			}
4050fcf3ce44SJohn Forte 			goto done;
4051fcf3ce44SJohn Forte 		}
4052fcf3ce44SJohn Forte 	}
4053fcf3ce44SJohn Forte 
4054fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4055fcf3ce44SJohn Forte 		goto done;
4056fcf3ce44SJohn Forte 	}
4057fcf3ce44SJohn Forte 
40588fe96085Stim szeto 	listCnt = stmfIoctl.stmf_obuf_nentries;
4059fcf3ce44SJohn Forte 
4060fcf3ce44SJohn Forte 	/*
4061fcf3ce44SJohn Forte 	 * allocate caller's buffer with the final size
4062fcf3ce44SJohn Forte 	 */
4063fcf3ce44SJohn Forte 	*luList = (stmfGuidList *)calloc(1, sizeof (stmfGuidList) +
40648fe96085Stim szeto 	    listCnt * sizeof (stmfGuid));
4065fcf3ce44SJohn Forte 	if (*luList == NULL) {
4066fcf3ce44SJohn Forte 		ret = STMF_ERROR_NOMEM;
4067fcf3ce44SJohn Forte 		goto done;
4068fcf3ce44SJohn Forte 	}
4069fcf3ce44SJohn Forte 
40708fe96085Stim szeto 	(*luList)->cnt = listCnt;
40718fe96085Stim szeto 
40728fe96085Stim szeto 	/* copy to caller's buffer */
40738fe96085Stim szeto 	for (i = 0; i < listCnt; i++) {
40748fe96085Stim szeto 		bcopy(&fLuList[i].lu_guid, (*luList)->guid[i].guid,
4075fcf3ce44SJohn Forte 		    sizeof (stmfGuid));
4076fcf3ce44SJohn Forte 	}
4077fcf3ce44SJohn Forte 
40788fe96085Stim szeto 	/*
40798fe96085Stim szeto 	 * sort the list. This gives a consistent view across gets
40808fe96085Stim szeto 	 */
40818fe96085Stim szeto 	qsort((void *)&((*luList)->guid[0]), (*luList)->cnt,
40828fe96085Stim szeto 	    sizeof (stmfGuid), guidCompare);
4083fcf3ce44SJohn Forte 
4084fcf3ce44SJohn Forte done:
4085fcf3ce44SJohn Forte 	(void) close(fd);
4086fcf3ce44SJohn Forte 	/*
4087fcf3ce44SJohn Forte 	 * free internal buffers
4088fcf3ce44SJohn Forte 	 */
4089fcf3ce44SJohn Forte 	free(fLuList);
4090fcf3ce44SJohn Forte 	return (ret);
4091fcf3ce44SJohn Forte }
4092fcf3ce44SJohn Forte 
4093fcf3ce44SJohn Forte /*
4094fcf3ce44SJohn Forte  * stmfGetLogicalUnitProperties
4095fcf3ce44SJohn Forte  *
4096fcf3ce44SJohn Forte  * Purpose:  Retrieves the properties for a logical unit
4097fcf3ce44SJohn Forte  *
4098fcf3ce44SJohn Forte  * lu - guid of the logical unit for which to retrieve properties
4099fcf3ce44SJohn Forte  * stmfLuProps - pointer to an stmfLogicalUnitProperties structure. On success,
4100fcf3ce44SJohn Forte  *               it contains the logical unit properties for the specified guid.
4101fcf3ce44SJohn Forte  */
4102fcf3ce44SJohn Forte int
4103fcf3ce44SJohn Forte stmfGetLogicalUnitProperties(stmfGuid *lu, stmfLogicalUnitProperties *luProps)
4104fcf3ce44SJohn Forte {
4105fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4106fcf3ce44SJohn Forte 	int stmfRet;
4107fcf3ce44SJohn Forte 	int fd;
4108fcf3ce44SJohn Forte 	int ioctlRet;
4109fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_GET_LU_PROPERTIES;
4110fcf3ce44SJohn Forte 	stmfViewEntryList *viewEntryList = NULL;
4111fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4112fcf3ce44SJohn Forte 	sioc_lu_props_t fLuProps;
4113fcf3ce44SJohn Forte 
41148fe96085Stim szeto 	if (lu == NULL || luProps == NULL) {
41158fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
4116fcf3ce44SJohn Forte 	}
4117fcf3ce44SJohn Forte 
4118fcf3ce44SJohn Forte 	bzero(luProps, sizeof (stmfLogicalUnitProperties));
4119fcf3ce44SJohn Forte 
4120fcf3ce44SJohn Forte 	/* call init */
4121fcf3ce44SJohn Forte 	ret = initializeConfig();
4122fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4123fcf3ce44SJohn Forte 		return (ret);
4124fcf3ce44SJohn Forte 	}
4125fcf3ce44SJohn Forte 
4126fcf3ce44SJohn Forte 	/*
4127fcf3ce44SJohn Forte 	 * Open control node for stmf
4128fcf3ce44SJohn Forte 	 */
4129fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
4130fcf3ce44SJohn Forte 		return (ret);
4131fcf3ce44SJohn Forte 
4132fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4133fcf3ce44SJohn Forte 	/*
4134fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
4135fcf3ce44SJohn Forte 	 */
4136fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4137fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGuid);
4138fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)lu;
4139fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&fLuProps;
4140fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (fLuProps);
4141fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4142fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4143fcf3ce44SJohn Forte 		switch (errno) {
4144fcf3ce44SJohn Forte 			case EBUSY:
4145fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4146fcf3ce44SJohn Forte 				break;
41478fe96085Stim szeto 			case EPERM:
4148fcf3ce44SJohn Forte 			case EACCES:
4149fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4150fcf3ce44SJohn Forte 				break;
4151fcf3ce44SJohn Forte 			case ENOENT:
4152fcf3ce44SJohn Forte 				stmfRet = stmfGetViewEntryList(lu,
4153fcf3ce44SJohn Forte 				    &viewEntryList);
4154fcf3ce44SJohn Forte 				if (stmfRet == STMF_STATUS_SUCCESS) {
4155fcf3ce44SJohn Forte 					luProps->status =
4156fcf3ce44SJohn Forte 					    STMF_LOGICAL_UNIT_UNREGISTERED;
4157fcf3ce44SJohn Forte 					if (viewEntryList->cnt > 0) {
4158fcf3ce44SJohn Forte 						ret = STMF_STATUS_SUCCESS;
4159fcf3ce44SJohn Forte 					} else {
4160fcf3ce44SJohn Forte 						ret = STMF_ERROR_NOT_FOUND;
4161fcf3ce44SJohn Forte 					}
4162fcf3ce44SJohn Forte 				} else {
4163fcf3ce44SJohn Forte 					ret = STMF_ERROR_NOT_FOUND;
4164fcf3ce44SJohn Forte 				}
4165fcf3ce44SJohn Forte 				stmfFreeMemory(viewEntryList);
4166fcf3ce44SJohn Forte 				break;
4167fcf3ce44SJohn Forte 			default:
4168fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4169fcf3ce44SJohn Forte 				    "stmfGetLogicalUnit:ioctl errno(%d)",
4170fcf3ce44SJohn Forte 				    errno);
4171fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4172fcf3ce44SJohn Forte 				break;
4173fcf3ce44SJohn Forte 		}
4174fcf3ce44SJohn Forte 		goto done;
4175fcf3ce44SJohn Forte 	}
4176fcf3ce44SJohn Forte 
4177fcf3ce44SJohn Forte 	bcopy(fLuProps.lu_provider_name, luProps->providerName,
4178fcf3ce44SJohn Forte 	    sizeof (fLuProps.lu_provider_name));
4179fcf3ce44SJohn Forte 	if (fLuProps.lu_state == STMF_STATE_ONLINE) {
4180fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_ONLINE;
4181fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_OFFLINE) {
4182fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_OFFLINE;
4183fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_ONLINING) {
4184fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_ONLINING;
4185fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_OFFLINING) {
4186fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_OFFLINING;
4187fcf3ce44SJohn Forte 	}
4188fcf3ce44SJohn Forte 	bcopy(fLuProps.lu_alias, luProps->alias, sizeof (luProps->alias));
4189fcf3ce44SJohn Forte done:
4190fcf3ce44SJohn Forte 	(void) close(fd);
4191fcf3ce44SJohn Forte 	return (ret);
4192fcf3ce44SJohn Forte }
4193fcf3ce44SJohn Forte 
4194fcf3ce44SJohn Forte /*
4195fcf3ce44SJohn Forte  * stmfGetState
4196fcf3ce44SJohn Forte  *
4197fcf3ce44SJohn Forte  * Purpose: retrieve the current state of the stmf module
4198fcf3ce44SJohn Forte  *
4199fcf3ce44SJohn Forte  * state - pointer to stmfState structure allocated by the caller
4200fcf3ce44SJohn Forte  *         On success, contains the state of stmf
4201fcf3ce44SJohn Forte  */
4202fcf3ce44SJohn Forte int
4203fcf3ce44SJohn Forte stmfGetState(stmfState *state)
4204fcf3ce44SJohn Forte {
4205fcf3ce44SJohn Forte 	int ret;
4206fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
4207fcf3ce44SJohn Forte 
4208fcf3ce44SJohn Forte 	if (state == NULL) {
4209fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4210fcf3ce44SJohn Forte 	}
4211fcf3ce44SJohn Forte 
4212fcf3ce44SJohn Forte 	ret = getStmfState(&iState);
4213fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4214fcf3ce44SJohn Forte 		return (ret);
4215fcf3ce44SJohn Forte 	}
4216fcf3ce44SJohn Forte 	switch (iState.state) {
4217fcf3ce44SJohn Forte 		case STMF_STATE_ONLINE:
4218fcf3ce44SJohn Forte 			state->operationalState =
4219fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_ONLINE;
4220fcf3ce44SJohn Forte 			break;
4221fcf3ce44SJohn Forte 		case STMF_STATE_OFFLINE:
4222fcf3ce44SJohn Forte 			state->operationalState =
4223fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_OFFLINE;
4224fcf3ce44SJohn Forte 			break;
4225fcf3ce44SJohn Forte 		case STMF_STATE_ONLINING:
4226fcf3ce44SJohn Forte 			state->operationalState =
4227fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_ONLINING;
4228fcf3ce44SJohn Forte 			break;
4229fcf3ce44SJohn Forte 		case STMF_STATE_OFFLINING:
4230fcf3ce44SJohn Forte 			state->operationalState =
4231fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_OFFLINING;
4232fcf3ce44SJohn Forte 			break;
4233fcf3ce44SJohn Forte 		default:
4234fcf3ce44SJohn Forte 			state->operationalState =
4235fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_UNKNOWN;
4236fcf3ce44SJohn Forte 			break;
4237fcf3ce44SJohn Forte 	}
4238fcf3ce44SJohn Forte 	switch (iState.config_state) {
4239fcf3ce44SJohn Forte 		case STMF_CONFIG_NONE:
4240fcf3ce44SJohn Forte 			state->configState = STMF_CONFIG_STATE_NONE;
4241fcf3ce44SJohn Forte 			break;
4242fcf3ce44SJohn Forte 		case STMF_CONFIG_INIT:
4243fcf3ce44SJohn Forte 			state->configState = STMF_CONFIG_STATE_INIT;
4244fcf3ce44SJohn Forte 			break;
4245fcf3ce44SJohn Forte 		case STMF_CONFIG_INIT_DONE:
4246fcf3ce44SJohn Forte 			state->configState =
4247fcf3ce44SJohn Forte 			    STMF_CONFIG_STATE_INIT_DONE;
4248fcf3ce44SJohn Forte 			break;
4249fcf3ce44SJohn Forte 		default:
4250fcf3ce44SJohn Forte 			state->configState =
4251fcf3ce44SJohn Forte 			    STMF_CONFIG_STATE_UNKNOWN;
4252fcf3ce44SJohn Forte 			break;
4253fcf3ce44SJohn Forte 	}
4254fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
4255fcf3ce44SJohn Forte }
4256fcf3ce44SJohn Forte 
4257fcf3ce44SJohn Forte /*
4258fcf3ce44SJohn Forte  * stmfGetViewEntryList
4259fcf3ce44SJohn Forte  *
4260fcf3ce44SJohn Forte  * Purpose: Retrieves the list of view entries for the specified
4261fcf3ce44SJohn Forte  *          logical unit.
4262fcf3ce44SJohn Forte  *
4263fcf3ce44SJohn Forte  * lu - the guid of the logical unit for which to retrieve the view entry list
4264fcf3ce44SJohn Forte  * viewEntryList - a pointer to a pointer to a stmfViewEntryList structure. On
4265fcf3ce44SJohn Forte  *                 success, contains the list of view entries.
4266fcf3ce44SJohn Forte  */
4267fcf3ce44SJohn Forte int
4268fcf3ce44SJohn Forte stmfGetViewEntryList(stmfGuid *lu, stmfViewEntryList **viewEntryList)
4269fcf3ce44SJohn Forte {
4270fcf3ce44SJohn Forte 	int ret;
42718fe96085Stim szeto 	int fd;
42728fe96085Stim szeto 	int ioctlRet;
42738fe96085Stim szeto 	int cmd = STMF_IOCTL_LU_VE_LIST;
42748fe96085Stim szeto 	int i;
42758fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
42768fe96085Stim szeto 	stmf_view_op_entry_t *fVeList;
42778fe96085Stim szeto 	uint32_t fVeListSize;
42788fe96085Stim szeto 	uint32_t listCnt;
4279fcf3ce44SJohn Forte 
4280fcf3ce44SJohn Forte 	if (lu == NULL || viewEntryList == NULL) {
4281fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4282fcf3ce44SJohn Forte 	}
4283fcf3ce44SJohn Forte 
42848fe96085Stim szeto 	/* call init */
42858fe96085Stim szeto 	ret = initializeConfig();
42868fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
42878fe96085Stim szeto 		return (ret);
42888fe96085Stim szeto 	}
42898fe96085Stim szeto 
42908fe96085Stim szeto 	/*
42918fe96085Stim szeto 	 * Open control node for stmf
42928fe96085Stim szeto 	 */
42938fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
42948fe96085Stim szeto 		return (ret);
42958fe96085Stim szeto 
42968fe96085Stim szeto 	/*
42978fe96085Stim szeto 	 * Allocate ioctl input buffer
42988fe96085Stim szeto 	 */
42998fe96085Stim szeto 	fVeListSize = ALLOC_VE;
43008fe96085Stim szeto 	fVeListSize = fVeListSize * (sizeof (stmf_view_op_entry_t));
43018fe96085Stim szeto 	fVeList = (stmf_view_op_entry_t *)calloc(1, fVeListSize);
43028fe96085Stim szeto 	if (fVeList == NULL) {
43038fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
43048fe96085Stim szeto 		goto done;
43058fe96085Stim szeto 	}
43068fe96085Stim szeto 
43078fe96085Stim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
43088fe96085Stim szeto 	/*
43098fe96085Stim szeto 	 * Issue ioctl to get the LU list
43108fe96085Stim szeto 	 */
43118fe96085Stim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
43128fe96085Stim szeto 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)lu;
43138fe96085Stim szeto 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGuid);
43148fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = fVeListSize;
43158fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fVeList;
43168fe96085Stim szeto 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
43178fe96085Stim szeto 	if (ioctlRet != 0) {
43188fe96085Stim szeto 		switch (errno) {
43198fe96085Stim szeto 			case EBUSY:
4320fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4321fcf3ce44SJohn Forte 				break;
43228fe96085Stim szeto 			case EPERM:
43238fe96085Stim szeto 			case EACCES:
43248fe96085Stim szeto 				ret = STMF_ERROR_PERM;
4325fcf3ce44SJohn Forte 				break;
4326fcf3ce44SJohn Forte 			default:
4327fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
43288fe96085Stim szeto 				    "stmfGetViewEntryList:ioctl errno(%d)",
43298fe96085Stim szeto 				    errno);
4330fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4331fcf3ce44SJohn Forte 				break;
4332fcf3ce44SJohn Forte 		}
43338fe96085Stim szeto 		goto done;
43348fe96085Stim szeto 	}
43358fe96085Stim szeto 	/*
43368fe96085Stim szeto 	 * Check whether input buffer was large enough
43378fe96085Stim szeto 	 */
43388fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_VE) {
43398fe96085Stim szeto 		bzero(&stmfIoctl, sizeof (stmfIoctl));
43408fe96085Stim szeto 		fVeListSize = stmfIoctl.stmf_obuf_max_nentries *
43418fe96085Stim szeto 		    sizeof (stmf_view_op_entry_t);
43428fe96085Stim szeto 		free(fVeList);
43438fe96085Stim szeto 		fVeList = (stmf_view_op_entry_t *)calloc(1, fVeListSize);
43448fe96085Stim szeto 		if (fVeList == NULL) {
43458fe96085Stim szeto 			return (STMF_ERROR_NOMEM);
43468fe96085Stim szeto 		}
43478fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = fVeListSize;
43488fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fVeList;
43498fe96085Stim szeto 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
43508fe96085Stim szeto 		if (ioctlRet != 0) {
43518fe96085Stim szeto 			switch (errno) {
43528fe96085Stim szeto 				case EBUSY:
43538fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
43548fe96085Stim szeto 					break;
43558fe96085Stim szeto 				case EPERM:
43568fe96085Stim szeto 				case EACCES:
43578fe96085Stim szeto 					ret = STMF_ERROR_PERM;
43588fe96085Stim szeto 					break;
43598fe96085Stim szeto 				default:
43608fe96085Stim szeto 					syslog(LOG_DEBUG,
43618fe96085Stim szeto 					    "stmfGetLogicalUnitList:"
43628fe96085Stim szeto 					    "ioctl errno(%d)", errno);
43638fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
43648fe96085Stim szeto 					break;
43658fe96085Stim szeto 			}
43668fe96085Stim szeto 			goto done;
43678fe96085Stim szeto 		}
43688fe96085Stim szeto 	}
4369fcf3ce44SJohn Forte 
43708fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
43718fe96085Stim szeto 		goto done;
43728fe96085Stim szeto 	}
43738fe96085Stim szeto 
43748fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_nentries == 0) {
43758fe96085Stim szeto 		ret = STMF_ERROR_NOT_FOUND;
43768fe96085Stim szeto 		goto done;
43778fe96085Stim szeto 	}
43788fe96085Stim szeto 
43798fe96085Stim szeto 	listCnt = stmfIoctl.stmf_obuf_nentries;
43808fe96085Stim szeto 
43818fe96085Stim szeto 	/*
43828fe96085Stim szeto 	 * allocate caller's buffer with the final size
43838fe96085Stim szeto 	 */
43848fe96085Stim szeto 	*viewEntryList = (stmfViewEntryList *)calloc(1,
43858fe96085Stim szeto 	    sizeof (stmfViewEntryList) + listCnt * sizeof (stmfViewEntry));
43868fe96085Stim szeto 	if (*viewEntryList == NULL) {
43878fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
43888fe96085Stim szeto 		goto done;
43898fe96085Stim szeto 	}
43908fe96085Stim szeto 
43918fe96085Stim szeto 	(*viewEntryList)->cnt = listCnt;
43928fe96085Stim szeto 
43938fe96085Stim szeto 	/* copy to caller's buffer */
43948fe96085Stim szeto 	for (i = 0; i < listCnt; i++) {
43958fe96085Stim szeto 		(*viewEntryList)->ve[i].veIndexValid = B_TRUE;
43968fe96085Stim szeto 		(*viewEntryList)->ve[i].veIndex = fVeList[i].ve_ndx;
43978fe96085Stim szeto 		if (fVeList[i].ve_all_hosts == 1) {
43988fe96085Stim szeto 			(*viewEntryList)->ve[i].allHosts = B_TRUE;
43998fe96085Stim szeto 		} else {
44008fe96085Stim szeto 			bcopy(fVeList[i].ve_host_group.name,
44018fe96085Stim szeto 			    (*viewEntryList)->ve[i].hostGroup,
44028fe96085Stim szeto 			    fVeList[i].ve_host_group.name_size);
44038fe96085Stim szeto 		}
44048fe96085Stim szeto 		if (fVeList[i].ve_all_targets == 1) {
44058fe96085Stim szeto 			(*viewEntryList)->ve[i].allTargets = B_TRUE;
44068fe96085Stim szeto 		} else {
44078fe96085Stim szeto 			bcopy(fVeList[i].ve_target_group.name,
44088fe96085Stim szeto 			    (*viewEntryList)->ve[i].targetGroup,
44098fe96085Stim szeto 			    fVeList[i].ve_target_group.name_size);
44108fe96085Stim szeto 		}
44118fe96085Stim szeto 		bcopy(fVeList[i].ve_lu_nbr, (*viewEntryList)->ve[i].luNbr,
44128fe96085Stim szeto 		    sizeof ((*viewEntryList)->ve[i].luNbr));
44138fe96085Stim szeto 		(*viewEntryList)->ve[i].luNbrValid = B_TRUE;
44148fe96085Stim szeto 	}
44158fe96085Stim szeto 
44168fe96085Stim szeto 	/*
44178fe96085Stim szeto 	 * sort the list. This gives a consistent view across gets
44188fe96085Stim szeto 	 */
44198fe96085Stim szeto 	qsort((void *)&((*viewEntryList)->ve[0]), (*viewEntryList)->cnt,
44208fe96085Stim szeto 	    sizeof (stmfViewEntry), viewEntryCompare);
44218fe96085Stim szeto 
44228fe96085Stim szeto done:
44238fe96085Stim szeto 	(void) close(fd);
44248fe96085Stim szeto 	/*
44258fe96085Stim szeto 	 * free internal buffers
44268fe96085Stim szeto 	 */
44278fe96085Stim szeto 	free(fVeList);
4428fcf3ce44SJohn Forte 	return (ret);
4429fcf3ce44SJohn Forte }
4430fcf3ce44SJohn Forte 
44318fe96085Stim szeto 
4432fcf3ce44SJohn Forte /*
4433fcf3ce44SJohn Forte  * loadHostGroups
4434fcf3ce44SJohn Forte  *
4435fcf3ce44SJohn Forte  * Purpose - issues the ioctl to load the host groups into stmf
4436fcf3ce44SJohn Forte  *
4437fcf3ce44SJohn Forte  * fd - file descriptor for the control node of stmf.
4438fcf3ce44SJohn Forte  * groupList - populated host group list
4439fcf3ce44SJohn Forte  */
4440fcf3ce44SJohn Forte static int
4441fcf3ce44SJohn Forte loadHostGroups(int fd, stmfGroupList *groupList)
4442fcf3ce44SJohn Forte {
4443fcf3ce44SJohn Forte 	int i, j;
4444fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4445fcf3ce44SJohn Forte 	stmfGroupProperties *groupProps = NULL;
4446fcf3ce44SJohn Forte 
4447fcf3ce44SJohn Forte 	for (i = 0; i < groupList->cnt; i++) {
4448fcf3ce44SJohn Forte 		if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_HOST_GROUP,
4449fcf3ce44SJohn Forte 		    &(groupList->name[i]))) != STMF_STATUS_SUCCESS) {
4450fcf3ce44SJohn Forte 			goto out;
4451fcf3ce44SJohn Forte 		}
44528fe96085Stim szeto 		ret = iLoadGroupMembersFromPs(&(groupList->name[i]),
44538fe96085Stim szeto 		    &groupProps, HOST_GROUP);
4454fcf3ce44SJohn Forte 		for (j = 0; j < groupProps->cnt; j++) {
4455fcf3ce44SJohn Forte 			if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_HG_ENTRY,
4456fcf3ce44SJohn Forte 			    &(groupList->name[i]), &(groupProps->name[j])))
4457fcf3ce44SJohn Forte 			    != STMF_STATUS_SUCCESS) {
4458fcf3ce44SJohn Forte 				goto out;
4459fcf3ce44SJohn Forte 			}
4460fcf3ce44SJohn Forte 		}
4461fcf3ce44SJohn Forte 	}
4462fcf3ce44SJohn Forte 
4463fcf3ce44SJohn Forte 
4464fcf3ce44SJohn Forte out:
4465fcf3ce44SJohn Forte 	stmfFreeMemory(groupProps);
4466fcf3ce44SJohn Forte 	return (ret);
4467fcf3ce44SJohn Forte }
4468fcf3ce44SJohn Forte 
4469fcf3ce44SJohn Forte /*
4470fcf3ce44SJohn Forte  * loadTargetGroups
4471fcf3ce44SJohn Forte  *
4472fcf3ce44SJohn Forte  * Purpose - issues the ioctl to load the target groups into stmf
4473fcf3ce44SJohn Forte  *
4474fcf3ce44SJohn Forte  * fd - file descriptor for the control node of stmf.
4475fcf3ce44SJohn Forte  * groupList - populated target group list.
4476fcf3ce44SJohn Forte  */
4477fcf3ce44SJohn Forte static int
4478fcf3ce44SJohn Forte loadTargetGroups(int fd, stmfGroupList *groupList)
4479fcf3ce44SJohn Forte {
4480fcf3ce44SJohn Forte 	int i, j;
4481fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4482fcf3ce44SJohn Forte 	stmfGroupProperties *groupProps = NULL;
4483fcf3ce44SJohn Forte 
4484fcf3ce44SJohn Forte 	for (i = 0; i < groupList->cnt; i++) {
4485fcf3ce44SJohn Forte 		if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_TARGET_GROUP,
4486fcf3ce44SJohn Forte 		    &(groupList->name[i]))) != STMF_STATUS_SUCCESS) {
4487fcf3ce44SJohn Forte 			goto out;
4488fcf3ce44SJohn Forte 		}
44898fe96085Stim szeto 		ret = iLoadGroupMembersFromPs(&(groupList->name[i]),
44908fe96085Stim szeto 		    &groupProps, TARGET_GROUP);
4491fcf3ce44SJohn Forte 		for (j = 0; j < groupProps->cnt; j++) {
4492fcf3ce44SJohn Forte 			if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_TG_ENTRY,
4493fcf3ce44SJohn Forte 			    &(groupList->name[i]), &(groupProps->name[j])))
4494fcf3ce44SJohn Forte 			    != STMF_STATUS_SUCCESS) {
4495fcf3ce44SJohn Forte 				goto out;
4496fcf3ce44SJohn Forte 			}
4497fcf3ce44SJohn Forte 		}
4498fcf3ce44SJohn Forte 	}
4499fcf3ce44SJohn Forte 
4500fcf3ce44SJohn Forte 
4501fcf3ce44SJohn Forte out:
4502fcf3ce44SJohn Forte 	stmfFreeMemory(groupProps);
4503fcf3ce44SJohn Forte 	return (ret);
4504fcf3ce44SJohn Forte }
4505fcf3ce44SJohn Forte 
4506fcf3ce44SJohn Forte 
4507fcf3ce44SJohn Forte /*
4508fcf3ce44SJohn Forte  * loadStore
4509fcf3ce44SJohn Forte  *
4510fcf3ce44SJohn Forte  * Purpose: Load the configuration data from the store
4511fcf3ce44SJohn Forte  *
4512fcf3ce44SJohn Forte  * First load the host groups and target groups, then the view entries
4513fcf3ce44SJohn Forte  * and finally the provider data
4514fcf3ce44SJohn Forte  *
4515fcf3ce44SJohn Forte  * fd - file descriptor of control node for stmf.
4516fcf3ce44SJohn Forte  */
4517fcf3ce44SJohn Forte static int
4518fcf3ce44SJohn Forte loadStore(int fd)
4519fcf3ce44SJohn Forte {
4520fcf3ce44SJohn Forte 	int ret;
4521fcf3ce44SJohn Forte 	int i, j;
4522fcf3ce44SJohn Forte 	stmfGroupList *groupList = NULL;
4523fcf3ce44SJohn Forte 	stmfGuidList *guidList = NULL;
4524fcf3ce44SJohn Forte 	stmfViewEntryList *viewEntryList = NULL;
4525fcf3ce44SJohn Forte 	stmfProviderList *providerList = NULL;
4526fcf3ce44SJohn Forte 	int providerType;
4527fcf3ce44SJohn Forte 	nvlist_t *nvl = NULL;
4528fcf3ce44SJohn Forte 
4529fcf3ce44SJohn Forte 
4530fcf3ce44SJohn Forte 
4531fcf3ce44SJohn Forte 	/* load host groups */
45328fe96085Stim szeto 	ret = iLoadGroupFromPs(&groupList, HOST_GROUP);
4533fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4534fcf3ce44SJohn Forte 		return (ret);
4535fcf3ce44SJohn Forte 	}
4536fcf3ce44SJohn Forte 	ret = loadHostGroups(fd, groupList);
4537fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4538fcf3ce44SJohn Forte 		goto out;
4539fcf3ce44SJohn Forte 	}
4540fcf3ce44SJohn Forte 
4541fcf3ce44SJohn Forte 	stmfFreeMemory(groupList);
4542fcf3ce44SJohn Forte 	groupList = NULL;
4543fcf3ce44SJohn Forte 
4544fcf3ce44SJohn Forte 	/* load target groups */
45458fe96085Stim szeto 	ret = iLoadGroupFromPs(&groupList, TARGET_GROUP);
4546fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4547fcf3ce44SJohn Forte 		goto out;
4548fcf3ce44SJohn Forte 	}
4549fcf3ce44SJohn Forte 	ret = loadTargetGroups(fd, groupList);
4550fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4551fcf3ce44SJohn Forte 		goto out;
4552fcf3ce44SJohn Forte 	}
4553fcf3ce44SJohn Forte 
4554fcf3ce44SJohn Forte 	stmfFreeMemory(groupList);
4555fcf3ce44SJohn Forte 	groupList = NULL;
4556fcf3ce44SJohn Forte 
4557fcf3ce44SJohn Forte 	/* Get the guid list */
4558fcf3ce44SJohn Forte 	ret = psGetLogicalUnitList(&guidList);
4559fcf3ce44SJohn Forte 	switch (ret) {
4560fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
4561fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
4562fcf3ce44SJohn Forte 			break;
4563fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
4564fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
4565fcf3ce44SJohn Forte 			break;
4566fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
4567fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
4568fcf3ce44SJohn Forte 			break;
4569fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4570fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
4571fcf3ce44SJohn Forte 			break;
4572fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
4573fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
4574fcf3ce44SJohn Forte 			break;
4575fcf3ce44SJohn Forte 		default:
4576fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
4577fcf3ce44SJohn Forte 			break;
4578fcf3ce44SJohn Forte 	}
4579fcf3ce44SJohn Forte 
4580fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4581fcf3ce44SJohn Forte 		goto out;
4582fcf3ce44SJohn Forte 	}
4583fcf3ce44SJohn Forte 
4584fcf3ce44SJohn Forte 	/*
4585fcf3ce44SJohn Forte 	 * We have the guid list, now get the corresponding
4586fcf3ce44SJohn Forte 	 * view entries for each guid
4587fcf3ce44SJohn Forte 	 */
4588fcf3ce44SJohn Forte 	for (i = 0; i < guidList->cnt; i++) {
4589fcf3ce44SJohn Forte 		ret = psGetViewEntryList(&guidList->guid[i], &viewEntryList);
4590fcf3ce44SJohn Forte 		switch (ret) {
4591fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
4592fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
4593fcf3ce44SJohn Forte 				break;
4594fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
4595fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4596fcf3ce44SJohn Forte 				break;
4597fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
4598fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4599fcf3ce44SJohn Forte 				break;
4600fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4601fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
4602fcf3ce44SJohn Forte 				break;
4603fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
4604fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
4605fcf3ce44SJohn Forte 				break;
4606fcf3ce44SJohn Forte 			default:
4607fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4608fcf3ce44SJohn Forte 				break;
4609fcf3ce44SJohn Forte 		}
4610fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
4611fcf3ce44SJohn Forte 			goto out;
4612fcf3ce44SJohn Forte 		}
4613fcf3ce44SJohn Forte 		for (j = 0; j < viewEntryList->cnt; j++) {
4614fcf3ce44SJohn Forte 			ret = addViewEntryIoctl(fd, &guidList->guid[i],
4615fcf3ce44SJohn Forte 			    &viewEntryList->ve[j]);
4616fcf3ce44SJohn Forte 			if (ret != STMF_STATUS_SUCCESS) {
4617fcf3ce44SJohn Forte 				goto out;
4618fcf3ce44SJohn Forte 			}
4619fcf3ce44SJohn Forte 		}
4620fcf3ce44SJohn Forte 	}
4621fcf3ce44SJohn Forte 
4622fcf3ce44SJohn Forte 	/* get the list of providers that have data */
4623fcf3ce44SJohn Forte 	ret = psGetProviderDataList(&providerList);
4624fcf3ce44SJohn Forte 	switch (ret) {
4625fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
4626fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
4627fcf3ce44SJohn Forte 			break;
4628fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
4629fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
4630fcf3ce44SJohn Forte 			break;
4631fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
4632fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
4633fcf3ce44SJohn Forte 			break;
4634fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4635fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
4636fcf3ce44SJohn Forte 			break;
4637fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
4638fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
4639fcf3ce44SJohn Forte 			break;
4640fcf3ce44SJohn Forte 		default:
4641fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
4642fcf3ce44SJohn Forte 			break;
4643fcf3ce44SJohn Forte 	}
4644fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4645fcf3ce44SJohn Forte 		goto out;
4646fcf3ce44SJohn Forte 	}
4647fcf3ce44SJohn Forte 
4648fcf3ce44SJohn Forte 	for (i = 0; i < providerList->cnt; i++) {
4649fcf3ce44SJohn Forte 		providerType = providerList->provider[i].providerType;
4650fcf3ce44SJohn Forte 		ret = psGetProviderData(providerList->provider[i].name,
4651fcf3ce44SJohn Forte 		    &nvl, providerType, NULL);
4652fcf3ce44SJohn Forte 		switch (ret) {
4653fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
4654fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
4655fcf3ce44SJohn Forte 				break;
4656fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
4657fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4658fcf3ce44SJohn Forte 				break;
4659fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
4660fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4661fcf3ce44SJohn Forte 				break;
4662fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4663fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
4664fcf3ce44SJohn Forte 				break;
4665fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
4666fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
4667fcf3ce44SJohn Forte 				break;
4668fcf3ce44SJohn Forte 			default:
4669fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4670fcf3ce44SJohn Forte 				break;
4671fcf3ce44SJohn Forte 		}
4672fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
4673fcf3ce44SJohn Forte 			goto out;
4674fcf3ce44SJohn Forte 		}
4675fcf3ce44SJohn Forte 
4676fcf3ce44SJohn Forte 		/* call setProviderData */
4677fcf3ce44SJohn Forte 		ret = setProviderData(fd, providerList->provider[i].name, nvl,
46788fe96085Stim szeto 		    providerType, NULL);
4679fcf3ce44SJohn Forte 		switch (ret) {
4680fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
4681fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
4682fcf3ce44SJohn Forte 				break;
4683fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
4684fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4685fcf3ce44SJohn Forte 				break;
4686fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
4687fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4688fcf3ce44SJohn Forte 				break;
4689fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4690fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
4691fcf3ce44SJohn Forte 				break;
4692fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
4693fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
4694fcf3ce44SJohn Forte 				break;
4695fcf3ce44SJohn Forte 			default:
4696fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4697fcf3ce44SJohn Forte 				break;
4698fcf3ce44SJohn Forte 		}
4699fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
4700fcf3ce44SJohn Forte 			goto out;
4701fcf3ce44SJohn Forte 		}
4702fcf3ce44SJohn Forte 
4703fcf3ce44SJohn Forte 		nvlist_free(nvl);
4704fcf3ce44SJohn Forte 		nvl = NULL;
4705fcf3ce44SJohn Forte 	}
4706fcf3ce44SJohn Forte out:
4707fcf3ce44SJohn Forte 	if (groupList != NULL) {
4708fcf3ce44SJohn Forte 		free(groupList);
4709fcf3ce44SJohn Forte 	}
4710fcf3ce44SJohn Forte 	if (guidList != NULL) {
4711fcf3ce44SJohn Forte 		free(guidList);
4712fcf3ce44SJohn Forte 	}
4713fcf3ce44SJohn Forte 	if (viewEntryList != NULL) {
4714fcf3ce44SJohn Forte 		free(viewEntryList);
4715fcf3ce44SJohn Forte 	}
4716fcf3ce44SJohn Forte 	if (nvl != NULL) {
4717fcf3ce44SJohn Forte 		nvlist_free(nvl);
4718fcf3ce44SJohn Forte 	}
4719fcf3ce44SJohn Forte 	return (ret);
4720fcf3ce44SJohn Forte }
4721fcf3ce44SJohn Forte 
4722fcf3ce44SJohn Forte /*
4723fcf3ce44SJohn Forte  * stmfLoadConfig
4724fcf3ce44SJohn Forte  *
4725fcf3ce44SJohn Forte  * Purpose - load the configuration data from smf into stmf
4726fcf3ce44SJohn Forte  *
4727fcf3ce44SJohn Forte  */
4728fcf3ce44SJohn Forte int
4729fcf3ce44SJohn Forte stmfLoadConfig(void)
4730fcf3ce44SJohn Forte {
47318fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
4732fcf3ce44SJohn Forte 	int fd;
4733fcf3ce44SJohn Forte 	stmf_state_desc_t stmfStateSet;
4734fcf3ce44SJohn Forte 	stmfState state;
4735fcf3ce44SJohn Forte 
47368fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
47378fe96085Stim szeto 		stmfStateSet.state = STMF_STATE_OFFLINE;
4738*640428aeSSue Gleeson 
47398fe96085Stim szeto 		if ((ret = openStmf(OPEN_EXCL_STMF, &fd))
47408fe96085Stim szeto 		    != STMF_STATUS_SUCCESS) {
47418fe96085Stim szeto 			return (ret);
47428fe96085Stim szeto 		}
4743*640428aeSSue Gleeson 		/*
4744*640428aeSSue Gleeson 		 * Configuration not stored persistently; nothing to
4745*640428aeSSue Gleeson 		 * initialize so do not set to STMF_CONFIG_INIT.
4746*640428aeSSue Gleeson 		 */
47478fe96085Stim szeto 		stmfStateSet.config_state = STMF_CONFIG_INIT_DONE;
47488fe96085Stim szeto 		goto done;
47498fe96085Stim szeto 	}
4750fcf3ce44SJohn Forte 
4751fcf3ce44SJohn Forte 	/* Check to ensure service exists */
4752fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
4753fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
4754fcf3ce44SJohn Forte 	}
4755fcf3ce44SJohn Forte 
4756fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
4757fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
4758fcf3ce44SJohn Forte 		if (state.operationalState != STMF_SERVICE_STATE_OFFLINE) {
4759fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_ONLINE);
4760fcf3ce44SJohn Forte 		}
4761fcf3ce44SJohn Forte 	} else {
4762fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
4763fcf3ce44SJohn Forte 	}
4764fcf3ce44SJohn Forte 
4765fcf3ce44SJohn Forte 
4766fcf3ce44SJohn Forte 	stmfStateSet.state = STMF_STATE_OFFLINE;
4767fcf3ce44SJohn Forte 	stmfStateSet.config_state = STMF_CONFIG_INIT;
4768fcf3ce44SJohn Forte 
4769fcf3ce44SJohn Forte 	/*
4770fcf3ce44SJohn Forte 	 * Open control node for stmf
4771fcf3ce44SJohn Forte 	 */
4772fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
4773fcf3ce44SJohn Forte 		return (ret);
4774fcf3ce44SJohn Forte 
4775fcf3ce44SJohn Forte 	ret = setStmfState(fd, &stmfStateSet, STMF_SERVICE_TYPE);
4776fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4777fcf3ce44SJohn Forte 		goto done;
4778fcf3ce44SJohn Forte 	}
4779fcf3ce44SJohn Forte 
4780fcf3ce44SJohn Forte 	/* Load the persistent configuration data */
4781fcf3ce44SJohn Forte 	ret = loadStore(fd);
4782fcf3ce44SJohn Forte 	if (ret != 0) {
4783fcf3ce44SJohn Forte 		goto done;
4784fcf3ce44SJohn Forte 	}
4785fcf3ce44SJohn Forte 
4786fcf3ce44SJohn Forte 	stmfStateSet.state = STMF_STATE_OFFLINE;
4787fcf3ce44SJohn Forte 	stmfStateSet.config_state = STMF_CONFIG_INIT_DONE;
4788fcf3ce44SJohn Forte 
4789fcf3ce44SJohn Forte done:
4790fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
4791fcf3ce44SJohn Forte 		ret = setStmfState(fd, &stmfStateSet, STMF_SERVICE_TYPE);
4792fcf3ce44SJohn Forte 	}
4793fcf3ce44SJohn Forte 	(void) close(fd);
4794fcf3ce44SJohn Forte 	return (ret);
4795fcf3ce44SJohn Forte }
4796fcf3ce44SJohn Forte 
47978fe96085Stim szeto 
4798fcf3ce44SJohn Forte /*
4799fcf3ce44SJohn Forte  * getStmfState
4800fcf3ce44SJohn Forte  *
4801fcf3ce44SJohn Forte  * stmfState - pointer to stmf_state_desc_t structure. Will contain the state
4802fcf3ce44SJohn Forte  *             information of the stmf service on success.
4803fcf3ce44SJohn Forte  */
4804fcf3ce44SJohn Forte static int
4805fcf3ce44SJohn Forte getStmfState(stmf_state_desc_t *stmfState)
4806fcf3ce44SJohn Forte {
4807fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4808fcf3ce44SJohn Forte 	int fd;
4809fcf3ce44SJohn Forte 	int ioctlRet;
4810fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4811fcf3ce44SJohn Forte 
4812fcf3ce44SJohn Forte 	/*
4813fcf3ce44SJohn Forte 	 * Open control node for stmf
4814fcf3ce44SJohn Forte 	 */
4815fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
4816fcf3ce44SJohn Forte 		return (ret);
4817fcf3ce44SJohn Forte 
4818fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4819fcf3ce44SJohn Forte 	/*
4820fcf3ce44SJohn Forte 	 * Issue ioctl to get the stmf state
4821fcf3ce44SJohn Forte 	 */
4822fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4823fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_state_desc_t);
4824fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)stmfState;
4825fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (stmf_state_desc_t);
4826fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)stmfState;
4827fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_GET_STMF_STATE, &stmfIoctl);
4828fcf3ce44SJohn Forte 
4829fcf3ce44SJohn Forte 	(void) close(fd);
4830fcf3ce44SJohn Forte 
4831fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4832fcf3ce44SJohn Forte 		switch (errno) {
4833fcf3ce44SJohn Forte 			case EBUSY:
4834fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4835fcf3ce44SJohn Forte 				break;
4836fcf3ce44SJohn Forte 			case EPERM:
4837fcf3ce44SJohn Forte 			case EACCES:
4838fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4839fcf3ce44SJohn Forte 				break;
4840fcf3ce44SJohn Forte 			default:
4841fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4842fcf3ce44SJohn Forte 				    "getStmfState:ioctl errno(%d)", errno);
4843fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4844fcf3ce44SJohn Forte 				break;
4845fcf3ce44SJohn Forte 		}
4846fcf3ce44SJohn Forte 	}
4847fcf3ce44SJohn Forte 	return (ret);
4848fcf3ce44SJohn Forte }
4849fcf3ce44SJohn Forte 
4850fcf3ce44SJohn Forte 
4851fcf3ce44SJohn Forte /*
4852fcf3ce44SJohn Forte  * setStmfState
4853fcf3ce44SJohn Forte  *
4854fcf3ce44SJohn Forte  * stmfState - pointer to caller set state structure
4855fcf3ce44SJohn Forte  * objectType - one of:
4856fcf3ce44SJohn Forte  *		LOGICAL_UNIT_TYPE
4857fcf3ce44SJohn Forte  *		TARGET_TYPE
4858fcf3ce44SJohn Forte  *		STMF_SERVICE_TYPE
4859fcf3ce44SJohn Forte  */
4860fcf3ce44SJohn Forte static int
4861fcf3ce44SJohn Forte setStmfState(int fd, stmf_state_desc_t *stmfState, int objectType)
4862fcf3ce44SJohn Forte {
4863fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4864fcf3ce44SJohn Forte 	int ioctlRet;
4865fcf3ce44SJohn Forte 	int cmd;
4866fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4867fcf3ce44SJohn Forte 
4868fcf3ce44SJohn Forte 	switch (objectType) {
4869fcf3ce44SJohn Forte 		case LOGICAL_UNIT_TYPE:
4870fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_LU_STATE;
4871fcf3ce44SJohn Forte 			break;
4872fcf3ce44SJohn Forte 		case TARGET_TYPE:
4873fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_TARGET_PORT_STATE;
4874fcf3ce44SJohn Forte 			break;
4875fcf3ce44SJohn Forte 		case STMF_SERVICE_TYPE:
4876fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_STMF_STATE;
4877fcf3ce44SJohn Forte 			break;
4878fcf3ce44SJohn Forte 		default:
4879fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
4880fcf3ce44SJohn Forte 			goto done;
4881fcf3ce44SJohn Forte 	}
4882fcf3ce44SJohn Forte 
4883fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4884fcf3ce44SJohn Forte 	/*
4885fcf3ce44SJohn Forte 	 * Issue ioctl to set the stmf state
4886fcf3ce44SJohn Forte 	 */
4887fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4888fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_state_desc_t);
4889fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)stmfState;
4890fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4891fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4892fcf3ce44SJohn Forte 		switch (errno) {
4893fcf3ce44SJohn Forte 			case EBUSY:
4894fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4895fcf3ce44SJohn Forte 				break;
48968fe96085Stim szeto 			case EPERM:
4897fcf3ce44SJohn Forte 			case EACCES:
4898fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4899fcf3ce44SJohn Forte 				break;
4900fcf3ce44SJohn Forte 			case ENOENT:
4901fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4902fcf3ce44SJohn Forte 				break;
4903fcf3ce44SJohn Forte 			default:
4904fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4905fcf3ce44SJohn Forte 				    "setStmfState:ioctl errno(%d)", errno);
4906fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4907fcf3ce44SJohn Forte 				break;
4908fcf3ce44SJohn Forte 		}
4909fcf3ce44SJohn Forte 	}
4910fcf3ce44SJohn Forte done:
4911fcf3ce44SJohn Forte 	return (ret);
4912fcf3ce44SJohn Forte }
4913fcf3ce44SJohn Forte 
4914fcf3ce44SJohn Forte /*
4915fcf3ce44SJohn Forte  * stmfOnline
4916fcf3ce44SJohn Forte  *
4917fcf3ce44SJohn Forte  * Purpose: Online stmf service
4918fcf3ce44SJohn Forte  *
4919fcf3ce44SJohn Forte  */
4920fcf3ce44SJohn Forte int
4921fcf3ce44SJohn Forte stmfOnline(void)
4922fcf3ce44SJohn Forte {
4923fcf3ce44SJohn Forte 	int ret;
4924fcf3ce44SJohn Forte 	int fd;
4925fcf3ce44SJohn Forte 	stmfState state;
4926fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
4927fcf3ce44SJohn Forte 
4928fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
4929fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
4930fcf3ce44SJohn Forte 		if (state.operationalState == STMF_SERVICE_STATE_ONLINE) {
4931fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_ONLINE);
4932fcf3ce44SJohn Forte 		}
4933fcf3ce44SJohn Forte 	} else {
4934fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
4935fcf3ce44SJohn Forte 	}
4936fcf3ce44SJohn Forte 	iState.state = STMF_STATE_ONLINE;
4937fcf3ce44SJohn Forte 	iState.config_state = STMF_CONFIG_NONE;
4938fcf3ce44SJohn Forte 	/*
4939fcf3ce44SJohn Forte 	 * Open control node for stmf
4940fcf3ce44SJohn Forte 	 * to make call to setStmfState()
4941fcf3ce44SJohn Forte 	 */
4942fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
4943fcf3ce44SJohn Forte 		return (ret);
4944fcf3ce44SJohn Forte 	ret = setStmfState(fd, &iState, STMF_SERVICE_TYPE);
4945fcf3ce44SJohn Forte 	(void) close(fd);
4946fcf3ce44SJohn Forte 	return (ret);
4947fcf3ce44SJohn Forte }
4948fcf3ce44SJohn Forte 
4949fcf3ce44SJohn Forte /*
4950fcf3ce44SJohn Forte  * stmfOffline
4951fcf3ce44SJohn Forte  *
4952fcf3ce44SJohn Forte  * Purpose: Offline stmf service
4953fcf3ce44SJohn Forte  *
4954fcf3ce44SJohn Forte  */
4955fcf3ce44SJohn Forte int
4956fcf3ce44SJohn Forte stmfOffline(void)
4957fcf3ce44SJohn Forte {
4958fcf3ce44SJohn Forte 	int ret;
4959fcf3ce44SJohn Forte 	int fd;
4960fcf3ce44SJohn Forte 	stmfState state;
4961fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
4962fcf3ce44SJohn Forte 
4963fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
4964fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
4965fcf3ce44SJohn Forte 		if (state.operationalState == STMF_SERVICE_STATE_OFFLINE) {
4966fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_OFFLINE);
4967fcf3ce44SJohn Forte 		}
4968fcf3ce44SJohn Forte 	} else {
4969fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
4970fcf3ce44SJohn Forte 	}
4971fcf3ce44SJohn Forte 	iState.state = STMF_STATE_OFFLINE;
4972fcf3ce44SJohn Forte 	iState.config_state = STMF_CONFIG_NONE;
4973fcf3ce44SJohn Forte 
4974fcf3ce44SJohn Forte 	/*
4975fcf3ce44SJohn Forte 	 * Open control node for stmf
4976fcf3ce44SJohn Forte 	 * to make call to setStmfState()
4977fcf3ce44SJohn Forte 	 */
4978fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
4979fcf3ce44SJohn Forte 		return (ret);
4980fcf3ce44SJohn Forte 	ret = setStmfState(fd, &iState, STMF_SERVICE_TYPE);
4981fcf3ce44SJohn Forte 	(void) close(fd);
4982fcf3ce44SJohn Forte 	return (ret);
4983fcf3ce44SJohn Forte }
4984fcf3ce44SJohn Forte 
4985fcf3ce44SJohn Forte 
4986fcf3ce44SJohn Forte /*
4987fcf3ce44SJohn Forte  * stmfOfflineTarget
4988fcf3ce44SJohn Forte  *
4989fcf3ce44SJohn Forte  * Purpose: Change state of target to offline
4990fcf3ce44SJohn Forte  *
4991fcf3ce44SJohn Forte  * devid - devid of the target to offline
4992fcf3ce44SJohn Forte  */
4993fcf3ce44SJohn Forte int
4994fcf3ce44SJohn Forte stmfOfflineTarget(stmfDevid *devid)
4995fcf3ce44SJohn Forte {
4996fcf3ce44SJohn Forte 	stmf_state_desc_t targetState;
4997fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4998fcf3ce44SJohn Forte 	int fd;
4999fcf3ce44SJohn Forte 
5000fcf3ce44SJohn Forte 	if (devid == NULL) {
5001fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5002fcf3ce44SJohn Forte 	}
5003fcf3ce44SJohn Forte 	bzero(&targetState, sizeof (targetState));
5004fcf3ce44SJohn Forte 
5005fcf3ce44SJohn Forte 	targetState.state = STMF_STATE_OFFLINE;
5006fcf3ce44SJohn Forte 	targetState.ident[IDENT_LENGTH_BYTE] = devid->identLength;
5007fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetState.ident[IDENT_LENGTH_BYTE + 1],
5008fcf3ce44SJohn Forte 	    devid->identLength);
5009fcf3ce44SJohn Forte 	/*
5010fcf3ce44SJohn Forte 	 * Open control node for stmf
5011fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5012fcf3ce44SJohn Forte 	 */
5013fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5014fcf3ce44SJohn Forte 		return (ret);
5015fcf3ce44SJohn Forte 	ret = setStmfState(fd, &targetState, TARGET_TYPE);
5016fcf3ce44SJohn Forte 	(void) close(fd);
5017fcf3ce44SJohn Forte 	return (ret);
5018fcf3ce44SJohn Forte }
5019fcf3ce44SJohn Forte 
5020fcf3ce44SJohn Forte /*
5021fcf3ce44SJohn Forte  * stmfOfflineLogicalUnit
5022fcf3ce44SJohn Forte  *
5023fcf3ce44SJohn Forte  * Purpose: Change state of logical unit to offline
5024fcf3ce44SJohn Forte  *
5025fcf3ce44SJohn Forte  * lu - guid of the logical unit to offline
5026fcf3ce44SJohn Forte  */
5027fcf3ce44SJohn Forte int
5028fcf3ce44SJohn Forte stmfOfflineLogicalUnit(stmfGuid *lu)
5029fcf3ce44SJohn Forte {
5030fcf3ce44SJohn Forte 	stmf_state_desc_t luState;
5031fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5032fcf3ce44SJohn Forte 	int fd;
5033fcf3ce44SJohn Forte 
5034fcf3ce44SJohn Forte 	if (lu == NULL) {
5035fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5036fcf3ce44SJohn Forte 	}
5037fcf3ce44SJohn Forte 
5038fcf3ce44SJohn Forte 	bzero(&luState, sizeof (luState));
5039fcf3ce44SJohn Forte 
5040fcf3ce44SJohn Forte 	luState.state = STMF_STATE_OFFLINE;
5041fcf3ce44SJohn Forte 	bcopy(lu, &luState.ident, sizeof (stmfGuid));
5042fcf3ce44SJohn Forte 	/*
5043fcf3ce44SJohn Forte 	 * Open control node for stmf
5044fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5045fcf3ce44SJohn Forte 	 */
5046fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5047fcf3ce44SJohn Forte 		return (ret);
5048fcf3ce44SJohn Forte 	ret = setStmfState(fd, &luState, LOGICAL_UNIT_TYPE);
5049fcf3ce44SJohn Forte 	(void) close(fd);
5050fcf3ce44SJohn Forte 	return (ret);
5051fcf3ce44SJohn Forte }
5052fcf3ce44SJohn Forte 
5053fcf3ce44SJohn Forte /*
5054fcf3ce44SJohn Forte  * stmfOnlineTarget
5055fcf3ce44SJohn Forte  *
5056fcf3ce44SJohn Forte  * Purpose: Change state of target to online
5057fcf3ce44SJohn Forte  *
5058fcf3ce44SJohn Forte  * devid - devid of the target to online
5059fcf3ce44SJohn Forte  */
5060fcf3ce44SJohn Forte int
5061fcf3ce44SJohn Forte stmfOnlineTarget(stmfDevid *devid)
5062fcf3ce44SJohn Forte {
5063fcf3ce44SJohn Forte 	stmf_state_desc_t targetState;
5064fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5065fcf3ce44SJohn Forte 	int fd;
5066fcf3ce44SJohn Forte 
5067fcf3ce44SJohn Forte 	if (devid == NULL) {
5068fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5069fcf3ce44SJohn Forte 	}
5070fcf3ce44SJohn Forte 	bzero(&targetState, sizeof (targetState));
5071fcf3ce44SJohn Forte 
5072fcf3ce44SJohn Forte 	targetState.state = STMF_STATE_ONLINE;
5073fcf3ce44SJohn Forte 	targetState.ident[IDENT_LENGTH_BYTE] = devid->identLength;
5074fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetState.ident[IDENT_LENGTH_BYTE + 1],
5075fcf3ce44SJohn Forte 	    devid->identLength);
5076fcf3ce44SJohn Forte 	/*
5077fcf3ce44SJohn Forte 	 * Open control node for stmf
5078fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5079fcf3ce44SJohn Forte 	 */
5080fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5081fcf3ce44SJohn Forte 		return (ret);
5082fcf3ce44SJohn Forte 	ret = setStmfState(fd, &targetState, TARGET_TYPE);
5083fcf3ce44SJohn Forte 	(void) close(fd);
5084fcf3ce44SJohn Forte 	return (ret);
5085fcf3ce44SJohn Forte }
5086fcf3ce44SJohn Forte 
5087fcf3ce44SJohn Forte /*
5088fcf3ce44SJohn Forte  * stmfOnlineLogicalUnit
5089fcf3ce44SJohn Forte  *
5090fcf3ce44SJohn Forte  * Purpose: Change state of logical unit to online
5091fcf3ce44SJohn Forte  *
5092fcf3ce44SJohn Forte  * lu - guid of the logical unit to online
5093fcf3ce44SJohn Forte  */
5094fcf3ce44SJohn Forte int
5095fcf3ce44SJohn Forte stmfOnlineLogicalUnit(stmfGuid *lu)
5096fcf3ce44SJohn Forte {
5097fcf3ce44SJohn Forte 	stmf_state_desc_t luState;
5098fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5099fcf3ce44SJohn Forte 	int fd;
5100fcf3ce44SJohn Forte 
5101fcf3ce44SJohn Forte 	if (lu == NULL) {
5102fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5103fcf3ce44SJohn Forte 	}
5104fcf3ce44SJohn Forte 
5105fcf3ce44SJohn Forte 	bzero(&luState, sizeof (luState));
5106fcf3ce44SJohn Forte 
5107fcf3ce44SJohn Forte 	luState.state = STMF_STATE_ONLINE;
5108fcf3ce44SJohn Forte 	bcopy(lu, &luState.ident, sizeof (stmfGuid));
5109fcf3ce44SJohn Forte 	/*
5110fcf3ce44SJohn Forte 	 * Open control node for stmf
5111fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5112fcf3ce44SJohn Forte 	 */
5113fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5114fcf3ce44SJohn Forte 		return (ret);
5115fcf3ce44SJohn Forte 	ret = setStmfState(fd, &luState, LOGICAL_UNIT_TYPE);
5116fcf3ce44SJohn Forte 	(void) close(fd);
5117fcf3ce44SJohn Forte 	return (ret);
5118fcf3ce44SJohn Forte }
5119fcf3ce44SJohn Forte 
5120fcf3ce44SJohn Forte /*
5121fcf3ce44SJohn Forte  * stmfRemoveFromHostGroup
5122fcf3ce44SJohn Forte  *
5123fcf3ce44SJohn Forte  * Purpose: Removes an initiator from an initiator group
5124fcf3ce44SJohn Forte  *
5125fcf3ce44SJohn Forte  * hostGroupName - name of an initiator group
5126fcf3ce44SJohn Forte  * hostName - name of host group member to remove
5127fcf3ce44SJohn Forte  */
5128fcf3ce44SJohn Forte int
5129fcf3ce44SJohn Forte stmfRemoveFromHostGroup(stmfGroupName *hostGroupName, stmfDevid *hostName)
5130fcf3ce44SJohn Forte {
5131fcf3ce44SJohn Forte 	int ret;
5132fcf3ce44SJohn Forte 	int fd;
5133fcf3ce44SJohn Forte 
5134fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
5135fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
5136fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || hostName == NULL) {
5137fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5138fcf3ce44SJohn Forte 	}
5139fcf3ce44SJohn Forte 
5140fcf3ce44SJohn Forte 	/* call init */
5141fcf3ce44SJohn Forte 	ret = initializeConfig();
5142fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5143fcf3ce44SJohn Forte 		return (ret);
5144fcf3ce44SJohn Forte 	}
5145fcf3ce44SJohn Forte 
5146fcf3ce44SJohn Forte 	/*
5147fcf3ce44SJohn Forte 	 * Open control node for stmf
5148fcf3ce44SJohn Forte 	 */
5149fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5150fcf3ce44SJohn Forte 		return (ret);
5151fcf3ce44SJohn Forte 
5152fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_REMOVE_HG_ENTRY,
5153fcf3ce44SJohn Forte 	    hostGroupName, hostName)) != STMF_STATUS_SUCCESS) {
5154fcf3ce44SJohn Forte 		goto done;
5155fcf3ce44SJohn Forte 	}
5156fcf3ce44SJohn Forte 
51578fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
51588fe96085Stim szeto 		goto done;
51598fe96085Stim szeto 	}
51608fe96085Stim szeto 
5161fcf3ce44SJohn Forte 	ret = psRemoveHostGroupMember((char *)hostGroupName,
5162fcf3ce44SJohn Forte 	    (char *)hostName->ident);
5163fcf3ce44SJohn Forte 	switch (ret) {
5164fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5165fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5166fcf3ce44SJohn Forte 			break;
5167fcf3ce44SJohn Forte 		case STMF_PS_ERROR_MEMBER_NOT_FOUND:
5168fcf3ce44SJohn Forte 			ret = STMF_ERROR_MEMBER_NOT_FOUND;
5169fcf3ce44SJohn Forte 			break;
5170fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
5171fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
5172fcf3ce44SJohn Forte 			break;
5173fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5174fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5175fcf3ce44SJohn Forte 			break;
5176fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5177fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5178fcf3ce44SJohn Forte 			break;
5179fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5180fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5181fcf3ce44SJohn Forte 			break;
5182fcf3ce44SJohn Forte 		default:
5183fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
5184fcf3ce44SJohn Forte 			    "stmfRemoveFromHostGroup"
5185fcf3ce44SJohn Forte 			    "psRemoveHostGroupMember:error(%d)", ret);
5186fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5187fcf3ce44SJohn Forte 			break;
5188fcf3ce44SJohn Forte 	}
5189fcf3ce44SJohn Forte 
5190fcf3ce44SJohn Forte done:
5191fcf3ce44SJohn Forte 	(void) close(fd);
5192fcf3ce44SJohn Forte 	return (ret);
5193fcf3ce44SJohn Forte }
5194fcf3ce44SJohn Forte 
5195fcf3ce44SJohn Forte /*
5196fcf3ce44SJohn Forte  * stmfRemoveFromTargetGroup
5197fcf3ce44SJohn Forte  *
5198fcf3ce44SJohn Forte  * Purpose: Removes a local port from a local port group
5199fcf3ce44SJohn Forte  *
5200fcf3ce44SJohn Forte  * targetGroupName - name of a target group
5201fcf3ce44SJohn Forte  * targetName - name of target to remove
5202fcf3ce44SJohn Forte  */
5203fcf3ce44SJohn Forte int
5204fcf3ce44SJohn Forte stmfRemoveFromTargetGroup(stmfGroupName *targetGroupName, stmfDevid *targetName)
5205fcf3ce44SJohn Forte {
5206fcf3ce44SJohn Forte 	int ret;
5207fcf3ce44SJohn Forte 	int fd;
5208fcf3ce44SJohn Forte 
5209fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
5210fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
5211fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || targetName == NULL) {
5212fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5213fcf3ce44SJohn Forte 	}
5214fcf3ce44SJohn Forte 
5215fcf3ce44SJohn Forte 	/* call init */
5216fcf3ce44SJohn Forte 	ret = initializeConfig();
5217fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5218fcf3ce44SJohn Forte 		return (ret);
5219fcf3ce44SJohn Forte 	}
5220fcf3ce44SJohn Forte 
5221fcf3ce44SJohn Forte 	/*
5222fcf3ce44SJohn Forte 	 * Open control node for stmf
5223fcf3ce44SJohn Forte 	 */
5224fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5225fcf3ce44SJohn Forte 		return (ret);
5226fcf3ce44SJohn Forte 
5227fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_REMOVE_TG_ENTRY,
5228fcf3ce44SJohn Forte 	    targetGroupName, targetName)) != STMF_STATUS_SUCCESS) {
5229fcf3ce44SJohn Forte 		goto done;
5230fcf3ce44SJohn Forte 	}
5231fcf3ce44SJohn Forte 
52328fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
52338fe96085Stim szeto 		goto done;
52348fe96085Stim szeto 	}
52358fe96085Stim szeto 
5236fcf3ce44SJohn Forte 	ret = psRemoveTargetGroupMember((char *)targetGroupName,
5237fcf3ce44SJohn Forte 	    (char *)targetName->ident);
5238fcf3ce44SJohn Forte 	switch (ret) {
5239fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5240fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5241fcf3ce44SJohn Forte 			break;
5242fcf3ce44SJohn Forte 		case STMF_PS_ERROR_MEMBER_NOT_FOUND:
5243fcf3ce44SJohn Forte 			ret = STMF_ERROR_MEMBER_NOT_FOUND;
5244fcf3ce44SJohn Forte 			break;
5245fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
5246fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
5247fcf3ce44SJohn Forte 			break;
5248fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5249fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5250fcf3ce44SJohn Forte 			break;
5251fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5252fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5253fcf3ce44SJohn Forte 			break;
5254fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5255fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5256fcf3ce44SJohn Forte 			break;
5257fcf3ce44SJohn Forte 		default:
5258fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
5259fcf3ce44SJohn Forte 			    "stmfRemoveFromTargetGroup"
5260fcf3ce44SJohn Forte 			    "psRemoveTargetGroupMember:error(%d)", ret);
5261fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5262fcf3ce44SJohn Forte 			break;
5263fcf3ce44SJohn Forte 	}
5264fcf3ce44SJohn Forte 
5265fcf3ce44SJohn Forte done:
5266fcf3ce44SJohn Forte 	(void) close(fd);
5267fcf3ce44SJohn Forte 	return (ret);
5268fcf3ce44SJohn Forte }
5269fcf3ce44SJohn Forte 
5270fcf3ce44SJohn Forte /*
5271fcf3ce44SJohn Forte  * stmfRemoveViewEntry
5272fcf3ce44SJohn Forte  *
5273fcf3ce44SJohn Forte  * Purpose: Removes a view entry from a logical unit
5274fcf3ce44SJohn Forte  *
5275fcf3ce44SJohn Forte  * lu - guid of lu for which view entry is being removed
5276fcf3ce44SJohn Forte  * viewEntryIndex - index of view entry to remove
5277fcf3ce44SJohn Forte  *
5278fcf3ce44SJohn Forte  */
5279fcf3ce44SJohn Forte int
5280fcf3ce44SJohn Forte stmfRemoveViewEntry(stmfGuid *lu, uint32_t viewEntryIndex)
5281fcf3ce44SJohn Forte {
5282fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5283fcf3ce44SJohn Forte 	int fd;
5284fcf3ce44SJohn Forte 	int ioctlRet;
5285fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
5286fcf3ce44SJohn Forte 	stmf_view_op_entry_t ioctlViewEntry;
5287fcf3ce44SJohn Forte 
5288fcf3ce44SJohn Forte 	if (lu == NULL) {
5289fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5290fcf3ce44SJohn Forte 	}
5291fcf3ce44SJohn Forte 
5292fcf3ce44SJohn Forte 	/* call init */
5293fcf3ce44SJohn Forte 	ret = initializeConfig();
5294fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5295fcf3ce44SJohn Forte 		return (ret);
5296fcf3ce44SJohn Forte 	}
5297fcf3ce44SJohn Forte 
5298fcf3ce44SJohn Forte 	/*
5299fcf3ce44SJohn Forte 	 * Open control node for stmf
5300fcf3ce44SJohn Forte 	 */
5301fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5302fcf3ce44SJohn Forte 		return (ret);
5303fcf3ce44SJohn Forte 
5304fcf3ce44SJohn Forte 	bzero(&ioctlViewEntry, sizeof (ioctlViewEntry));
5305fcf3ce44SJohn Forte 	ioctlViewEntry.ve_ndx_valid = B_TRUE;
5306fcf3ce44SJohn Forte 	ioctlViewEntry.ve_ndx = viewEntryIndex;
5307fcf3ce44SJohn Forte 	bcopy(lu, &ioctlViewEntry.ve_guid, sizeof (stmfGuid));
5308fcf3ce44SJohn Forte 
5309fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
5310fcf3ce44SJohn Forte 	/*
5311fcf3ce44SJohn Forte 	 * Issue ioctl to add to the view entry
5312fcf3ce44SJohn Forte 	 */
5313fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
5314fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ioctlViewEntry);
5315fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ioctlViewEntry;
5316fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_REMOVE_VIEW_ENTRY, &stmfIoctl);
5317fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
5318fcf3ce44SJohn Forte 		switch (errno) {
5319fcf3ce44SJohn Forte 			case EBUSY:
5320fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
5321fcf3ce44SJohn Forte 				break;
53228fe96085Stim szeto 			case EPERM:
53238fe96085Stim szeto 				ret = STMF_ERROR_PERM;
53248fe96085Stim szeto 				break;
5325fcf3ce44SJohn Forte 			case EACCES:
5326fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
5327fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
5328fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
5329fcf3ce44SJohn Forte 						break;
5330fcf3ce44SJohn Forte 					default:
5331fcf3ce44SJohn Forte 						ret = STMF_ERROR_PERM;
5332fcf3ce44SJohn Forte 						break;
5333fcf3ce44SJohn Forte 				}
5334fcf3ce44SJohn Forte 				break;
5335fcf3ce44SJohn Forte 			case ENODEV:
5336fcf3ce44SJohn Forte 			case ENOENT:
5337fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
5338fcf3ce44SJohn Forte 				break;
5339fcf3ce44SJohn Forte 			default:
5340fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
5341fcf3ce44SJohn Forte 				    "stmfRemoveViewEntry:ioctl errno(%d)",
5342fcf3ce44SJohn Forte 				    errno);
5343fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
5344fcf3ce44SJohn Forte 				break;
5345fcf3ce44SJohn Forte 		}
5346fcf3ce44SJohn Forte 		goto done;
5347fcf3ce44SJohn Forte 	}
5348fcf3ce44SJohn Forte 
53498fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
53508fe96085Stim szeto 		goto done;
53518fe96085Stim szeto 	}
53528fe96085Stim szeto 
5353fcf3ce44SJohn Forte 	ret = psRemoveViewEntry(lu, viewEntryIndex);
5354fcf3ce44SJohn Forte 	switch (ret) {
5355fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5356fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5357fcf3ce44SJohn Forte 			break;
5358fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
5359fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
5360fcf3ce44SJohn Forte 			break;
5361fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5362fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5363fcf3ce44SJohn Forte 			break;
5364fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5365fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5366fcf3ce44SJohn Forte 			break;
5367fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5368fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5369fcf3ce44SJohn Forte 			break;
5370fcf3ce44SJohn Forte 		default:
5371fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
5372fcf3ce44SJohn Forte 			    "stmfRemoveViewEntry" "psRemoveViewEntry:error(%d)",
5373fcf3ce44SJohn Forte 			    ret);
5374fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5375fcf3ce44SJohn Forte 			break;
5376fcf3ce44SJohn Forte 	}
5377fcf3ce44SJohn Forte 
5378fcf3ce44SJohn Forte done:
5379fcf3ce44SJohn Forte 	(void) close(fd);
5380fcf3ce44SJohn Forte 	return (ret);
5381fcf3ce44SJohn Forte }
5382fcf3ce44SJohn Forte 
5383fcf3ce44SJohn Forte /*
5384fcf3ce44SJohn Forte  * stmfSetProviderData
5385fcf3ce44SJohn Forte  *
5386fcf3ce44SJohn Forte  * Purpose: set the provider data
5387fcf3ce44SJohn Forte  *
5388fcf3ce44SJohn Forte  * providerName - unique name of provider
5389fcf3ce44SJohn Forte  * nvl - nvlist to set
5390fcf3ce44SJohn Forte  * providerType - type of provider for which to set data
5391fcf3ce44SJohn Forte  *		STMF_LU_PROVIDER_TYPE
5392fcf3ce44SJohn Forte  *		STMF_PORT_PROVIDER_TYPE
5393fcf3ce44SJohn Forte  */
5394fcf3ce44SJohn Forte int
5395fcf3ce44SJohn Forte stmfSetProviderData(char *providerName, nvlist_t *nvl, int providerType)
5396fcf3ce44SJohn Forte {
5397fcf3ce44SJohn Forte 	return (stmfSetProviderDataProt(providerName, nvl, providerType,
5398fcf3ce44SJohn Forte 	    NULL));
5399fcf3ce44SJohn Forte }
5400fcf3ce44SJohn Forte 
5401fcf3ce44SJohn Forte /*
5402fcf3ce44SJohn Forte  * stmfSetProviderDataProt
5403fcf3ce44SJohn Forte  *
5404fcf3ce44SJohn Forte  * Purpose: set the provider data
5405fcf3ce44SJohn Forte  *
5406fcf3ce44SJohn Forte  * providerName - unique name of provider
5407fcf3ce44SJohn Forte  * nvl - nvlist to set
5408fcf3ce44SJohn Forte  * providerType - type of provider for which to set data
5409fcf3ce44SJohn Forte  *		STMF_LU_PROVIDER_TYPE
5410fcf3ce44SJohn Forte  *		STMF_PORT_PROVIDER_TYPE
5411fcf3ce44SJohn Forte  * setToken - Stale data token returned in the stmfGetProviderDataProt()
5412fcf3ce44SJohn Forte  *	      call or NULL.
5413fcf3ce44SJohn Forte  */
5414fcf3ce44SJohn Forte int
5415fcf3ce44SJohn Forte stmfSetProviderDataProt(char *providerName, nvlist_t *nvl, int providerType,
5416fcf3ce44SJohn Forte     uint64_t *setToken)
5417fcf3ce44SJohn Forte {
5418fcf3ce44SJohn Forte 	int ret;
5419fcf3ce44SJohn Forte 	int fd;
5420fcf3ce44SJohn Forte 
5421fcf3ce44SJohn Forte 	if (providerName == NULL || nvl == NULL) {
5422fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5423fcf3ce44SJohn Forte 	}
5424fcf3ce44SJohn Forte 
5425fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
5426fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
5427fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5428fcf3ce44SJohn Forte 	}
5429fcf3ce44SJohn Forte 
5430fcf3ce44SJohn Forte 	/* call init */
5431fcf3ce44SJohn Forte 	ret = initializeConfig();
5432fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5433fcf3ce44SJohn Forte 		return (ret);
5434fcf3ce44SJohn Forte 	}
5435fcf3ce44SJohn Forte 
5436fcf3ce44SJohn Forte 	/*
5437fcf3ce44SJohn Forte 	 * Open control node for stmf
5438fcf3ce44SJohn Forte 	 */
5439fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5440fcf3ce44SJohn Forte 		return (ret);
5441fcf3ce44SJohn Forte 
54428fe96085Stim szeto 	ret = setProviderData(fd, providerName, nvl, providerType, setToken);
5443fcf3ce44SJohn Forte 
5444fcf3ce44SJohn Forte 	(void) close(fd);
5445fcf3ce44SJohn Forte 
5446fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5447fcf3ce44SJohn Forte 		goto done;
5448fcf3ce44SJohn Forte 	}
5449fcf3ce44SJohn Forte 
54508fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
54518fe96085Stim szeto 		goto done;
54528fe96085Stim szeto 	}
54538fe96085Stim szeto 
5454fcf3ce44SJohn Forte 	/* setting driver provider data successful. Now persist it */
54558fe96085Stim szeto 	ret = psSetProviderData(providerName, nvl, providerType, NULL);
5456fcf3ce44SJohn Forte 	switch (ret) {
5457fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5458fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5459fcf3ce44SJohn Forte 			break;
5460fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
5461fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
5462fcf3ce44SJohn Forte 			break;
5463fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5464fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5465fcf3ce44SJohn Forte 			break;
5466fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5467fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5468fcf3ce44SJohn Forte 			break;
5469fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5470fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5471fcf3ce44SJohn Forte 			break;
5472fcf3ce44SJohn Forte 		case STMF_PS_ERROR_PROV_DATA_STALE:
5473fcf3ce44SJohn Forte 			ret = STMF_ERROR_PROV_DATA_STALE;
5474fcf3ce44SJohn Forte 			break;
5475fcf3ce44SJohn Forte 		default:
5476fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
5477fcf3ce44SJohn Forte 			    "stmfSetProviderData"
5478fcf3ce44SJohn Forte 			    "psSetProviderData:error(%d)", ret);
5479fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5480fcf3ce44SJohn Forte 			break;
5481fcf3ce44SJohn Forte 	}
5482fcf3ce44SJohn Forte 
5483fcf3ce44SJohn Forte done:
5484fcf3ce44SJohn Forte 	return (ret);
5485fcf3ce44SJohn Forte }
5486fcf3ce44SJohn Forte 
5487fcf3ce44SJohn Forte /*
54888fe96085Stim szeto  * getProviderData
54898fe96085Stim szeto  *
54908fe96085Stim szeto  * Purpose: set the provider data from stmf
54918fe96085Stim szeto  *
54928fe96085Stim szeto  * providerName - unique name of provider
54938fe96085Stim szeto  * nvl - nvlist to load/retrieve
54948fe96085Stim szeto  * providerType - logical unit or port provider
54958fe96085Stim szeto  * setToken - returned stale data token
54968fe96085Stim szeto  */
54978fe96085Stim szeto int
54988fe96085Stim szeto getProviderData(char *providerName, nvlist_t **nvl, int providerType,
54998fe96085Stim szeto     uint64_t *setToken)
55008fe96085Stim szeto {
55018fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
55028fe96085Stim szeto 	int fd;
55038fe96085Stim szeto 	int ioctlRet;
55048fe96085Stim szeto 	size_t nvlistSize = ALLOC_PP_DATA_SIZE;
55058fe96085Stim szeto 	int retryCnt = 0;
55068fe96085Stim szeto 	int retryCntMax = MAX_PROVIDER_RETRY;
55078fe96085Stim szeto 	stmf_ppioctl_data_t ppi = {0}, *ppi_out = NULL;
55088fe96085Stim szeto 	boolean_t retry = B_TRUE;
55098fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
55108fe96085Stim szeto 
55118fe96085Stim szeto 	if (providerName == NULL) {
55128fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
55138fe96085Stim szeto 	}
55148fe96085Stim szeto 
55158fe96085Stim szeto 	/*
55168fe96085Stim szeto 	 * Open control node for stmf
55178fe96085Stim szeto 	 */
55188fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
55198fe96085Stim szeto 		return (ret);
55208fe96085Stim szeto 
55218fe96085Stim szeto 	/* set provider name and provider type */
55228fe96085Stim szeto 	if (strlcpy(ppi.ppi_name, providerName,
55238fe96085Stim szeto 	    sizeof (ppi.ppi_name)) >=
55248fe96085Stim szeto 	    sizeof (ppi.ppi_name)) {
55258fe96085Stim szeto 		ret = STMF_ERROR_INVALID_ARG;
55268fe96085Stim szeto 		goto done;
55278fe96085Stim szeto 	}
55288fe96085Stim szeto 	switch (providerType) {
55298fe96085Stim szeto 		case STMF_LU_PROVIDER_TYPE:
55308fe96085Stim szeto 			ppi.ppi_lu_provider = 1;
55318fe96085Stim szeto 			break;
55328fe96085Stim szeto 		case STMF_PORT_PROVIDER_TYPE:
55338fe96085Stim szeto 			ppi.ppi_port_provider = 1;
55348fe96085Stim szeto 			break;
55358fe96085Stim szeto 		default:
55368fe96085Stim szeto 			ret = STMF_ERROR_INVALID_ARG;
55378fe96085Stim szeto 			goto done;
55388fe96085Stim szeto 	}
55398fe96085Stim szeto 
55408fe96085Stim szeto 	do {
55418fe96085Stim szeto 		/* allocate memory for ioctl */
55428fe96085Stim szeto 		ppi_out = (stmf_ppioctl_data_t *)calloc(1, nvlistSize +
55438fe96085Stim szeto 		    sizeof (stmf_ppioctl_data_t));
55448fe96085Stim szeto 		if (ppi_out == NULL) {
55458fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
55468fe96085Stim szeto 			goto done;
55478fe96085Stim szeto 
55488fe96085Stim szeto 		}
55498fe96085Stim szeto 
55508fe96085Stim szeto 		/* set the size of the ioctl data to allocated buffer */
55518fe96085Stim szeto 		ppi.ppi_data_size = nvlistSize;
55528fe96085Stim szeto 
55538fe96085Stim szeto 		bzero(&stmfIoctl, sizeof (stmfIoctl));
55548fe96085Stim szeto 
55558fe96085Stim szeto 		stmfIoctl.stmf_version = STMF_VERSION_1;
55568fe96085Stim szeto 		stmfIoctl.stmf_ibuf_size = sizeof (stmf_ppioctl_data_t);
55578fe96085Stim szeto 		stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ppi;
55588fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = sizeof (stmf_ppioctl_data_t) +
55598fe96085Stim szeto 		    nvlistSize;
55608fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)ppi_out;
55618fe96085Stim szeto 		ioctlRet = ioctl(fd, STMF_IOCTL_GET_PP_DATA, &stmfIoctl);
55628fe96085Stim szeto 		if (ioctlRet != 0) {
55638fe96085Stim szeto 			switch (errno) {
55648fe96085Stim szeto 				case EBUSY:
55658fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
55668fe96085Stim szeto 					break;
55678fe96085Stim szeto 				case EPERM:
55688fe96085Stim szeto 				case EACCES:
55698fe96085Stim szeto 					ret = STMF_ERROR_PERM;
55708fe96085Stim szeto 					break;
55718fe96085Stim szeto 				case EINVAL:
55728fe96085Stim szeto 					if (stmfIoctl.stmf_error ==
55738fe96085Stim szeto 					    STMF_IOCERR_INSUFFICIENT_BUF) {
55748fe96085Stim szeto 						nvlistSize =
55758fe96085Stim szeto 						    ppi_out->ppi_data_size;
55768fe96085Stim szeto 						free(ppi_out);
55778fe96085Stim szeto 						ppi_out = NULL;
55788fe96085Stim szeto 						if (retryCnt++ > retryCntMax) {
55798fe96085Stim szeto 							retry = B_FALSE;
55808fe96085Stim szeto 							ret = STMF_ERROR_BUSY;
55818fe96085Stim szeto 						} else {
55828fe96085Stim szeto 							ret =
55838fe96085Stim szeto 							    STMF_STATUS_SUCCESS;
55848fe96085Stim szeto 						}
55858fe96085Stim szeto 					} else {
55868fe96085Stim szeto 						syslog(LOG_DEBUG,
55878fe96085Stim szeto 						    "getProviderData:ioctl"
55888fe96085Stim szeto 						    "unable to retrieve "
55898fe96085Stim szeto 						    "nvlist");
55908fe96085Stim szeto 						ret = STMF_STATUS_ERROR;
55918fe96085Stim szeto 					}
55928fe96085Stim szeto 					break;
55938fe96085Stim szeto 				case ENOENT:
55948fe96085Stim szeto 					ret = STMF_ERROR_NOT_FOUND;
55958fe96085Stim szeto 					break;
55968fe96085Stim szeto 				default:
55978fe96085Stim szeto 					syslog(LOG_DEBUG,
55988fe96085Stim szeto 					    "getProviderData:ioctl errno(%d)",
55998fe96085Stim szeto 					    errno);
56008fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
56018fe96085Stim szeto 					break;
56028fe96085Stim szeto 			}
56038fe96085Stim szeto 			if (ret != STMF_STATUS_SUCCESS)
56048fe96085Stim szeto 				goto done;
56058fe96085Stim szeto 		}
56068fe96085Stim szeto 	} while (retry && stmfIoctl.stmf_error == STMF_IOCERR_INSUFFICIENT_BUF);
56078fe96085Stim szeto 
56088fe96085Stim szeto 	if ((ret = nvlist_unpack((char *)ppi_out->ppi_data,
56098fe96085Stim szeto 	    ppi_out->ppi_data_size, nvl, 0)) != 0) {
56108fe96085Stim szeto 		ret = STMF_STATUS_ERROR;
56118fe96085Stim szeto 		goto done;
56128fe96085Stim szeto 	}
56138fe96085Stim szeto 
56148fe96085Stim szeto 	/* caller has asked for new token */
56158fe96085Stim szeto 	if (setToken) {
56168fe96085Stim szeto 		*setToken = ppi_out->ppi_token;
56178fe96085Stim szeto 	}
56188fe96085Stim szeto done:
56198fe96085Stim szeto 	free(ppi_out);
56208fe96085Stim szeto 	(void) close(fd);
56218fe96085Stim szeto 	return (ret);
56228fe96085Stim szeto }
56238fe96085Stim szeto 
56248fe96085Stim szeto /*
5625fcf3ce44SJohn Forte  * setProviderData
5626fcf3ce44SJohn Forte  *
56278fe96085Stim szeto  * Purpose: set the provider data in stmf
5628fcf3ce44SJohn Forte  *
5629fcf3ce44SJohn Forte  * providerName - unique name of provider
5630fcf3ce44SJohn Forte  * nvl - nvlist to set
5631fcf3ce44SJohn Forte  * providerType - logical unit or port provider
56328fe96085Stim szeto  * setToken - stale data token to check if not NULL
5633fcf3ce44SJohn Forte  */
5634fcf3ce44SJohn Forte static int
56358fe96085Stim szeto setProviderData(int fd, char *providerName, nvlist_t *nvl, int providerType,
56368fe96085Stim szeto     uint64_t *setToken)
5637fcf3ce44SJohn Forte {
5638fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5639fcf3ce44SJohn Forte 	int ioctlRet;
5640fcf3ce44SJohn Forte 	size_t nvlistEncodedSize;
5641fcf3ce44SJohn Forte 	stmf_ppioctl_data_t *ppi = NULL;
56428fe96085Stim szeto 	uint64_t outToken;
5643fcf3ce44SJohn Forte 	char *allocatedNvBuffer;
5644fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
5645fcf3ce44SJohn Forte 
5646fcf3ce44SJohn Forte 	if (providerName == NULL) {
5647fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5648fcf3ce44SJohn Forte 	}
5649fcf3ce44SJohn Forte 
5650fcf3ce44SJohn Forte 	/* get size of encoded nvlist */
5651fcf3ce44SJohn Forte 	if (nvlist_size(nvl, &nvlistEncodedSize, NV_ENCODE_XDR) != 0) {
5652fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
5653fcf3ce44SJohn Forte 	}
5654fcf3ce44SJohn Forte 
5655fcf3ce44SJohn Forte 	/* allocate memory for ioctl */
5656fcf3ce44SJohn Forte 	ppi = (stmf_ppioctl_data_t *)calloc(1, nvlistEncodedSize +
5657fcf3ce44SJohn Forte 	    sizeof (stmf_ppioctl_data_t));
5658fcf3ce44SJohn Forte 	if (ppi == NULL) {
5659fcf3ce44SJohn Forte 		return (STMF_ERROR_NOMEM);
5660fcf3ce44SJohn Forte 	}
5661fcf3ce44SJohn Forte 
56628fe96085Stim szeto 	if (setToken) {
56638fe96085Stim szeto 		ppi->ppi_token_valid = 1;
56648fe96085Stim szeto 		ppi->ppi_token = *setToken;
56658fe96085Stim szeto 	}
56668fe96085Stim szeto 
5667fcf3ce44SJohn Forte 	allocatedNvBuffer = (char *)&ppi->ppi_data;
5668fcf3ce44SJohn Forte 	if (nvlist_pack(nvl, &allocatedNvBuffer, &nvlistEncodedSize,
5669fcf3ce44SJohn Forte 	    NV_ENCODE_XDR, 0) != 0) {
5670fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
5671fcf3ce44SJohn Forte 	}
5672fcf3ce44SJohn Forte 
5673fcf3ce44SJohn Forte 	/* set provider name and provider type */
5674fcf3ce44SJohn Forte 	(void) strncpy(ppi->ppi_name, providerName, sizeof (ppi->ppi_name));
5675fcf3ce44SJohn Forte 	switch (providerType) {
5676fcf3ce44SJohn Forte 		case STMF_LU_PROVIDER_TYPE:
5677fcf3ce44SJohn Forte 			ppi->ppi_lu_provider = 1;
5678fcf3ce44SJohn Forte 			break;
5679fcf3ce44SJohn Forte 		case STMF_PORT_PROVIDER_TYPE:
5680fcf3ce44SJohn Forte 			ppi->ppi_port_provider = 1;
5681fcf3ce44SJohn Forte 			break;
5682fcf3ce44SJohn Forte 		default:
5683fcf3ce44SJohn Forte 			return (STMF_ERROR_INVALID_ARG);
5684fcf3ce44SJohn Forte 	}
5685fcf3ce44SJohn Forte 
5686fcf3ce44SJohn Forte 	/* set the size of the ioctl data to packed data size */
5687fcf3ce44SJohn Forte 	ppi->ppi_data_size = nvlistEncodedSize;
5688fcf3ce44SJohn Forte 
5689fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
5690fcf3ce44SJohn Forte 
5691fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
5692fcf3ce44SJohn Forte 	/*
5693fcf3ce44SJohn Forte 	 * Subtracting 8 from the size as that is the size of the last member
5694fcf3ce44SJohn Forte 	 * of the structure where the packed data resides
5695fcf3ce44SJohn Forte 	 */
5696fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = nvlistEncodedSize +
5697fcf3ce44SJohn Forte 	    sizeof (stmf_ppioctl_data_t) - 8;
5698fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)ppi;
56998fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = sizeof (uint64_t);
57008fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&outToken;
5701fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_LOAD_PP_DATA, &stmfIoctl);
5702fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
5703fcf3ce44SJohn Forte 		switch (errno) {
5704fcf3ce44SJohn Forte 			case EBUSY:
5705fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
5706fcf3ce44SJohn Forte 				break;
57078fe96085Stim szeto 			case EPERM:
5708fcf3ce44SJohn Forte 			case EACCES:
5709fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
5710fcf3ce44SJohn Forte 				break;
57118fe96085Stim szeto 			case EINVAL:
57128fe96085Stim szeto 				if (stmfIoctl.stmf_error ==
57138fe96085Stim szeto 				    STMF_IOCERR_PPD_UPDATED) {
57148fe96085Stim szeto 					ret = STMF_ERROR_PROV_DATA_STALE;
57158fe96085Stim szeto 				} else {
57168fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
57178fe96085Stim szeto 				}
57188fe96085Stim szeto 				break;
5719fcf3ce44SJohn Forte 			default:
5720fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
5721fcf3ce44SJohn Forte 				    "setProviderData:ioctl errno(%d)", errno);
5722fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
5723fcf3ce44SJohn Forte 				break;
5724fcf3ce44SJohn Forte 		}
5725fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS)
5726fcf3ce44SJohn Forte 			goto done;
5727fcf3ce44SJohn Forte 	}
5728fcf3ce44SJohn Forte 
57298fe96085Stim szeto 	/* caller has asked for new token */
57308fe96085Stim szeto 	if (setToken) {
57318fe96085Stim szeto 		*setToken = outToken;
57328fe96085Stim szeto 	}
5733fcf3ce44SJohn Forte done:
5734fcf3ce44SJohn Forte 	free(ppi);
5735fcf3ce44SJohn Forte 	return (ret);
5736fcf3ce44SJohn Forte }
57378fe96085Stim szeto 
57388fe96085Stim szeto /*
57398fe96085Stim szeto  * set the persistence method in the library only or library and service
57408fe96085Stim szeto  */
57418fe96085Stim szeto int
57428fe96085Stim szeto stmfSetPersistMethod(uint8_t persistType, boolean_t serviceSet)
57438fe96085Stim szeto {
57448fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
57458fe96085Stim szeto 	int oldPersist;
57468fe96085Stim szeto 
57478fe96085Stim szeto 	(void) pthread_mutex_lock(&persistenceTypeLock);
57488fe96085Stim szeto 	oldPersist = iPersistType;
57498fe96085Stim szeto 	if (persistType == STMF_PERSIST_NONE ||
57508fe96085Stim szeto 	    persistType == STMF_PERSIST_SMF) {
57518fe96085Stim szeto 		iLibSetPersist = B_TRUE;
57528fe96085Stim szeto 		iPersistType = persistType;
57538fe96085Stim szeto 	} else {
57548fe96085Stim szeto 		(void) pthread_mutex_unlock(&persistenceTypeLock);
57558fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
57568fe96085Stim szeto 	}
57578fe96085Stim szeto 	/* Is this for this library open or in SMF */
57588fe96085Stim szeto 	if (serviceSet == B_TRUE) {
57598fe96085Stim szeto 		ret = psSetServicePersist(persistType);
57608fe96085Stim szeto 		if (ret != STMF_PS_SUCCESS) {
57618fe96085Stim szeto 			ret = STMF_ERROR_PERSIST_TYPE;
57628fe96085Stim szeto 			/* Set to old value */
57638fe96085Stim szeto 			iPersistType = oldPersist;
57648fe96085Stim szeto 		}
57658fe96085Stim szeto 	}
57668fe96085Stim szeto 	(void) pthread_mutex_unlock(&persistenceTypeLock);
57678fe96085Stim szeto 
57688fe96085Stim szeto 	return (ret);
57698fe96085Stim szeto }
57708fe96085Stim szeto 
57718fe96085Stim szeto /*
57728fe96085Stim szeto  * Only returns internal state for persist. If unset, goes to ps. If that
57738fe96085Stim szeto  * fails, returns default setting
57748fe96085Stim szeto  */
57758fe96085Stim szeto static uint8_t
57768fe96085Stim szeto iGetPersistMethod()
57778fe96085Stim szeto {
57788fe96085Stim szeto 
57798fe96085Stim szeto 	uint8_t persistType = 0;
57808fe96085Stim szeto 
57818fe96085Stim szeto 	(void) pthread_mutex_lock(&persistenceTypeLock);
57828fe96085Stim szeto 	if (iLibSetPersist) {
57838fe96085Stim szeto 		persistType = iPersistType;
57848fe96085Stim szeto 	} else {
57858fe96085Stim szeto 		int ret;
57868fe96085Stim szeto 		ret = psGetServicePersist(&persistType);
57878fe96085Stim szeto 		if (ret != STMF_PS_SUCCESS) {
57888fe96085Stim szeto 			/* set to default */
57898fe96085Stim szeto 			persistType = STMF_DEFAULT_PERSIST;
57908fe96085Stim szeto 		}
57918fe96085Stim szeto 	}
57928fe96085Stim szeto 	(void) pthread_mutex_unlock(&persistenceTypeLock);
57938fe96085Stim szeto 	return (persistType);
57948fe96085Stim szeto }
57958fe96085Stim szeto 
57968fe96085Stim szeto /*
57978fe96085Stim szeto  * Returns either library state or persistent config state depending on
57988fe96085Stim szeto  * serviceState
57998fe96085Stim szeto  */
58008fe96085Stim szeto int
58018fe96085Stim szeto stmfGetPersistMethod(uint8_t *persistType, boolean_t serviceState)
58028fe96085Stim szeto {
58038fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
58048fe96085Stim szeto 
58058fe96085Stim szeto 	if (persistType == NULL) {
58068fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
58078fe96085Stim szeto 	}
58088fe96085Stim szeto 	if (serviceState) {
58098fe96085Stim szeto 		ret = psGetServicePersist(persistType);
58108fe96085Stim szeto 		if (ret != STMF_PS_SUCCESS) {
58118fe96085Stim szeto 			ret = STMF_ERROR_PERSIST_TYPE;
58128fe96085Stim szeto 		}
58138fe96085Stim szeto 	} else {
58148fe96085Stim szeto 		(void) pthread_mutex_lock(&persistenceTypeLock);
58158fe96085Stim szeto 		if (iLibSetPersist) {
58168fe96085Stim szeto 			*persistType = iPersistType;
58178fe96085Stim szeto 		} else {
58188fe96085Stim szeto 			*persistType = STMF_DEFAULT_PERSIST;
58198fe96085Stim szeto 		}
58208fe96085Stim szeto 		(void) pthread_mutex_unlock(&persistenceTypeLock);
58218fe96085Stim szeto 	}
58228fe96085Stim szeto 
58238fe96085Stim szeto 	return (ret);
58248fe96085Stim szeto }
5825