xref: /titanic_44/usr/src/lib/libstmf/common/stmf.c (revision fdcc480a6215c1e81d58d8e7af8c84fd6f1faa50)
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>
5045039663SJohn Forte #include <sys/pppt_ioctl.h>
51fcf3ce44SJohn Forte 
52fcf3ce44SJohn Forte #define	STMF_PATH    "/devices/pseudo/stmf@0:admin"
538fe96085Stim szeto #define	SBD_PATH    "/devices/pseudo/stmf_sbd@0:admin"
5445039663SJohn Forte #define	PPPT_PATH    "/devices/pseudo/pppt@0:pppt"
55fcf3ce44SJohn Forte 
56fcf3ce44SJohn Forte #define	EUI "eui."
57fcf3ce44SJohn Forte #define	WWN "wwn."
58fcf3ce44SJohn Forte #define	IQN "iqn."
598fe96085Stim szeto #define	LU_ASCII_GUID_SIZE 32
608fe96085Stim szeto #define	LU_GUID_SIZE 16
618fe96085Stim szeto #define	OUI_ASCII_SIZE 6
62*fdcc480aSJohn Forte #define	HOST_ID_ASCII_SIZE 8
638fe96085Stim szeto #define	OUI_SIZE 3
64*fdcc480aSJohn Forte #define	HOST_ID_SIZE 4
65fcf3ce44SJohn Forte #define	IDENT_LENGTH_BYTE 3
66fcf3ce44SJohn Forte 
678fe96085Stim szeto /* various initial allocation values */
688fe96085Stim szeto #define	ALLOC_LU		8192
698fe96085Stim szeto #define	ALLOC_TARGET_PORT	2048
708fe96085Stim szeto #define	ALLOC_PROVIDER		64
718fe96085Stim szeto #define	ALLOC_GROUP		2048
728fe96085Stim szeto #define	ALLOC_SESSION		2048
738fe96085Stim szeto #define	ALLOC_VE		256
748fe96085Stim szeto #define	ALLOC_PP_DATA_SIZE	128*1024
758fe96085Stim szeto #define	ALLOC_GRP_MEMBER	256
768fe96085Stim szeto 
77fcf3ce44SJohn Forte #define	MAX_ISCSI_NAME	223
788fe96085Stim szeto #define	MAX_SERIAL_SIZE 252 + 1
798fe96085Stim szeto #define	MAX_LU_ALIAS_SIZE 256
808fe96085Stim szeto #define	MAX_SBD_PROPS	MAXPATHLEN + MAX_SERIAL_SIZE + MAX_LU_ALIAS_SIZE
81fcf3ce44SJohn Forte 
82fcf3ce44SJohn Forte #define	OPEN_STMF 0
83fcf3ce44SJohn Forte #define	OPEN_EXCL_STMF O_EXCL
84fcf3ce44SJohn Forte 
858fe96085Stim szeto #define	OPEN_SBD 0
868fe96085Stim szeto #define	OPEN_EXCL_SBD O_EXCL
878fe96085Stim szeto 
8845039663SJohn Forte #define	OPEN_PPPT 0
8945039663SJohn Forte #define	OPEN_EXCL_PPPT O_EXCL
9045039663SJohn Forte 
91fcf3ce44SJohn Forte #define	LOGICAL_UNIT_TYPE 0
92fcf3ce44SJohn Forte #define	TARGET_TYPE 1
93fcf3ce44SJohn Forte #define	STMF_SERVICE_TYPE 2
94fcf3ce44SJohn Forte 
958fe96085Stim szeto #define	HOST_GROUP   1
968fe96085Stim szeto #define	TARGET_GROUP 2
978fe96085Stim szeto 
988fe96085Stim szeto /* set default persistence here */
998fe96085Stim szeto #define	STMF_DEFAULT_PERSIST	STMF_PERSIST_SMF
1008fe96085Stim szeto 
1018fe96085Stim szeto #define	MAX_PROVIDER_RETRY 30
1028fe96085Stim szeto 
103fcf3ce44SJohn Forte static int openStmf(int, int *fd);
1048fe96085Stim szeto static int openSbd(int, int *fd);
10545039663SJohn Forte static int openPppt(int, int *fd);
106fcf3ce44SJohn Forte static int groupIoctl(int fd, int cmd, stmfGroupName *);
107fcf3ce44SJohn Forte static int loadStore(int fd);
108fcf3ce44SJohn Forte static int initializeConfig();
109fcf3ce44SJohn Forte static int groupMemberIoctl(int fd, int cmd, stmfGroupName *, stmfDevid *);
110fcf3ce44SJohn Forte static int guidCompare(const void *, const void *);
111fcf3ce44SJohn Forte static int addViewEntryIoctl(int fd, stmfGuid *, stmfViewEntry *);
112fcf3ce44SJohn Forte static int loadHostGroups(int fd, stmfGroupList *);
113fcf3ce44SJohn Forte static int loadTargetGroups(int fd, stmfGroupList *);
114fcf3ce44SJohn Forte static int getStmfState(stmf_state_desc_t *);
115fcf3ce44SJohn Forte static int setStmfState(int fd, stmf_state_desc_t *, int);
1168fe96085Stim szeto static int setProviderData(int fd, char *, nvlist_t *, int, uint64_t *);
1178fe96085Stim szeto static int createDiskResource(luResourceImpl *);
1188fe96085Stim szeto static int createDiskLu(diskResource *, stmfGuid *);
1198fe96085Stim szeto static int deleteDiskLu(stmfGuid *luGuid);
1208fe96085Stim szeto static int getDiskProp(luResourceImpl *, uint32_t, char *, size_t *);
1218fe96085Stim szeto static int getDiskAllProps(stmfGuid *luGuid, luResource *hdl);
1228fe96085Stim szeto static int loadDiskPropsFromDriver(luResourceImpl *, sbd_lu_props_t *);
1238fe96085Stim szeto static int removeGuidFromDiskStore(stmfGuid *);
1248fe96085Stim szeto static int addGuidToDiskStore(stmfGuid *, char *);
1258fe96085Stim szeto static int persistDiskGuid(stmfGuid *, char *, boolean_t);
1268fe96085Stim szeto static int setDiskProp(luResourceImpl *, uint32_t, const char *);
1278fe96085Stim szeto static int checkHexUpper(char *);
1288fe96085Stim szeto static int strToShift(const char *);
1298fe96085Stim szeto static int niceStrToNum(const char *, uint64_t *);
1308fe96085Stim szeto static void diskError(uint32_t, int *);
1318fe96085Stim szeto static int importDiskLu(char *fname, stmfGuid *);
1328fe96085Stim szeto static int modifyDiskLu(diskResource *, stmfGuid *, const char *);
1338fe96085Stim szeto static int modifyDiskLuProp(stmfGuid *, const char *, uint32_t, const char *);
1348fe96085Stim szeto static int validateModifyDiskProp(uint32_t);
1358fe96085Stim szeto static uint8_t iGetPersistMethod();
1368fe96085Stim szeto static int groupListIoctl(stmfGroupList **, int);
1378fe96085Stim szeto static int iLoadGroupFromPs(stmfGroupList **, int);
1388fe96085Stim szeto static int groupMemberListIoctl(stmfGroupName *, stmfGroupProperties **, int);
1398fe96085Stim szeto static int getProviderData(char *, nvlist_t **, int, uint64_t *);
14045039663SJohn Forte static int setDiskStandby(stmfGuid *luGuid);
1418fe96085Stim szeto static int viewEntryCompare(const void *, const void *);
14245039663SJohn Forte static void deleteNonActiveLus();
1438fe96085Stim szeto 
1448fe96085Stim szeto static pthread_mutex_t persistenceTypeLock = PTHREAD_MUTEX_INITIALIZER;
1458fe96085Stim szeto static int iPersistType = 0;
1468fe96085Stim szeto /* when B_TRUE, no need to access SMF anymore. Just use iPersistType */
1478fe96085Stim szeto static boolean_t iLibSetPersist = B_FALSE;
148fcf3ce44SJohn Forte 
149fcf3ce44SJohn Forte /*
150fcf3ce44SJohn Forte  * Open for stmf module
151fcf3ce44SJohn Forte  *
152fcf3ce44SJohn Forte  * flag - open flag (OPEN_STMF, OPEN_EXCL_STMF)
153fcf3ce44SJohn Forte  * fd - pointer to integer. On success, contains the stmf file descriptor
154fcf3ce44SJohn Forte  */
155fcf3ce44SJohn Forte static int
156fcf3ce44SJohn Forte openStmf(int flag, int *fd)
157fcf3ce44SJohn Forte {
158fcf3ce44SJohn Forte 	int ret = STMF_STATUS_ERROR;
159fcf3ce44SJohn Forte 
160fcf3ce44SJohn Forte 	if ((*fd = open(STMF_PATH, O_NDELAY | O_RDONLY | flag)) != -1) {
161fcf3ce44SJohn Forte 		ret = STMF_STATUS_SUCCESS;
162fcf3ce44SJohn Forte 	} else {
163fcf3ce44SJohn Forte 		if (errno == EBUSY) {
164fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1658fe96085Stim szeto 		} else if (errno == EACCES) {
1668fe96085Stim szeto 			ret = STMF_ERROR_PERM;
167fcf3ce44SJohn Forte 		} else {
168fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
169fcf3ce44SJohn Forte 		}
170fcf3ce44SJohn Forte 		syslog(LOG_DEBUG, "openStmf:open failure:%s:errno(%d)",
171fcf3ce44SJohn Forte 		    STMF_PATH, errno);
172fcf3ce44SJohn Forte 	}
173fcf3ce44SJohn Forte 
174fcf3ce44SJohn Forte 	return (ret);
175fcf3ce44SJohn Forte }
176fcf3ce44SJohn Forte 
177fcf3ce44SJohn Forte /*
1788fe96085Stim szeto  * Open for sbd module
1798fe96085Stim szeto  *
18045039663SJohn Forte  * flag - open flag (OPEN_SBD, OPEN_EXCL_SBD)
1818fe96085Stim szeto  * fd - pointer to integer. On success, contains the stmf file descriptor
1828fe96085Stim szeto  */
1838fe96085Stim szeto static int
1848fe96085Stim szeto openSbd(int flag, int *fd)
1858fe96085Stim szeto {
1868fe96085Stim szeto 	int ret = STMF_STATUS_ERROR;
1878fe96085Stim szeto 
1888fe96085Stim szeto 	if ((*fd = open(SBD_PATH, O_NDELAY | O_RDONLY | flag)) != -1) {
1898fe96085Stim szeto 		ret = STMF_STATUS_SUCCESS;
1908fe96085Stim szeto 	} else {
1918fe96085Stim szeto 		if (errno == EBUSY) {
1928fe96085Stim szeto 			ret = STMF_ERROR_BUSY;
1938fe96085Stim szeto 		} else if (errno == EACCES) {
1948fe96085Stim szeto 			ret = STMF_ERROR_PERM;
1958fe96085Stim szeto 		} else {
1968fe96085Stim szeto 			ret = STMF_STATUS_ERROR;
1978fe96085Stim szeto 		}
1988fe96085Stim szeto 		syslog(LOG_DEBUG, "openSbd:open failure:%s:errno(%d)",
1998fe96085Stim szeto 		    SBD_PATH, errno);
2008fe96085Stim szeto 	}
2018fe96085Stim szeto 
2028fe96085Stim szeto 	return (ret);
2038fe96085Stim szeto }
2048fe96085Stim szeto 
2058fe96085Stim szeto /*
20645039663SJohn Forte  * Open for pppt module
20745039663SJohn Forte  *
20845039663SJohn Forte  * flag - open flag (OPEN_PPPT, OPEN_EXCL_PPPT)
20945039663SJohn Forte  * fd - pointer to integer. On success, contains the stmf file descriptor
21045039663SJohn Forte  */
21145039663SJohn Forte static int
21245039663SJohn Forte openPppt(int flag, int *fd)
21345039663SJohn Forte {
21445039663SJohn Forte 	int ret = STMF_STATUS_ERROR;
21545039663SJohn Forte 
21645039663SJohn Forte 	if ((*fd = open(PPPT_PATH, O_RDONLY | flag)) != -1) {
21745039663SJohn Forte 		ret = STMF_STATUS_SUCCESS;
21845039663SJohn Forte 	} else {
21945039663SJohn Forte 		if (errno == EBUSY) {
22045039663SJohn Forte 			ret = STMF_ERROR_BUSY;
22145039663SJohn Forte 		} else if (errno == EACCES) {
22245039663SJohn Forte 			ret = STMF_ERROR_PERM;
22345039663SJohn Forte 		} else {
22445039663SJohn Forte 			ret = STMF_STATUS_ERROR;
22545039663SJohn Forte 		}
22645039663SJohn Forte 		syslog(LOG_DEBUG, "openPppt:open failure:%s:errno(%d)",
22745039663SJohn Forte 		    PPPT_PATH, errno);
22845039663SJohn Forte 	}
22945039663SJohn Forte 
23045039663SJohn Forte 	return (ret);
23145039663SJohn Forte }
23245039663SJohn Forte 
23345039663SJohn Forte /*
234fcf3ce44SJohn Forte  * initializeConfig
235fcf3ce44SJohn Forte  *
236fcf3ce44SJohn Forte  * This routine should be called before any ioctl requiring initialization
237fcf3ce44SJohn Forte  * which is basically everything except stmfGetState(), setStmfState() and
238fcf3ce44SJohn Forte  * stmfLoadConfig().
239fcf3ce44SJohn Forte  */
240fcf3ce44SJohn Forte static int
241fcf3ce44SJohn Forte initializeConfig()
242fcf3ce44SJohn Forte {
243fcf3ce44SJohn Forte 	int ret;
244fcf3ce44SJohn Forte 	stmfState state;
245fcf3ce44SJohn Forte 
246fcf3ce44SJohn Forte 
247fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
248fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
249fcf3ce44SJohn Forte 		return (ret);
250fcf3ce44SJohn Forte 	}
251fcf3ce44SJohn Forte 
252fcf3ce44SJohn Forte 	/* if we've already initialized or in the process, return success */
253fcf3ce44SJohn Forte 	if (state.configState == STMF_CONFIG_STATE_INIT_DONE ||
254fcf3ce44SJohn Forte 	    state.configState == STMF_CONFIG_STATE_INIT) {
255fcf3ce44SJohn Forte 		return (STMF_STATUS_SUCCESS);
256fcf3ce44SJohn Forte 	}
257fcf3ce44SJohn Forte 
258fcf3ce44SJohn Forte 	ret = stmfLoadConfig();
259fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
260fcf3ce44SJohn Forte 		syslog(LOG_DEBUG,
261fcf3ce44SJohn Forte 		    "initializeConfig:stmfLoadConfig:error(%d)", ret);
262fcf3ce44SJohn Forte 		return (ret);
263fcf3ce44SJohn Forte 	}
264fcf3ce44SJohn Forte 
265fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
266fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
267fcf3ce44SJohn Forte 		syslog(LOG_DEBUG,
268fcf3ce44SJohn Forte 		    "initializeConfig:stmfGetState:error(%d)", ret);
269fcf3ce44SJohn Forte 		return (ret);
270fcf3ce44SJohn Forte 	}
271fcf3ce44SJohn Forte 
272fcf3ce44SJohn Forte 	if (state.configState != STMF_CONFIG_STATE_INIT_DONE) {
273fcf3ce44SJohn Forte 		syslog(LOG_DEBUG, "initializeConfig:state.configState(%d)",
274fcf3ce44SJohn Forte 		    state.configState);
275fcf3ce44SJohn Forte 		ret = STMF_STATUS_ERROR;
276fcf3ce44SJohn Forte 	}
277fcf3ce44SJohn Forte 
278fcf3ce44SJohn Forte 	return (ret);
279fcf3ce44SJohn Forte }
280fcf3ce44SJohn Forte 
281fcf3ce44SJohn Forte 
282fcf3ce44SJohn Forte /*
283fcf3ce44SJohn Forte  * groupIoctl
284fcf3ce44SJohn Forte  *
285fcf3ce44SJohn Forte  * Purpose: issue ioctl for create/delete on group
286fcf3ce44SJohn Forte  *
287fcf3ce44SJohn Forte  * cmd - valid STMF ioctl group cmd
288fcf3ce44SJohn Forte  * groupName - groupName to create or delete
289fcf3ce44SJohn Forte  */
290fcf3ce44SJohn Forte static int
291fcf3ce44SJohn Forte groupIoctl(int fd, int cmd, stmfGroupName *groupName)
292fcf3ce44SJohn Forte {
293fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
294fcf3ce44SJohn Forte 	int ioctlRet;
295fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
296fcf3ce44SJohn Forte 	stmf_group_name_t iGroupName;
297fcf3ce44SJohn Forte 
298fcf3ce44SJohn Forte 	bzero(&iGroupName, sizeof (iGroupName));
299fcf3ce44SJohn Forte 
300fcf3ce44SJohn Forte 	bcopy(groupName, &iGroupName.name, strlen((char *)groupName));
301fcf3ce44SJohn Forte 
302fcf3ce44SJohn Forte 	iGroupName.name_size = strlen((char *)groupName);
303fcf3ce44SJohn Forte 
304fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
305fcf3ce44SJohn Forte 	/*
306fcf3ce44SJohn Forte 	 * Issue ioctl to create the host group
307fcf3ce44SJohn Forte 	 */
308fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
309fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (iGroupName);
310fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&iGroupName;
311fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
312fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
313fcf3ce44SJohn Forte 		switch (errno) {
3148fe96085Stim szeto 			case EPERM:
315fcf3ce44SJohn Forte 			case EACCES:
316fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
317fcf3ce44SJohn Forte 				break;
318fcf3ce44SJohn Forte 			default:
319fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
320fcf3ce44SJohn Forte 					case STMF_IOCERR_TG_EXISTS:
321fcf3ce44SJohn Forte 					case STMF_IOCERR_HG_EXISTS:
322fcf3ce44SJohn Forte 						ret = STMF_ERROR_EXISTS;
323fcf3ce44SJohn Forte 						break;
324fcf3ce44SJohn Forte 					case STMF_IOCERR_TG_IN_USE:
325fcf3ce44SJohn Forte 					case STMF_IOCERR_HG_IN_USE:
326fcf3ce44SJohn Forte 						ret = STMF_ERROR_GROUP_IN_USE;
327fcf3ce44SJohn Forte 						break;
328fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG:
329fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG:
330fcf3ce44SJohn Forte 						ret = STMF_ERROR_NOT_FOUND;
331fcf3ce44SJohn Forte 						break;
332fcf3ce44SJohn Forte 					default:
333fcf3ce44SJohn Forte 						syslog(LOG_DEBUG,
334fcf3ce44SJohn Forte 						    "groupIoctl:error(%d)",
335fcf3ce44SJohn Forte 						    stmfIoctl.stmf_error);
336fcf3ce44SJohn Forte 						ret = STMF_STATUS_ERROR;
337fcf3ce44SJohn Forte 						break;
338fcf3ce44SJohn Forte 				}
339fcf3ce44SJohn Forte 				break;
340fcf3ce44SJohn Forte 		}
341fcf3ce44SJohn Forte 	}
342fcf3ce44SJohn Forte done:
343fcf3ce44SJohn Forte 	return (ret);
344fcf3ce44SJohn Forte }
345fcf3ce44SJohn Forte 
346fcf3ce44SJohn Forte /*
3478fe96085Stim szeto  * groupMemberIoctl
348fcf3ce44SJohn Forte  *
349fcf3ce44SJohn Forte  * Purpose: issue ioctl for add/remove member on group
350fcf3ce44SJohn Forte  *
351fcf3ce44SJohn Forte  * cmd - valid STMF ioctl group member cmd
352fcf3ce44SJohn Forte  * groupName - groupName to add to or remove from
353fcf3ce44SJohn Forte  * devid - group member to add or remove
354fcf3ce44SJohn Forte  */
355fcf3ce44SJohn Forte static int
356fcf3ce44SJohn Forte groupMemberIoctl(int fd, int cmd, stmfGroupName *groupName, stmfDevid *devid)
357fcf3ce44SJohn Forte {
358fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
359fcf3ce44SJohn Forte 	int ioctlRet;
360fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
361fcf3ce44SJohn Forte 	stmf_group_op_data_t stmfGroupData;
362fcf3ce44SJohn Forte 
363fcf3ce44SJohn Forte 	bzero(&stmfGroupData, sizeof (stmfGroupData));
364fcf3ce44SJohn Forte 
365fcf3ce44SJohn Forte 	bcopy(groupName, &stmfGroupData.group.name, strlen((char *)groupName));
366fcf3ce44SJohn Forte 
367fcf3ce44SJohn Forte 	stmfGroupData.group.name_size = strlen((char *)groupName);
368fcf3ce44SJohn Forte 	stmfGroupData.ident[IDENT_LENGTH_BYTE] = devid->identLength;
369fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &stmfGroupData.ident[IDENT_LENGTH_BYTE + 1],
370fcf3ce44SJohn Forte 	    devid->identLength);
371fcf3ce44SJohn Forte 
372fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
373fcf3ce44SJohn Forte 	/*
374fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
375fcf3ce44SJohn Forte 	 */
376fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
377fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGroupData);
378fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&stmfGroupData;
379fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
380fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
381fcf3ce44SJohn Forte 		switch (errno) {
382fcf3ce44SJohn Forte 			case EBUSY:
3835c8cac22Stim szeto 				switch (stmfIoctl.stmf_error) {
3845c8cac22Stim szeto 					case STMF_IOCERR_TG_NEED_TG_OFFLINE:
3855c8cac22Stim szeto 						ret = STMF_ERROR_TG_ONLINE;
3865c8cac22Stim szeto 						break;
3875c8cac22Stim szeto 					default:
388fcf3ce44SJohn Forte 						ret = STMF_ERROR_BUSY;
389fcf3ce44SJohn Forte 						break;
3905c8cac22Stim szeto 				}
3915c8cac22Stim szeto 				break;
3928fe96085Stim szeto 			case EPERM:
393fcf3ce44SJohn Forte 			case EACCES:
394fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
395fcf3ce44SJohn Forte 				break;
396fcf3ce44SJohn Forte 			default:
397fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
398fcf3ce44SJohn Forte 					case STMF_IOCERR_TG_ENTRY_EXISTS:
399fcf3ce44SJohn Forte 					case STMF_IOCERR_HG_ENTRY_EXISTS:
400fcf3ce44SJohn Forte 						ret = STMF_ERROR_EXISTS;
401fcf3ce44SJohn Forte 						break;
402fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG_ENTRY:
403fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG_ENTRY:
404fcf3ce44SJohn Forte 						ret =
405fcf3ce44SJohn Forte 						    STMF_ERROR_MEMBER_NOT_FOUND;
406fcf3ce44SJohn Forte 						break;
407fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG:
408fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG:
409fcf3ce44SJohn Forte 						ret =
410fcf3ce44SJohn Forte 						    STMF_ERROR_GROUP_NOT_FOUND;
411fcf3ce44SJohn Forte 						break;
412fcf3ce44SJohn Forte 					default:
413fcf3ce44SJohn Forte 						syslog(LOG_DEBUG,
414fcf3ce44SJohn Forte 						    "groupMemberIoctl:error"
415fcf3ce44SJohn Forte 						    "(%d)",
416fcf3ce44SJohn Forte 						    stmfIoctl.stmf_error);
417fcf3ce44SJohn Forte 						ret = STMF_STATUS_ERROR;
418fcf3ce44SJohn Forte 						break;
419fcf3ce44SJohn Forte 				}
420fcf3ce44SJohn Forte 				break;
421fcf3ce44SJohn Forte 		}
422fcf3ce44SJohn Forte 	}
423fcf3ce44SJohn Forte done:
424fcf3ce44SJohn Forte 	return (ret);
425fcf3ce44SJohn Forte }
426fcf3ce44SJohn Forte 
427fcf3ce44SJohn Forte /*
4288fe96085Stim szeto  * qsort function
4298fe96085Stim szeto  * sort on veIndex
4308fe96085Stim szeto  */
4318fe96085Stim szeto static int
4328fe96085Stim szeto viewEntryCompare(const void *p1, const void *p2)
4338fe96085Stim szeto {
4348fe96085Stim szeto 
4358fe96085Stim szeto 	stmfViewEntry *v1 = (stmfViewEntry *)p1, *v2 = (stmfViewEntry *)p2;
4368fe96085Stim szeto 	if (v1->veIndex > v2->veIndex)
4378fe96085Stim szeto 		return (1);
4388fe96085Stim szeto 	if (v1->veIndex < v2->veIndex)
4398fe96085Stim szeto 		return (-1);
4408fe96085Stim szeto 	return (0);
4418fe96085Stim szeto }
4428fe96085Stim szeto 
4438fe96085Stim szeto /*
444fcf3ce44SJohn Forte  * guidCompare
445fcf3ce44SJohn Forte  *
446fcf3ce44SJohn Forte  * qsort function
447fcf3ce44SJohn Forte  * sort on guid
448fcf3ce44SJohn Forte  */
449fcf3ce44SJohn Forte static int
450fcf3ce44SJohn Forte guidCompare(const void *p1, const void *p2)
451fcf3ce44SJohn Forte {
452fcf3ce44SJohn Forte 
453fcf3ce44SJohn Forte 	stmfGuid *g1 = (stmfGuid *)p1, *g2 = (stmfGuid *)p2;
454fcf3ce44SJohn Forte 	int i;
455fcf3ce44SJohn Forte 
456fcf3ce44SJohn Forte 	for (i = 0; i < sizeof (stmfGuid); i++) {
457fcf3ce44SJohn Forte 		if (g1->guid[i] > g2->guid[i])
458fcf3ce44SJohn Forte 			return (1);
459fcf3ce44SJohn Forte 		if (g1->guid[i] < g2->guid[i])
460fcf3ce44SJohn Forte 			return (-1);
461fcf3ce44SJohn Forte 	}
462fcf3ce44SJohn Forte 
463fcf3ce44SJohn Forte 	return (0);
464fcf3ce44SJohn Forte }
465fcf3ce44SJohn Forte 
466fcf3ce44SJohn Forte /*
467fcf3ce44SJohn Forte  * stmfAddToHostGroup
468fcf3ce44SJohn Forte  *
469fcf3ce44SJohn Forte  * Purpose: Adds an initiator to an existing host group
470fcf3ce44SJohn Forte  *
471fcf3ce44SJohn Forte  * hostGroupName - name of an existing host group
472fcf3ce44SJohn Forte  * hostName - name of initiator to add
473fcf3ce44SJohn Forte  */
474fcf3ce44SJohn Forte int
475fcf3ce44SJohn Forte stmfAddToHostGroup(stmfGroupName *hostGroupName, stmfDevid *hostName)
476fcf3ce44SJohn Forte {
477fcf3ce44SJohn Forte 	int ret;
478fcf3ce44SJohn Forte 	int fd;
479fcf3ce44SJohn Forte 
480fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
481fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
482fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || hostName == NULL) {
483fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
484fcf3ce44SJohn Forte 	}
485fcf3ce44SJohn Forte 
486fcf3ce44SJohn Forte 	/* call init */
487fcf3ce44SJohn Forte 	ret = initializeConfig();
488fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
489fcf3ce44SJohn Forte 		return (ret);
490fcf3ce44SJohn Forte 	}
491fcf3ce44SJohn Forte 
492fcf3ce44SJohn Forte 	/*
493fcf3ce44SJohn Forte 	 * Open control node for stmf
494fcf3ce44SJohn Forte 	 */
495fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
496fcf3ce44SJohn Forte 		return (ret);
497fcf3ce44SJohn Forte 
498fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_HG_ENTRY, hostGroupName,
499fcf3ce44SJohn Forte 	    hostName)) != STMF_STATUS_SUCCESS) {
500fcf3ce44SJohn Forte 		goto done;
501fcf3ce44SJohn Forte 	}
502fcf3ce44SJohn Forte 
5038fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
5048fe96085Stim szeto 		goto done;
5058fe96085Stim szeto 	}
5068fe96085Stim szeto 
507fcf3ce44SJohn Forte 	ret = psAddHostGroupMember((char *)hostGroupName,
508fcf3ce44SJohn Forte 	    (char *)hostName->ident);
509fcf3ce44SJohn Forte 	switch (ret) {
510fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
511fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
512fcf3ce44SJohn Forte 			break;
513fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
514fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
515fcf3ce44SJohn Forte 			break;
516fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
517fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
518fcf3ce44SJohn Forte 			break;
519fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
520fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
521fcf3ce44SJohn Forte 			break;
522fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
523fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
524fcf3ce44SJohn Forte 			break;
525fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
526fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
527fcf3ce44SJohn Forte 			break;
528fcf3ce44SJohn Forte 		default:
529fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
530fcf3ce44SJohn Forte 			    "stmfAddToHostGroup:psAddHostGroupMember:error(%d)",
531fcf3ce44SJohn Forte 			    ret);
532fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
533fcf3ce44SJohn Forte 			break;
534fcf3ce44SJohn Forte 	}
535fcf3ce44SJohn Forte 
536fcf3ce44SJohn Forte done:
537fcf3ce44SJohn Forte 	(void) close(fd);
538fcf3ce44SJohn Forte 	return (ret);
539fcf3ce44SJohn Forte }
540fcf3ce44SJohn Forte 
541fcf3ce44SJohn Forte /*
542fcf3ce44SJohn Forte  * stmfAddToTargetGroup
543fcf3ce44SJohn Forte  *
544fcf3ce44SJohn Forte  * Purpose: Adds a local port to an existing target group
545fcf3ce44SJohn Forte  *
546fcf3ce44SJohn Forte  * targetGroupName - name of an existing target group
547fcf3ce44SJohn Forte  * targetName - name of target to add
548fcf3ce44SJohn Forte  */
549fcf3ce44SJohn Forte int
550fcf3ce44SJohn Forte stmfAddToTargetGroup(stmfGroupName *targetGroupName, stmfDevid *targetName)
551fcf3ce44SJohn Forte {
552fcf3ce44SJohn Forte 	int ret;
553fcf3ce44SJohn Forte 	int fd;
554fcf3ce44SJohn Forte 
555fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
556fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
557fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || targetName == NULL) {
558fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
559fcf3ce44SJohn Forte 	}
560fcf3ce44SJohn Forte 
561fcf3ce44SJohn Forte 	/* call init */
562fcf3ce44SJohn Forte 	ret = initializeConfig();
563fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
564fcf3ce44SJohn Forte 		return (ret);
565fcf3ce44SJohn Forte 	}
566fcf3ce44SJohn Forte 
567fcf3ce44SJohn Forte 	/*
568fcf3ce44SJohn Forte 	 * Open control node for stmf
569fcf3ce44SJohn Forte 	 */
570fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
571fcf3ce44SJohn Forte 		return (ret);
572fcf3ce44SJohn Forte 
573fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_TG_ENTRY,
574fcf3ce44SJohn Forte 	    targetGroupName, targetName)) != STMF_STATUS_SUCCESS) {
575fcf3ce44SJohn Forte 		goto done;
576fcf3ce44SJohn Forte 	}
577fcf3ce44SJohn Forte 
5788fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
5798fe96085Stim szeto 		goto done;
5808fe96085Stim szeto 	}
5818fe96085Stim szeto 
582fcf3ce44SJohn Forte 	ret = psAddTargetGroupMember((char *)targetGroupName,
583fcf3ce44SJohn Forte 	    (char *)targetName->ident);
584fcf3ce44SJohn Forte 	switch (ret) {
585fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
586fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
587fcf3ce44SJohn Forte 			break;
588fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
589fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
590fcf3ce44SJohn Forte 			break;
591fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
592fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
593fcf3ce44SJohn Forte 			break;
594fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
595fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
596fcf3ce44SJohn Forte 			break;
597fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
598fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
599fcf3ce44SJohn Forte 			break;
600fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
601fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
602fcf3ce44SJohn Forte 			break;
603fcf3ce44SJohn Forte 		default:
604fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
605fcf3ce44SJohn Forte 			    "stmfAddToTargetGroup:psAddTargetGroupMember:"
606fcf3ce44SJohn Forte 			    "error(%d)", ret);
607fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
608fcf3ce44SJohn Forte 			break;
609fcf3ce44SJohn Forte 	}
610fcf3ce44SJohn Forte 
611fcf3ce44SJohn Forte done:
612fcf3ce44SJohn Forte 	(void) close(fd);
613fcf3ce44SJohn Forte 	return (ret);
614fcf3ce44SJohn Forte }
615fcf3ce44SJohn Forte 
616fcf3ce44SJohn Forte /*
617fcf3ce44SJohn Forte  * addViewEntryIoctl
618fcf3ce44SJohn Forte  *
619fcf3ce44SJohn Forte  * Purpose: Issues ioctl to add a view entry
620fcf3ce44SJohn Forte  *
621fcf3ce44SJohn Forte  * lu - Logical Unit identifier to which the view entry is added
622fcf3ce44SJohn Forte  * viewEntry - view entry to add
623fcf3ce44SJohn Forte  * init - When set to B_TRUE, we are in the init state, i.e. don't call open
624fcf3ce44SJohn Forte  */
625fcf3ce44SJohn Forte static int
626fcf3ce44SJohn Forte addViewEntryIoctl(int fd, stmfGuid *lu, stmfViewEntry *viewEntry)
627fcf3ce44SJohn Forte {
628fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
629fcf3ce44SJohn Forte 	int ioctlRet;
630fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
631fcf3ce44SJohn Forte 	stmf_view_op_entry_t ioctlViewEntry;
632fcf3ce44SJohn Forte 
633fcf3ce44SJohn Forte 	bzero(&ioctlViewEntry, sizeof (ioctlViewEntry));
634fcf3ce44SJohn Forte 	/*
635fcf3ce44SJohn Forte 	 * don't set ve_ndx or ve_ndx_valid as ve_ndx_valid should be
636fcf3ce44SJohn Forte 	 * false on input
637fcf3ce44SJohn Forte 	 */
638fcf3ce44SJohn Forte 	ioctlViewEntry.ve_lu_number_valid = viewEntry->luNbrValid;
639fcf3ce44SJohn Forte 	ioctlViewEntry.ve_all_hosts = viewEntry->allHosts;
640fcf3ce44SJohn Forte 	ioctlViewEntry.ve_all_targets = viewEntry->allTargets;
641fcf3ce44SJohn Forte 
642fcf3ce44SJohn Forte 	if (viewEntry->allHosts == B_FALSE) {
643fcf3ce44SJohn Forte 		bcopy(viewEntry->hostGroup, &ioctlViewEntry.ve_host_group.name,
644fcf3ce44SJohn Forte 		    sizeof (stmfGroupName));
645fcf3ce44SJohn Forte 		ioctlViewEntry.ve_host_group.name_size =
646fcf3ce44SJohn Forte 		    strlen((char *)viewEntry->hostGroup);
647fcf3ce44SJohn Forte 	}
648fcf3ce44SJohn Forte 	if (viewEntry->allTargets == B_FALSE) {
649fcf3ce44SJohn Forte 		bcopy(viewEntry->targetGroup,
650fcf3ce44SJohn Forte 		    &ioctlViewEntry.ve_target_group.name,
651fcf3ce44SJohn Forte 		    sizeof (stmfGroupName));
652fcf3ce44SJohn Forte 		ioctlViewEntry.ve_target_group.name_size =
653fcf3ce44SJohn Forte 		    strlen((char *)viewEntry->targetGroup);
654fcf3ce44SJohn Forte 	}
655fcf3ce44SJohn Forte 	if (viewEntry->luNbrValid) {
656fcf3ce44SJohn Forte 		bcopy(viewEntry->luNbr, &ioctlViewEntry.ve_lu_nbr,
657fcf3ce44SJohn Forte 		    sizeof (ioctlViewEntry.ve_lu_nbr));
658fcf3ce44SJohn Forte 	}
659fcf3ce44SJohn Forte 	bcopy(lu, &ioctlViewEntry.ve_guid, sizeof (stmfGuid));
660fcf3ce44SJohn Forte 
661fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
662fcf3ce44SJohn Forte 	/*
663fcf3ce44SJohn Forte 	 * Issue ioctl to add to the view entry
664fcf3ce44SJohn Forte 	 */
665fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
666fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ioctlViewEntry);
667fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ioctlViewEntry;
668fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (ioctlViewEntry);
669fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&ioctlViewEntry;
670fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_ADD_VIEW_ENTRY, &stmfIoctl);
671fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
672fcf3ce44SJohn Forte 		switch (errno) {
673fcf3ce44SJohn Forte 			case EBUSY:
674fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
675fcf3ce44SJohn Forte 				break;
6768fe96085Stim szeto 			case EPERM:
6778fe96085Stim szeto 				ret = STMF_ERROR_PERM;
6788fe96085Stim szeto 				break;
679fcf3ce44SJohn Forte 			case EACCES:
680fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
681fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
682fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
683fcf3ce44SJohn Forte 						break;
684fcf3ce44SJohn Forte 					default:
685fcf3ce44SJohn Forte 						ret = STMF_ERROR_PERM;
686fcf3ce44SJohn Forte 						break;
687fcf3ce44SJohn Forte 				}
688fcf3ce44SJohn Forte 				break;
689fcf3ce44SJohn Forte 			default:
690fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
691fcf3ce44SJohn Forte 					case STMF_IOCERR_LU_NUMBER_IN_USE:
692fcf3ce44SJohn Forte 						ret = STMF_ERROR_LUN_IN_USE;
693fcf3ce44SJohn Forte 						break;
694fcf3ce44SJohn Forte 					case STMF_IOCERR_VIEW_ENTRY_CONFLICT:
695fcf3ce44SJohn Forte 						ret = STMF_ERROR_VE_CONFLICT;
696fcf3ce44SJohn Forte 						break;
697fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
698fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
699fcf3ce44SJohn Forte 						break;
700fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG:
701fcf3ce44SJohn Forte 						ret = STMF_ERROR_INVALID_HG;
702fcf3ce44SJohn Forte 						break;
703fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG:
704fcf3ce44SJohn Forte 						ret = STMF_ERROR_INVALID_TG;
705fcf3ce44SJohn Forte 						break;
706fcf3ce44SJohn Forte 					default:
707fcf3ce44SJohn Forte 						syslog(LOG_DEBUG,
708fcf3ce44SJohn Forte 						    "addViewEntryIoctl"
709fcf3ce44SJohn Forte 						    ":error(%d)",
710fcf3ce44SJohn Forte 						    stmfIoctl.stmf_error);
711fcf3ce44SJohn Forte 						ret = STMF_STATUS_ERROR;
712fcf3ce44SJohn Forte 						break;
713fcf3ce44SJohn Forte 				}
714fcf3ce44SJohn Forte 				break;
715fcf3ce44SJohn Forte 		}
716fcf3ce44SJohn Forte 		goto done;
717fcf3ce44SJohn Forte 	}
718fcf3ce44SJohn Forte 
719fcf3ce44SJohn Forte 	/* copy lu nbr back to caller's view entry on success */
720fcf3ce44SJohn Forte 	viewEntry->veIndex = ioctlViewEntry.ve_ndx;
721fcf3ce44SJohn Forte 	if (ioctlViewEntry.ve_lu_number_valid) {
722fcf3ce44SJohn Forte 		bcopy(&ioctlViewEntry.ve_lu_nbr, viewEntry->luNbr,
723fcf3ce44SJohn Forte 		    sizeof (ioctlViewEntry.ve_lu_nbr));
724fcf3ce44SJohn Forte 	}
725fcf3ce44SJohn Forte 	viewEntry->luNbrValid = B_TRUE;
726fcf3ce44SJohn Forte 
727fcf3ce44SJohn Forte done:
728fcf3ce44SJohn Forte 	return (ret);
729fcf3ce44SJohn Forte }
730fcf3ce44SJohn Forte 
731fcf3ce44SJohn Forte /*
732fcf3ce44SJohn Forte  * stmfAddViewEntry
733fcf3ce44SJohn Forte  *
734fcf3ce44SJohn Forte  * Purpose: Adds a view entry to a logical unit
735fcf3ce44SJohn Forte  *
736fcf3ce44SJohn Forte  * lu - guid of the logical unit to which the view entry is added
737fcf3ce44SJohn Forte  * viewEntry - view entry structure to add
738fcf3ce44SJohn Forte  */
739fcf3ce44SJohn Forte int
740fcf3ce44SJohn Forte stmfAddViewEntry(stmfGuid *lu, stmfViewEntry *viewEntry)
741fcf3ce44SJohn Forte {
742fcf3ce44SJohn Forte 	int ret;
743fcf3ce44SJohn Forte 	int fd;
744fcf3ce44SJohn Forte 	stmfViewEntry iViewEntry;
745fcf3ce44SJohn Forte 
746fcf3ce44SJohn Forte 	if (lu == NULL || viewEntry == NULL) {
747fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
748fcf3ce44SJohn Forte 	}
749fcf3ce44SJohn Forte 
750fcf3ce44SJohn Forte 	/* initialize and set internal view entry */
751fcf3ce44SJohn Forte 	bzero(&iViewEntry, sizeof (iViewEntry));
752fcf3ce44SJohn Forte 
753fcf3ce44SJohn Forte 	if (!viewEntry->allHosts) {
754fcf3ce44SJohn Forte 		bcopy(viewEntry->hostGroup, iViewEntry.hostGroup,
755fcf3ce44SJohn Forte 		    sizeof (iViewEntry.hostGroup));
756fcf3ce44SJohn Forte 	} else {
757fcf3ce44SJohn Forte 		iViewEntry.allHosts = B_TRUE;
758fcf3ce44SJohn Forte 	}
759fcf3ce44SJohn Forte 
760fcf3ce44SJohn Forte 	if (!viewEntry->allTargets) {
761fcf3ce44SJohn Forte 		bcopy(viewEntry->targetGroup, iViewEntry.targetGroup,
762fcf3ce44SJohn Forte 		    sizeof (iViewEntry.targetGroup));
763fcf3ce44SJohn Forte 	} else {
764fcf3ce44SJohn Forte 		iViewEntry.allTargets = B_TRUE;
765fcf3ce44SJohn Forte 	}
766fcf3ce44SJohn Forte 
767fcf3ce44SJohn Forte 	if (viewEntry->luNbrValid) {
768fcf3ce44SJohn Forte 		iViewEntry.luNbrValid = B_TRUE;
769fcf3ce44SJohn Forte 		bcopy(viewEntry->luNbr, iViewEntry.luNbr,
770fcf3ce44SJohn Forte 		    sizeof (iViewEntry.luNbr));
771fcf3ce44SJohn Forte 	}
772fcf3ce44SJohn Forte 
773fcf3ce44SJohn Forte 	/*
774fcf3ce44SJohn Forte 	 * set users return view entry index valid flag to false
775fcf3ce44SJohn Forte 	 * in case of failure
776fcf3ce44SJohn Forte 	 */
777fcf3ce44SJohn Forte 	viewEntry->veIndexValid = B_FALSE;
778fcf3ce44SJohn Forte 
779fcf3ce44SJohn Forte 	/* Check to ensure service exists */
780fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
781fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
782fcf3ce44SJohn Forte 	}
783fcf3ce44SJohn Forte 
784fcf3ce44SJohn Forte 	/* call init */
785fcf3ce44SJohn Forte 	ret = initializeConfig();
786fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
787fcf3ce44SJohn Forte 		return (ret);
788fcf3ce44SJohn Forte 	}
789fcf3ce44SJohn Forte 
790fcf3ce44SJohn Forte 	/*
791fcf3ce44SJohn Forte 	 * Open control node for stmf
792fcf3ce44SJohn Forte 	 */
793fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
794fcf3ce44SJohn Forte 		return (ret);
795fcf3ce44SJohn Forte 
796fcf3ce44SJohn Forte 	/*
797fcf3ce44SJohn Forte 	 * First add the view entry to the driver
798fcf3ce44SJohn Forte 	 */
799fcf3ce44SJohn Forte 	ret = addViewEntryIoctl(fd, lu, &iViewEntry);
800fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
801fcf3ce44SJohn Forte 		goto done;
802fcf3ce44SJohn Forte 	}
803fcf3ce44SJohn Forte 
8048fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
8058fe96085Stim szeto 		goto done;
8068fe96085Stim szeto 	}
8078fe96085Stim szeto 
808fcf3ce44SJohn Forte 	/*
809fcf3ce44SJohn Forte 	 * If the add to driver was successful, add it to the persistent
810fcf3ce44SJohn Forte 	 * store.
811fcf3ce44SJohn Forte 	 */
812fcf3ce44SJohn Forte 	ret = psAddViewEntry(lu, &iViewEntry);
813fcf3ce44SJohn Forte 	switch (ret) {
814fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
815fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
816fcf3ce44SJohn Forte 			break;
817fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
818fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
819fcf3ce44SJohn Forte 			break;
820fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
821fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
822fcf3ce44SJohn Forte 			break;
823fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
824fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
825fcf3ce44SJohn Forte 			break;
826fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
827fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
828fcf3ce44SJohn Forte 			break;
829fcf3ce44SJohn Forte 		default:
830fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
831fcf3ce44SJohn Forte 			    "stmfAddViewEntry:psAddViewEntry:error(%d)", ret);
832fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
833fcf3ce44SJohn Forte 			break;
834fcf3ce44SJohn Forte 	}
835fcf3ce44SJohn Forte 
836fcf3ce44SJohn Forte done:
837fcf3ce44SJohn Forte 	(void) close(fd);
838fcf3ce44SJohn Forte 
839fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
840fcf3ce44SJohn Forte 		/* set caller's view entry on success */
841fcf3ce44SJohn Forte 		viewEntry->veIndexValid = iViewEntry.veIndexValid;
842fcf3ce44SJohn Forte 		viewEntry->veIndex = iViewEntry.veIndex;
843fcf3ce44SJohn Forte 		viewEntry->luNbrValid = B_TRUE;
844fcf3ce44SJohn Forte 		bcopy(iViewEntry.luNbr, viewEntry->luNbr,
845fcf3ce44SJohn Forte 		    sizeof (iViewEntry.luNbr));
846fcf3ce44SJohn Forte 	}
847fcf3ce44SJohn Forte 	return (ret);
848fcf3ce44SJohn Forte }
849fcf3ce44SJohn Forte 
850fcf3ce44SJohn Forte /*
851fcf3ce44SJohn Forte  * stmfClearProviderData
852fcf3ce44SJohn Forte  *
853fcf3ce44SJohn Forte  * Purpose: delete all provider data for specified provider
854fcf3ce44SJohn Forte  *
855fcf3ce44SJohn Forte  * providerName - name of provider for which data should be deleted
856fcf3ce44SJohn Forte  */
857fcf3ce44SJohn Forte int
858fcf3ce44SJohn Forte stmfClearProviderData(char *providerName, int providerType)
859fcf3ce44SJohn Forte {
860fcf3ce44SJohn Forte 	int ret;
861fcf3ce44SJohn Forte 	int fd;
862fcf3ce44SJohn Forte 	int ioctlRet;
863fcf3ce44SJohn Forte 	int savedErrno;
864fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
865fcf3ce44SJohn Forte 	stmf_ppioctl_data_t ppi;
866fcf3ce44SJohn Forte 
867fcf3ce44SJohn Forte 	/* call init */
868fcf3ce44SJohn Forte 	ret = initializeConfig();
869fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
870fcf3ce44SJohn Forte 		return (ret);
871fcf3ce44SJohn Forte 	}
872fcf3ce44SJohn Forte 
873fcf3ce44SJohn Forte 	if (providerName == NULL) {
874fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
875fcf3ce44SJohn Forte 	}
876fcf3ce44SJohn Forte 
877fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
878fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
879fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
880fcf3ce44SJohn Forte 	}
881fcf3ce44SJohn Forte 
882fcf3ce44SJohn Forte 	/*
883fcf3ce44SJohn Forte 	 * Open control node for stmf
884fcf3ce44SJohn Forte 	 */
885fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
886fcf3ce44SJohn Forte 		return (ret);
887fcf3ce44SJohn Forte 
888fcf3ce44SJohn Forte 	bzero(&ppi, sizeof (ppi));
889fcf3ce44SJohn Forte 
890fcf3ce44SJohn Forte 	(void) strncpy(ppi.ppi_name, providerName, sizeof (ppi.ppi_name));
891fcf3ce44SJohn Forte 
892fcf3ce44SJohn Forte 	switch (providerType) {
893fcf3ce44SJohn Forte 		case STMF_LU_PROVIDER_TYPE:
894fcf3ce44SJohn Forte 			ppi.ppi_lu_provider = 1;
895fcf3ce44SJohn Forte 			break;
896fcf3ce44SJohn Forte 		case STMF_PORT_PROVIDER_TYPE:
897fcf3ce44SJohn Forte 			ppi.ppi_port_provider = 1;
898fcf3ce44SJohn Forte 			break;
899fcf3ce44SJohn Forte 		default:
900fcf3ce44SJohn Forte 			ret = STMF_ERROR_INVALID_ARG;
901fcf3ce44SJohn Forte 			goto done;
902fcf3ce44SJohn Forte 	}
903fcf3ce44SJohn Forte 
904fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
905fcf3ce44SJohn Forte 
906fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
907fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_ppioctl_data_t);
908fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ppi;
909fcf3ce44SJohn Forte 
910fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_CLEAR_PP_DATA, &stmfIoctl);
911fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
912fcf3ce44SJohn Forte 		savedErrno = errno;
913fcf3ce44SJohn Forte 		switch (savedErrno) {
914fcf3ce44SJohn Forte 			case EBUSY:
915fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
916fcf3ce44SJohn Forte 				break;
9178fe96085Stim szeto 			case EPERM:
918fcf3ce44SJohn Forte 			case EACCES:
919fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
920fcf3ce44SJohn Forte 				break;
921fcf3ce44SJohn Forte 			default:
922fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
923fcf3ce44SJohn Forte 				    "stmfClearProviderData:ioctl error(%d)",
924fcf3ce44SJohn Forte 				    ioctlRet);
925fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
926fcf3ce44SJohn Forte 				break;
927fcf3ce44SJohn Forte 		}
928fcf3ce44SJohn Forte 		if (savedErrno != ENOENT) {
929fcf3ce44SJohn Forte 			goto done;
930fcf3ce44SJohn Forte 		}
931fcf3ce44SJohn Forte 	}
932fcf3ce44SJohn Forte 
9338fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
9348fe96085Stim szeto 		goto done;
9358fe96085Stim szeto 	}
9368fe96085Stim szeto 
937fcf3ce44SJohn Forte 	ret = psClearProviderData(providerName, providerType);
938fcf3ce44SJohn Forte 	switch (ret) {
939fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
940fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
941fcf3ce44SJohn Forte 			break;
942fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
943fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
944fcf3ce44SJohn Forte 			break;
945fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
946fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
947fcf3ce44SJohn Forte 			break;
948fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
949fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
950fcf3ce44SJohn Forte 			break;
951fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
952fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
953fcf3ce44SJohn Forte 			break;
954fcf3ce44SJohn Forte 		default:
955fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
956fcf3ce44SJohn Forte 			    "stmfClearProviderData:psClearProviderData"
957fcf3ce44SJohn Forte 			    ":error(%d)", ret);
958fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
959fcf3ce44SJohn Forte 			break;
960fcf3ce44SJohn Forte 	}
961fcf3ce44SJohn Forte 
962fcf3ce44SJohn Forte done:
963fcf3ce44SJohn Forte 	(void) close(fd);
964fcf3ce44SJohn Forte 	return (ret);
965fcf3ce44SJohn Forte }
966fcf3ce44SJohn Forte 
967fcf3ce44SJohn Forte /*
968fcf3ce44SJohn Forte  * stmfCreateHostGroup
969fcf3ce44SJohn Forte  *
970fcf3ce44SJohn Forte  * Purpose: Create a new initiator group
971fcf3ce44SJohn Forte  *
972fcf3ce44SJohn Forte  * hostGroupName - name of host group to create
973fcf3ce44SJohn Forte  */
974fcf3ce44SJohn Forte int
975fcf3ce44SJohn Forte stmfCreateHostGroup(stmfGroupName *hostGroupName)
976fcf3ce44SJohn Forte {
977fcf3ce44SJohn Forte 	int ret;
978fcf3ce44SJohn Forte 	int fd;
979fcf3ce44SJohn Forte 
980fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
981fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
982fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName))) {
983fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
984fcf3ce44SJohn Forte 	}
985fcf3ce44SJohn Forte 
986fcf3ce44SJohn Forte 	/* Check to ensure service exists */
987fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
988fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
989fcf3ce44SJohn Forte 	}
990fcf3ce44SJohn Forte 
991fcf3ce44SJohn Forte 	/* call init */
992fcf3ce44SJohn Forte 	ret = initializeConfig();
993fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
994fcf3ce44SJohn Forte 		return (ret);
995fcf3ce44SJohn Forte 	}
996fcf3ce44SJohn Forte 
997fcf3ce44SJohn Forte 	/*
998fcf3ce44SJohn Forte 	 * Open control node for stmf
999fcf3ce44SJohn Forte 	 */
1000fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
1001fcf3ce44SJohn Forte 		return (ret);
1002fcf3ce44SJohn Forte 
1003fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_HOST_GROUP,
1004fcf3ce44SJohn Forte 	    hostGroupName)) != STMF_STATUS_SUCCESS) {
1005fcf3ce44SJohn Forte 		goto done;
1006fcf3ce44SJohn Forte 	}
1007fcf3ce44SJohn Forte 
10088fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
10098fe96085Stim szeto 		goto done;
10108fe96085Stim szeto 	}
10118fe96085Stim szeto 
1012fcf3ce44SJohn Forte 	ret = psCreateHostGroup((char *)hostGroupName);
1013fcf3ce44SJohn Forte 	switch (ret) {
1014fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
1015fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
1016fcf3ce44SJohn Forte 			break;
1017fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
1018fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
1019fcf3ce44SJohn Forte 			break;
1020fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
1021fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1022fcf3ce44SJohn Forte 			break;
1023fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
1024fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
1025fcf3ce44SJohn Forte 			break;
1026fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
1027fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
1028fcf3ce44SJohn Forte 			break;
1029fcf3ce44SJohn Forte 		default:
1030fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
1031fcf3ce44SJohn Forte 			    "stmfCreateHostGroup:psCreateHostGroup:error(%d)",
1032fcf3ce44SJohn Forte 			    ret);
1033fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
1034fcf3ce44SJohn Forte 			break;
1035fcf3ce44SJohn Forte 	}
1036fcf3ce44SJohn Forte 
1037fcf3ce44SJohn Forte done:
1038fcf3ce44SJohn Forte 	(void) close(fd);
1039fcf3ce44SJohn Forte 	return (ret);
1040fcf3ce44SJohn Forte }
1041fcf3ce44SJohn Forte 
1042fcf3ce44SJohn Forte /*
10438fe96085Stim szeto  * stmfCreateLu
10448fe96085Stim szeto  *
10458fe96085Stim szeto  * Purpose: Create a logical unit
10468fe96085Stim szeto  *
10478fe96085Stim szeto  * hdl - handle to logical unit resource created via stmfCreateLuResource
10488fe96085Stim szeto  *
10498fe96085Stim szeto  * luGuid - If non-NULL, on success, contains the guid of the created logical
10508fe96085Stim szeto  *	    unit
10518fe96085Stim szeto  */
10528fe96085Stim szeto int
10538fe96085Stim szeto stmfCreateLu(luResource hdl, stmfGuid *luGuid)
10548fe96085Stim szeto {
10558fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
10568fe96085Stim szeto 	luResourceImpl *luPropsHdl = hdl;
10578fe96085Stim szeto 
10588fe96085Stim szeto 	if (hdl == NULL) {
10598fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
10608fe96085Stim szeto 	}
10618fe96085Stim szeto 
10628fe96085Stim szeto 	if (luPropsHdl->type == STMF_DISK) {
10638fe96085Stim szeto 		ret = createDiskLu((diskResource *)luPropsHdl->resource,
10648fe96085Stim szeto 		    luGuid);
10658fe96085Stim szeto 	} else {
10668fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
10678fe96085Stim szeto 	}
10688fe96085Stim szeto 
10698fe96085Stim szeto 	return (ret);
10708fe96085Stim szeto }
10718fe96085Stim szeto 
10728fe96085Stim szeto /*
10738fe96085Stim szeto  * stmfCreateLuResource
10748fe96085Stim szeto  *
10758fe96085Stim szeto  * Purpose: Create resource handle for a logical unit
10768fe96085Stim szeto  *
10778fe96085Stim szeto  * dType - Type of logical unit resource to create
10788fe96085Stim szeto  *	   Can be: STMF_DISK
10798fe96085Stim szeto  *
10808fe96085Stim szeto  * hdl - pointer to luResource
10818fe96085Stim szeto  */
10828fe96085Stim szeto int
10838fe96085Stim szeto stmfCreateLuResource(uint16_t dType, luResource *hdl)
10848fe96085Stim szeto {
10858fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
10868fe96085Stim szeto 
10878fe96085Stim szeto 	if (dType != STMF_DISK || hdl == NULL) {
10888fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
10898fe96085Stim szeto 	}
10908fe96085Stim szeto 
10918fe96085Stim szeto 	*hdl = calloc(1, sizeof (luResourceImpl));
10928fe96085Stim szeto 	if (*hdl == NULL) {
10938fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
10948fe96085Stim szeto 	}
10958fe96085Stim szeto 
10968fe96085Stim szeto 	ret = createDiskResource((luResourceImpl *)*hdl);
10978fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
10988fe96085Stim szeto 		free(*hdl);
10998fe96085Stim szeto 		return (ret);
11008fe96085Stim szeto 	}
11018fe96085Stim szeto 
11028fe96085Stim szeto 	return (STMF_STATUS_SUCCESS);
11038fe96085Stim szeto }
11048fe96085Stim szeto 
11058fe96085Stim szeto /*
11068fe96085Stim szeto  * Creates a disk logical unit
11078fe96085Stim szeto  *
11088fe96085Stim szeto  * disk - pointer to diskResource structure that represents the properties
11098fe96085Stim szeto  *        for the disk logical unit to be created.
11108fe96085Stim szeto  */
11118fe96085Stim szeto static int
11128fe96085Stim szeto createDiskLu(diskResource *disk, stmfGuid *createdGuid)
11138fe96085Stim szeto {
11148fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
11158fe96085Stim szeto 	int dataFileNameLen = 0;
11168fe96085Stim szeto 	int metaFileNameLen = 0;
11178fe96085Stim szeto 	int serialNumLen = 0;
11188fe96085Stim szeto 	int luAliasLen = 0;
11192f624233SNattuvetty Bhavyan 	int luMgmtUrlLen = 0;
11208fe96085Stim szeto 	int sluBufSize = 0;
11218fe96085Stim szeto 	int bufOffset = 0;
11228fe96085Stim szeto 	int fd = 0;
11238fe96085Stim szeto 	int ioctlRet;
11248fe96085Stim szeto 	int savedErrno;
11258fe96085Stim szeto 	stmfGuid guid;
11268fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
11278fe96085Stim szeto 
11288fe96085Stim szeto 	sbd_create_and_reg_lu_t *sbdLu = NULL;
11298fe96085Stim szeto 
11308fe96085Stim szeto 	/*
11318fe96085Stim szeto 	 * Open control node for sbd
11328fe96085Stim szeto 	 */
11338fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
11348fe96085Stim szeto 		return (ret);
11358fe96085Stim szeto 
11368fe96085Stim szeto 	/* data file name must be specified */
11378fe96085Stim szeto 	if (disk->luDataFileNameValid) {
11388fe96085Stim szeto 		dataFileNameLen = strlen(disk->luDataFileName);
11398fe96085Stim szeto 	} else {
11408fe96085Stim szeto 		(void) close(fd);
11418fe96085Stim szeto 		return (STMF_ERROR_MISSING_PROP_VAL);
11428fe96085Stim szeto 	}
11438fe96085Stim szeto 
11448fe96085Stim szeto 	sluBufSize += dataFileNameLen + 1;
11458fe96085Stim szeto 
11468fe96085Stim szeto 	if (disk->luMetaFileNameValid) {
11478fe96085Stim szeto 		metaFileNameLen = strlen(disk->luMetaFileName);
11488fe96085Stim szeto 		sluBufSize += metaFileNameLen + 1;
11498fe96085Stim szeto 	}
11508fe96085Stim szeto 
11518fe96085Stim szeto 	serialNumLen = strlen(disk->serialNum);
11528fe96085Stim szeto 	sluBufSize += serialNumLen;
11538fe96085Stim szeto 
11548fe96085Stim szeto 	if (disk->luAliasValid) {
11558fe96085Stim szeto 		luAliasLen = strlen(disk->luAlias);
11568fe96085Stim szeto 		sluBufSize += luAliasLen + 1;
11578fe96085Stim szeto 	}
11588fe96085Stim szeto 
11592f624233SNattuvetty Bhavyan 	if (disk->luMgmtUrlValid) {
11602f624233SNattuvetty Bhavyan 		luMgmtUrlLen = strlen(disk->luMgmtUrl);
11612f624233SNattuvetty Bhavyan 		sluBufSize += luMgmtUrlLen + 1;
11622f624233SNattuvetty Bhavyan 	}
11632f624233SNattuvetty Bhavyan 
11648fe96085Stim szeto 	/*
11658fe96085Stim szeto 	 * 8 is the size of the buffer set aside for
11668fe96085Stim szeto 	 * concatenation of variable length fields
11678fe96085Stim szeto 	 */
11688fe96085Stim szeto 	sbdLu = (sbd_create_and_reg_lu_t *)calloc(1,
11698fe96085Stim szeto 	    sizeof (sbd_create_and_reg_lu_t) + sluBufSize - 8);
11708fe96085Stim szeto 	if (sbdLu == NULL) {
11718fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
11728fe96085Stim szeto 	}
11738fe96085Stim szeto 
11748fe96085Stim szeto 	sbdLu->slu_struct_size = sizeof (sbd_create_and_reg_lu_t) +
11758fe96085Stim szeto 	    sluBufSize - 8;
11768fe96085Stim szeto 
11778fe96085Stim szeto 	if (metaFileNameLen) {
11788fe96085Stim szeto 		sbdLu->slu_meta_fname_valid = 1;
11798fe96085Stim szeto 		sbdLu->slu_meta_fname_off = bufOffset;
11808fe96085Stim szeto 		bcopy(disk->luMetaFileName, &(sbdLu->slu_buf[bufOffset]),
11818fe96085Stim szeto 		    metaFileNameLen + 1);
11828fe96085Stim szeto 		bufOffset += metaFileNameLen + 1;
11838fe96085Stim szeto 	}
11848fe96085Stim szeto 
11858fe96085Stim szeto 	bcopy(disk->luDataFileName, &(sbdLu->slu_buf[bufOffset]),
11868fe96085Stim szeto 	    dataFileNameLen + 1);
11878fe96085Stim szeto 	sbdLu->slu_data_fname_off = bufOffset;
11888fe96085Stim szeto 	bufOffset += dataFileNameLen + 1;
11898fe96085Stim szeto 
11908fe96085Stim szeto 	/* currently, serial # is not passed null terminated to the driver */
11918fe96085Stim szeto 	if (disk->serialNumValid) {
11928fe96085Stim szeto 		sbdLu->slu_serial_valid = 1;
11938fe96085Stim szeto 		sbdLu->slu_serial_off = bufOffset;
11948fe96085Stim szeto 		sbdLu->slu_serial_size = serialNumLen;
11958fe96085Stim szeto 		bcopy(disk->serialNum, &(sbdLu->slu_buf[bufOffset]),
11968fe96085Stim szeto 		    serialNumLen);
11978fe96085Stim szeto 		bufOffset += serialNumLen;
11988fe96085Stim szeto 	}
11998fe96085Stim szeto 
12008fe96085Stim szeto 	if (disk->luAliasValid) {
12018fe96085Stim szeto 		sbdLu->slu_alias_valid = 1;
12028fe96085Stim szeto 		sbdLu->slu_alias_off = bufOffset;
12038fe96085Stim szeto 		bcopy(disk->luAlias, &(sbdLu->slu_buf[bufOffset]),
12048fe96085Stim szeto 		    luAliasLen + 1);
12058fe96085Stim szeto 		bufOffset += luAliasLen + 1;
12068fe96085Stim szeto 	}
12078fe96085Stim szeto 
12082f624233SNattuvetty Bhavyan 	if (disk->luMgmtUrlValid) {
12092f624233SNattuvetty Bhavyan 		sbdLu->slu_mgmt_url_valid = 1;
12102f624233SNattuvetty Bhavyan 		sbdLu->slu_mgmt_url_off = bufOffset;
12112f624233SNattuvetty Bhavyan 		bcopy(disk->luMgmtUrl, &(sbdLu->slu_buf[bufOffset]),
12122f624233SNattuvetty Bhavyan 		    luMgmtUrlLen + 1);
12132f624233SNattuvetty Bhavyan 		bufOffset += luMgmtUrlLen + 1;
12142f624233SNattuvetty Bhavyan 	}
12152f624233SNattuvetty Bhavyan 
12168fe96085Stim szeto 	if (disk->luSizeValid) {
12178fe96085Stim szeto 		sbdLu->slu_lu_size_valid = 1;
12188fe96085Stim szeto 		sbdLu->slu_lu_size = disk->luSize;
12198fe96085Stim szeto 	}
12208fe96085Stim szeto 
12218fe96085Stim szeto 	if (disk->luGuidValid) {
12228fe96085Stim szeto 		sbdLu->slu_guid_valid = 1;
12238fe96085Stim szeto 		bcopy(disk->luGuid, sbdLu->slu_guid, sizeof (disk->luGuid));
12248fe96085Stim szeto 	}
12258fe96085Stim szeto 
12268fe96085Stim szeto 	if (disk->vidValid) {
12278fe96085Stim szeto 		sbdLu->slu_vid_valid = 1;
12288fe96085Stim szeto 		bcopy(disk->vid, sbdLu->slu_vid, sizeof (disk->vid));
12298fe96085Stim szeto 	}
12308fe96085Stim szeto 
12318fe96085Stim szeto 	if (disk->pidValid) {
12328fe96085Stim szeto 		sbdLu->slu_pid_valid = 1;
12338fe96085Stim szeto 		bcopy(disk->pid, sbdLu->slu_pid, sizeof (disk->pid));
12348fe96085Stim szeto 	}
12358fe96085Stim szeto 
12368fe96085Stim szeto 	if (disk->revValid) {
12378fe96085Stim szeto 		sbdLu->slu_rev_valid = 1;
12388fe96085Stim szeto 		bcopy(disk->rev, sbdLu->slu_rev, sizeof (disk->rev));
12398fe96085Stim szeto 	}
12408fe96085Stim szeto 
12418fe96085Stim szeto 	if (disk->companyIdValid) {
12428fe96085Stim szeto 		sbdLu->slu_company_id_valid = 1;
12438fe96085Stim szeto 		sbdLu->slu_company_id = disk->companyId;
12448fe96085Stim szeto 	}
12458fe96085Stim szeto 
1246*fdcc480aSJohn Forte 	if (disk->hostIdValid) {
1247*fdcc480aSJohn Forte 		sbdLu->slu_host_id_valid = 1;
1248*fdcc480aSJohn Forte 		sbdLu->slu_host_id = disk->hostId;
1249*fdcc480aSJohn Forte 	}
1250*fdcc480aSJohn Forte 
12518fe96085Stim szeto 	if (disk->blkSizeValid) {
12528fe96085Stim szeto 		sbdLu->slu_blksize_valid = 1;
12538fe96085Stim szeto 		sbdLu->slu_blksize = disk->blkSize;
12548fe96085Stim szeto 	}
12558fe96085Stim szeto 
12568fe96085Stim szeto 	if (disk->writeProtectEnableValid) {
12578fe96085Stim szeto 		if (disk->writeProtectEnable) {
12588fe96085Stim szeto 			sbdLu->slu_write_protected = 1;
12598fe96085Stim szeto 		}
12608fe96085Stim szeto 	}
12618fe96085Stim szeto 
12628fe96085Stim szeto 	if (disk->writebackCacheDisableValid) {
12638fe96085Stim szeto 		sbdLu->slu_writeback_cache_disable_valid = 1;
12648fe96085Stim szeto 		if (disk->writebackCacheDisable) {
12658fe96085Stim szeto 			sbdLu->slu_writeback_cache_disable = 1;
12668fe96085Stim szeto 		}
12678fe96085Stim szeto 	}
12688fe96085Stim szeto 
12698fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
12708fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdLu->slu_struct_size;
12718fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdLu;
12728fe96085Stim szeto 	sbdIoctl.stmf_obuf_size = sbdLu->slu_struct_size;
12738fe96085Stim szeto 	sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdLu;
12748fe96085Stim szeto 
12758fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_CREATE_AND_REGISTER_LU, &sbdIoctl);
12768fe96085Stim szeto 	if (ioctlRet != 0) {
12778fe96085Stim szeto 		savedErrno = errno;
12788fe96085Stim szeto 		switch (savedErrno) {
12798fe96085Stim szeto 			case EBUSY:
12808fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
12818fe96085Stim szeto 				break;
12828fe96085Stim szeto 			case EPERM:
12838fe96085Stim szeto 			case EACCES:
12848fe96085Stim szeto 				ret = STMF_ERROR_PERM;
12858fe96085Stim szeto 				break;
12868fe96085Stim szeto 			default:
12878fe96085Stim szeto 				diskError(sbdIoctl.stmf_error, &ret);
12888fe96085Stim szeto 				if (ret == STMF_STATUS_ERROR) {
12898fe96085Stim szeto 					syslog(LOG_DEBUG,
12908fe96085Stim szeto 					"createDiskLu:ioctl "
12918fe96085Stim szeto 					"error(%d) (%d) (%d)", ioctlRet,
12928fe96085Stim szeto 					    sbdIoctl.stmf_error, savedErrno);
12938fe96085Stim szeto 				}
12948fe96085Stim szeto 				break;
12958fe96085Stim szeto 		}
12968fe96085Stim szeto 	}
12978fe96085Stim szeto 
12988fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
12998fe96085Stim szeto 		goto done;
13008fe96085Stim szeto 	}
13018fe96085Stim szeto 
13028fe96085Stim szeto 	/*
13038fe96085Stim szeto 	 * on success, copy the resulting guid into the caller's guid if not
13048fe96085Stim szeto 	 * NULL
13058fe96085Stim szeto 	 */
13068fe96085Stim szeto 	if (createdGuid) {
13078fe96085Stim szeto 		bcopy(sbdLu->slu_guid, createdGuid->guid,
13088fe96085Stim szeto 		    sizeof (sbdLu->slu_guid));
13098fe96085Stim szeto 	}
13108fe96085Stim szeto 
13118fe96085Stim szeto 	bcopy(sbdLu->slu_guid, guid.guid, sizeof (sbdLu->slu_guid));
13128fe96085Stim szeto 	if (disk->luMetaFileNameValid) {
13138fe96085Stim szeto 		ret = addGuidToDiskStore(&guid, disk->luMetaFileName);
13148fe96085Stim szeto 	} else {
13158fe96085Stim szeto 		ret = addGuidToDiskStore(&guid, disk->luDataFileName);
13168fe96085Stim szeto 	}
13178fe96085Stim szeto done:
13188fe96085Stim szeto 	free(sbdLu);
13198fe96085Stim szeto 	(void) close(fd);
13208fe96085Stim szeto 	return (ret);
13218fe96085Stim szeto }
13228fe96085Stim szeto 
13238fe96085Stim szeto 
13248fe96085Stim szeto /*
13258fe96085Stim szeto  * stmfImportLu
13268fe96085Stim szeto  *
13278fe96085Stim szeto  * Purpose: Import a previously created logical unit
13288fe96085Stim szeto  *
13298fe96085Stim szeto  * dType - Type of logical unit
13308fe96085Stim szeto  *         Can be: STMF_DISK
13318fe96085Stim szeto  *
13328fe96085Stim szeto  * luGuid - If non-NULL, on success, contains the guid of the imported logical
13338fe96085Stim szeto  *	    unit
13348fe96085Stim szeto  *
13358fe96085Stim szeto  * fname - A file name where the metadata resides
13368fe96085Stim szeto  *
13378fe96085Stim szeto  */
13388fe96085Stim szeto int
13398fe96085Stim szeto stmfImportLu(uint16_t dType, char *fname, stmfGuid *luGuid)
13408fe96085Stim szeto {
13418fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
13428fe96085Stim szeto 
13438fe96085Stim szeto 	if (dType == STMF_DISK) {
13448fe96085Stim szeto 		ret = importDiskLu(fname, luGuid);
13458fe96085Stim szeto 	} else {
13468fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
13478fe96085Stim szeto 	}
13488fe96085Stim szeto 
13498fe96085Stim szeto 	return (ret);
13508fe96085Stim szeto }
13518fe96085Stim szeto 
13528fe96085Stim szeto /*
13538fe96085Stim szeto  * importDiskLu
13548fe96085Stim szeto  *
13558fe96085Stim szeto  * filename - filename to import
13568fe96085Stim szeto  * createdGuid - if not NULL, on success contains the imported guid
13578fe96085Stim szeto  *
13588fe96085Stim szeto  */
13598fe96085Stim szeto static int
13608fe96085Stim szeto importDiskLu(char *fname, stmfGuid *createdGuid)
13618fe96085Stim szeto {
13628fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
13638fe96085Stim szeto 	int fd = 0;
13648fe96085Stim szeto 	int ioctlRet;
13658fe96085Stim szeto 	int savedErrno;
13668fe96085Stim szeto 	int metaFileNameLen;
13678fe96085Stim szeto 	stmfGuid iGuid;
13688fe96085Stim szeto 	int iluBufSize = 0;
13698fe96085Stim szeto 	sbd_import_lu_t *sbdLu = NULL;
13708fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
13718fe96085Stim szeto 
13728fe96085Stim szeto 	if (fname == NULL) {
13738fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
13748fe96085Stim szeto 	}
13758fe96085Stim szeto 
13768fe96085Stim szeto 	/*
13778fe96085Stim szeto 	 * Open control node for sbd
13788fe96085Stim szeto 	 */
13798fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
13808fe96085Stim szeto 		return (ret);
13818fe96085Stim szeto 
13828fe96085Stim szeto 	metaFileNameLen = strlen(fname);
13838fe96085Stim szeto 	iluBufSize += metaFileNameLen + 1;
13848fe96085Stim szeto 
13858fe96085Stim szeto 	/*
13868fe96085Stim szeto 	 * 8 is the size of the buffer set aside for
13878fe96085Stim szeto 	 * concatenation of variable length fields
13888fe96085Stim szeto 	 */
13898fe96085Stim szeto 	sbdLu = (sbd_import_lu_t *)calloc(1,
13908fe96085Stim szeto 	    sizeof (sbd_import_lu_t) + iluBufSize - 8);
13918fe96085Stim szeto 	if (sbdLu == NULL) {
13928fe96085Stim szeto 		(void) close(fd);
13938fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
13948fe96085Stim szeto 	}
13958fe96085Stim szeto 
13968fe96085Stim szeto 	/*
13978fe96085Stim szeto 	 * Accept either a data file or meta data file.
13988fe96085Stim szeto 	 * sbd will do the right thing here either way.
13998fe96085Stim szeto 	 * i.e. if it's a data file, it assumes that the
14008fe96085Stim szeto 	 * meta data is shared with the data.
14018fe96085Stim szeto 	 */
14028fe96085Stim szeto 	(void) strncpy(sbdLu->ilu_meta_fname, fname, metaFileNameLen);
14038fe96085Stim szeto 
14048fe96085Stim szeto 	sbdLu->ilu_struct_size = sizeof (sbd_import_lu_t) + iluBufSize - 8;
14058fe96085Stim szeto 
14068fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
14078fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdLu->ilu_struct_size;
14088fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdLu;
14098fe96085Stim szeto 	sbdIoctl.stmf_obuf_size = sbdLu->ilu_struct_size;
14108fe96085Stim szeto 	sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdLu;
14118fe96085Stim szeto 
14128fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_IMPORT_LU, &sbdIoctl);
14138fe96085Stim szeto 	if (ioctlRet != 0) {
14148fe96085Stim szeto 		savedErrno = errno;
14158fe96085Stim szeto 		switch (savedErrno) {
14168fe96085Stim szeto 			case EBUSY:
14178fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
14188fe96085Stim szeto 				break;
14198fe96085Stim szeto 			case EPERM:
14208fe96085Stim szeto 			case EACCES:
14218fe96085Stim szeto 				ret = STMF_ERROR_PERM;
14228fe96085Stim szeto 				break;
14238fe96085Stim szeto 			default:
14248fe96085Stim szeto 				diskError(sbdIoctl.stmf_error, &ret);
14258fe96085Stim szeto 				if (ret == STMF_STATUS_ERROR) {
14268fe96085Stim szeto 					syslog(LOG_DEBUG,
14278fe96085Stim szeto 					"importDiskLu:ioctl "
14288fe96085Stim szeto 					"error(%d) (%d) (%d)", ioctlRet,
14298fe96085Stim szeto 					    sbdIoctl.stmf_error, savedErrno);
14308fe96085Stim szeto 				}
14318fe96085Stim szeto 				break;
14328fe96085Stim szeto 		}
14338fe96085Stim szeto 	}
14348fe96085Stim szeto 
14358fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
14368fe96085Stim szeto 		goto done;
14378fe96085Stim szeto 	}
14388fe96085Stim szeto 
14398fe96085Stim szeto 	/*
14408fe96085Stim szeto 	 * on success, copy the resulting guid into the caller's guid if not
14418fe96085Stim szeto 	 * NULL and add it to the persistent store for sbd
14428fe96085Stim szeto 	 */
14438fe96085Stim szeto 	if (createdGuid) {
14448fe96085Stim szeto 		bcopy(sbdLu->ilu_ret_guid, createdGuid->guid,
14458fe96085Stim szeto 		    sizeof (sbdLu->ilu_ret_guid));
14468fe96085Stim szeto 		ret = addGuidToDiskStore(createdGuid, fname);
14478fe96085Stim szeto 	} else {
14488fe96085Stim szeto 		bcopy(sbdLu->ilu_ret_guid, iGuid.guid,
14498fe96085Stim szeto 		    sizeof (sbdLu->ilu_ret_guid));
14508fe96085Stim szeto 		ret = addGuidToDiskStore(&iGuid, fname);
14518fe96085Stim szeto 	}
14528fe96085Stim szeto done:
14538fe96085Stim szeto 	free(sbdLu);
14548fe96085Stim szeto 	(void) close(fd);
14558fe96085Stim szeto 	return (ret);
14568fe96085Stim szeto }
14578fe96085Stim szeto 
14588fe96085Stim szeto /*
14598fe96085Stim szeto  * diskError
14608fe96085Stim szeto  *
14618fe96085Stim szeto  * Purpose: Translate sbd driver error
14628fe96085Stim szeto  */
14638fe96085Stim szeto static void
14648fe96085Stim szeto diskError(uint32_t stmfError, int *ret)
14658fe96085Stim szeto {
14668fe96085Stim szeto 	switch (stmfError) {
14678fe96085Stim szeto 		case SBD_RET_META_CREATION_FAILED:
14688fe96085Stim szeto 		case SBD_RET_ZFS_META_CREATE_FAILED:
14698fe96085Stim szeto 			*ret = STMF_ERROR_META_CREATION;
14708fe96085Stim szeto 			break;
14718fe96085Stim szeto 		case SBD_RET_INVALID_BLKSIZE:
14728fe96085Stim szeto 			*ret = STMF_ERROR_INVALID_BLKSIZE;
14738fe96085Stim szeto 			break;
14748fe96085Stim szeto 		case SBD_RET_FILE_ALREADY_REGISTERED:
14758fe96085Stim szeto 			*ret = STMF_ERROR_FILE_IN_USE;
14768fe96085Stim szeto 			break;
14778fe96085Stim szeto 		case SBD_RET_GUID_ALREADY_REGISTERED:
14788fe96085Stim szeto 			*ret = STMF_ERROR_GUID_IN_USE;
14798fe96085Stim szeto 			break;
14808fe96085Stim szeto 		case SBD_RET_META_PATH_NOT_ABSOLUTE:
14818fe96085Stim szeto 		case SBD_RET_META_FILE_LOOKUP_FAILED:
14828fe96085Stim szeto 		case SBD_RET_META_FILE_OPEN_FAILED:
14838fe96085Stim szeto 		case SBD_RET_META_FILE_GETATTR_FAILED:
14848fe96085Stim szeto 		case SBD_RET_NO_META:
14858fe96085Stim szeto 			*ret = STMF_ERROR_META_FILE_NAME;
14868fe96085Stim szeto 			break;
14878fe96085Stim szeto 		case SBD_RET_DATA_PATH_NOT_ABSOLUTE:
14888fe96085Stim szeto 		case SBD_RET_DATA_FILE_LOOKUP_FAILED:
14898fe96085Stim szeto 		case SBD_RET_DATA_FILE_OPEN_FAILED:
14908fe96085Stim szeto 		case SBD_RET_DATA_FILE_GETATTR_FAILED:
14918fe96085Stim szeto 			*ret = STMF_ERROR_DATA_FILE_NAME;
14928fe96085Stim szeto 			break;
14938fe96085Stim szeto 		case SBD_RET_FILE_SIZE_ERROR:
14948fe96085Stim szeto 			*ret = STMF_ERROR_FILE_SIZE_INVALID;
14958fe96085Stim szeto 			break;
14968fe96085Stim szeto 		case SBD_RET_SIZE_OUT_OF_RANGE:
14978fe96085Stim szeto 			*ret = STMF_ERROR_SIZE_OUT_OF_RANGE;
14988fe96085Stim szeto 			break;
14998fe96085Stim szeto 		case SBD_RET_LU_BUSY:
15008fe96085Stim szeto 			*ret = STMF_ERROR_LU_BUSY;
15018fe96085Stim szeto 			break;
15028fe96085Stim szeto 		case SBD_RET_WRITE_CACHE_SET_FAILED:
15038fe96085Stim szeto 			*ret = STMF_ERROR_WRITE_CACHE_SET;
15048fe96085Stim szeto 			break;
150545039663SJohn Forte 		case SBD_RET_ACCESS_STATE_FAILED:
150645039663SJohn Forte 			*ret = STMF_ERROR_ACCESS_STATE_SET;
150745039663SJohn Forte 			break;
15088fe96085Stim szeto 		default:
15098fe96085Stim szeto 			*ret = STMF_STATUS_ERROR;
15108fe96085Stim szeto 			break;
15118fe96085Stim szeto 	}
15128fe96085Stim szeto }
15138fe96085Stim szeto 
15148fe96085Stim szeto /*
15158fe96085Stim szeto  * Creates a logical unit resource of type STMF_DISK.
15168fe96085Stim szeto  *
15178fe96085Stim szeto  * No defaults should be set here as all defaults are derived from the
15188fe96085Stim szeto  * driver's default settings.
15198fe96085Stim szeto  */
15208fe96085Stim szeto static int
15218fe96085Stim szeto createDiskResource(luResourceImpl *hdl)
15228fe96085Stim szeto {
15238fe96085Stim szeto 	hdl->type = STMF_DISK;
15248fe96085Stim szeto 
15258fe96085Stim szeto 	hdl->resource = calloc(1, sizeof (diskResource));
15268fe96085Stim szeto 	if (hdl->resource == NULL) {
15278fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
15288fe96085Stim szeto 	}
15298fe96085Stim szeto 
15308fe96085Stim szeto 	return (STMF_STATUS_SUCCESS);
15318fe96085Stim szeto }
15328fe96085Stim szeto 
15338fe96085Stim szeto /*
15348fe96085Stim szeto  * stmfDeleteLu
15358fe96085Stim szeto  *
15368fe96085Stim szeto  * Purpose: Delete a logical unit
15378fe96085Stim szeto  *
15388fe96085Stim szeto  * hdl - handle to logical unit resource created via stmfCreateLuResource
15398fe96085Stim szeto  *
15408fe96085Stim szeto  * luGuid - If non-NULL, on success, contains the guid of the created logical
15418fe96085Stim szeto  *	    unit
15428fe96085Stim szeto  */
15438fe96085Stim szeto int
15448fe96085Stim szeto stmfDeleteLu(stmfGuid *luGuid)
15458fe96085Stim szeto {
15468fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
15478fe96085Stim szeto 	stmfLogicalUnitProperties luProps;
15488fe96085Stim szeto 
15498fe96085Stim szeto 	if (luGuid == NULL) {
15508fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
15518fe96085Stim szeto 	}
15528fe96085Stim szeto 
15538fe96085Stim szeto 	/* Check logical unit provider name to call correct dtype function */
15548fe96085Stim szeto 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
15558fe96085Stim szeto 	    != STMF_STATUS_SUCCESS) {
15568fe96085Stim szeto 		return (ret);
15578fe96085Stim szeto 	} else {
15588fe96085Stim szeto 		if (strcmp(luProps.providerName, "sbd") == 0) {
15598fe96085Stim szeto 			ret = deleteDiskLu(luGuid);
15608fe96085Stim szeto 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
15618fe96085Stim szeto 			return (STMF_ERROR_NOT_FOUND);
15628fe96085Stim szeto 		} else {
15638fe96085Stim szeto 			return (STMF_ERROR_INVALID_ARG);
15648fe96085Stim szeto 		}
15658fe96085Stim szeto 	}
15668fe96085Stim szeto 
15678fe96085Stim szeto 	return (ret);
15688fe96085Stim szeto }
15698fe96085Stim szeto 
15708fe96085Stim szeto static int
15718fe96085Stim szeto deleteDiskLu(stmfGuid *luGuid)
15728fe96085Stim szeto {
15738fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
15748fe96085Stim szeto 	int fd;
15758fe96085Stim szeto 	int savedErrno;
15768fe96085Stim szeto 	int ioctlRet;
15778fe96085Stim szeto 	sbd_delete_lu_t deleteLu = {0};
15788fe96085Stim szeto 
15798fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
15808fe96085Stim szeto 
15818fe96085Stim szeto 	/*
15828fe96085Stim szeto 	 * Open control node for sbd
15838fe96085Stim szeto 	 */
15848fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
15858fe96085Stim szeto 		return (ret);
15868fe96085Stim szeto 
15878fe96085Stim szeto 	ret = removeGuidFromDiskStore(luGuid);
15888fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
15898fe96085Stim szeto 		goto done;
15908fe96085Stim szeto 	}
15918fe96085Stim szeto 
15928fe96085Stim szeto 	bcopy(luGuid, deleteLu.dlu_guid, sizeof (deleteLu.dlu_guid));
15938fe96085Stim szeto 	deleteLu.dlu_by_guid = 1;
15948fe96085Stim szeto 
15958fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
15968fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sizeof (deleteLu);
15978fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)&deleteLu;
15988fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_DELETE_LU, &sbdIoctl);
15998fe96085Stim szeto 	if (ioctlRet != 0) {
16008fe96085Stim szeto 		savedErrno = errno;
16018fe96085Stim szeto 		switch (savedErrno) {
16028fe96085Stim szeto 			case EBUSY:
16038fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
16048fe96085Stim szeto 				break;
16058fe96085Stim szeto 			case EPERM:
16068fe96085Stim szeto 			case EACCES:
16078fe96085Stim szeto 				ret = STMF_ERROR_PERM;
16088fe96085Stim szeto 				break;
16098fe96085Stim szeto 			case ENOENT:
16108fe96085Stim szeto 				ret = STMF_ERROR_NOT_FOUND;
16118fe96085Stim szeto 				break;
16128fe96085Stim szeto 			default:
16138fe96085Stim szeto 				syslog(LOG_DEBUG,
16148fe96085Stim szeto 				    "deleteDiskLu:ioctl error(%d) (%d) (%d)",
16158fe96085Stim szeto 				    ioctlRet, sbdIoctl.stmf_error, savedErrno);
16168fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
16178fe96085Stim szeto 				break;
16188fe96085Stim szeto 		}
16198fe96085Stim szeto 	}
16208fe96085Stim szeto 
16218fe96085Stim szeto done:
16228fe96085Stim szeto 	(void) close(fd);
16238fe96085Stim szeto 	return (ret);
16248fe96085Stim szeto }
16258fe96085Stim szeto 
16268fe96085Stim szeto /*
162745039663SJohn Forte  * stmfLuStandby
162845039663SJohn Forte  *
162945039663SJohn Forte  * Purpose: Sets access state to standby
163045039663SJohn Forte  *
163145039663SJohn Forte  * luGuid - guid of registered logical unit
163245039663SJohn Forte  *
163345039663SJohn Forte  */
163445039663SJohn Forte int
163545039663SJohn Forte stmfLuStandby(stmfGuid *luGuid)
163645039663SJohn Forte {
163745039663SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
163845039663SJohn Forte 	stmfLogicalUnitProperties luProps;
163945039663SJohn Forte 
164045039663SJohn Forte 	if (luGuid == NULL) {
164145039663SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
164245039663SJohn Forte 	}
164345039663SJohn Forte 
164445039663SJohn Forte 	/* Check logical unit provider name to call correct dtype function */
164545039663SJohn Forte 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
164645039663SJohn Forte 	    != STMF_STATUS_SUCCESS) {
164745039663SJohn Forte 		return (ret);
164845039663SJohn Forte 	} else {
164945039663SJohn Forte 		if (strcmp(luProps.providerName, "sbd") == 0) {
165045039663SJohn Forte 			ret = setDiskStandby(luGuid);
165145039663SJohn Forte 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
165245039663SJohn Forte 			return (STMF_ERROR_NOT_FOUND);
165345039663SJohn Forte 		} else {
165445039663SJohn Forte 			return (STMF_ERROR_INVALID_ARG);
165545039663SJohn Forte 		}
165645039663SJohn Forte 	}
165745039663SJohn Forte 
165845039663SJohn Forte 	return (ret);
165945039663SJohn Forte }
166045039663SJohn Forte 
166145039663SJohn Forte static int
166245039663SJohn Forte setDiskStandby(stmfGuid *luGuid)
166345039663SJohn Forte {
166445039663SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
166545039663SJohn Forte 	stmf_iocdata_t sbdIoctl = {0};
166645039663SJohn Forte 	sbd_set_lu_standby_t sbdLu = {0};
166745039663SJohn Forte 	int ioctlRet;
166845039663SJohn Forte 	int savedErrno;
166945039663SJohn Forte 	int fd = 0;
167045039663SJohn Forte 
167145039663SJohn Forte 	/*
167245039663SJohn Forte 	 * Open control node for sbd
167345039663SJohn Forte 	 */
167445039663SJohn Forte 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
167545039663SJohn Forte 		return (ret);
167645039663SJohn Forte 
167745039663SJohn Forte 	bcopy(luGuid, &sbdLu.stlu_guid, sizeof (stmfGuid));
167845039663SJohn Forte 
167945039663SJohn Forte 	sbdIoctl.stmf_version = STMF_VERSION_1;
168045039663SJohn Forte 	sbdIoctl.stmf_ibuf_size = sizeof (sbd_set_lu_standby_t);
168145039663SJohn Forte 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)&sbdLu;
168245039663SJohn Forte 
168345039663SJohn Forte 	ioctlRet = ioctl(fd, SBD_IOCTL_SET_LU_STANDBY, &sbdIoctl);
168445039663SJohn Forte 	if (ioctlRet != 0) {
168545039663SJohn Forte 		savedErrno = errno;
168645039663SJohn Forte 		switch (savedErrno) {
168745039663SJohn Forte 			case EBUSY:
168845039663SJohn Forte 				ret = STMF_ERROR_BUSY;
168945039663SJohn Forte 				break;
169045039663SJohn Forte 			case EPERM:
169145039663SJohn Forte 			case EACCES:
169245039663SJohn Forte 				ret = STMF_ERROR_PERM;
169345039663SJohn Forte 				break;
169445039663SJohn Forte 			default:
169545039663SJohn Forte 				diskError(sbdIoctl.stmf_error, &ret);
169645039663SJohn Forte 				if (ret == STMF_STATUS_ERROR) {
169745039663SJohn Forte 					syslog(LOG_DEBUG,
169845039663SJohn Forte 					"setDiskStandby:ioctl "
169945039663SJohn Forte 					"error(%d) (%d) (%d)", ioctlRet,
170045039663SJohn Forte 					    sbdIoctl.stmf_error, savedErrno);
170145039663SJohn Forte 				}
170245039663SJohn Forte 				break;
170345039663SJohn Forte 		}
170445039663SJohn Forte 	}
170545039663SJohn Forte 	return (ret);
170645039663SJohn Forte }
170745039663SJohn Forte 
170845039663SJohn Forte /*
17098fe96085Stim szeto  * stmfModifyLu
17108fe96085Stim szeto  *
17118fe96085Stim szeto  * Purpose: Modify properties of a logical unit
17128fe96085Stim szeto  *
17138fe96085Stim szeto  * luGuid - guid of registered logical unit
17148fe96085Stim szeto  * prop - property to modify
17158fe96085Stim szeto  * propVal - property value to set
17168fe96085Stim szeto  *
17178fe96085Stim szeto  */
17188fe96085Stim szeto int
17198fe96085Stim szeto stmfModifyLu(stmfGuid *luGuid, uint32_t prop, const char *propVal)
17208fe96085Stim szeto {
17218fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
17228fe96085Stim szeto 	stmfLogicalUnitProperties luProps;
17238fe96085Stim szeto 
17248fe96085Stim szeto 	if (luGuid == NULL) {
17258fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
17268fe96085Stim szeto 	}
17278fe96085Stim szeto 
17288fe96085Stim szeto 	/* Check logical unit provider name to call correct dtype function */
17298fe96085Stim szeto 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
17308fe96085Stim szeto 	    != STMF_STATUS_SUCCESS) {
17318fe96085Stim szeto 		return (ret);
17328fe96085Stim szeto 	} else {
17338fe96085Stim szeto 		if (strcmp(luProps.providerName, "sbd") == 0) {
17348fe96085Stim szeto 			ret = modifyDiskLuProp(luGuid, NULL, prop, propVal);
17358fe96085Stim szeto 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
17368fe96085Stim szeto 			return (STMF_ERROR_NOT_FOUND);
17378fe96085Stim szeto 		} else {
17388fe96085Stim szeto 			return (STMF_ERROR_INVALID_ARG);
17398fe96085Stim szeto 		}
17408fe96085Stim szeto 	}
17418fe96085Stim szeto 
17428fe96085Stim szeto 	return (ret);
17438fe96085Stim szeto }
17448fe96085Stim szeto 
17458fe96085Stim szeto /*
17468fe96085Stim szeto  * stmfModifyLuByFname
17478fe96085Stim szeto  *
17488fe96085Stim szeto  * Purpose: Modify a device by filename. Device does not need to be registered.
17498fe96085Stim szeto  *
17508fe96085Stim szeto  * dType - type of device to modify
17518fe96085Stim szeto  *         STMF_DISK
17528fe96085Stim szeto  *
17538fe96085Stim szeto  * fname - filename or meta filename
17548fe96085Stim szeto  * prop - valid property identifier
17558fe96085Stim szeto  * propVal - property value
17568fe96085Stim szeto  *
17578fe96085Stim szeto  */
17588fe96085Stim szeto int
17598fe96085Stim szeto stmfModifyLuByFname(uint16_t dType, const char *fname, uint32_t prop,
17608fe96085Stim szeto     const char *propVal)
17618fe96085Stim szeto {
17628fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
17638fe96085Stim szeto 	if (fname == NULL) {
17648fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
17658fe96085Stim szeto 	}
17668fe96085Stim szeto 
17678fe96085Stim szeto 	if (dType == STMF_DISK) {
17688fe96085Stim szeto 		ret = modifyDiskLuProp(NULL, fname, prop, propVal);
17698fe96085Stim szeto 	} else {
17708fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
17718fe96085Stim szeto 	}
17728fe96085Stim szeto 
17738fe96085Stim szeto 	return (ret);
17748fe96085Stim szeto }
17758fe96085Stim szeto 
17768fe96085Stim szeto static int
17778fe96085Stim szeto modifyDiskLuProp(stmfGuid *luGuid, const char *fname, uint32_t prop,
17788fe96085Stim szeto     const char *propVal)
17798fe96085Stim szeto {
17808fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
17818fe96085Stim szeto 	luResource hdl = NULL;
17828fe96085Stim szeto 	luResourceImpl *luPropsHdl;
17838fe96085Stim szeto 
17848fe96085Stim szeto 	ret = stmfCreateLuResource(STMF_DISK, &hdl);
17858fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
17868fe96085Stim szeto 		return (ret);
17878fe96085Stim szeto 	}
17888fe96085Stim szeto 	ret = validateModifyDiskProp(prop);
17898fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
17908fe96085Stim szeto 		(void) stmfFreeLuResource(hdl);
17918fe96085Stim szeto 		return (STMF_ERROR_INVALID_PROP);
17928fe96085Stim szeto 	}
17938fe96085Stim szeto 	ret = stmfSetLuProp(hdl, prop, propVal);
17948fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
17958fe96085Stim szeto 		(void) stmfFreeLuResource(hdl);
17968fe96085Stim szeto 		return (ret);
17978fe96085Stim szeto 	}
17988fe96085Stim szeto 	luPropsHdl = hdl;
17998fe96085Stim szeto 	ret = modifyDiskLu((diskResource *)luPropsHdl->resource, luGuid, fname);
18008fe96085Stim szeto 	(void) stmfFreeLuResource(hdl);
18018fe96085Stim szeto 	return (ret);
18028fe96085Stim szeto }
18038fe96085Stim szeto 
18048fe96085Stim szeto static int
18058fe96085Stim szeto validateModifyDiskProp(uint32_t prop)
18068fe96085Stim szeto {
18078fe96085Stim szeto 	switch (prop) {
18088fe96085Stim szeto 		case STMF_LU_PROP_ALIAS:
18098fe96085Stim szeto 		case STMF_LU_PROP_SIZE:
18102f624233SNattuvetty Bhavyan 		case STMF_LU_PROP_MGMT_URL:
18118fe96085Stim szeto 		case STMF_LU_PROP_WRITE_PROTECT:
18128fe96085Stim szeto 		case STMF_LU_PROP_WRITE_CACHE_DISABLE:
18138fe96085Stim szeto 			return (STMF_STATUS_SUCCESS);
18148fe96085Stim szeto 			break;
18158fe96085Stim szeto 		default:
18168fe96085Stim szeto 			return (STMF_STATUS_ERROR);
18178fe96085Stim szeto 			break;
18188fe96085Stim szeto 	}
18198fe96085Stim szeto }
18208fe96085Stim szeto 
18218fe96085Stim szeto static int
18228fe96085Stim szeto modifyDiskLu(diskResource *disk, stmfGuid *luGuid, const char *fname)
18238fe96085Stim szeto {
18248fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
18258fe96085Stim szeto 	int luAliasLen = 0;
18262f624233SNattuvetty Bhavyan 	int luMgmtUrlLen = 0;
18278fe96085Stim szeto 	int mluBufSize = 0;
18288fe96085Stim szeto 	int bufOffset = 0;
18298fe96085Stim szeto 	int fd = 0;
18308fe96085Stim szeto 	int ioctlRet;
18318fe96085Stim szeto 	int savedErrno;
18328fe96085Stim szeto 	int fnameSize = 0;
18338fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
18348fe96085Stim szeto 
18358fe96085Stim szeto 	sbd_modify_lu_t *sbdLu = NULL;
18368fe96085Stim szeto 
18378fe96085Stim szeto 	if (luGuid == NULL && fname == NULL) {
18388fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
18398fe96085Stim szeto 	}
18408fe96085Stim szeto 
18418fe96085Stim szeto 	if (fname) {
18428fe96085Stim szeto 		fnameSize = strlen(fname) + 1;
18438fe96085Stim szeto 		mluBufSize += fnameSize;
18448fe96085Stim szeto 	}
18458fe96085Stim szeto 
18468fe96085Stim szeto 	/*
18478fe96085Stim szeto 	 * Open control node for sbd
18488fe96085Stim szeto 	 */
18498fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
18508fe96085Stim szeto 		return (ret);
18518fe96085Stim szeto 
18528fe96085Stim szeto 	if (disk->luAliasValid) {
18538fe96085Stim szeto 		luAliasLen = strlen(disk->luAlias);
18548fe96085Stim szeto 		mluBufSize += luAliasLen + 1;
18558fe96085Stim szeto 	}
18568fe96085Stim szeto 
18572f624233SNattuvetty Bhavyan 	if (disk->luMgmtUrlValid) {
18582f624233SNattuvetty Bhavyan 		luMgmtUrlLen = strlen(disk->luMgmtUrl);
18592f624233SNattuvetty Bhavyan 		mluBufSize += luMgmtUrlLen + 1;
18602f624233SNattuvetty Bhavyan 	}
18612f624233SNattuvetty Bhavyan 
18628fe96085Stim szeto 	/*
18638fe96085Stim szeto 	 * 8 is the size of the buffer set aside for
18648fe96085Stim szeto 	 * concatenation of variable length fields
18658fe96085Stim szeto 	 */
18668fe96085Stim szeto 	sbdLu = (sbd_modify_lu_t *)calloc(1,
18678fe96085Stim szeto 	    sizeof (sbd_modify_lu_t) + mluBufSize - 8 + fnameSize);
18688fe96085Stim szeto 	if (sbdLu == NULL) {
18698fe96085Stim szeto 		(void) close(fd);
18708fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
18718fe96085Stim szeto 	}
18728fe96085Stim szeto 
18738fe96085Stim szeto 	sbdLu->mlu_struct_size = sizeof (sbd_modify_lu_t) +
18748fe96085Stim szeto 	    mluBufSize - 8 + fnameSize;
18758fe96085Stim szeto 
18768fe96085Stim szeto 	if (disk->luAliasValid) {
18778fe96085Stim szeto 		sbdLu->mlu_alias_valid = 1;
18788fe96085Stim szeto 		sbdLu->mlu_alias_off = bufOffset;
18798fe96085Stim szeto 		bcopy(disk->luAlias, &(sbdLu->mlu_buf[bufOffset]),
18808fe96085Stim szeto 		    luAliasLen + 1);
18818fe96085Stim szeto 		bufOffset += luAliasLen + 1;
18828fe96085Stim szeto 	}
18838fe96085Stim szeto 
18842f624233SNattuvetty Bhavyan 	if (disk->luMgmtUrlValid) {
18852f624233SNattuvetty Bhavyan 		sbdLu->mlu_mgmt_url_valid = 1;
18862f624233SNattuvetty Bhavyan 		sbdLu->mlu_mgmt_url_off = bufOffset;
18872f624233SNattuvetty Bhavyan 		bcopy(disk->luMgmtUrl, &(sbdLu->mlu_buf[bufOffset]),
18882f624233SNattuvetty Bhavyan 		    luMgmtUrlLen + 1);
18892f624233SNattuvetty Bhavyan 		bufOffset += luMgmtUrlLen + 1;
18902f624233SNattuvetty Bhavyan 	}
18912f624233SNattuvetty Bhavyan 
18928fe96085Stim szeto 	if (disk->luSizeValid) {
18938fe96085Stim szeto 		sbdLu->mlu_lu_size_valid = 1;
18948fe96085Stim szeto 		sbdLu->mlu_lu_size = disk->luSize;
18958fe96085Stim szeto 	}
18968fe96085Stim szeto 
18978fe96085Stim szeto 	if (disk->writeProtectEnableValid) {
18988fe96085Stim szeto 		sbdLu->mlu_write_protected_valid = 1;
18998fe96085Stim szeto 		if (disk->writeProtectEnable) {
19008fe96085Stim szeto 			sbdLu->mlu_write_protected = 1;
19018fe96085Stim szeto 		}
19028fe96085Stim szeto 	}
19038fe96085Stim szeto 
19048fe96085Stim szeto 	if (disk->writebackCacheDisableValid) {
19058fe96085Stim szeto 		sbdLu->mlu_writeback_cache_disable_valid = 1;
19068fe96085Stim szeto 		if (disk->writebackCacheDisable) {
19078fe96085Stim szeto 			sbdLu->mlu_writeback_cache_disable = 1;
19088fe96085Stim szeto 		}
19098fe96085Stim szeto 	}
19108fe96085Stim szeto 
19118fe96085Stim szeto 	if (luGuid) {
19128fe96085Stim szeto 		bcopy(luGuid, sbdLu->mlu_input_guid, sizeof (stmfGuid));
19138fe96085Stim szeto 		sbdLu->mlu_by_guid = 1;
19148fe96085Stim szeto 	} else {
19158fe96085Stim szeto 		sbdLu->mlu_fname_off = bufOffset;
19168fe96085Stim szeto 		bcopy(fname, &(sbdLu->mlu_buf[bufOffset]), fnameSize + 1);
19178fe96085Stim szeto 		sbdLu->mlu_by_fname = 1;
19188fe96085Stim szeto 	}
19198fe96085Stim szeto 
19208fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
19218fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdLu->mlu_struct_size;
19228fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdLu;
19238fe96085Stim szeto 
19248fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_MODIFY_LU, &sbdIoctl);
19258fe96085Stim szeto 	if (ioctlRet != 0) {
19268fe96085Stim szeto 		savedErrno = errno;
19278fe96085Stim szeto 		switch (savedErrno) {
19288fe96085Stim szeto 			case EBUSY:
19298fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
19308fe96085Stim szeto 				break;
19318fe96085Stim szeto 			case EPERM:
19328fe96085Stim szeto 			case EACCES:
19338fe96085Stim szeto 				ret = STMF_ERROR_PERM;
19348fe96085Stim szeto 				break;
19358fe96085Stim szeto 			default:
19368fe96085Stim szeto 				diskError(sbdIoctl.stmf_error, &ret);
19378fe96085Stim szeto 				if (ret == STMF_STATUS_ERROR) {
19388fe96085Stim szeto 					syslog(LOG_DEBUG,
19398fe96085Stim szeto 					"modifyDiskLu:ioctl "
19408fe96085Stim szeto 					"error(%d) (%d) (%d)", ioctlRet,
19418fe96085Stim szeto 					    sbdIoctl.stmf_error, savedErrno);
19428fe96085Stim szeto 				}
19438fe96085Stim szeto 				break;
19448fe96085Stim szeto 		}
19458fe96085Stim szeto 	}
19468fe96085Stim szeto 
19478fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
19488fe96085Stim szeto 		goto done;
19498fe96085Stim szeto 	}
19508fe96085Stim szeto 
19518fe96085Stim szeto done:
19528fe96085Stim szeto 	free(sbdLu);
19538fe96085Stim szeto 	(void) close(fd);
19548fe96085Stim szeto 	return (ret);
19558fe96085Stim szeto }
19568fe96085Stim szeto 
19578fe96085Stim szeto /*
19588fe96085Stim szeto  * removeGuidFromDiskStore
19598fe96085Stim szeto  *
19608fe96085Stim szeto  * Purpose: delete a logical unit from the sbd provider data
19618fe96085Stim szeto  */
19628fe96085Stim szeto static int
19638fe96085Stim szeto removeGuidFromDiskStore(stmfGuid *guid)
19648fe96085Stim szeto {
19658fe96085Stim szeto 	return (persistDiskGuid(guid, NULL, B_FALSE));
19668fe96085Stim szeto }
19678fe96085Stim szeto 
19688fe96085Stim szeto 
19698fe96085Stim szeto /*
19708fe96085Stim szeto  * addGuidToDiskStore
19718fe96085Stim szeto  *
19728fe96085Stim szeto  * Purpose: add a logical unit to the sbd provider data
19738fe96085Stim szeto  */
19748fe96085Stim szeto static int
19758fe96085Stim szeto addGuidToDiskStore(stmfGuid *guid, char *filename)
19768fe96085Stim szeto {
19778fe96085Stim szeto 	return (persistDiskGuid(guid, filename, B_TRUE));
19788fe96085Stim szeto }
19798fe96085Stim szeto 
19808fe96085Stim szeto 
19818fe96085Stim szeto /*
19828fe96085Stim szeto  * persistDiskGuid
19838fe96085Stim szeto  *
19848fe96085Stim szeto  * Purpose: Persist or unpersist a guid for the sbd provider data
19858fe96085Stim szeto  *
19868fe96085Stim szeto  */
19878fe96085Stim szeto static int
19888fe96085Stim szeto persistDiskGuid(stmfGuid *guid, char *filename, boolean_t persist)
19898fe96085Stim szeto {
19908fe96085Stim szeto 	char	    guidAsciiBuf[LU_ASCII_GUID_SIZE + 1] = {0};
19918fe96085Stim szeto 	nvlist_t    *nvl = NULL;
19928fe96085Stim szeto 
19938fe96085Stim szeto 	uint64_t    setToken;
19948fe96085Stim szeto 	boolean_t   retryGetProviderData = B_FALSE;
19958fe96085Stim szeto 	boolean_t   newData = B_FALSE;
19968fe96085Stim szeto 	int	    ret = STMF_STATUS_SUCCESS;
19978fe96085Stim szeto 	int	    retryCnt = 0;
19988fe96085Stim szeto 	int	    stmfRet;
19998fe96085Stim szeto 
20008fe96085Stim szeto 	/* if we're persisting a guid, there must be a filename */
20018fe96085Stim szeto 	if (persist && !filename) {
20028fe96085Stim szeto 		return (1);
20038fe96085Stim szeto 	}
20048fe96085Stim szeto 
20058fe96085Stim szeto 	/* guid is stored in lowercase ascii hex */
20068fe96085Stim szeto 	(void) snprintf(guidAsciiBuf, sizeof (guidAsciiBuf),
20078fe96085Stim szeto 	    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
20088fe96085Stim szeto 	    "%02x%02x%02x%02x%02x%02x",
20098fe96085Stim szeto 	    guid->guid[0], guid->guid[1], guid->guid[2], guid->guid[3],
20108fe96085Stim szeto 	    guid->guid[4], guid->guid[5], guid->guid[6], guid->guid[7],
20118fe96085Stim szeto 	    guid->guid[8], guid->guid[9], guid->guid[10], guid->guid[11],
20128fe96085Stim szeto 	    guid->guid[12], guid->guid[13], guid->guid[14], guid->guid[15]);
20138fe96085Stim szeto 
20148fe96085Stim szeto 
20158fe96085Stim szeto 	do {
20168fe96085Stim szeto 		retryGetProviderData = B_FALSE;
20178fe96085Stim szeto 		stmfRet = stmfGetProviderDataProt("sbd", &nvl,
20188fe96085Stim szeto 		    STMF_LU_PROVIDER_TYPE, &setToken);
20198fe96085Stim szeto 		if (stmfRet != STMF_STATUS_SUCCESS) {
20208fe96085Stim szeto 			if (persist && stmfRet == STMF_ERROR_NOT_FOUND) {
20218fe96085Stim szeto 				ret = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
20228fe96085Stim szeto 				if (ret != 0) {
20238fe96085Stim szeto 					syslog(LOG_DEBUG,
20248fe96085Stim szeto 					    "unpersistGuid:nvlist_alloc(%d)",
20258fe96085Stim szeto 					    ret);
20268fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
20278fe96085Stim szeto 					goto done;
20288fe96085Stim szeto 				}
20298fe96085Stim szeto 				newData = B_TRUE;
20308fe96085Stim szeto 			} else {
203145039663SJohn Forte 				/*
203245039663SJohn Forte 				 * if we're persisting the data, it's
203345039663SJohn Forte 				 * an error. Otherwise, just return
203445039663SJohn Forte 				 */
203545039663SJohn Forte 				if (persist) {
20368fe96085Stim szeto 					ret = stmfRet;
203745039663SJohn Forte 				}
20388fe96085Stim szeto 				goto done;
20398fe96085Stim szeto 			}
20408fe96085Stim szeto 		}
20418fe96085Stim szeto 		if (persist) {
20428fe96085Stim szeto 			ret = nvlist_add_string(nvl, guidAsciiBuf, filename);
20438fe96085Stim szeto 		} else {
20448fe96085Stim szeto 			ret = nvlist_remove(nvl, guidAsciiBuf,
20458fe96085Stim szeto 			    DATA_TYPE_STRING);
20468fe96085Stim szeto 			if (ret == ENOENT) {
20478fe96085Stim szeto 				ret = 0;
20488fe96085Stim szeto 			}
20498fe96085Stim szeto 		}
20508fe96085Stim szeto 		if (ret == 0) {
20518fe96085Stim szeto 			if (newData) {
20528fe96085Stim szeto 				stmfRet = stmfSetProviderDataProt("sbd", nvl,
20538fe96085Stim szeto 				    STMF_LU_PROVIDER_TYPE, NULL);
20548fe96085Stim szeto 			} else {
20558fe96085Stim szeto 				stmfRet = stmfSetProviderDataProt("sbd", nvl,
20568fe96085Stim szeto 				    STMF_LU_PROVIDER_TYPE, &setToken);
20578fe96085Stim szeto 			}
20588fe96085Stim szeto 			if (stmfRet != STMF_STATUS_SUCCESS) {
20598fe96085Stim szeto 				if (stmfRet == STMF_ERROR_BUSY) {
20608fe96085Stim szeto 					/* get/set failed, try again */
20618fe96085Stim szeto 					retryGetProviderData = B_TRUE;
20628fe96085Stim szeto 					if (retryCnt++ > MAX_PROVIDER_RETRY) {
20638fe96085Stim szeto 						ret = stmfRet;
20648fe96085Stim szeto 						break;
20658fe96085Stim szeto 					}
20668fe96085Stim szeto 					continue;
20678fe96085Stim szeto 				} else if (stmfRet ==
20688fe96085Stim szeto 				    STMF_ERROR_PROV_DATA_STALE) {
20698fe96085Stim szeto 					/* update failed, try again */
20708fe96085Stim szeto 					nvlist_free(nvl);
20718fe96085Stim szeto 					nvl = NULL;
20728fe96085Stim szeto 					retryGetProviderData = B_TRUE;
20738fe96085Stim szeto 					if (retryCnt++ > MAX_PROVIDER_RETRY) {
20748fe96085Stim szeto 						ret = stmfRet;
20758fe96085Stim szeto 						break;
20768fe96085Stim szeto 					}
20778fe96085Stim szeto 					continue;
20788fe96085Stim szeto 				} else {
20798fe96085Stim szeto 					syslog(LOG_DEBUG,
20808fe96085Stim szeto 					    "unpersistGuid:error(%x)", stmfRet);
20818fe96085Stim szeto 					ret = stmfRet;
20828fe96085Stim szeto 				}
20838fe96085Stim szeto 				break;
20848fe96085Stim szeto 			}
20858fe96085Stim szeto 		} else {
20868fe96085Stim szeto 			syslog(LOG_DEBUG,
20878fe96085Stim szeto 			    "unpersistGuid:error nvlist_add/remove(%d)",
20888fe96085Stim szeto 			    ret);
20898fe96085Stim szeto 			ret = STMF_STATUS_ERROR;
20908fe96085Stim szeto 		}
20918fe96085Stim szeto 	} while (retryGetProviderData);
20928fe96085Stim szeto 
20938fe96085Stim szeto done:
20948fe96085Stim szeto 	nvlist_free(nvl);
20958fe96085Stim szeto 	return (ret);
20968fe96085Stim szeto }
20978fe96085Stim szeto 
20988fe96085Stim szeto 
20998fe96085Stim szeto /*
21008fe96085Stim szeto  * stmfGetLuProp
21018fe96085Stim szeto  *
21028fe96085Stim szeto  * Purpose: Get current value for a resource property
21038fe96085Stim szeto  *
21048fe96085Stim szeto  * hdl - luResource from a previous call to stmfCreateLuResource
21058fe96085Stim szeto  *
21068fe96085Stim szeto  * resourceProp - a valid resource property type
21078fe96085Stim szeto  *
21088fe96085Stim szeto  * propVal - void pointer to a pointer of the value to be retrieved
21098fe96085Stim szeto  */
21108fe96085Stim szeto int
21118fe96085Stim szeto stmfGetLuProp(luResource hdl, uint32_t prop, char *propVal, size_t *propLen)
21128fe96085Stim szeto {
21138fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
21148fe96085Stim szeto 	luResourceImpl *luPropsHdl = hdl;
21158fe96085Stim szeto 	if (hdl == NULL || propLen == NULL || propVal == NULL) {
21168fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
21178fe96085Stim szeto 	}
21188fe96085Stim szeto 
21198fe96085Stim szeto 	if (luPropsHdl->type == STMF_DISK) {
21208fe96085Stim szeto 		ret = getDiskProp(luPropsHdl, prop, propVal, propLen);
21218fe96085Stim szeto 	} else {
21228fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
21238fe96085Stim szeto 	}
21248fe96085Stim szeto 
21258fe96085Stim szeto 	return (ret);
21268fe96085Stim szeto }
21278fe96085Stim szeto 
21288fe96085Stim szeto /*
21298fe96085Stim szeto  * stmfGetLuResource
21308fe96085Stim szeto  *
21318fe96085Stim szeto  * Purpose: Get a logical unit resource handle for a given logical unit.
21328fe96085Stim szeto  *
21338fe96085Stim szeto  * hdl - pointer to luResource
21348fe96085Stim szeto  */
21358fe96085Stim szeto int
21368fe96085Stim szeto stmfGetLuResource(stmfGuid *luGuid, luResource *hdl)
21378fe96085Stim szeto {
21388fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
21398fe96085Stim szeto 	stmfLogicalUnitProperties luProps;
21408fe96085Stim szeto 
21418fe96085Stim szeto 
21428fe96085Stim szeto 	/* Check logical unit provider name to call correct dtype function */
21438fe96085Stim szeto 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
21448fe96085Stim szeto 	    != STMF_STATUS_SUCCESS) {
21458fe96085Stim szeto 		return (ret);
21468fe96085Stim szeto 	} else {
21478fe96085Stim szeto 		if (strcmp(luProps.providerName, "sbd") == 0) {
21488fe96085Stim szeto 			ret = getDiskAllProps(luGuid, hdl);
21498fe96085Stim szeto 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
21508fe96085Stim szeto 			return (STMF_ERROR_NOT_FOUND);
21518fe96085Stim szeto 		} else {
21528fe96085Stim szeto 			return (STMF_ERROR_INVALID_ARG);
21538fe96085Stim szeto 		}
21548fe96085Stim szeto 	}
21558fe96085Stim szeto 
21568fe96085Stim szeto 	return (ret);
21578fe96085Stim szeto }
21588fe96085Stim szeto 
21598fe96085Stim szeto /*
21608fe96085Stim szeto  * getDiskAllProps
21618fe96085Stim szeto  *
21628fe96085Stim szeto  * Purpose: load all disk properties from sbd driver
21638fe96085Stim szeto  *
21648fe96085Stim szeto  * luGuid - guid of disk device for which properties are to be retrieved
21658fe96085Stim szeto  * hdl - allocated luResource into which properties are to be copied
21668fe96085Stim szeto  *
21678fe96085Stim szeto  */
21688fe96085Stim szeto static int
21698fe96085Stim szeto getDiskAllProps(stmfGuid *luGuid, luResource *hdl)
21708fe96085Stim szeto {
21718fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
21728fe96085Stim szeto 	int fd;
21738fe96085Stim szeto 	sbd_lu_props_t *sbdProps;
21748fe96085Stim szeto 	int ioctlRet;
21758fe96085Stim szeto 	int savedErrno;
21768fe96085Stim szeto 	int sbdPropsSize = sizeof (*sbdProps) + MAX_SBD_PROPS;
21778fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
21788fe96085Stim szeto 
21798fe96085Stim szeto 	/*
21808fe96085Stim szeto 	 * Open control node for sbd
21818fe96085Stim szeto 	 */
21828fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
21838fe96085Stim szeto 		return (ret);
21848fe96085Stim szeto 
21858fe96085Stim szeto 
21868fe96085Stim szeto 	*hdl = calloc(1, sizeof (luResourceImpl));
21878fe96085Stim szeto 	if (*hdl == NULL) {
21888fe96085Stim szeto 		(void) close(fd);
21898fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
21908fe96085Stim szeto 	}
21918fe96085Stim szeto 
21928fe96085Stim szeto 	sbdProps = calloc(1, sbdPropsSize);
21938fe96085Stim szeto 	if (sbdProps == NULL) {
21948fe96085Stim szeto 		free(*hdl);
21958fe96085Stim szeto 		(void) close(fd);
21968fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
21978fe96085Stim szeto 	}
21988fe96085Stim szeto 
21998fe96085Stim szeto 	ret = createDiskResource((luResourceImpl *)*hdl);
22008fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
22018fe96085Stim szeto 		free(*hdl);
220245039663SJohn Forte 		free(sbdProps);
22038fe96085Stim szeto 		(void) close(fd);
22048fe96085Stim szeto 		return (ret);
22058fe96085Stim szeto 	}
22068fe96085Stim szeto 
22078fe96085Stim szeto 	sbdProps->slp_input_guid = 1;
22088fe96085Stim szeto 	bcopy(luGuid, sbdProps->slp_guid, sizeof (sbdProps->slp_guid));
22098fe96085Stim szeto 
22108fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
22118fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdPropsSize;
22128fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdProps;
22138fe96085Stim szeto 	sbdIoctl.stmf_obuf_size = sbdPropsSize;
22148fe96085Stim szeto 	sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdProps;
22158fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_GET_LU_PROPS, &sbdIoctl);
22168fe96085Stim szeto 	if (ioctlRet != 0) {
22178fe96085Stim szeto 		savedErrno = errno;
22188fe96085Stim szeto 		switch (savedErrno) {
22198fe96085Stim szeto 			case EBUSY:
22208fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
22218fe96085Stim szeto 				break;
22228fe96085Stim szeto 			case EPERM:
22238fe96085Stim szeto 			case EACCES:
22248fe96085Stim szeto 				ret = STMF_ERROR_PERM;
22258fe96085Stim szeto 				break;
22268fe96085Stim szeto 			case ENOENT:
22278fe96085Stim szeto 				ret = STMF_ERROR_NOT_FOUND;
22288fe96085Stim szeto 				break;
22298fe96085Stim szeto 			default:
22308fe96085Stim szeto 				syslog(LOG_DEBUG,
22318fe96085Stim szeto 				    "getDiskAllProps:ioctl error(%d) (%d) (%d)",
22328fe96085Stim szeto 				    ioctlRet, sbdIoctl.stmf_error, savedErrno);
22338fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
22348fe96085Stim szeto 				break;
22358fe96085Stim szeto 		}
22368fe96085Stim szeto 	}
22378fe96085Stim szeto 
22388fe96085Stim szeto 	if (ret == STMF_STATUS_SUCCESS) {
22398fe96085Stim szeto 		ret = loadDiskPropsFromDriver((luResourceImpl *)*hdl, sbdProps);
22408fe96085Stim szeto 	}
22418fe96085Stim szeto 
224245039663SJohn Forte 	free(sbdProps);
22438fe96085Stim szeto 	(void) close(fd);
22448fe96085Stim szeto 	return (ret);
22458fe96085Stim szeto }
22468fe96085Stim szeto 
22478fe96085Stim szeto /*
22488fe96085Stim szeto  * loadDiskPropsFromDriver
22498fe96085Stim szeto  *
22508fe96085Stim szeto  * Purpose: Retrieve all disk type properties from sbd driver
22518fe96085Stim szeto  *
22528fe96085Stim szeto  * hdl - Allocated luResourceImpl
22538fe96085Stim szeto  * sbdProps - sbd_lu_props_t structure returned from sbd driver
22548fe96085Stim szeto  *
22558fe96085Stim szeto  */
22568fe96085Stim szeto static int
22578fe96085Stim szeto loadDiskPropsFromDriver(luResourceImpl *hdl, sbd_lu_props_t *sbdProps)
22588fe96085Stim szeto {
22598fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
22608fe96085Stim szeto 	diskResource *diskLu = hdl->resource;
22618fe96085Stim szeto 	/* copy guid */
22628fe96085Stim szeto 	diskLu->luGuidValid = B_TRUE;
22638fe96085Stim szeto 	bcopy(sbdProps->slp_guid, diskLu->luGuid, sizeof (sbdProps->slp_guid));
22648fe96085Stim szeto 
22658fe96085Stim szeto 	if (sbdProps->slp_separate_meta && sbdProps->slp_meta_fname_valid) {
22668fe96085Stim szeto 		diskLu->luMetaFileNameValid = B_TRUE;
22678fe96085Stim szeto 		if (strlcpy(diskLu->luMetaFileName,
22688fe96085Stim szeto 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_meta_fname_off]),
22698fe96085Stim szeto 		    sizeof (diskLu->luMetaFileName)) >=
22708fe96085Stim szeto 		    sizeof (diskLu->luMetaFileName)) {
22718fe96085Stim szeto 			return (STMF_STATUS_ERROR);
22728fe96085Stim szeto 		}
22738fe96085Stim szeto 	}
22748fe96085Stim szeto 
22758fe96085Stim szeto 	if (sbdProps->slp_data_fname_valid) {
22768fe96085Stim szeto 		diskLu->luDataFileNameValid = B_TRUE;
22778fe96085Stim szeto 		if (strlcpy(diskLu->luDataFileName,
22788fe96085Stim szeto 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_data_fname_off]),
22798fe96085Stim szeto 		    sizeof (diskLu->luDataFileName)) >=
22808fe96085Stim szeto 		    sizeof (diskLu->luDataFileName)) {
22818fe96085Stim szeto 			return (STMF_STATUS_ERROR);
22828fe96085Stim szeto 		}
22838fe96085Stim szeto 	}
22848fe96085Stim szeto 
22858fe96085Stim szeto 	if (sbdProps->slp_serial_valid) {
22868fe96085Stim szeto 		diskLu->serialNumValid = B_TRUE;
22878fe96085Stim szeto 		bcopy(&(sbdProps->slp_buf[sbdProps->slp_serial_off]),
22888fe96085Stim szeto 		    diskLu->serialNum, sbdProps->slp_serial_size);
22898fe96085Stim szeto 	}
22908fe96085Stim szeto 
22912f624233SNattuvetty Bhavyan 	if (sbdProps->slp_mgmt_url_valid) {
22922f624233SNattuvetty Bhavyan 		diskLu->luMgmtUrlValid = B_TRUE;
22932f624233SNattuvetty Bhavyan 		if (strlcpy(diskLu->luMgmtUrl,
22942f624233SNattuvetty Bhavyan 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_mgmt_url_off]),
22952f624233SNattuvetty Bhavyan 		    sizeof (diskLu->luMgmtUrl)) >=
22962f624233SNattuvetty Bhavyan 		    sizeof (diskLu->luMgmtUrl)) {
22972f624233SNattuvetty Bhavyan 			return (STMF_STATUS_ERROR);
22982f624233SNattuvetty Bhavyan 		}
22992f624233SNattuvetty Bhavyan 	}
23002f624233SNattuvetty Bhavyan 
23018fe96085Stim szeto 	if (sbdProps->slp_alias_valid) {
23028fe96085Stim szeto 		diskLu->luAliasValid = B_TRUE;
23038fe96085Stim szeto 		if (strlcpy(diskLu->luAlias,
23048fe96085Stim szeto 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_alias_off]),
23058fe96085Stim szeto 		    sizeof (diskLu->luAlias)) >=
23068fe96085Stim szeto 		    sizeof (diskLu->luAlias)) {
23078fe96085Stim szeto 			return (STMF_STATUS_ERROR);
23088fe96085Stim szeto 		}
23098fe96085Stim szeto 	} else { /* set alias to data filename if not set */
23108fe96085Stim szeto 		if (sbdProps->slp_data_fname_valid) {
23118fe96085Stim szeto 			diskLu->luAliasValid = B_TRUE;
23128fe96085Stim szeto 			if (strlcpy(diskLu->luAlias,
23138fe96085Stim szeto 			    (char *)&(sbdProps->slp_buf[
23148fe96085Stim szeto 			    sbdProps->slp_data_fname_off]),
23158fe96085Stim szeto 			    sizeof (diskLu->luAlias)) >=
23168fe96085Stim szeto 			    sizeof (diskLu->luAlias)) {
23178fe96085Stim szeto 				return (STMF_STATUS_ERROR);
23188fe96085Stim szeto 			}
23198fe96085Stim szeto 		}
23208fe96085Stim szeto 	}
23218fe96085Stim szeto 
23228fe96085Stim szeto 	diskLu->vidValid = B_TRUE;
23238fe96085Stim szeto 	bcopy(sbdProps->slp_vid, diskLu->vid, sizeof (diskLu->vid));
23248fe96085Stim szeto 
23258fe96085Stim szeto 	diskLu->pidValid = B_TRUE;
23268fe96085Stim szeto 	bcopy(sbdProps->slp_pid, diskLu->pid, sizeof (diskLu->pid));
23278fe96085Stim szeto 
23288fe96085Stim szeto 	diskLu->revValid = B_TRUE;
23298fe96085Stim szeto 	bcopy(sbdProps->slp_rev, diskLu->rev, sizeof (diskLu->rev));
23308fe96085Stim szeto 
23318fe96085Stim szeto 	diskLu->writeProtectEnableValid = B_TRUE;
23328fe96085Stim szeto 	if (sbdProps->slp_write_protected) {
23338fe96085Stim szeto 		diskLu->writeProtectEnable = B_TRUE;
23348fe96085Stim szeto 	}
23358fe96085Stim szeto 
23368fe96085Stim szeto 	diskLu->writebackCacheDisableValid = B_TRUE;
23378fe96085Stim szeto 	if (sbdProps->slp_writeback_cache_disable_cur) {
23388fe96085Stim szeto 		diskLu->writebackCacheDisable = B_TRUE;
23398fe96085Stim szeto 	}
23408fe96085Stim szeto 
23418fe96085Stim szeto 	diskLu->blkSizeValid = B_TRUE;
23428fe96085Stim szeto 	diskLu->blkSize = sbdProps->slp_blksize;
23438fe96085Stim szeto 
23448fe96085Stim szeto 	diskLu->luSizeValid = B_TRUE;
23458fe96085Stim szeto 	diskLu->luSize = sbdProps->slp_lu_size;
23468fe96085Stim szeto 
234745039663SJohn Forte 	diskLu->accessState = sbdProps->slp_access_state;
234845039663SJohn Forte 
23498fe96085Stim szeto 	return (ret);
23508fe96085Stim szeto }
23518fe96085Stim szeto 
23528fe96085Stim szeto 
23538fe96085Stim szeto /*
23548fe96085Stim szeto  * stmfSetLuProp
23558fe96085Stim szeto  *
23568fe96085Stim szeto  * Purpose: set a property on an luResource
23578fe96085Stim szeto  *
23588fe96085Stim szeto  * hdl - allocated luResource
23598fe96085Stim szeto  * prop - property identifier
23608fe96085Stim szeto  * propVal - property value to be set
23618fe96085Stim szeto  */
23628fe96085Stim szeto int
23638fe96085Stim szeto stmfSetLuProp(luResource hdl, uint32_t prop, const char *propVal)
23648fe96085Stim szeto {
23658fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
23668fe96085Stim szeto 	luResourceImpl *luPropsHdl = hdl;
23678fe96085Stim szeto 	if (hdl == NULL) {
23688fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
23698fe96085Stim szeto 	}
23708fe96085Stim szeto 
23718fe96085Stim szeto 	if (luPropsHdl->type == STMF_DISK) {
23728fe96085Stim szeto 		ret = setDiskProp(luPropsHdl, prop, propVal);
23738fe96085Stim szeto 	} else {
23748fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
23758fe96085Stim szeto 	}
23768fe96085Stim szeto 
23778fe96085Stim szeto 	return (ret);
23788fe96085Stim szeto }
23798fe96085Stim szeto 
23808fe96085Stim szeto /*
23818fe96085Stim szeto  * getDiskProp
23828fe96085Stim szeto  *
23838fe96085Stim szeto  * Purpose: retrieve a given property from a logical unit resource of type disk
23848fe96085Stim szeto  *
23858fe96085Stim szeto  * hdl - allocated luResourceImpl
23868fe96085Stim szeto  * prop - property identifier
23878fe96085Stim szeto  * propVal - pointer to character to contain the retrieved property value
23888fe96085Stim szeto  * propLen - On input this is the length of propVal. On failure, it contains the
23898fe96085Stim szeto  *           number of bytes required for propVal
23908fe96085Stim szeto  */
23918fe96085Stim szeto static int
23928fe96085Stim szeto getDiskProp(luResourceImpl *hdl, uint32_t prop, char *propVal, size_t *propLen)
23938fe96085Stim szeto {
23948fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
23958fe96085Stim szeto 	diskResource *diskLu = hdl->resource;
239645039663SJohn Forte 	char accessState[20];
23978fe96085Stim szeto 	size_t reqLen;
23988fe96085Stim szeto 
239945039663SJohn Forte 	if (prop == STMF_LU_PROP_ACCESS_STATE) {
240045039663SJohn Forte 		if (diskLu->accessState == SBD_LU_ACTIVE) {
240145039663SJohn Forte 			(void) strlcpy(accessState, STMF_ACCESS_ACTIVE,
240245039663SJohn Forte 			    sizeof (accessState));
240345039663SJohn Forte 		} else if (diskLu->accessState == SBD_LU_TRANSITION_TO_ACTIVE) {
240445039663SJohn Forte 			(void) strlcpy(accessState,
240545039663SJohn Forte 			    STMF_ACCESS_STANDBY_TO_ACTIVE,
240645039663SJohn Forte 			    sizeof (accessState));
240745039663SJohn Forte 		} else if (diskLu->accessState == SBD_LU_STANDBY) {
240845039663SJohn Forte 			(void) strlcpy(accessState, STMF_ACCESS_STANDBY,
240945039663SJohn Forte 			    sizeof (accessState));
241045039663SJohn Forte 		} else if (diskLu->accessState ==
241145039663SJohn Forte 		    SBD_LU_TRANSITION_TO_STANDBY) {
241245039663SJohn Forte 			(void) strlcpy(accessState,
241345039663SJohn Forte 			    STMF_ACCESS_ACTIVE_TO_STANDBY,
241445039663SJohn Forte 			    sizeof (accessState));
241545039663SJohn Forte 		}
241645039663SJohn Forte 		if ((reqLen = strlcpy(propVal, accessState,
241745039663SJohn Forte 		    *propLen)) >= *propLen) {
241845039663SJohn Forte 			*propLen = reqLen + 1;
241945039663SJohn Forte 			return (STMF_ERROR_INVALID_ARG);
242045039663SJohn Forte 		}
242145039663SJohn Forte 		return (0);
242245039663SJohn Forte 	}
242345039663SJohn Forte 
242445039663SJohn Forte 	if (diskLu->accessState != SBD_LU_ACTIVE) {
242545039663SJohn Forte 		return (STMF_ERROR_NO_PROP_STANDBY);
242645039663SJohn Forte 	}
242745039663SJohn Forte 
24288fe96085Stim szeto 	switch (prop) {
24298fe96085Stim szeto 		case STMF_LU_PROP_BLOCK_SIZE:
24308fe96085Stim szeto 			if (diskLu->blkSizeValid == B_FALSE) {
24318fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
24328fe96085Stim szeto 			}
24338fe96085Stim szeto 			reqLen = snprintf(propVal, *propLen, "%llu",
24348fe96085Stim szeto 			    (u_longlong_t)diskLu->blkSize);
24358fe96085Stim szeto 			if (reqLen >= *propLen) {
24368fe96085Stim szeto 				*propLen = reqLen + 1;
24378fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24388fe96085Stim szeto 			}
24398fe96085Stim szeto 			break;
24408fe96085Stim szeto 		case STMF_LU_PROP_FILENAME:
24418fe96085Stim szeto 			if (diskLu->luDataFileNameValid == B_FALSE) {
24428fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
24438fe96085Stim szeto 			}
24448fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->luDataFileName,
24458fe96085Stim szeto 			    *propLen)) >= *propLen) {
24468fe96085Stim szeto 				*propLen = reqLen + 1;
24478fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24488fe96085Stim szeto 			}
24498fe96085Stim szeto 			break;
24508fe96085Stim szeto 		case STMF_LU_PROP_META_FILENAME:
24518fe96085Stim szeto 			if (diskLu->luMetaFileNameValid == B_FALSE) {
24528fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
24538fe96085Stim szeto 			}
24548fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->luMetaFileName,
24558fe96085Stim szeto 			    *propLen)) >= *propLen) {
24568fe96085Stim szeto 				*propLen = reqLen + 1;
24578fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24588fe96085Stim szeto 			}
24598fe96085Stim szeto 			break;
24602f624233SNattuvetty Bhavyan 		case STMF_LU_PROP_MGMT_URL:
24612f624233SNattuvetty Bhavyan 			if (diskLu->luMgmtUrlValid == B_FALSE) {
24622f624233SNattuvetty Bhavyan 				return (STMF_ERROR_NO_PROP);
24632f624233SNattuvetty Bhavyan 			}
24642f624233SNattuvetty Bhavyan 			if ((reqLen = strlcpy(propVal, diskLu->luMgmtUrl,
24652f624233SNattuvetty Bhavyan 			    *propLen)) >= *propLen) {
24662f624233SNattuvetty Bhavyan 				*propLen = reqLen + 1;
24672f624233SNattuvetty Bhavyan 				return (STMF_ERROR_INVALID_ARG);
24682f624233SNattuvetty Bhavyan 			}
24692f624233SNattuvetty Bhavyan 			break;
24708fe96085Stim szeto 		case STMF_LU_PROP_GUID:
24718fe96085Stim szeto 			if (diskLu->luGuidValid == B_FALSE) {
24728fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
24738fe96085Stim szeto 			}
24748fe96085Stim szeto 			reqLen = snprintf(propVal, *propLen,
24758fe96085Stim szeto 			    "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X"
24768fe96085Stim szeto 			    "%02X%02X%02X%02X",
24778fe96085Stim szeto 			    diskLu->luGuid[0], diskLu->luGuid[1],
24788fe96085Stim szeto 			    diskLu->luGuid[2], diskLu->luGuid[3],
24798fe96085Stim szeto 			    diskLu->luGuid[4], diskLu->luGuid[5],
24808fe96085Stim szeto 			    diskLu->luGuid[6], diskLu->luGuid[7],
24818fe96085Stim szeto 			    diskLu->luGuid[8], diskLu->luGuid[9],
24828fe96085Stim szeto 			    diskLu->luGuid[10], diskLu->luGuid[11],
24838fe96085Stim szeto 			    diskLu->luGuid[12], diskLu->luGuid[13],
24848fe96085Stim szeto 			    diskLu->luGuid[14], diskLu->luGuid[15]);
24858fe96085Stim szeto 			if (reqLen >= *propLen) {
24868fe96085Stim szeto 				*propLen = reqLen + 1;
24878fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24888fe96085Stim szeto 			}
24898fe96085Stim szeto 			break;
24908fe96085Stim szeto 		case STMF_LU_PROP_SERIAL_NUM:
24918fe96085Stim szeto 			if (diskLu->serialNumValid == B_FALSE) {
24928fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
24938fe96085Stim szeto 			}
24948fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->serialNum,
24958fe96085Stim szeto 			    *propLen)) >= *propLen) {
24968fe96085Stim szeto 				*propLen = reqLen + 1;
24978fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
24988fe96085Stim szeto 			}
24998fe96085Stim szeto 			break;
25008fe96085Stim szeto 		case STMF_LU_PROP_SIZE:
25018fe96085Stim szeto 			if (diskLu->luSizeValid == B_FALSE) {
25028fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
25038fe96085Stim szeto 			}
25048fe96085Stim szeto 			(void) snprintf(propVal, *propLen, "%llu",
25058fe96085Stim szeto 			    (u_longlong_t)diskLu->luSize);
25068fe96085Stim szeto 			break;
25078fe96085Stim szeto 		case STMF_LU_PROP_ALIAS:
25088fe96085Stim szeto 			if (diskLu->luAliasValid == B_FALSE) {
25098fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
25108fe96085Stim szeto 			}
25118fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->luAlias,
25128fe96085Stim szeto 			    *propLen)) >= *propLen) {
25138fe96085Stim szeto 				*propLen = reqLen + 1;
25148fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
25158fe96085Stim szeto 			}
25168fe96085Stim szeto 			break;
25178fe96085Stim szeto 		case STMF_LU_PROP_VID:
25188fe96085Stim szeto 			if (diskLu->vidValid == B_FALSE) {
25198fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
25208fe96085Stim szeto 			}
25218fe96085Stim szeto 			if (*propLen <= sizeof (diskLu->vid)) {
25228fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
25238fe96085Stim szeto 			}
25248fe96085Stim szeto 			bcopy(diskLu->vid, propVal, sizeof (diskLu->vid));
25258fe96085Stim szeto 			propVal[sizeof (diskLu->vid)] = 0;
25268fe96085Stim szeto 			break;
25278fe96085Stim szeto 		case STMF_LU_PROP_PID:
25288fe96085Stim szeto 			if (diskLu->pidValid == B_FALSE) {
25298fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
25308fe96085Stim szeto 			}
25318fe96085Stim szeto 			if (*propLen <= sizeof (diskLu->pid)) {
25328fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
25338fe96085Stim szeto 			}
25348fe96085Stim szeto 			bcopy(diskLu->pid, propVal, sizeof (diskLu->pid));
25358fe96085Stim szeto 			propVal[sizeof (diskLu->pid)] = 0;
25368fe96085Stim szeto 			break;
25378fe96085Stim szeto 		case STMF_LU_PROP_WRITE_PROTECT:
25388fe96085Stim szeto 			if (diskLu->writeProtectEnableValid == B_FALSE) {
25398fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
25408fe96085Stim szeto 			}
25418fe96085Stim szeto 			if (diskLu->writeProtectEnable) {
25428fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "true",
25438fe96085Stim szeto 				    *propLen)) >= *propLen) {
25448fe96085Stim szeto 					*propLen = reqLen + 1;
25458fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
25468fe96085Stim szeto 				}
25478fe96085Stim szeto 			} else {
25488fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "false",
25498fe96085Stim szeto 				    *propLen)) >= *propLen) {
25508fe96085Stim szeto 					*propLen = reqLen + 1;
25518fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
25528fe96085Stim szeto 				}
25538fe96085Stim szeto 			}
25548fe96085Stim szeto 			break;
25558fe96085Stim szeto 		case STMF_LU_PROP_WRITE_CACHE_DISABLE:
25568fe96085Stim szeto 			if (diskLu->writebackCacheDisableValid == B_FALSE) {
25578fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
25588fe96085Stim szeto 			}
25598fe96085Stim szeto 			if (diskLu->writebackCacheDisable) {
25608fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "true",
25618fe96085Stim szeto 				    *propLen)) >= *propLen) {
25628fe96085Stim szeto 					*propLen = reqLen + 1;
25638fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
25648fe96085Stim szeto 				}
25658fe96085Stim szeto 			} else {
25668fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "false",
25678fe96085Stim szeto 				    *propLen)) >= *propLen) {
25688fe96085Stim szeto 					*propLen = reqLen + 1;
25698fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
25708fe96085Stim szeto 				}
25718fe96085Stim szeto 			}
25728fe96085Stim szeto 			break;
25738fe96085Stim szeto 		default:
25748fe96085Stim szeto 			ret = STMF_ERROR_NO_PROP;
25758fe96085Stim szeto 			break;
25768fe96085Stim szeto 	}
25778fe96085Stim szeto 
25788fe96085Stim szeto 	return (ret);
25798fe96085Stim szeto }
25808fe96085Stim szeto 
25818fe96085Stim szeto /*
25828fe96085Stim szeto  * setDiskProp
25838fe96085Stim szeto  *
25848fe96085Stim szeto  * Purpose: set properties for resource of type disk
25858fe96085Stim szeto  *
25868fe96085Stim szeto  * hdl - allocated luResourceImpl
25878fe96085Stim szeto  * resourceProp - valid resource identifier
25888fe96085Stim szeto  * propVal - valid resource value
25898fe96085Stim szeto  */
25908fe96085Stim szeto static int
25918fe96085Stim szeto setDiskProp(luResourceImpl *hdl, uint32_t resourceProp, const char *propVal)
25928fe96085Stim szeto {
25938fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
25948fe96085Stim szeto 	int i;
25958fe96085Stim szeto 	diskResource *diskLu = hdl->resource;
25968fe96085Stim szeto 	unsigned long long numericProp = 0;
25978fe96085Stim szeto 	char guidProp[LU_ASCII_GUID_SIZE + 1];
25988fe96085Stim szeto 	char ouiProp[OUI_ASCII_SIZE + 1];
2599*fdcc480aSJohn Forte 	char hostIdProp[HOST_ID_ASCII_SIZE + 1];
26008fe96085Stim szeto 	unsigned int oui[OUI_SIZE];
2601*fdcc480aSJohn Forte 	unsigned int hostId[HOST_ID_SIZE];
26028fe96085Stim szeto 	unsigned int guid[LU_GUID_SIZE];
26038fe96085Stim szeto 	int propSize;
26048fe96085Stim szeto 
26058fe96085Stim szeto 
26068fe96085Stim szeto 	if (propVal == NULL) {
26078fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
26088fe96085Stim szeto 	}
26098fe96085Stim szeto 
26108fe96085Stim szeto 	switch (resourceProp) {
26118fe96085Stim szeto 		case STMF_LU_PROP_ALIAS:
26128fe96085Stim szeto 			if (strlcpy(diskLu->luAlias, propVal,
26138fe96085Stim szeto 			    sizeof (diskLu->luAlias)) >=
26148fe96085Stim szeto 			    sizeof (diskLu->luAlias)) {
26158fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
26168fe96085Stim szeto 			}
26178fe96085Stim szeto 			diskLu->luAliasValid = B_TRUE;
26188fe96085Stim szeto 			break;
26198fe96085Stim szeto 		case STMF_LU_PROP_BLOCK_SIZE:
26208fe96085Stim szeto 			(void) sscanf(propVal, "%llu", &numericProp);
26218fe96085Stim szeto 			if (numericProp > UINT16_MAX) {
26228fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
26238fe96085Stim szeto 			}
26248fe96085Stim szeto 			diskLu->blkSize = numericProp;
26258fe96085Stim szeto 			diskLu->blkSizeValid = B_TRUE;
26268fe96085Stim szeto 			break;
26278fe96085Stim szeto 		case STMF_LU_PROP_COMPANY_ID:
26288fe96085Stim szeto 			if ((strlcpy(ouiProp, propVal, sizeof (ouiProp))) >=
26298fe96085Stim szeto 			    sizeof (ouiProp)) {
26308fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
26318fe96085Stim szeto 			}
26328fe96085Stim szeto 			if (checkHexUpper(ouiProp) != 0) {
26338fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
26348fe96085Stim szeto 			}
26358fe96085Stim szeto 			(void) sscanf(ouiProp, "%2X%2X%2X",
26368fe96085Stim szeto 			    &oui[0], &oui[1], &oui[2]);
26378fe96085Stim szeto 
26388fe96085Stim szeto 			diskLu->companyId = 0;
26398fe96085Stim szeto 			diskLu->companyId += oui[0] << 16;
26408fe96085Stim szeto 			diskLu->companyId += oui[1] << 8;
26418fe96085Stim szeto 			diskLu->companyId += oui[2];
2642*fdcc480aSJohn Forte 			if (diskLu->companyId == 0) {
2643*fdcc480aSJohn Forte 				return (STMF_ERROR_INVALID_ARG);
2644*fdcc480aSJohn Forte 			}
26458fe96085Stim szeto 			diskLu->companyIdValid = B_TRUE;
26468fe96085Stim szeto 			break;
2647*fdcc480aSJohn Forte 		case STMF_LU_PROP_HOST_ID:
2648*fdcc480aSJohn Forte 			if ((strlcpy(hostIdProp, propVal,
2649*fdcc480aSJohn Forte 			    sizeof (hostIdProp))) >= sizeof (hostIdProp)) {
2650*fdcc480aSJohn Forte 				return (STMF_ERROR_INVALID_ARG);
2651*fdcc480aSJohn Forte 			}
2652*fdcc480aSJohn Forte 			if (checkHexUpper(hostIdProp) != 0) {
2653*fdcc480aSJohn Forte 				return (STMF_ERROR_INVALID_ARG);
2654*fdcc480aSJohn Forte 			}
2655*fdcc480aSJohn Forte 			(void) sscanf(hostIdProp, "%2X%2X%2X%2X",
2656*fdcc480aSJohn Forte 			    &hostId[0], &hostId[1], &hostId[2], &hostId[3]);
2657*fdcc480aSJohn Forte 
2658*fdcc480aSJohn Forte 			diskLu->hostId = 0;
2659*fdcc480aSJohn Forte 			diskLu->hostId += hostId[0] << 24;
2660*fdcc480aSJohn Forte 			diskLu->hostId += hostId[1] << 16;
2661*fdcc480aSJohn Forte 			diskLu->hostId += hostId[2] << 8;
2662*fdcc480aSJohn Forte 			diskLu->hostId += hostId[3];
2663*fdcc480aSJohn Forte 			if (diskLu->hostId == 0) {
2664*fdcc480aSJohn Forte 				return (STMF_ERROR_INVALID_ARG);
2665*fdcc480aSJohn Forte 			}
2666*fdcc480aSJohn Forte 			diskLu->hostIdValid = B_TRUE;
2667*fdcc480aSJohn Forte 			break;
26688fe96085Stim szeto 		case STMF_LU_PROP_GUID:
26698fe96085Stim szeto 			if (strlen(propVal) != LU_ASCII_GUID_SIZE) {
26708fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
26718fe96085Stim szeto 			}
26728fe96085Stim szeto 
26738fe96085Stim szeto 			if ((strlcpy(guidProp, propVal, sizeof (guidProp))) >=
26748fe96085Stim szeto 			    sizeof (guidProp)) {
26758fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
26768fe96085Stim szeto 			}
26778fe96085Stim szeto 
26788fe96085Stim szeto 			if (checkHexUpper(guidProp) != 0) {
26798fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
26808fe96085Stim szeto 			}
26818fe96085Stim szeto 
26828fe96085Stim szeto 			(void) sscanf(guidProp,
26838fe96085Stim szeto 			    "%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X",
26848fe96085Stim szeto 			    &guid[0], &guid[1], &guid[2], &guid[3], &guid[4],
26858fe96085Stim szeto 			    &guid[5], &guid[6], &guid[7], &guid[8], &guid[9],
26868fe96085Stim szeto 			    &guid[10], &guid[11], &guid[12], &guid[13],
26878fe96085Stim szeto 			    &guid[14], &guid[15]);
26888fe96085Stim szeto 			for (i = 0; i < sizeof (diskLu->luGuid); i++) {
26898fe96085Stim szeto 				diskLu->luGuid[i] = guid[i];
26908fe96085Stim szeto 			}
26918fe96085Stim szeto 			diskLu->luGuidValid = B_TRUE;
26928fe96085Stim szeto 			break;
26938fe96085Stim szeto 		case STMF_LU_PROP_FILENAME:
26948fe96085Stim szeto 			if ((strlcpy(diskLu->luDataFileName, propVal,
26958fe96085Stim szeto 			    sizeof (diskLu->luDataFileName))) >=
26968fe96085Stim szeto 			    sizeof (diskLu->luDataFileName)) {
26978fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
26988fe96085Stim szeto 			}
26998fe96085Stim szeto 			diskLu->luDataFileNameValid = B_TRUE;
27008fe96085Stim szeto 			break;
27018fe96085Stim szeto 		case STMF_LU_PROP_META_FILENAME:
27028fe96085Stim szeto 			if ((strlcpy(diskLu->luMetaFileName, propVal,
27038fe96085Stim szeto 			    sizeof (diskLu->luMetaFileName))) >=
27048fe96085Stim szeto 			    sizeof (diskLu->luMetaFileName)) {
27058fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
27068fe96085Stim szeto 			}
27078fe96085Stim szeto 			diskLu->luMetaFileNameValid = B_TRUE;
27088fe96085Stim szeto 			break;
27092f624233SNattuvetty Bhavyan 		case STMF_LU_PROP_MGMT_URL:
27102f624233SNattuvetty Bhavyan 			if ((strlcpy(diskLu->luMgmtUrl, propVal,
27112f624233SNattuvetty Bhavyan 			    sizeof (diskLu->luMgmtUrl))) >=
27122f624233SNattuvetty Bhavyan 			    sizeof (diskLu->luMgmtUrl)) {
27132f624233SNattuvetty Bhavyan 				return (STMF_ERROR_INVALID_PROPSIZE);
27142f624233SNattuvetty Bhavyan 			}
27152f624233SNattuvetty Bhavyan 			diskLu->luMgmtUrlValid = B_TRUE;
27162f624233SNattuvetty Bhavyan 			break;
27178fe96085Stim szeto 		case STMF_LU_PROP_PID:
27188fe96085Stim szeto 			if ((propSize = strlen(propVal)) >
27198fe96085Stim szeto 			    sizeof (diskLu->pid)) {
27208fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
27218fe96085Stim szeto 			}
27228fe96085Stim szeto 			(void) strncpy(diskLu->pid, propVal, propSize);
27238fe96085Stim szeto 			diskLu->pidValid = B_TRUE;
27248fe96085Stim szeto 			break;
27258fe96085Stim szeto 		case STMF_LU_PROP_SERIAL_NUM:
27268fe96085Stim szeto 			if ((propSize = strlen(propVal)) >
27278fe96085Stim szeto 			    (sizeof (diskLu->serialNum) - 1)) {
27288fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
27298fe96085Stim szeto 			}
27308fe96085Stim szeto 			(void) strncpy(diskLu->serialNum, propVal, propSize);
27318fe96085Stim szeto 			diskLu->serialNumValid = B_TRUE;
27328fe96085Stim szeto 			break;
27338fe96085Stim szeto 		case STMF_LU_PROP_SIZE:
27348fe96085Stim szeto 			if ((niceStrToNum(propVal, &diskLu->luSize) != 0)) {
27358fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
27368fe96085Stim szeto 			}
27378fe96085Stim szeto 			diskLu->luSizeValid = B_TRUE;
27388fe96085Stim szeto 			break;
27398fe96085Stim szeto 		case STMF_LU_PROP_VID:
27408fe96085Stim szeto 			if ((propSize = strlen(propVal)) >
27418fe96085Stim szeto 			    sizeof (diskLu->vid)) {
27428fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
27438fe96085Stim szeto 			}
27448fe96085Stim szeto 			(void) strncpy(diskLu->vid, propVal, propSize);
27458fe96085Stim szeto 			diskLu->vidValid = B_TRUE;
27468fe96085Stim szeto 			break;
27478fe96085Stim szeto 		case STMF_LU_PROP_WRITE_PROTECT:
27488fe96085Stim szeto 			if (strcasecmp(propVal, "TRUE") == 0) {
27498fe96085Stim szeto 				diskLu->writeProtectEnable = B_TRUE;
27508fe96085Stim szeto 			} else if (strcasecmp(propVal, "FALSE") == 0) {
27518fe96085Stim szeto 				diskLu->writeProtectEnable = B_FALSE;
27528fe96085Stim szeto 			} else {
27538fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
27548fe96085Stim szeto 			}
27558fe96085Stim szeto 			diskLu->writeProtectEnableValid = B_TRUE;
27568fe96085Stim szeto 			break;
27578fe96085Stim szeto 		case STMF_LU_PROP_WRITE_CACHE_DISABLE:
27588fe96085Stim szeto 			if (strcasecmp(propVal, "TRUE") == 0) {
27598fe96085Stim szeto 				diskLu->writebackCacheDisable = B_TRUE;
27608fe96085Stim szeto 			} else if (strcasecmp(propVal, "FALSE") == 0) {
27618fe96085Stim szeto 				diskLu->writebackCacheDisable = B_FALSE;
27628fe96085Stim szeto 			} else {
27638fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
27648fe96085Stim szeto 			}
27658fe96085Stim szeto 			diskLu->writebackCacheDisableValid = B_TRUE;
27668fe96085Stim szeto 			break;
276745039663SJohn Forte 		case STMF_LU_PROP_ACCESS_STATE:
276845039663SJohn Forte 			ret = STMF_ERROR_INVALID_PROP;
276945039663SJohn Forte 			break;
27708fe96085Stim szeto 		default:
27718fe96085Stim szeto 			ret = STMF_ERROR_NO_PROP;
27728fe96085Stim szeto 			break;
27738fe96085Stim szeto 	}
27748fe96085Stim szeto 	return (ret);
27758fe96085Stim szeto }
27768fe96085Stim szeto 
27778fe96085Stim szeto static int
27788fe96085Stim szeto checkHexUpper(char *buf)
27798fe96085Stim szeto {
27808fe96085Stim szeto 	int i;
27818fe96085Stim szeto 
27828fe96085Stim szeto 	for (i = 0; i < strlen(buf); i++) {
27838fe96085Stim szeto 		if (isxdigit(buf[i])) {
27848fe96085Stim szeto 			buf[i] = toupper(buf[i]);
27858fe96085Stim szeto 			continue;
27868fe96085Stim szeto 		}
27878fe96085Stim szeto 		return (-1);
27888fe96085Stim szeto 	}
27898fe96085Stim szeto 
27908fe96085Stim szeto 	return (0);
27918fe96085Stim szeto }
27928fe96085Stim szeto 
27938fe96085Stim szeto /*
27948fe96085Stim szeto  * Given a numeric suffix, convert the value into a number of bits that the
27958fe96085Stim szeto  * resulting value must be shifted.
27968fe96085Stim szeto  * Code lifted from libzfs_util.c
27978fe96085Stim szeto  */
27988fe96085Stim szeto static int
27998fe96085Stim szeto strToShift(const char *buf)
28008fe96085Stim szeto {
28018fe96085Stim szeto 	const char *ends = "BKMGTPE";
28028fe96085Stim szeto 	int i;
28038fe96085Stim szeto 
28048fe96085Stim szeto 	if (buf[0] == '\0')
28058fe96085Stim szeto 		return (0);
28068fe96085Stim szeto 
28078fe96085Stim szeto 	for (i = 0; i < strlen(ends); i++) {
28088fe96085Stim szeto 		if (toupper(buf[0]) == ends[i])
28098fe96085Stim szeto 			return (10*i);
28108fe96085Stim szeto 	}
28118fe96085Stim szeto 
28128fe96085Stim szeto 	return (-1);
28138fe96085Stim szeto }
28148fe96085Stim szeto 
28158fe96085Stim szeto int
28168fe96085Stim szeto stmfFreeLuResource(luResource hdl)
28178fe96085Stim szeto {
28188fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
28198fe96085Stim szeto 	if (hdl == NULL) {
28208fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
28218fe96085Stim szeto 	}
28228fe96085Stim szeto 
28238fe96085Stim szeto 	luResourceImpl *hdlImpl = hdl;
28248fe96085Stim szeto 	free(hdlImpl->resource);
28258fe96085Stim szeto 	free(hdlImpl);
28268fe96085Stim szeto 	return (ret);
28278fe96085Stim szeto }
28288fe96085Stim szeto 
28298fe96085Stim szeto /*
28308fe96085Stim szeto  * Convert a string of the form '100G' into a real number. Used when setting
28318fe96085Stim szeto  * the size of a logical unit.
28328fe96085Stim szeto  * Code lifted from libzfs_util.c
28338fe96085Stim szeto  */
28348fe96085Stim szeto static int
28358fe96085Stim szeto niceStrToNum(const char *value, uint64_t *num)
28368fe96085Stim szeto {
28378fe96085Stim szeto 	char *end;
28388fe96085Stim szeto 	int shift;
28398fe96085Stim szeto 
28408fe96085Stim szeto 	*num = 0;
28418fe96085Stim szeto 
28428fe96085Stim szeto 	/* Check to see if this looks like a number.  */
28438fe96085Stim szeto 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
28448fe96085Stim szeto 		return (-1);
28458fe96085Stim szeto 	}
28468fe96085Stim szeto 
28478fe96085Stim szeto 	/* Rely on stroull() to process the numeric portion.  */
28488fe96085Stim szeto 	errno = 0;
28498fe96085Stim szeto 	*num = strtoull(value, &end, 10);
28508fe96085Stim szeto 
28518fe96085Stim szeto 	/*
28528fe96085Stim szeto 	 * Check for ERANGE, which indicates that the value is too large to fit
28538fe96085Stim szeto 	 * in a 64-bit value.
28548fe96085Stim szeto 	 */
28558fe96085Stim szeto 	if (errno == ERANGE) {
28568fe96085Stim szeto 		return (-1);
28578fe96085Stim szeto 	}
28588fe96085Stim szeto 
28598fe96085Stim szeto 	/*
28608fe96085Stim szeto 	 * If we have a decimal value, then do the computation with floating
28618fe96085Stim szeto 	 * point arithmetic.  Otherwise, use standard arithmetic.
28628fe96085Stim szeto 	 */
28638fe96085Stim szeto 	if (*end == '.') {
28648fe96085Stim szeto 		double fval = strtod(value, &end);
28658fe96085Stim szeto 
28668fe96085Stim szeto 		if ((shift = strToShift(end)) == -1) {
28678fe96085Stim szeto 			return (-1);
28688fe96085Stim szeto 		}
28698fe96085Stim szeto 
28708fe96085Stim szeto 		fval *= pow(2, shift);
28718fe96085Stim szeto 
28728fe96085Stim szeto 		if (fval > UINT64_MAX) {
28738fe96085Stim szeto 			return (-1);
28748fe96085Stim szeto 		}
28758fe96085Stim szeto 
28768fe96085Stim szeto 		*num = (uint64_t)fval;
28778fe96085Stim szeto 	} else {
28788fe96085Stim szeto 		if ((shift = strToShift(end)) == -1) {
28798fe96085Stim szeto 			return (-1);
28808fe96085Stim szeto 		}
28818fe96085Stim szeto 
28828fe96085Stim szeto 		/* Check for overflow */
28838fe96085Stim szeto 		if (shift >= 64 || (*num << shift) >> shift != *num) {
28848fe96085Stim szeto 			return (-1);
28858fe96085Stim szeto 		}
28868fe96085Stim szeto 
28878fe96085Stim szeto 		*num <<= shift;
28888fe96085Stim szeto 	}
28898fe96085Stim szeto 
28908fe96085Stim szeto 	return (0);
28918fe96085Stim szeto }
28928fe96085Stim szeto 
28938fe96085Stim szeto /*
2894fcf3ce44SJohn Forte  * stmfCreateTargetGroup
2895fcf3ce44SJohn Forte  *
2896fcf3ce44SJohn Forte  * Purpose: Create a local port group
2897fcf3ce44SJohn Forte  *
2898fcf3ce44SJohn Forte  * targetGroupName - name of local port group to create
2899fcf3ce44SJohn Forte  */
2900fcf3ce44SJohn Forte int
2901fcf3ce44SJohn Forte stmfCreateTargetGroup(stmfGroupName *targetGroupName)
2902fcf3ce44SJohn Forte {
2903fcf3ce44SJohn Forte 	int ret;
2904fcf3ce44SJohn Forte 	int fd;
2905fcf3ce44SJohn Forte 
2906fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
2907fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
2908fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName))) {
2909fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2910fcf3ce44SJohn Forte 	}
2911fcf3ce44SJohn Forte 
2912fcf3ce44SJohn Forte 	/* Check to ensure service exists */
2913fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
2914fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
2915fcf3ce44SJohn Forte 	}
2916fcf3ce44SJohn Forte 
2917fcf3ce44SJohn Forte 	/* call init */
2918fcf3ce44SJohn Forte 	ret = initializeConfig();
2919fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
2920fcf3ce44SJohn Forte 		return (ret);
2921fcf3ce44SJohn Forte 	}
2922fcf3ce44SJohn Forte 
2923fcf3ce44SJohn Forte 	/*
2924fcf3ce44SJohn Forte 	 * Open control node for stmf
2925fcf3ce44SJohn Forte 	 */
2926fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
2927fcf3ce44SJohn Forte 		return (ret);
2928fcf3ce44SJohn Forte 
2929fcf3ce44SJohn Forte 	/*
2930fcf3ce44SJohn Forte 	 * Add the group to the driver
2931fcf3ce44SJohn Forte 	 */
2932fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_TARGET_GROUP,
2933fcf3ce44SJohn Forte 	    targetGroupName)) != STMF_STATUS_SUCCESS) {
2934fcf3ce44SJohn Forte 		goto done;
2935fcf3ce44SJohn Forte 	}
2936fcf3ce44SJohn Forte 
29378fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
29388fe96085Stim szeto 		goto done;
29398fe96085Stim szeto 	}
29408fe96085Stim szeto 
2941fcf3ce44SJohn Forte 	/*
2942fcf3ce44SJohn Forte 	 * If the add to the driver was successful, add it to the persistent
2943fcf3ce44SJohn Forte 	 * store.
2944fcf3ce44SJohn Forte 	 */
2945fcf3ce44SJohn Forte 	ret = psCreateTargetGroup((char *)targetGroupName);
2946fcf3ce44SJohn Forte 	switch (ret) {
2947fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
2948fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
2949fcf3ce44SJohn Forte 			break;
2950fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
2951fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
2952fcf3ce44SJohn Forte 			break;
2953fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
2954fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
2955fcf3ce44SJohn Forte 			break;
2956fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
2957fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
2958fcf3ce44SJohn Forte 			break;
2959fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
2960fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
2961fcf3ce44SJohn Forte 			break;
2962fcf3ce44SJohn Forte 		default:
2963fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
2964fcf3ce44SJohn Forte 			    "stmfCreateTargetGroup:psCreateTargetGroup"
2965fcf3ce44SJohn Forte 			    ":error(%d)", ret);
2966fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
2967fcf3ce44SJohn Forte 			break;
2968fcf3ce44SJohn Forte 	}
2969fcf3ce44SJohn Forte 
2970fcf3ce44SJohn Forte done:
2971fcf3ce44SJohn Forte 	(void) close(fd);
2972fcf3ce44SJohn Forte 	return (ret);
2973fcf3ce44SJohn Forte }
2974fcf3ce44SJohn Forte 
2975fcf3ce44SJohn Forte /*
2976fcf3ce44SJohn Forte  * stmfDeleteHostGroup
2977fcf3ce44SJohn Forte  *
2978fcf3ce44SJohn Forte  * Purpose: Delete an initiator or local port group
2979fcf3ce44SJohn Forte  *
2980fcf3ce44SJohn Forte  * hostGroupName - group to delete
2981fcf3ce44SJohn Forte  */
2982fcf3ce44SJohn Forte int
2983fcf3ce44SJohn Forte stmfDeleteHostGroup(stmfGroupName *hostGroupName)
2984fcf3ce44SJohn Forte {
2985fcf3ce44SJohn Forte 	int ret;
2986fcf3ce44SJohn Forte 	int fd;
2987fcf3ce44SJohn Forte 
2988fcf3ce44SJohn Forte 	if (hostGroupName == NULL) {
2989fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
2990fcf3ce44SJohn Forte 	}
2991fcf3ce44SJohn Forte 
2992fcf3ce44SJohn Forte 	/* Check to ensure service exists */
2993fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
2994fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
2995fcf3ce44SJohn Forte 	}
2996fcf3ce44SJohn Forte 
2997fcf3ce44SJohn Forte 	/* call init */
2998fcf3ce44SJohn Forte 	ret = initializeConfig();
2999fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3000fcf3ce44SJohn Forte 		return (ret);
3001fcf3ce44SJohn Forte 	}
3002fcf3ce44SJohn Forte 
3003fcf3ce44SJohn Forte 	/*
3004fcf3ce44SJohn Forte 	 * Open control node for stmf
3005fcf3ce44SJohn Forte 	 */
3006fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3007fcf3ce44SJohn Forte 		return (ret);
3008fcf3ce44SJohn Forte 
3009fcf3ce44SJohn Forte 	/*
3010fcf3ce44SJohn Forte 	 * Remove the group from the driver
3011fcf3ce44SJohn Forte 	 */
3012fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_REMOVE_HOST_GROUP,
3013fcf3ce44SJohn Forte 	    hostGroupName)) != STMF_STATUS_SUCCESS) {
3014fcf3ce44SJohn Forte 		goto done;
3015fcf3ce44SJohn Forte 	}
3016fcf3ce44SJohn Forte 
30178fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
30188fe96085Stim szeto 		goto done;
30198fe96085Stim szeto 	}
30208fe96085Stim szeto 
3021fcf3ce44SJohn Forte 	/*
3022fcf3ce44SJohn Forte 	 * If the remove from the driver was successful, remove it from the
3023fcf3ce44SJohn Forte 	 * persistent store.
3024fcf3ce44SJohn Forte 	 */
3025fcf3ce44SJohn Forte 	ret = psDeleteHostGroup((char *)hostGroupName);
3026fcf3ce44SJohn Forte 	switch (ret) {
3027fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3028fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3029fcf3ce44SJohn Forte 			break;
3030fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3031fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3032fcf3ce44SJohn Forte 			break;
3033fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3034fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3035fcf3ce44SJohn Forte 			break;
3036fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3037fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3038fcf3ce44SJohn Forte 			break;
3039fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3040fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3041fcf3ce44SJohn Forte 			break;
3042fcf3ce44SJohn Forte 		default:
3043fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3044fcf3ce44SJohn Forte 			    "stmfDeleteHostGroup:psDeleteHostGroup:error(%d)",
3045fcf3ce44SJohn Forte 			    ret);
3046fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3047fcf3ce44SJohn Forte 			break;
3048fcf3ce44SJohn Forte 	}
3049fcf3ce44SJohn Forte 
3050fcf3ce44SJohn Forte done:
3051fcf3ce44SJohn Forte 	(void) close(fd);
3052fcf3ce44SJohn Forte 	return (ret);
3053fcf3ce44SJohn Forte }
3054fcf3ce44SJohn Forte 
3055fcf3ce44SJohn Forte /*
3056fcf3ce44SJohn Forte  * stmfDeleteTargetGroup
3057fcf3ce44SJohn Forte  *
3058fcf3ce44SJohn Forte  * Purpose: Delete an initiator or local port group
3059fcf3ce44SJohn Forte  *
3060fcf3ce44SJohn Forte  * targetGroupName - group to delete
3061fcf3ce44SJohn Forte  */
3062fcf3ce44SJohn Forte int
3063fcf3ce44SJohn Forte stmfDeleteTargetGroup(stmfGroupName *targetGroupName)
3064fcf3ce44SJohn Forte {
3065fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
3066fcf3ce44SJohn Forte 	int fd;
3067fcf3ce44SJohn Forte 
3068fcf3ce44SJohn Forte 	if (targetGroupName == NULL) {
3069fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3070fcf3ce44SJohn Forte 	}
3071fcf3ce44SJohn Forte 
3072fcf3ce44SJohn Forte 	/* Check to ensure service exists */
3073fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
3074fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
3075fcf3ce44SJohn Forte 	}
3076fcf3ce44SJohn Forte 
3077fcf3ce44SJohn Forte 	/* call init */
3078fcf3ce44SJohn Forte 	ret = initializeConfig();
3079fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3080fcf3ce44SJohn Forte 		return (ret);
3081fcf3ce44SJohn Forte 	}
3082fcf3ce44SJohn Forte 
3083fcf3ce44SJohn Forte 	/*
3084fcf3ce44SJohn Forte 	 * Open control node for stmf
3085fcf3ce44SJohn Forte 	 */
3086fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3087fcf3ce44SJohn Forte 		return (ret);
3088fcf3ce44SJohn Forte 
3089fcf3ce44SJohn Forte 	/*
3090fcf3ce44SJohn Forte 	 * Remove the group from the driver
3091fcf3ce44SJohn Forte 	 */
3092fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_REMOVE_TARGET_GROUP,
3093fcf3ce44SJohn Forte 	    targetGroupName)) != STMF_STATUS_SUCCESS) {
3094fcf3ce44SJohn Forte 		goto done;
3095fcf3ce44SJohn Forte 	}
3096fcf3ce44SJohn Forte 
30978fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
30988fe96085Stim szeto 		goto done;
30998fe96085Stim szeto 	}
31008fe96085Stim szeto 
3101fcf3ce44SJohn Forte 	/*
3102fcf3ce44SJohn Forte 	 * If the remove from the driver was successful, remove it from the
3103fcf3ce44SJohn Forte 	 * persistent store.
3104fcf3ce44SJohn Forte 	 */
3105fcf3ce44SJohn Forte 	ret = psDeleteTargetGroup((char *)targetGroupName);
3106fcf3ce44SJohn Forte 	switch (ret) {
3107fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3108fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3109fcf3ce44SJohn Forte 			break;
3110fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3111fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3112fcf3ce44SJohn Forte 			break;
3113fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3114fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3115fcf3ce44SJohn Forte 			break;
3116fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3117fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3118fcf3ce44SJohn Forte 			break;
3119fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3120fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3121fcf3ce44SJohn Forte 			break;
3122fcf3ce44SJohn Forte 		default:
3123fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3124fcf3ce44SJohn Forte 			    "stmfDeleteTargetGroup:psDeleteTargetGroup"
3125fcf3ce44SJohn Forte 			    ":error(%d)", ret);
3126fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3127fcf3ce44SJohn Forte 			break;
3128fcf3ce44SJohn Forte 	}
3129fcf3ce44SJohn Forte 
3130fcf3ce44SJohn Forte done:
3131fcf3ce44SJohn Forte 	(void) close(fd);
3132fcf3ce44SJohn Forte 	return (ret);
3133fcf3ce44SJohn Forte }
3134fcf3ce44SJohn Forte 
3135fcf3ce44SJohn Forte /*
3136fcf3ce44SJohn Forte  * stmfDevidFromIscsiName
3137fcf3ce44SJohn Forte  *
3138fcf3ce44SJohn Forte  * Purpose: convert an iSCSI name to an stmf devid
3139fcf3ce44SJohn Forte  *
3140fcf3ce44SJohn Forte  * iscsiName - unicode nul terminated utf-8 encoded iSCSI name
3141fcf3ce44SJohn Forte  * devid - on success, contains the converted iscsi name
3142fcf3ce44SJohn Forte  */
3143fcf3ce44SJohn Forte int
3144fcf3ce44SJohn Forte stmfDevidFromIscsiName(char *iscsiName, stmfDevid *devid)
3145fcf3ce44SJohn Forte {
3146fcf3ce44SJohn Forte 	if (devid == NULL || iscsiName == NULL)
3147fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3148fcf3ce44SJohn Forte 
3149fcf3ce44SJohn Forte 	bzero(devid, sizeof (stmfDevid));
3150fcf3ce44SJohn Forte 
3151fcf3ce44SJohn Forte 	/* Validate size of target */
3152fcf3ce44SJohn Forte 	if ((devid->identLength = strlen(iscsiName)) > MAX_ISCSI_NAME ||
3153fcf3ce44SJohn Forte 	    devid->identLength < strlen(EUI) ||
3154fcf3ce44SJohn Forte 	    devid->identLength < strlen(IQN)) {
3155fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3156fcf3ce44SJohn Forte 	}
3157fcf3ce44SJohn Forte 
3158fcf3ce44SJohn Forte 	if ((strncmp(iscsiName, EUI, strlen(EUI)) != 0) &&
3159fcf3ce44SJohn Forte 	    strncmp(iscsiName, IQN, strlen(IQN)) != 0) {
3160fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3161fcf3ce44SJohn Forte 	}
3162fcf3ce44SJohn Forte 
3163fcf3ce44SJohn Forte 	/* copy UTF-8 bytes to ident */
3164fcf3ce44SJohn Forte 	bcopy(iscsiName, devid->ident, devid->identLength);
3165fcf3ce44SJohn Forte 
3166fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
3167fcf3ce44SJohn Forte }
3168fcf3ce44SJohn Forte 
3169fcf3ce44SJohn Forte /*
3170fcf3ce44SJohn Forte  * stmfDevidFromWwn
3171fcf3ce44SJohn Forte  *
3172fcf3ce44SJohn Forte  * Purpose: convert a WWN to an stmf devid
3173fcf3ce44SJohn Forte  *
3174fcf3ce44SJohn Forte  * wwn - 8-byte wwn identifier
3175fcf3ce44SJohn Forte  * devid - on success, contains the converted wwn
3176fcf3ce44SJohn Forte  */
3177fcf3ce44SJohn Forte int
3178fcf3ce44SJohn Forte stmfDevidFromWwn(uchar_t *wwn, stmfDevid *devid)
3179fcf3ce44SJohn Forte {
3180fcf3ce44SJohn Forte 	if (wwn == NULL || devid == NULL)
3181fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3182fcf3ce44SJohn Forte 
3183fcf3ce44SJohn Forte 	bzero(devid, sizeof (stmfDevid));
3184fcf3ce44SJohn Forte 
3185fcf3ce44SJohn Forte 	/* Copy eui prefix */
3186fcf3ce44SJohn Forte 	(void) bcopy(WWN, devid->ident, strlen(WWN));
3187fcf3ce44SJohn Forte 
3188fcf3ce44SJohn Forte 	/* Convert to ASCII uppercase hexadecimal string */
3189fcf3ce44SJohn Forte 	(void) snprintf((char *)&devid->ident[strlen(WWN)],
3190fcf3ce44SJohn Forte 	    sizeof (devid->ident), "%02X%02X%02X%02X%02X%02X%02X%02X",
3191fcf3ce44SJohn Forte 	    wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
3192fcf3ce44SJohn Forte 
3193fcf3ce44SJohn Forte 	devid->identLength = strlen((char *)devid->ident);
3194fcf3ce44SJohn Forte 
3195fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
3196fcf3ce44SJohn Forte }
3197fcf3ce44SJohn Forte 
3198fcf3ce44SJohn Forte /*
3199fcf3ce44SJohn Forte  * stmfFreeMemory
3200fcf3ce44SJohn Forte  *
3201fcf3ce44SJohn Forte  * Purpose: Free memory allocated by this library
3202fcf3ce44SJohn Forte  *
3203fcf3ce44SJohn Forte  * memory - previously allocated pointer of memory managed by library
3204fcf3ce44SJohn Forte  */
3205fcf3ce44SJohn Forte void
3206fcf3ce44SJohn Forte stmfFreeMemory(void *memory)
3207fcf3ce44SJohn Forte {
3208fcf3ce44SJohn Forte 	free(memory);
3209fcf3ce44SJohn Forte }
3210fcf3ce44SJohn Forte 
3211fcf3ce44SJohn Forte /*
32128fe96085Stim szeto  * get host group, target group list from stmf
3213fcf3ce44SJohn Forte  *
32148fe96085Stim szeto  * groupType - HOST_GROUP, TARGET_GROUP
3215fcf3ce44SJohn Forte  */
32168fe96085Stim szeto static int
32178fe96085Stim szeto groupListIoctl(stmfGroupList **groupList, int groupType)
3218fcf3ce44SJohn Forte {
3219fcf3ce44SJohn Forte 	int ret;
32208fe96085Stim szeto 	int fd;
32218fe96085Stim szeto 	int ioctlRet;
32228fe96085Stim szeto 	int i;
32238fe96085Stim szeto 	int cmd;
32248fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
32258fe96085Stim szeto 	/* framework group list */
32268fe96085Stim szeto 	stmf_group_name_t *iGroupList = NULL;
32278fe96085Stim szeto 	uint32_t groupListSize;
3228fcf3ce44SJohn Forte 
32298fe96085Stim szeto 	if (groupList == NULL) {
3230fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3231fcf3ce44SJohn Forte 	}
3232fcf3ce44SJohn Forte 
32338fe96085Stim szeto 	if (groupType == HOST_GROUP) {
32348fe96085Stim szeto 		cmd = STMF_IOCTL_GET_HG_LIST;
32358fe96085Stim szeto 	} else if (groupType == TARGET_GROUP) {
32368fe96085Stim szeto 		cmd = STMF_IOCTL_GET_TG_LIST;
32378fe96085Stim szeto 	} else {
32388fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
32398fe96085Stim szeto 	}
32408fe96085Stim szeto 
32418fe96085Stim szeto 	/* call init */
32428fe96085Stim szeto 	ret = initializeConfig();
32438fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
32448fe96085Stim szeto 		return (ret);
32458fe96085Stim szeto 	}
32468fe96085Stim szeto 
32478fe96085Stim szeto 	/*
32488fe96085Stim szeto 	 * Open control node for stmf
32498fe96085Stim szeto 	 */
32508fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
32518fe96085Stim szeto 		return (ret);
32528fe96085Stim szeto 
32538fe96085Stim szeto 	/*
32548fe96085Stim szeto 	 * Allocate ioctl input buffer
32558fe96085Stim szeto 	 */
32568fe96085Stim szeto 	groupListSize = ALLOC_GROUP;
32578fe96085Stim szeto 	groupListSize = groupListSize * (sizeof (stmf_group_name_t));
32588fe96085Stim szeto 	iGroupList = (stmf_group_name_t *)calloc(1, groupListSize);
32598fe96085Stim szeto 	if (iGroupList == NULL) {
32608fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
32618fe96085Stim szeto 		goto done;
32628fe96085Stim szeto 	}
32638fe96085Stim szeto 
32648fe96085Stim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
32658fe96085Stim szeto 	/*
32668fe96085Stim szeto 	 * Issue ioctl to get the group list
32678fe96085Stim szeto 	 */
32688fe96085Stim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
32698fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = groupListSize;
32708fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupList;
32718fe96085Stim szeto 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
32728fe96085Stim szeto 	if (ioctlRet != 0) {
32738fe96085Stim szeto 		switch (errno) {
32748fe96085Stim szeto 			case EBUSY:
32758fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
32768fe96085Stim szeto 				break;
32778fe96085Stim szeto 			case EPERM:
32788fe96085Stim szeto 			case EACCES:
32798fe96085Stim szeto 				ret = STMF_ERROR_PERM;
32808fe96085Stim szeto 				break;
32818fe96085Stim szeto 			default:
32828fe96085Stim szeto 				syslog(LOG_DEBUG,
32838fe96085Stim szeto 				    "groupListIoctl:ioctl errno(%d)",
32848fe96085Stim szeto 				    errno);
32858fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
32868fe96085Stim szeto 				break;
32878fe96085Stim szeto 		}
32888fe96085Stim szeto 		goto done;
32898fe96085Stim szeto 	}
32908fe96085Stim szeto 	/*
32918fe96085Stim szeto 	 * Check whether input buffer was large enough
32928fe96085Stim szeto 	 */
32938fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_GROUP) {
32948fe96085Stim szeto 		groupListSize = stmfIoctl.stmf_obuf_max_nentries *
32958fe96085Stim szeto 		    sizeof (stmf_group_name_t);
32968fe96085Stim szeto 		iGroupList = realloc(iGroupList, groupListSize);
32978fe96085Stim szeto 		if (iGroupList == NULL) {
32988fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
32998fe96085Stim szeto 			goto done;
33008fe96085Stim szeto 		}
33018fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = groupListSize;
33028fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupList;
33038fe96085Stim szeto 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
33048fe96085Stim szeto 		if (ioctlRet != 0) {
33058fe96085Stim szeto 			switch (errno) {
33068fe96085Stim szeto 				case EBUSY:
33078fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
33088fe96085Stim szeto 					break;
33098fe96085Stim szeto 				case EPERM:
33108fe96085Stim szeto 				case EACCES:
33118fe96085Stim szeto 					ret = STMF_ERROR_PERM;
33128fe96085Stim szeto 					break;
33138fe96085Stim szeto 				default:
33148fe96085Stim szeto 					syslog(LOG_DEBUG,
33158fe96085Stim szeto 					    "groupListIoctl:ioctl errno(%d)",
33168fe96085Stim szeto 					    errno);
33178fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
33188fe96085Stim szeto 					break;
33198fe96085Stim szeto 			}
33208fe96085Stim szeto 			goto done;
33218fe96085Stim szeto 		}
33228fe96085Stim szeto 	}
33238fe96085Stim szeto 
33248fe96085Stim szeto 	/* allocate and copy to caller's buffer */
33254f2997b2Ssrivijitha dugganapalli 	*groupList = (stmfGroupList *)calloc(1, sizeof (stmfGroupList) +
33264f2997b2Ssrivijitha dugganapalli 	    sizeof (stmfGroupName) * stmfIoctl.stmf_obuf_nentries);
33278fe96085Stim szeto 	if (*groupList == NULL) {
33288fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
33298fe96085Stim szeto 		goto done;
33308fe96085Stim szeto 	}
33318fe96085Stim szeto 	(*groupList)->cnt = stmfIoctl.stmf_obuf_nentries;
33328fe96085Stim szeto 	for (i = 0; i < stmfIoctl.stmf_obuf_nentries; i++) {
33332f624233SNattuvetty Bhavyan 		bcopy(iGroupList[i].name, (*groupList)->name[i],
33348fe96085Stim szeto 		    sizeof (stmfGroupName));
33358fe96085Stim szeto 	}
33368fe96085Stim szeto 
33378fe96085Stim szeto done:
33388fe96085Stim szeto 	free(iGroupList);
33398fe96085Stim szeto 	(void) close(fd);
33408fe96085Stim szeto 	return (ret);
33418fe96085Stim szeto }
33428fe96085Stim szeto 
33438fe96085Stim szeto /*
33448fe96085Stim szeto  * get host group members, target group members from stmf
33458fe96085Stim szeto  *
33468fe96085Stim szeto  * groupProps - allocated on success
33478fe96085Stim szeto  *
33488fe96085Stim szeto  * groupType - HOST_GROUP, TARGET_GROUP
33498fe96085Stim szeto  */
33508fe96085Stim szeto static int
33518fe96085Stim szeto groupMemberListIoctl(stmfGroupName *groupName, stmfGroupProperties **groupProps,
33528fe96085Stim szeto     int groupType)
33538fe96085Stim szeto {
33548fe96085Stim szeto 	int ret;
33558fe96085Stim szeto 	int fd;
33568fe96085Stim szeto 	int ioctlRet;
33578fe96085Stim szeto 	int i;
33588fe96085Stim szeto 	int cmd;
33598fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
33608fe96085Stim szeto 	/* framework group list */
33618fe96085Stim szeto 	stmf_group_name_t iGroupName;
33628fe96085Stim szeto 	stmf_ge_ident_t *iGroupMembers;
33638fe96085Stim szeto 	uint32_t groupListSize;
33648fe96085Stim szeto 
33658fe96085Stim szeto 	if (groupName == NULL) {
33668fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
33678fe96085Stim szeto 	}
33688fe96085Stim szeto 
33698fe96085Stim szeto 	if (groupType == HOST_GROUP) {
33708fe96085Stim szeto 		cmd = STMF_IOCTL_GET_HG_ENTRIES;
33718fe96085Stim szeto 	} else if (groupType == TARGET_GROUP) {
33728fe96085Stim szeto 		cmd = STMF_IOCTL_GET_TG_ENTRIES;
33738fe96085Stim szeto 	} else {
33748fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
33758fe96085Stim szeto 	}
33768fe96085Stim szeto 
33778fe96085Stim szeto 	/* call init */
33788fe96085Stim szeto 	ret = initializeConfig();
33798fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
33808fe96085Stim szeto 		return (ret);
33818fe96085Stim szeto 	}
33828fe96085Stim szeto 
33838fe96085Stim szeto 	/*
33848fe96085Stim szeto 	 * Open control node for stmf
33858fe96085Stim szeto 	 */
33868fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
33878fe96085Stim szeto 		return (ret);
33888fe96085Stim szeto 
33898fe96085Stim szeto 	bzero(&iGroupName, sizeof (iGroupName));
33908fe96085Stim szeto 
33918fe96085Stim szeto 	bcopy(groupName, &iGroupName.name, strlen((char *)groupName));
33928fe96085Stim szeto 
33938fe96085Stim szeto 	iGroupName.name_size = strlen((char *)groupName);
33948fe96085Stim szeto 
33958fe96085Stim szeto 	/*
33968fe96085Stim szeto 	 * Allocate ioctl input buffer
33978fe96085Stim szeto 	 */
33988fe96085Stim szeto 	groupListSize = ALLOC_GRP_MEMBER;
33998fe96085Stim szeto 	groupListSize = groupListSize * (sizeof (stmf_ge_ident_t));
34008fe96085Stim szeto 	iGroupMembers = (stmf_ge_ident_t *)calloc(1, groupListSize);
34018fe96085Stim szeto 	if (iGroupMembers == NULL) {
34028fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
34038fe96085Stim szeto 		goto done;
34048fe96085Stim szeto 	}
34058fe96085Stim szeto 
34068fe96085Stim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
34078fe96085Stim szeto 	/*
34088fe96085Stim szeto 	 * Issue ioctl to get the group list
34098fe96085Stim szeto 	 */
34108fe96085Stim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
34118fe96085Stim szeto 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&iGroupName;
34128fe96085Stim szeto 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_group_name_t);
34138fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = groupListSize;
34148fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupMembers;
34158fe96085Stim szeto 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
34168fe96085Stim szeto 	if (ioctlRet != 0) {
34178fe96085Stim szeto 		switch (errno) {
34188fe96085Stim szeto 			case EBUSY:
34198fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
34208fe96085Stim szeto 				break;
34218fe96085Stim szeto 			case EPERM:
34228fe96085Stim szeto 			case EACCES:
34238fe96085Stim szeto 				ret = STMF_ERROR_PERM;
34248fe96085Stim szeto 				break;
34258fe96085Stim szeto 			default:
34268fe96085Stim szeto 				syslog(LOG_DEBUG,
34278fe96085Stim szeto 				    "groupListIoctl:ioctl errno(%d)",
34288fe96085Stim szeto 				    errno);
34298fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
34308fe96085Stim szeto 				break;
34318fe96085Stim szeto 		}
34328fe96085Stim szeto 		goto done;
34338fe96085Stim szeto 	}
34348fe96085Stim szeto 	/*
34358fe96085Stim szeto 	 * Check whether input buffer was large enough
34368fe96085Stim szeto 	 */
34378fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_GRP_MEMBER) {
34388fe96085Stim szeto 		groupListSize = stmfIoctl.stmf_obuf_max_nentries *
34398fe96085Stim szeto 		    sizeof (stmf_ge_ident_t);
34408fe96085Stim szeto 		iGroupMembers = realloc(iGroupMembers, groupListSize);
34418fe96085Stim szeto 		if (iGroupMembers == NULL) {
34428fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
34438fe96085Stim szeto 			goto done;
34448fe96085Stim szeto 		}
34458fe96085Stim szeto 		stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&iGroupName;
34468fe96085Stim szeto 		stmfIoctl.stmf_ibuf_size = sizeof (stmf_group_name_t);
34478fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = groupListSize;
34488fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupMembers;
34498fe96085Stim szeto 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
34508fe96085Stim szeto 		if (ioctlRet != 0) {
34518fe96085Stim szeto 			switch (errno) {
34528fe96085Stim szeto 				case EBUSY:
34538fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
34548fe96085Stim szeto 					break;
34558fe96085Stim szeto 				case EPERM:
34568fe96085Stim szeto 				case EACCES:
34578fe96085Stim szeto 					ret = STMF_ERROR_PERM;
34588fe96085Stim szeto 					break;
34598fe96085Stim szeto 				default:
34608fe96085Stim szeto 					syslog(LOG_DEBUG,
34618fe96085Stim szeto 					    "groupListIoctl:ioctl errno(%d)",
34628fe96085Stim szeto 					    errno);
34638fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
34648fe96085Stim szeto 					break;
34658fe96085Stim szeto 			}
34668fe96085Stim szeto 			goto done;
34678fe96085Stim szeto 		}
34688fe96085Stim szeto 	}
34698fe96085Stim szeto 
34708fe96085Stim szeto 	/* allocate and copy to caller's buffer */
34718fe96085Stim szeto 	*groupProps = (stmfGroupProperties *)calloc(1,
34724f2997b2Ssrivijitha dugganapalli 	    sizeof (stmfGroupProperties) +
34734f2997b2Ssrivijitha dugganapalli 	    sizeof (stmfDevid) * stmfIoctl.stmf_obuf_nentries);
34748fe96085Stim szeto 	if (*groupProps == NULL) {
34758fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
34768fe96085Stim szeto 		goto done;
34778fe96085Stim szeto 	}
34788fe96085Stim szeto 	(*groupProps)->cnt = stmfIoctl.stmf_obuf_nentries;
34798fe96085Stim szeto 	for (i = 0; i < stmfIoctl.stmf_obuf_nentries; i++) {
34808fe96085Stim szeto 		(*groupProps)->name[i].identLength =
34812f624233SNattuvetty Bhavyan 		    iGroupMembers[i].ident_size;
34822f624233SNattuvetty Bhavyan 		bcopy(iGroupMembers[i].ident, (*groupProps)->name[i].ident,
34832f624233SNattuvetty Bhavyan 		    iGroupMembers[i].ident_size);
34848fe96085Stim szeto 	}
34858fe96085Stim szeto 
34868fe96085Stim szeto done:
34878fe96085Stim szeto 	free(iGroupMembers);
34888fe96085Stim szeto 	(void) close(fd);
34898fe96085Stim szeto 	return (ret);
34908fe96085Stim szeto }
34918fe96085Stim szeto 
34928fe96085Stim szeto /*
34938fe96085Stim szeto  * Purpose: access persistent config data for host groups and target groups
34948fe96085Stim szeto  */
34958fe96085Stim szeto static int
34968fe96085Stim szeto iLoadGroupFromPs(stmfGroupList **groupList, int type)
34978fe96085Stim szeto {
34988fe96085Stim szeto 	int ret;
34998fe96085Stim szeto 
35008fe96085Stim szeto 	if (groupList == NULL) {
35018fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
35028fe96085Stim szeto 	}
35038fe96085Stim szeto 
35048fe96085Stim szeto 	if (type == HOST_GROUP) {
35058fe96085Stim szeto 		ret = psGetHostGroupList(groupList);
35068fe96085Stim szeto 	} else if (type == TARGET_GROUP) {
35078fe96085Stim szeto 		ret = psGetTargetGroupList(groupList);
35088fe96085Stim szeto 	} else {
35098fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
35108fe96085Stim szeto 	}
3511fcf3ce44SJohn Forte 	switch (ret) {
3512fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3513fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3514fcf3ce44SJohn Forte 			break;
3515fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3516fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3517fcf3ce44SJohn Forte 			break;
3518fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3519fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3520fcf3ce44SJohn Forte 			break;
3521fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3522fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3523fcf3ce44SJohn Forte 			break;
3524fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3525fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3526fcf3ce44SJohn Forte 			break;
3527fcf3ce44SJohn Forte 		default:
3528fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3529fcf3ce44SJohn Forte 			    "stmfGetHostGroupList:psGetHostGroupList:error(%d)",
3530fcf3ce44SJohn Forte 			    ret);
3531fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3532fcf3ce44SJohn Forte 			break;
3533fcf3ce44SJohn Forte 	}
3534fcf3ce44SJohn Forte 
3535fcf3ce44SJohn Forte 	return (ret);
3536fcf3ce44SJohn Forte }
3537fcf3ce44SJohn Forte 
3538fcf3ce44SJohn Forte /*
35398fe96085Stim szeto  * stmfGetHostGroupList
3540fcf3ce44SJohn Forte  *
35418fe96085Stim szeto  * Purpose: Retrieves the list of initiator group oids
3542fcf3ce44SJohn Forte  *
35438fe96085Stim szeto  * hostGroupList - pointer to pointer to hostGroupList structure
35448fe96085Stim szeto  *                 on success, this contains the host group list.
3545fcf3ce44SJohn Forte  */
3546fcf3ce44SJohn Forte int
35478fe96085Stim szeto stmfGetHostGroupList(stmfGroupList **hostGroupList)
3548fcf3ce44SJohn Forte {
35498fe96085Stim szeto 	int ret = STMF_STATUS_ERROR;
3550fcf3ce44SJohn Forte 
35518fe96085Stim szeto 	if (hostGroupList == NULL) {
3552fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3553fcf3ce44SJohn Forte 	}
3554fcf3ce44SJohn Forte 
35558fe96085Stim szeto 	ret = groupListIoctl(hostGroupList, HOST_GROUP);
35568fe96085Stim szeto 	return (ret);
35578fe96085Stim szeto }
35588fe96085Stim szeto 
35598fe96085Stim szeto 
35608fe96085Stim szeto /*
35618fe96085Stim szeto  * Purpose: access persistent config data for host groups and target groups
35628fe96085Stim szeto  */
35638fe96085Stim szeto static int
35648fe96085Stim szeto iLoadGroupMembersFromPs(stmfGroupName *groupName,
35658fe96085Stim szeto     stmfGroupProperties **groupProp, int type)
35668fe96085Stim szeto {
35678fe96085Stim szeto 	int ret;
35688fe96085Stim szeto 
35698fe96085Stim szeto 	if (groupName == NULL) {
35708fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
35718fe96085Stim szeto 	}
35728fe96085Stim szeto 
35738fe96085Stim szeto 	if (type == HOST_GROUP) {
3574fcf3ce44SJohn Forte 		ret = psGetHostGroupMemberList((char *)groupName, groupProp);
35758fe96085Stim szeto 	} else if (type == TARGET_GROUP) {
35768fe96085Stim szeto 		ret = psGetTargetGroupMemberList((char *)groupName, groupProp);
35778fe96085Stim szeto 	} else {
35788fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
35798fe96085Stim szeto 	}
3580fcf3ce44SJohn Forte 	switch (ret) {
3581fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3582fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3583fcf3ce44SJohn Forte 			break;
3584fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3585fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3586fcf3ce44SJohn Forte 			break;
3587fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3588fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3589fcf3ce44SJohn Forte 			break;
3590fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3591fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3592fcf3ce44SJohn Forte 			break;
3593fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3594fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3595fcf3ce44SJohn Forte 			break;
3596fcf3ce44SJohn Forte 		default:
3597fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
35988fe96085Stim szeto 			    "iLoadGroupMembersFromPs:psGetHostGroupList:"
35998fe96085Stim szeto 			    "error(%d)", ret);
3600fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3601fcf3ce44SJohn Forte 			break;
3602fcf3ce44SJohn Forte 	}
3603fcf3ce44SJohn Forte 
3604fcf3ce44SJohn Forte 	return (ret);
3605fcf3ce44SJohn Forte }
3606fcf3ce44SJohn Forte 
3607fcf3ce44SJohn Forte /*
36088fe96085Stim szeto  * stmfGetHostGroupMembers
36098fe96085Stim szeto  *
36108fe96085Stim szeto  * Purpose: Retrieves the group properties for a host group
36118fe96085Stim szeto  *
36128fe96085Stim szeto  * groupName - name of group for which to retrieve host group members.
36138fe96085Stim szeto  * groupProp - pointer to pointer to stmfGroupProperties structure
36148fe96085Stim szeto  *             on success, this contains the list of group members.
36158fe96085Stim szeto  */
36168fe96085Stim szeto int
36178fe96085Stim szeto stmfGetHostGroupMembers(stmfGroupName *groupName,
36188fe96085Stim szeto     stmfGroupProperties **groupProp)
36198fe96085Stim szeto {
36208fe96085Stim szeto 	int ret;
36218fe96085Stim szeto 
36228fe96085Stim szeto 	if (groupName == NULL || groupProp == NULL) {
36238fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
36248fe96085Stim szeto 	}
36258fe96085Stim szeto 
36268fe96085Stim szeto 	ret = groupMemberListIoctl(groupName, groupProp, HOST_GROUP);
36278fe96085Stim szeto 
36288fe96085Stim szeto 	return (ret);
36298fe96085Stim szeto }
36308fe96085Stim szeto 
36318fe96085Stim szeto /*
3632fcf3ce44SJohn Forte  * stmfGetProviderData
3633fcf3ce44SJohn Forte  *
3634fcf3ce44SJohn Forte  * Purpose: Get provider data list
3635fcf3ce44SJohn Forte  *
3636fcf3ce44SJohn Forte  * providerName - name of provider for which to retrieve the data
3637fcf3ce44SJohn Forte  * nvl - pointer to nvlist_t pointer which will contain the nvlist data
3638fcf3ce44SJohn Forte  *       retrieved.
3639fcf3ce44SJohn Forte  * providerType - type of provider for which to retrieve data.
3640fcf3ce44SJohn Forte  *		    STMF_LU_PROVIDER_TYPE
3641fcf3ce44SJohn Forte  *		    STMF_PORT_PROVIDER_TYPE
3642fcf3ce44SJohn Forte  */
3643fcf3ce44SJohn Forte int
3644fcf3ce44SJohn Forte stmfGetProviderData(char *providerName, nvlist_t **nvl, int providerType)
3645fcf3ce44SJohn Forte {
3646fcf3ce44SJohn Forte 	return (stmfGetProviderDataProt(providerName, nvl, providerType,
3647fcf3ce44SJohn Forte 	    NULL));
3648fcf3ce44SJohn Forte }
3649fcf3ce44SJohn Forte 
3650fcf3ce44SJohn Forte /*
3651fcf3ce44SJohn Forte  * stmfGetProviderDataProt
3652fcf3ce44SJohn Forte  *
3653fcf3ce44SJohn Forte  * Purpose: Get provider data list with token
3654fcf3ce44SJohn Forte  *
3655fcf3ce44SJohn Forte  * providerName - name of provider for which to retrieve the data
3656fcf3ce44SJohn Forte  * nvl - pointer to nvlist_t pointer which will contain the nvlist data
3657fcf3ce44SJohn Forte  *       retrieved.
3658fcf3ce44SJohn Forte  * providerType - type of provider for which to retrieve data.
3659fcf3ce44SJohn Forte  *		    STMF_LU_PROVIDER_TYPE
3660fcf3ce44SJohn Forte  *		    STMF_PORT_PROVIDER_TYPE
3661fcf3ce44SJohn Forte  * setToken - Returns the stale data token
3662fcf3ce44SJohn Forte  */
3663fcf3ce44SJohn Forte int
3664fcf3ce44SJohn Forte stmfGetProviderDataProt(char *providerName, nvlist_t **nvl, int providerType,
3665fcf3ce44SJohn Forte     uint64_t *setToken)
3666fcf3ce44SJohn Forte {
3667fcf3ce44SJohn Forte 	int ret;
3668fcf3ce44SJohn Forte 
3669fcf3ce44SJohn Forte 	if (providerName == NULL || nvl == NULL) {
3670fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3671fcf3ce44SJohn Forte 	}
3672fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
3673fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
3674fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3675fcf3ce44SJohn Forte 	}
3676fcf3ce44SJohn Forte 	/* call init */
3677fcf3ce44SJohn Forte 	ret = initializeConfig();
3678fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3679fcf3ce44SJohn Forte 		return (ret);
3680fcf3ce44SJohn Forte 	}
36818fe96085Stim szeto 	return (getProviderData(providerName, nvl, providerType, setToken));
3682fcf3ce44SJohn Forte }
3683fcf3ce44SJohn Forte 
3684fcf3ce44SJohn Forte /*
3685fcf3ce44SJohn Forte  * stmfGetProviderDataList
3686fcf3ce44SJohn Forte  *
3687fcf3ce44SJohn Forte  * Purpose: Get the list of providers currently persisting data
3688fcf3ce44SJohn Forte  *
3689fcf3ce44SJohn Forte  * providerList - pointer to pointer to an stmfProviderList structure allocated
3690fcf3ce44SJohn Forte  *                by the caller. Will contain the list of providers on success.
3691fcf3ce44SJohn Forte  */
3692fcf3ce44SJohn Forte int
3693fcf3ce44SJohn Forte stmfGetProviderDataList(stmfProviderList **providerList)
3694fcf3ce44SJohn Forte {
3695fcf3ce44SJohn Forte 	int ret;
3696fcf3ce44SJohn Forte 
3697fcf3ce44SJohn Forte 	ret = psGetProviderDataList(providerList);
3698fcf3ce44SJohn Forte 	switch (ret) {
3699fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3700fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3701fcf3ce44SJohn Forte 			break;
3702fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3703fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3704fcf3ce44SJohn Forte 			break;
3705fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3706fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3707fcf3ce44SJohn Forte 			break;
3708fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3709fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3710fcf3ce44SJohn Forte 			break;
3711fcf3ce44SJohn Forte 		default:
3712fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3713fcf3ce44SJohn Forte 			    "stmfGetProviderDataList:psGetProviderDataList"
3714fcf3ce44SJohn Forte 			    ":error(%d)", ret);
3715fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3716fcf3ce44SJohn Forte 			break;
3717fcf3ce44SJohn Forte 	}
3718fcf3ce44SJohn Forte 
3719fcf3ce44SJohn Forte 	return (ret);
3720fcf3ce44SJohn Forte }
3721fcf3ce44SJohn Forte 
3722fcf3ce44SJohn Forte 
3723fcf3ce44SJohn Forte /*
3724fcf3ce44SJohn Forte  * stmfGetSessionList
3725fcf3ce44SJohn Forte  *
3726fcf3ce44SJohn Forte  * Purpose: Retrieves the session list for a target (devid)
3727fcf3ce44SJohn Forte  *
3728fcf3ce44SJohn Forte  * devid - devid of target for which to retrieve session information.
3729fcf3ce44SJohn Forte  * sessionList - pointer to pointer to stmfSessionList structure
3730fcf3ce44SJohn Forte  *             on success, this contains the list of initiator sessions.
3731fcf3ce44SJohn Forte  */
3732fcf3ce44SJohn Forte int
3733fcf3ce44SJohn Forte stmfGetSessionList(stmfDevid *devid, stmfSessionList **sessionList)
3734fcf3ce44SJohn Forte {
3735fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
3736fcf3ce44SJohn Forte 	int fd;
3737fcf3ce44SJohn Forte 	int ioctlRet;
3738fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_SESSION_LIST;
3739fcf3ce44SJohn Forte 	int i;
3740fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
374156261083SCharles Ting 	slist_scsi_session_t *fSessionList, *fSessionListP = NULL;
3742fcf3ce44SJohn Forte 	uint8_t ident[260];
3743fcf3ce44SJohn Forte 	uint32_t fSessionListSize;
3744fcf3ce44SJohn Forte 
3745fcf3ce44SJohn Forte 	if (sessionList == NULL || devid == NULL) {
3746fcf3ce44SJohn Forte 		ret = STMF_ERROR_INVALID_ARG;
3747fcf3ce44SJohn Forte 	}
3748fcf3ce44SJohn Forte 
3749fcf3ce44SJohn Forte 	/* call init */
3750fcf3ce44SJohn Forte 	ret = initializeConfig();
3751fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3752fcf3ce44SJohn Forte 		return (ret);
3753fcf3ce44SJohn Forte 	}
3754fcf3ce44SJohn Forte 
3755fcf3ce44SJohn Forte 	/*
3756fcf3ce44SJohn Forte 	 * Open control node for stmf
3757fcf3ce44SJohn Forte 	 */
3758fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3759fcf3ce44SJohn Forte 		return (ret);
3760fcf3ce44SJohn Forte 
3761fcf3ce44SJohn Forte 	/*
3762fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
3763fcf3ce44SJohn Forte 	 */
37648fe96085Stim szeto 	fSessionListSize = ALLOC_SESSION;
3765fcf3ce44SJohn Forte 	fSessionListSize = fSessionListSize * (sizeof (slist_scsi_session_t));
3766fcf3ce44SJohn Forte 	fSessionList = (slist_scsi_session_t *)calloc(1, fSessionListSize);
376756261083SCharles Ting 	fSessionListP = fSessionList;
3768fcf3ce44SJohn Forte 	if (fSessionList == NULL) {
376956261083SCharles Ting 		ret = STMF_ERROR_NOMEM;
377056261083SCharles Ting 		goto done;
3771fcf3ce44SJohn Forte 	}
3772fcf3ce44SJohn Forte 
3773fcf3ce44SJohn Forte 	ident[IDENT_LENGTH_BYTE] = devid->identLength;
3774fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &ident[IDENT_LENGTH_BYTE + 1],
3775fcf3ce44SJohn Forte 	    devid->identLength);
3776fcf3ce44SJohn Forte 
3777fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
3778fcf3ce44SJohn Forte 	/*
3779fcf3ce44SJohn Forte 	 * Issue ioctl to get the session list
3780fcf3ce44SJohn Forte 	 */
3781fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
3782fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ident;
3783fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ident);
3784fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fSessionListSize;
3785fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fSessionList;
3786fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
3787fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
3788fcf3ce44SJohn Forte 		switch (errno) {
3789fcf3ce44SJohn Forte 			case EBUSY:
3790fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
3791fcf3ce44SJohn Forte 				break;
37928fe96085Stim szeto 			case EPERM:
3793fcf3ce44SJohn Forte 			case EACCES:
3794fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
3795fcf3ce44SJohn Forte 				break;
3796fcf3ce44SJohn Forte 			default:
3797fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
3798fcf3ce44SJohn Forte 				    "stmfGetSessionList:ioctl errno(%d)",
3799fcf3ce44SJohn Forte 				    errno);
3800fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
3801fcf3ce44SJohn Forte 				break;
3802fcf3ce44SJohn Forte 		}
3803fcf3ce44SJohn Forte 		goto done;
3804fcf3ce44SJohn Forte 	}
3805fcf3ce44SJohn Forte 	/*
3806fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
3807fcf3ce44SJohn Forte 	 */
38088fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_SESSION) {
3809fcf3ce44SJohn Forte 		fSessionListSize = stmfIoctl.stmf_obuf_max_nentries *
3810fcf3ce44SJohn Forte 		    sizeof (slist_scsi_session_t);
3811fcf3ce44SJohn Forte 		fSessionList = realloc(fSessionList, fSessionListSize);
3812fcf3ce44SJohn Forte 		if (fSessionList == NULL) {
381356261083SCharles Ting 			ret = STMF_ERROR_NOMEM;
381456261083SCharles Ting 			goto done;
3815fcf3ce44SJohn Forte 		}
381656261083SCharles Ting 		fSessionListP = fSessionList;
3817fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fSessionListSize;
3818fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fSessionList;
3819fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
3820fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
3821fcf3ce44SJohn Forte 			switch (errno) {
3822fcf3ce44SJohn Forte 				case EBUSY:
3823fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
3824fcf3ce44SJohn Forte 					break;
38258fe96085Stim szeto 				case EPERM:
3826fcf3ce44SJohn Forte 				case EACCES:
3827fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
3828fcf3ce44SJohn Forte 					break;
3829fcf3ce44SJohn Forte 				default:
3830fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
3831fcf3ce44SJohn Forte 					    "stmfGetSessionList:ioctl "
3832fcf3ce44SJohn Forte 					    "errno(%d)", errno);
3833fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
3834fcf3ce44SJohn Forte 					break;
3835fcf3ce44SJohn Forte 			}
3836fcf3ce44SJohn Forte 			goto done;
3837fcf3ce44SJohn Forte 		}
3838fcf3ce44SJohn Forte 	}
3839fcf3ce44SJohn Forte 
3840fcf3ce44SJohn Forte 	/*
3841fcf3ce44SJohn Forte 	 * allocate caller's buffer with the final size
3842fcf3ce44SJohn Forte 	 */
3843fcf3ce44SJohn Forte 	*sessionList = (stmfSessionList *)calloc(1, sizeof (stmfSessionList) +
3844fcf3ce44SJohn Forte 	    stmfIoctl.stmf_obuf_max_nentries * sizeof (stmfSession));
3845fcf3ce44SJohn Forte 	if (*sessionList == NULL) {
3846fcf3ce44SJohn Forte 		ret = STMF_ERROR_NOMEM;
3847fcf3ce44SJohn Forte 		free(sessionList);
3848fcf3ce44SJohn Forte 		goto done;
3849fcf3ce44SJohn Forte 	}
3850fcf3ce44SJohn Forte 
3851fcf3ce44SJohn Forte 	(*sessionList)->cnt = stmfIoctl.stmf_obuf_max_nentries;
3852fcf3ce44SJohn Forte 
3853fcf3ce44SJohn Forte 	/*
3854fcf3ce44SJohn Forte 	 * copy session info to caller's buffer
3855fcf3ce44SJohn Forte 	 */
3856fcf3ce44SJohn Forte 	for (i = 0; i < (*sessionList)->cnt; i++) {
3857fcf3ce44SJohn Forte 		(*sessionList)->session[i].initiator.identLength =
3858fcf3ce44SJohn Forte 		    fSessionList->initiator[IDENT_LENGTH_BYTE];
3859fcf3ce44SJohn Forte 		bcopy(&(fSessionList->initiator[IDENT_LENGTH_BYTE + 1]),
3860fcf3ce44SJohn Forte 		    (*sessionList)->session[i].initiator.ident,
3861fcf3ce44SJohn Forte 		    STMF_IDENT_LENGTH);
3862fcf3ce44SJohn Forte 		bcopy(&(fSessionList->alias),
3863fcf3ce44SJohn Forte 		    &((*sessionList)->session[i].alias),
3864fcf3ce44SJohn Forte 		    sizeof ((*sessionList)->session[i].alias));
3865fcf3ce44SJohn Forte 		bcopy(&(fSessionList++->creation_time),
3866fcf3ce44SJohn Forte 		    &((*sessionList)->session[i].creationTime),
3867fcf3ce44SJohn Forte 		    sizeof (time_t));
3868fcf3ce44SJohn Forte 	}
3869fcf3ce44SJohn Forte done:
3870fcf3ce44SJohn Forte 	(void) close(fd);
387156261083SCharles Ting 	free(fSessionListP);
3872fcf3ce44SJohn Forte 	return (ret);
3873fcf3ce44SJohn Forte }
3874fcf3ce44SJohn Forte 
3875fcf3ce44SJohn Forte /*
3876fcf3ce44SJohn Forte  * stmfGetTargetGroupList
3877fcf3ce44SJohn Forte  *
3878fcf3ce44SJohn Forte  * Purpose: Retrieves the list of target groups
3879fcf3ce44SJohn Forte  *
3880fcf3ce44SJohn Forte  * targetGroupList - pointer to a pointer to an stmfGroupList structure. On
3881fcf3ce44SJohn Forte  *		     success, it contains the list of target groups.
3882fcf3ce44SJohn Forte  */
3883fcf3ce44SJohn Forte int
3884fcf3ce44SJohn Forte stmfGetTargetGroupList(stmfGroupList **targetGroupList)
3885fcf3ce44SJohn Forte {
3886fcf3ce44SJohn Forte 	int ret;
3887fcf3ce44SJohn Forte 
3888fcf3ce44SJohn Forte 	if (targetGroupList == NULL) {
3889fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3890fcf3ce44SJohn Forte 	}
3891fcf3ce44SJohn Forte 
38928fe96085Stim szeto 	ret = groupListIoctl(targetGroupList, TARGET_GROUP);
3893fcf3ce44SJohn Forte 	return (ret);
3894fcf3ce44SJohn Forte }
3895fcf3ce44SJohn Forte 
3896fcf3ce44SJohn Forte /*
3897fcf3ce44SJohn Forte  * stmfGetTargetGroupMembers
3898fcf3ce44SJohn Forte  *
3899fcf3ce44SJohn Forte  * Purpose: Retrieves the group members for a target group
3900fcf3ce44SJohn Forte  *
3901fcf3ce44SJohn Forte  * groupName - name of target group for which to retrieve members.
3902fcf3ce44SJohn Forte  * groupProp - pointer to pointer to stmfGroupProperties structure
3903fcf3ce44SJohn Forte  *             on success, this contains the list of group members.
3904fcf3ce44SJohn Forte  */
3905fcf3ce44SJohn Forte int
3906fcf3ce44SJohn Forte stmfGetTargetGroupMembers(stmfGroupName *groupName,
3907fcf3ce44SJohn Forte     stmfGroupProperties **groupProp)
3908fcf3ce44SJohn Forte {
3909fcf3ce44SJohn Forte 	int ret;
3910fcf3ce44SJohn Forte 
3911fcf3ce44SJohn Forte 	if (groupName == NULL || groupProp == NULL) {
3912fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3913fcf3ce44SJohn Forte 	}
3914fcf3ce44SJohn Forte 
39158fe96085Stim szeto 	ret = groupMemberListIoctl(groupName, groupProp, TARGET_GROUP);
3916fcf3ce44SJohn Forte 
3917fcf3ce44SJohn Forte 	return (ret);
3918fcf3ce44SJohn Forte }
3919fcf3ce44SJohn Forte 
3920fcf3ce44SJohn Forte /*
3921fcf3ce44SJohn Forte  * stmfGetTargetList
3922fcf3ce44SJohn Forte  *
3923fcf3ce44SJohn Forte  * Purpose: Retrieves the list of target ports
3924fcf3ce44SJohn Forte  *
3925fcf3ce44SJohn Forte  * targetList - pointer to a pointer to an stmfDevidList structure.
3926fcf3ce44SJohn Forte  *		    On success, it contains the list of local ports (target).
3927fcf3ce44SJohn Forte  */
3928fcf3ce44SJohn Forte int
3929fcf3ce44SJohn Forte stmfGetTargetList(stmfDevidList **targetList)
3930fcf3ce44SJohn Forte {
3931fcf3ce44SJohn Forte 	int ret;
3932fcf3ce44SJohn Forte 	int fd;
3933fcf3ce44SJohn Forte 	int ioctlRet;
3934fcf3ce44SJohn Forte 	int i;
3935fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
3936fcf3ce44SJohn Forte 	/* framework target port list */
39378fe96085Stim szeto 	slist_target_port_t *fTargetList, *fTargetListP = NULL;
3938fcf3ce44SJohn Forte 	uint32_t fTargetListSize;
3939fcf3ce44SJohn Forte 
3940fcf3ce44SJohn Forte 	if (targetList == NULL) {
3941fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3942fcf3ce44SJohn Forte 	}
3943fcf3ce44SJohn Forte 
3944fcf3ce44SJohn Forte 	/* call init */
3945fcf3ce44SJohn Forte 	ret = initializeConfig();
3946fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3947fcf3ce44SJohn Forte 		return (ret);
3948fcf3ce44SJohn Forte 	}
3949fcf3ce44SJohn Forte 
3950fcf3ce44SJohn Forte 	/*
3951fcf3ce44SJohn Forte 	 * Open control node for stmf
3952fcf3ce44SJohn Forte 	 */
3953fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3954fcf3ce44SJohn Forte 		return (ret);
3955fcf3ce44SJohn Forte 
3956fcf3ce44SJohn Forte 	/*
3957fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
3958fcf3ce44SJohn Forte 	 */
39598fe96085Stim szeto 	fTargetListSize = ALLOC_TARGET_PORT * sizeof (slist_target_port_t);
39603e7352aeSJohn Forte 	fTargetListP = fTargetList =
39613e7352aeSJohn Forte 	    (slist_target_port_t *)calloc(1, fTargetListSize);
3962fcf3ce44SJohn Forte 	if (fTargetList == NULL) {
39638fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
3964fcf3ce44SJohn Forte 		goto done;
3965fcf3ce44SJohn Forte 	}
3966fcf3ce44SJohn Forte 
3967fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
3968fcf3ce44SJohn Forte 	/*
39693e7352aeSJohn Forte 	 * Issue ioctl to retrieve target list
3970fcf3ce44SJohn Forte 	 */
3971fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
3972fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fTargetListSize;
3973fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fTargetList;
3974fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_TARGET_PORT_LIST, &stmfIoctl);
3975fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
3976fcf3ce44SJohn Forte 		switch (errno) {
3977fcf3ce44SJohn Forte 			case EBUSY:
3978fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
3979fcf3ce44SJohn Forte 				break;
39808fe96085Stim szeto 			case EPERM:
3981fcf3ce44SJohn Forte 			case EACCES:
3982fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
3983fcf3ce44SJohn Forte 				break;
3984fcf3ce44SJohn Forte 			default:
3985fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
3986fcf3ce44SJohn Forte 				    "stmfGetTargetList:ioctl errno(%d)", errno);
3987fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
3988fcf3ce44SJohn Forte 				break;
3989fcf3ce44SJohn Forte 		}
3990fcf3ce44SJohn Forte 		goto done;
3991fcf3ce44SJohn Forte 	}
3992fcf3ce44SJohn Forte 	/*
3993fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
3994fcf3ce44SJohn Forte 	 */
39958fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_TARGET_PORT) {
3996fcf3ce44SJohn Forte 		fTargetListSize = stmfIoctl.stmf_obuf_max_nentries *
399776602b8dSJohn Forte 		    sizeof (slist_target_port_t);
39983e7352aeSJohn Forte 		fTargetListP = fTargetList =
39993e7352aeSJohn Forte 		    realloc(fTargetList, fTargetListSize);
4000fcf3ce44SJohn Forte 		if (fTargetList == NULL) {
40018fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
40028fe96085Stim szeto 			goto done;
4003fcf3ce44SJohn Forte 		}
4004fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fTargetListSize;
4005fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fTargetList;
4006fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, STMF_IOCTL_TARGET_PORT_LIST,
4007fcf3ce44SJohn Forte 		    &stmfIoctl);
4008fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
4009fcf3ce44SJohn Forte 			switch (errno) {
4010fcf3ce44SJohn Forte 				case EBUSY:
4011fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
4012fcf3ce44SJohn Forte 					break;
40138fe96085Stim szeto 				case EPERM:
4014fcf3ce44SJohn Forte 				case EACCES:
4015fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
4016fcf3ce44SJohn Forte 					break;
4017fcf3ce44SJohn Forte 				default:
4018fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
4019fcf3ce44SJohn Forte 					    "stmfGetTargetList:ioctl errno(%d)",
4020fcf3ce44SJohn Forte 					    errno);
4021fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
4022fcf3ce44SJohn Forte 					break;
4023fcf3ce44SJohn Forte 			}
4024fcf3ce44SJohn Forte 			goto done;
4025fcf3ce44SJohn Forte 		}
4026fcf3ce44SJohn Forte 	}
4027fcf3ce44SJohn Forte 
4028fcf3ce44SJohn Forte 	*targetList = (stmfDevidList *)calloc(1,
4029fcf3ce44SJohn Forte 	    stmfIoctl.stmf_obuf_max_nentries * sizeof (stmfDevid) +
4030fcf3ce44SJohn Forte 	    sizeof (stmfDevidList));
40318fe96085Stim szeto 	if (*targetList == NULL) {
40328fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
40338fe96085Stim szeto 		goto done;
40348fe96085Stim szeto 	}
4035fcf3ce44SJohn Forte 
4036fcf3ce44SJohn Forte 	(*targetList)->cnt = stmfIoctl.stmf_obuf_max_nentries;
4037fcf3ce44SJohn Forte 	for (i = 0; i < stmfIoctl.stmf_obuf_max_nentries; i++, fTargetList++) {
4038fcf3ce44SJohn Forte 		(*targetList)->devid[i].identLength =
4039fcf3ce44SJohn Forte 		    fTargetList->target[IDENT_LENGTH_BYTE];
4040fcf3ce44SJohn Forte 		bcopy(&fTargetList->target[IDENT_LENGTH_BYTE + 1],
4041fcf3ce44SJohn Forte 		    &(*targetList)->devid[i].ident,
4042fcf3ce44SJohn Forte 		    fTargetList->target[IDENT_LENGTH_BYTE]);
4043fcf3ce44SJohn Forte 	}
4044fcf3ce44SJohn Forte 
4045fcf3ce44SJohn Forte done:
4046fcf3ce44SJohn Forte 	(void) close(fd);
40473e7352aeSJohn Forte 	free(fTargetListP);
4048fcf3ce44SJohn Forte 	return (ret);
4049fcf3ce44SJohn Forte }
4050fcf3ce44SJohn Forte 
4051fcf3ce44SJohn Forte /*
4052fcf3ce44SJohn Forte  * stmfGetTargetProperties
4053fcf3ce44SJohn Forte  *
4054fcf3ce44SJohn Forte  * Purpose:  Retrieves the properties for a logical unit
4055fcf3ce44SJohn Forte  *
4056fcf3ce44SJohn Forte  * devid - devid of the target for which to retrieve properties
4057fcf3ce44SJohn Forte  * targetProps - pointer to an stmfTargetProperties structure.
4058fcf3ce44SJohn Forte  *		On success, it contains the target properties for
4059fcf3ce44SJohn Forte  *		the specified devid.
4060fcf3ce44SJohn Forte  */
4061fcf3ce44SJohn Forte int
4062fcf3ce44SJohn Forte stmfGetTargetProperties(stmfDevid *devid, stmfTargetProperties *targetProps)
4063fcf3ce44SJohn Forte {
4064fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4065fcf3ce44SJohn Forte 	int fd;
4066fcf3ce44SJohn Forte 	int ioctlRet;
4067fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4068fcf3ce44SJohn Forte 	sioc_target_port_props_t targetProperties;
406945039663SJohn Forte 	scsi_devid_desc_t *scsiDevid;
4070fcf3ce44SJohn Forte 
4071fcf3ce44SJohn Forte 	if (devid == NULL || targetProps == NULL) {
4072fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4073fcf3ce44SJohn Forte 	}
4074fcf3ce44SJohn Forte 
4075fcf3ce44SJohn Forte 	/* call init */
4076fcf3ce44SJohn Forte 	ret = initializeConfig();
4077fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4078fcf3ce44SJohn Forte 		return (ret);
4079fcf3ce44SJohn Forte 	}
4080fcf3ce44SJohn Forte 
4081fcf3ce44SJohn Forte 	/*
4082fcf3ce44SJohn Forte 	 * Open control node for stmf
4083fcf3ce44SJohn Forte 	 */
4084fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
4085fcf3ce44SJohn Forte 		return (ret);
4086fcf3ce44SJohn Forte 
4087fcf3ce44SJohn Forte 	targetProperties.tgt_id[IDENT_LENGTH_BYTE] = devid->identLength;
4088fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetProperties.tgt_id[IDENT_LENGTH_BYTE + 1],
4089fcf3ce44SJohn Forte 	    devid->identLength);
4090fcf3ce44SJohn Forte 
4091fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4092fcf3ce44SJohn Forte 	/*
4093fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
4094fcf3ce44SJohn Forte 	 */
4095fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4096fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (targetProperties.tgt_id);
4097fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&targetProperties.tgt_id;
4098fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&targetProperties;
4099fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (targetProperties);
4100fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_GET_TARGET_PORT_PROPERTIES,
4101fcf3ce44SJohn Forte 	    &stmfIoctl);
4102fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4103fcf3ce44SJohn Forte 		switch (errno) {
4104fcf3ce44SJohn Forte 			case EBUSY:
4105fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4106fcf3ce44SJohn Forte 				break;
41078fe96085Stim szeto 			case EPERM:
4108fcf3ce44SJohn Forte 			case EACCES:
4109fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4110fcf3ce44SJohn Forte 				break;
4111fcf3ce44SJohn Forte 			case ENOENT:
4112fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4113fcf3ce44SJohn Forte 				break;
4114fcf3ce44SJohn Forte 			default:
4115fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4116fcf3ce44SJohn Forte 				    "stmfGetTargetProperties:ioctl errno(%d)",
4117fcf3ce44SJohn Forte 				    errno);
4118fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4119fcf3ce44SJohn Forte 				break;
4120fcf3ce44SJohn Forte 		}
4121fcf3ce44SJohn Forte 		goto done;
4122fcf3ce44SJohn Forte 	}
4123fcf3ce44SJohn Forte 
4124fcf3ce44SJohn Forte 	bcopy(targetProperties.tgt_provider_name, targetProps->providerName,
4125fcf3ce44SJohn Forte 	    sizeof (targetProperties.tgt_provider_name));
4126fcf3ce44SJohn Forte 	if (targetProperties.tgt_state == STMF_STATE_ONLINE) {
4127fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_ONLINE;
4128fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_OFFLINE) {
4129fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_OFFLINE;
4130fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_ONLINING) {
4131fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_ONLINING;
4132fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_OFFLINING) {
4133fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_OFFLINING;
4134fcf3ce44SJohn Forte 	}
4135fcf3ce44SJohn Forte 	bcopy(targetProperties.tgt_alias, targetProps->alias,
4136fcf3ce44SJohn Forte 	    sizeof (targetProps->alias));
413745039663SJohn Forte 
413845039663SJohn Forte 	scsiDevid = (scsi_devid_desc_t *)&targetProperties.tgt_id;
413945039663SJohn Forte 	targetProps->protocol = scsiDevid->protocol_id;
414045039663SJohn Forte 
4141fcf3ce44SJohn Forte done:
4142fcf3ce44SJohn Forte 	(void) close(fd);
4143fcf3ce44SJohn Forte 	return (ret);
4144fcf3ce44SJohn Forte }
4145fcf3ce44SJohn Forte 
4146fcf3ce44SJohn Forte /*
4147fcf3ce44SJohn Forte  * stmfGetLogicalUnitList
4148fcf3ce44SJohn Forte  *
4149fcf3ce44SJohn Forte  * Purpose: Retrieves list of logical unit Object IDs
4150fcf3ce44SJohn Forte  *
4151fcf3ce44SJohn Forte  * luList - pointer to a pointer to a stmfGuidList structure. On success,
4152fcf3ce44SJohn Forte  *          it contains the list of logical unit guids.
4153fcf3ce44SJohn Forte  *
4154fcf3ce44SJohn Forte  */
4155fcf3ce44SJohn Forte int
4156fcf3ce44SJohn Forte stmfGetLogicalUnitList(stmfGuidList **luList)
4157fcf3ce44SJohn Forte {
4158fcf3ce44SJohn Forte 	int ret;
4159fcf3ce44SJohn Forte 	int fd;
4160fcf3ce44SJohn Forte 	int ioctlRet;
4161fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_LU_LIST;
41628fe96085Stim szeto 	int i;
4163fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4164fcf3ce44SJohn Forte 	slist_lu_t *fLuList;
4165fcf3ce44SJohn Forte 	uint32_t fLuListSize;
41668fe96085Stim szeto 	uint32_t listCnt;
4167fcf3ce44SJohn Forte 
4168fcf3ce44SJohn Forte 	if (luList == NULL) {
4169fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4170fcf3ce44SJohn Forte 	}
4171fcf3ce44SJohn Forte 
4172fcf3ce44SJohn Forte 	/* call init */
4173fcf3ce44SJohn Forte 	ret = initializeConfig();
4174fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4175fcf3ce44SJohn Forte 		return (ret);
4176fcf3ce44SJohn Forte 	}
4177fcf3ce44SJohn Forte 
4178fcf3ce44SJohn Forte 	/*
4179fcf3ce44SJohn Forte 	 * Open control node for stmf
4180fcf3ce44SJohn Forte 	 */
4181fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
4182fcf3ce44SJohn Forte 		return (ret);
4183fcf3ce44SJohn Forte 
4184fcf3ce44SJohn Forte 	/*
4185fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
4186fcf3ce44SJohn Forte 	 */
41878fe96085Stim szeto 	fLuListSize = ALLOC_LU;
4188fcf3ce44SJohn Forte 	fLuListSize = fLuListSize * (sizeof (slist_lu_t));
4189fcf3ce44SJohn Forte 	fLuList = (slist_lu_t *)calloc(1, fLuListSize);
4190fcf3ce44SJohn Forte 	if (fLuList == NULL) {
41918fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
41928fe96085Stim szeto 		goto done;
4193fcf3ce44SJohn Forte 	}
4194fcf3ce44SJohn Forte 
4195fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4196fcf3ce44SJohn Forte 	/*
4197fcf3ce44SJohn Forte 	 * Issue ioctl to get the LU list
4198fcf3ce44SJohn Forte 	 */
4199fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4200fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fLuListSize;
4201fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fLuList;
4202fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4203fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4204fcf3ce44SJohn Forte 		switch (errno) {
4205fcf3ce44SJohn Forte 			case EBUSY:
4206fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4207fcf3ce44SJohn Forte 				break;
42088fe96085Stim szeto 			case EPERM:
4209fcf3ce44SJohn Forte 			case EACCES:
4210fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4211fcf3ce44SJohn Forte 				break;
4212fcf3ce44SJohn Forte 			default:
4213fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4214fcf3ce44SJohn Forte 				    "stmfGetLogicalUnitList:ioctl errno(%d)",
4215fcf3ce44SJohn Forte 				    errno);
4216fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4217fcf3ce44SJohn Forte 				break;
4218fcf3ce44SJohn Forte 		}
4219fcf3ce44SJohn Forte 		goto done;
4220fcf3ce44SJohn Forte 	}
4221fcf3ce44SJohn Forte 	/*
4222fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
4223fcf3ce44SJohn Forte 	 */
42248fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_LU) {
4225fcf3ce44SJohn Forte 		fLuListSize = stmfIoctl.stmf_obuf_max_nentries *
4226fcf3ce44SJohn Forte 		    sizeof (slist_lu_t);
42278fe96085Stim szeto 		free(fLuList);
42288fe96085Stim szeto 		fLuList = (slist_lu_t *)calloc(1, fLuListSize);
4229fcf3ce44SJohn Forte 		if (fLuList == NULL) {
42308fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
42318fe96085Stim szeto 			goto done;
4232fcf3ce44SJohn Forte 		}
4233fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fLuListSize;
4234fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fLuList;
4235fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4236fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
4237fcf3ce44SJohn Forte 			switch (errno) {
4238fcf3ce44SJohn Forte 				case EBUSY:
4239fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
4240fcf3ce44SJohn Forte 					break;
42418fe96085Stim szeto 				case EPERM:
4242fcf3ce44SJohn Forte 				case EACCES:
4243fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
4244fcf3ce44SJohn Forte 					break;
4245fcf3ce44SJohn Forte 				default:
4246fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
4247fcf3ce44SJohn Forte 					    "stmfGetLogicalUnitList:"
4248fcf3ce44SJohn Forte 					    "ioctl errno(%d)", errno);
4249fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
4250fcf3ce44SJohn Forte 					break;
4251fcf3ce44SJohn Forte 			}
4252fcf3ce44SJohn Forte 			goto done;
4253fcf3ce44SJohn Forte 		}
4254fcf3ce44SJohn Forte 	}
4255fcf3ce44SJohn Forte 
4256fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4257fcf3ce44SJohn Forte 		goto done;
4258fcf3ce44SJohn Forte 	}
4259fcf3ce44SJohn Forte 
42608fe96085Stim szeto 	listCnt = stmfIoctl.stmf_obuf_nentries;
4261fcf3ce44SJohn Forte 
4262fcf3ce44SJohn Forte 	/*
4263fcf3ce44SJohn Forte 	 * allocate caller's buffer with the final size
4264fcf3ce44SJohn Forte 	 */
4265fcf3ce44SJohn Forte 	*luList = (stmfGuidList *)calloc(1, sizeof (stmfGuidList) +
42668fe96085Stim szeto 	    listCnt * sizeof (stmfGuid));
4267fcf3ce44SJohn Forte 	if (*luList == NULL) {
4268fcf3ce44SJohn Forte 		ret = STMF_ERROR_NOMEM;
4269fcf3ce44SJohn Forte 		goto done;
4270fcf3ce44SJohn Forte 	}
4271fcf3ce44SJohn Forte 
42728fe96085Stim szeto 	(*luList)->cnt = listCnt;
42738fe96085Stim szeto 
42748fe96085Stim szeto 	/* copy to caller's buffer */
42758fe96085Stim szeto 	for (i = 0; i < listCnt; i++) {
42768fe96085Stim szeto 		bcopy(&fLuList[i].lu_guid, (*luList)->guid[i].guid,
4277fcf3ce44SJohn Forte 		    sizeof (stmfGuid));
4278fcf3ce44SJohn Forte 	}
4279fcf3ce44SJohn Forte 
42808fe96085Stim szeto 	/*
42818fe96085Stim szeto 	 * sort the list. This gives a consistent view across gets
42828fe96085Stim szeto 	 */
42838fe96085Stim szeto 	qsort((void *)&((*luList)->guid[0]), (*luList)->cnt,
42848fe96085Stim szeto 	    sizeof (stmfGuid), guidCompare);
4285fcf3ce44SJohn Forte 
4286fcf3ce44SJohn Forte done:
4287fcf3ce44SJohn Forte 	(void) close(fd);
4288fcf3ce44SJohn Forte 	/*
4289fcf3ce44SJohn Forte 	 * free internal buffers
4290fcf3ce44SJohn Forte 	 */
4291fcf3ce44SJohn Forte 	free(fLuList);
4292fcf3ce44SJohn Forte 	return (ret);
4293fcf3ce44SJohn Forte }
4294fcf3ce44SJohn Forte 
4295fcf3ce44SJohn Forte /*
4296fcf3ce44SJohn Forte  * stmfGetLogicalUnitProperties
4297fcf3ce44SJohn Forte  *
4298fcf3ce44SJohn Forte  * Purpose:  Retrieves the properties for a logical unit
4299fcf3ce44SJohn Forte  *
4300fcf3ce44SJohn Forte  * lu - guid of the logical unit for which to retrieve properties
4301fcf3ce44SJohn Forte  * stmfLuProps - pointer to an stmfLogicalUnitProperties structure. On success,
4302fcf3ce44SJohn Forte  *               it contains the logical unit properties for the specified guid.
4303fcf3ce44SJohn Forte  */
4304fcf3ce44SJohn Forte int
4305fcf3ce44SJohn Forte stmfGetLogicalUnitProperties(stmfGuid *lu, stmfLogicalUnitProperties *luProps)
4306fcf3ce44SJohn Forte {
4307fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4308fcf3ce44SJohn Forte 	int stmfRet;
4309fcf3ce44SJohn Forte 	int fd;
4310fcf3ce44SJohn Forte 	int ioctlRet;
4311fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_GET_LU_PROPERTIES;
4312fcf3ce44SJohn Forte 	stmfViewEntryList *viewEntryList = NULL;
4313fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4314fcf3ce44SJohn Forte 	sioc_lu_props_t fLuProps;
4315fcf3ce44SJohn Forte 
43168fe96085Stim szeto 	if (lu == NULL || luProps == NULL) {
43178fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
4318fcf3ce44SJohn Forte 	}
4319fcf3ce44SJohn Forte 
4320fcf3ce44SJohn Forte 	bzero(luProps, sizeof (stmfLogicalUnitProperties));
4321fcf3ce44SJohn Forte 
4322fcf3ce44SJohn Forte 	/* call init */
4323fcf3ce44SJohn Forte 	ret = initializeConfig();
4324fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4325fcf3ce44SJohn Forte 		return (ret);
4326fcf3ce44SJohn Forte 	}
4327fcf3ce44SJohn Forte 
4328fcf3ce44SJohn Forte 	/*
4329fcf3ce44SJohn Forte 	 * Open control node for stmf
4330fcf3ce44SJohn Forte 	 */
4331fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
4332fcf3ce44SJohn Forte 		return (ret);
4333fcf3ce44SJohn Forte 
4334fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4335fcf3ce44SJohn Forte 	/*
4336fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
4337fcf3ce44SJohn Forte 	 */
4338fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4339fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGuid);
4340fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)lu;
4341fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&fLuProps;
4342fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (fLuProps);
4343fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4344fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4345fcf3ce44SJohn Forte 		switch (errno) {
4346fcf3ce44SJohn Forte 			case EBUSY:
4347fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4348fcf3ce44SJohn Forte 				break;
43498fe96085Stim szeto 			case EPERM:
4350fcf3ce44SJohn Forte 			case EACCES:
4351fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4352fcf3ce44SJohn Forte 				break;
4353fcf3ce44SJohn Forte 			case ENOENT:
4354fcf3ce44SJohn Forte 				stmfRet = stmfGetViewEntryList(lu,
4355fcf3ce44SJohn Forte 				    &viewEntryList);
4356fcf3ce44SJohn Forte 				if (stmfRet == STMF_STATUS_SUCCESS) {
4357fcf3ce44SJohn Forte 					luProps->status =
4358fcf3ce44SJohn Forte 					    STMF_LOGICAL_UNIT_UNREGISTERED;
4359fcf3ce44SJohn Forte 					if (viewEntryList->cnt > 0) {
4360fcf3ce44SJohn Forte 						ret = STMF_STATUS_SUCCESS;
4361fcf3ce44SJohn Forte 					} else {
4362fcf3ce44SJohn Forte 						ret = STMF_ERROR_NOT_FOUND;
4363fcf3ce44SJohn Forte 					}
4364fcf3ce44SJohn Forte 				} else {
4365fcf3ce44SJohn Forte 					ret = STMF_ERROR_NOT_FOUND;
4366fcf3ce44SJohn Forte 				}
4367fcf3ce44SJohn Forte 				stmfFreeMemory(viewEntryList);
4368fcf3ce44SJohn Forte 				break;
4369fcf3ce44SJohn Forte 			default:
4370fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4371fcf3ce44SJohn Forte 				    "stmfGetLogicalUnit:ioctl errno(%d)",
4372fcf3ce44SJohn Forte 				    errno);
4373fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4374fcf3ce44SJohn Forte 				break;
4375fcf3ce44SJohn Forte 		}
4376fcf3ce44SJohn Forte 		goto done;
4377fcf3ce44SJohn Forte 	}
4378fcf3ce44SJohn Forte 
4379fcf3ce44SJohn Forte 	bcopy(fLuProps.lu_provider_name, luProps->providerName,
4380fcf3ce44SJohn Forte 	    sizeof (fLuProps.lu_provider_name));
4381fcf3ce44SJohn Forte 	if (fLuProps.lu_state == STMF_STATE_ONLINE) {
4382fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_ONLINE;
4383fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_OFFLINE) {
4384fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_OFFLINE;
4385fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_ONLINING) {
4386fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_ONLINING;
4387fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_OFFLINING) {
4388fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_OFFLINING;
4389fcf3ce44SJohn Forte 	}
4390fcf3ce44SJohn Forte 	bcopy(fLuProps.lu_alias, luProps->alias, sizeof (luProps->alias));
4391fcf3ce44SJohn Forte done:
4392fcf3ce44SJohn Forte 	(void) close(fd);
4393fcf3ce44SJohn Forte 	return (ret);
4394fcf3ce44SJohn Forte }
4395fcf3ce44SJohn Forte 
4396fcf3ce44SJohn Forte /*
4397fcf3ce44SJohn Forte  * stmfGetState
4398fcf3ce44SJohn Forte  *
4399fcf3ce44SJohn Forte  * Purpose: retrieve the current state of the stmf module
4400fcf3ce44SJohn Forte  *
4401fcf3ce44SJohn Forte  * state - pointer to stmfState structure allocated by the caller
4402fcf3ce44SJohn Forte  *         On success, contains the state of stmf
4403fcf3ce44SJohn Forte  */
4404fcf3ce44SJohn Forte int
4405fcf3ce44SJohn Forte stmfGetState(stmfState *state)
4406fcf3ce44SJohn Forte {
4407fcf3ce44SJohn Forte 	int ret;
4408fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
4409fcf3ce44SJohn Forte 
4410fcf3ce44SJohn Forte 	if (state == NULL) {
4411fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4412fcf3ce44SJohn Forte 	}
4413fcf3ce44SJohn Forte 
4414fcf3ce44SJohn Forte 	ret = getStmfState(&iState);
4415fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4416fcf3ce44SJohn Forte 		return (ret);
4417fcf3ce44SJohn Forte 	}
4418fcf3ce44SJohn Forte 	switch (iState.state) {
4419fcf3ce44SJohn Forte 		case STMF_STATE_ONLINE:
4420fcf3ce44SJohn Forte 			state->operationalState =
4421fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_ONLINE;
4422fcf3ce44SJohn Forte 			break;
4423fcf3ce44SJohn Forte 		case STMF_STATE_OFFLINE:
4424fcf3ce44SJohn Forte 			state->operationalState =
4425fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_OFFLINE;
4426fcf3ce44SJohn Forte 			break;
4427fcf3ce44SJohn Forte 		case STMF_STATE_ONLINING:
4428fcf3ce44SJohn Forte 			state->operationalState =
4429fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_ONLINING;
4430fcf3ce44SJohn Forte 			break;
4431fcf3ce44SJohn Forte 		case STMF_STATE_OFFLINING:
4432fcf3ce44SJohn Forte 			state->operationalState =
4433fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_OFFLINING;
4434fcf3ce44SJohn Forte 			break;
4435fcf3ce44SJohn Forte 		default:
4436fcf3ce44SJohn Forte 			state->operationalState =
4437fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_UNKNOWN;
4438fcf3ce44SJohn Forte 			break;
4439fcf3ce44SJohn Forte 	}
4440fcf3ce44SJohn Forte 	switch (iState.config_state) {
4441fcf3ce44SJohn Forte 		case STMF_CONFIG_NONE:
4442fcf3ce44SJohn Forte 			state->configState = STMF_CONFIG_STATE_NONE;
4443fcf3ce44SJohn Forte 			break;
4444fcf3ce44SJohn Forte 		case STMF_CONFIG_INIT:
4445fcf3ce44SJohn Forte 			state->configState = STMF_CONFIG_STATE_INIT;
4446fcf3ce44SJohn Forte 			break;
4447fcf3ce44SJohn Forte 		case STMF_CONFIG_INIT_DONE:
4448fcf3ce44SJohn Forte 			state->configState =
4449fcf3ce44SJohn Forte 			    STMF_CONFIG_STATE_INIT_DONE;
4450fcf3ce44SJohn Forte 			break;
4451fcf3ce44SJohn Forte 		default:
4452fcf3ce44SJohn Forte 			state->configState =
4453fcf3ce44SJohn Forte 			    STMF_CONFIG_STATE_UNKNOWN;
4454fcf3ce44SJohn Forte 			break;
4455fcf3ce44SJohn Forte 	}
4456fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
4457fcf3ce44SJohn Forte }
4458fcf3ce44SJohn Forte 
4459fcf3ce44SJohn Forte /*
4460fcf3ce44SJohn Forte  * stmfGetViewEntryList
4461fcf3ce44SJohn Forte  *
4462fcf3ce44SJohn Forte  * Purpose: Retrieves the list of view entries for the specified
4463fcf3ce44SJohn Forte  *          logical unit.
4464fcf3ce44SJohn Forte  *
4465fcf3ce44SJohn Forte  * lu - the guid of the logical unit for which to retrieve the view entry list
4466fcf3ce44SJohn Forte  * viewEntryList - a pointer to a pointer to a stmfViewEntryList structure. On
4467fcf3ce44SJohn Forte  *                 success, contains the list of view entries.
4468fcf3ce44SJohn Forte  */
4469fcf3ce44SJohn Forte int
4470fcf3ce44SJohn Forte stmfGetViewEntryList(stmfGuid *lu, stmfViewEntryList **viewEntryList)
4471fcf3ce44SJohn Forte {
4472fcf3ce44SJohn Forte 	int ret;
44738fe96085Stim szeto 	int fd;
44748fe96085Stim szeto 	int ioctlRet;
44758fe96085Stim szeto 	int cmd = STMF_IOCTL_LU_VE_LIST;
44768fe96085Stim szeto 	int i;
44778fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
44788fe96085Stim szeto 	stmf_view_op_entry_t *fVeList;
44798fe96085Stim szeto 	uint32_t fVeListSize;
44808fe96085Stim szeto 	uint32_t listCnt;
4481fcf3ce44SJohn Forte 
4482fcf3ce44SJohn Forte 	if (lu == NULL || viewEntryList == NULL) {
4483fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4484fcf3ce44SJohn Forte 	}
4485fcf3ce44SJohn Forte 
44868fe96085Stim szeto 	/* call init */
44878fe96085Stim szeto 	ret = initializeConfig();
44888fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
44898fe96085Stim szeto 		return (ret);
44908fe96085Stim szeto 	}
44918fe96085Stim szeto 
44928fe96085Stim szeto 	/*
44938fe96085Stim szeto 	 * Open control node for stmf
44948fe96085Stim szeto 	 */
44958fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
44968fe96085Stim szeto 		return (ret);
44978fe96085Stim szeto 
44988fe96085Stim szeto 	/*
44998fe96085Stim szeto 	 * Allocate ioctl input buffer
45008fe96085Stim szeto 	 */
45018fe96085Stim szeto 	fVeListSize = ALLOC_VE;
45028fe96085Stim szeto 	fVeListSize = fVeListSize * (sizeof (stmf_view_op_entry_t));
45038fe96085Stim szeto 	fVeList = (stmf_view_op_entry_t *)calloc(1, fVeListSize);
45048fe96085Stim szeto 	if (fVeList == NULL) {
45058fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
45068fe96085Stim szeto 		goto done;
45078fe96085Stim szeto 	}
45088fe96085Stim szeto 
45098fe96085Stim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
45108fe96085Stim szeto 	/*
45118fe96085Stim szeto 	 * Issue ioctl to get the LU list
45128fe96085Stim szeto 	 */
45138fe96085Stim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
45148fe96085Stim szeto 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)lu;
45158fe96085Stim szeto 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGuid);
45168fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = fVeListSize;
45178fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fVeList;
45188fe96085Stim szeto 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
45198fe96085Stim szeto 	if (ioctlRet != 0) {
45208fe96085Stim szeto 		switch (errno) {
45218fe96085Stim szeto 			case EBUSY:
4522fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4523fcf3ce44SJohn Forte 				break;
45248fe96085Stim szeto 			case EPERM:
45258fe96085Stim szeto 			case EACCES:
45268fe96085Stim szeto 				ret = STMF_ERROR_PERM;
4527fcf3ce44SJohn Forte 				break;
4528fcf3ce44SJohn Forte 			default:
4529fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
45308fe96085Stim szeto 				    "stmfGetViewEntryList:ioctl errno(%d)",
45318fe96085Stim szeto 				    errno);
4532fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4533fcf3ce44SJohn Forte 				break;
4534fcf3ce44SJohn Forte 		}
45358fe96085Stim szeto 		goto done;
45368fe96085Stim szeto 	}
45378fe96085Stim szeto 	/*
45388fe96085Stim szeto 	 * Check whether input buffer was large enough
45398fe96085Stim szeto 	 */
45408fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_VE) {
45418fe96085Stim szeto 		bzero(&stmfIoctl, sizeof (stmfIoctl));
45428fe96085Stim szeto 		fVeListSize = stmfIoctl.stmf_obuf_max_nentries *
45438fe96085Stim szeto 		    sizeof (stmf_view_op_entry_t);
45448fe96085Stim szeto 		free(fVeList);
45458fe96085Stim szeto 		fVeList = (stmf_view_op_entry_t *)calloc(1, fVeListSize);
45468fe96085Stim szeto 		if (fVeList == NULL) {
45478fe96085Stim szeto 			return (STMF_ERROR_NOMEM);
45488fe96085Stim szeto 		}
45498fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = fVeListSize;
45508fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fVeList;
45518fe96085Stim szeto 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
45528fe96085Stim szeto 		if (ioctlRet != 0) {
45538fe96085Stim szeto 			switch (errno) {
45548fe96085Stim szeto 				case EBUSY:
45558fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
45568fe96085Stim szeto 					break;
45578fe96085Stim szeto 				case EPERM:
45588fe96085Stim szeto 				case EACCES:
45598fe96085Stim szeto 					ret = STMF_ERROR_PERM;
45608fe96085Stim szeto 					break;
45618fe96085Stim szeto 				default:
45628fe96085Stim szeto 					syslog(LOG_DEBUG,
45638fe96085Stim szeto 					    "stmfGetLogicalUnitList:"
45648fe96085Stim szeto 					    "ioctl errno(%d)", errno);
45658fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
45668fe96085Stim szeto 					break;
45678fe96085Stim szeto 			}
45688fe96085Stim szeto 			goto done;
45698fe96085Stim szeto 		}
45708fe96085Stim szeto 	}
4571fcf3ce44SJohn Forte 
45728fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
45738fe96085Stim szeto 		goto done;
45748fe96085Stim szeto 	}
45758fe96085Stim szeto 
45768fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_nentries == 0) {
45778fe96085Stim szeto 		ret = STMF_ERROR_NOT_FOUND;
45788fe96085Stim szeto 		goto done;
45798fe96085Stim szeto 	}
45808fe96085Stim szeto 
45818fe96085Stim szeto 	listCnt = stmfIoctl.stmf_obuf_nentries;
45828fe96085Stim szeto 
45838fe96085Stim szeto 	/*
45848fe96085Stim szeto 	 * allocate caller's buffer with the final size
45858fe96085Stim szeto 	 */
45868fe96085Stim szeto 	*viewEntryList = (stmfViewEntryList *)calloc(1,
45878fe96085Stim szeto 	    sizeof (stmfViewEntryList) + listCnt * sizeof (stmfViewEntry));
45888fe96085Stim szeto 	if (*viewEntryList == NULL) {
45898fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
45908fe96085Stim szeto 		goto done;
45918fe96085Stim szeto 	}
45928fe96085Stim szeto 
45938fe96085Stim szeto 	(*viewEntryList)->cnt = listCnt;
45948fe96085Stim szeto 
45958fe96085Stim szeto 	/* copy to caller's buffer */
45968fe96085Stim szeto 	for (i = 0; i < listCnt; i++) {
45978fe96085Stim szeto 		(*viewEntryList)->ve[i].veIndexValid = B_TRUE;
45988fe96085Stim szeto 		(*viewEntryList)->ve[i].veIndex = fVeList[i].ve_ndx;
45998fe96085Stim szeto 		if (fVeList[i].ve_all_hosts == 1) {
46008fe96085Stim szeto 			(*viewEntryList)->ve[i].allHosts = B_TRUE;
46018fe96085Stim szeto 		} else {
46028fe96085Stim szeto 			bcopy(fVeList[i].ve_host_group.name,
46038fe96085Stim szeto 			    (*viewEntryList)->ve[i].hostGroup,
46048fe96085Stim szeto 			    fVeList[i].ve_host_group.name_size);
46058fe96085Stim szeto 		}
46068fe96085Stim szeto 		if (fVeList[i].ve_all_targets == 1) {
46078fe96085Stim szeto 			(*viewEntryList)->ve[i].allTargets = B_TRUE;
46088fe96085Stim szeto 		} else {
46098fe96085Stim szeto 			bcopy(fVeList[i].ve_target_group.name,
46108fe96085Stim szeto 			    (*viewEntryList)->ve[i].targetGroup,
46118fe96085Stim szeto 			    fVeList[i].ve_target_group.name_size);
46128fe96085Stim szeto 		}
46138fe96085Stim szeto 		bcopy(fVeList[i].ve_lu_nbr, (*viewEntryList)->ve[i].luNbr,
46148fe96085Stim szeto 		    sizeof ((*viewEntryList)->ve[i].luNbr));
46158fe96085Stim szeto 		(*viewEntryList)->ve[i].luNbrValid = B_TRUE;
46168fe96085Stim szeto 	}
46178fe96085Stim szeto 
46188fe96085Stim szeto 	/*
46198fe96085Stim szeto 	 * sort the list. This gives a consistent view across gets
46208fe96085Stim szeto 	 */
46218fe96085Stim szeto 	qsort((void *)&((*viewEntryList)->ve[0]), (*viewEntryList)->cnt,
46228fe96085Stim szeto 	    sizeof (stmfViewEntry), viewEntryCompare);
46238fe96085Stim szeto 
46248fe96085Stim szeto done:
46258fe96085Stim szeto 	(void) close(fd);
46268fe96085Stim szeto 	/*
46278fe96085Stim szeto 	 * free internal buffers
46288fe96085Stim szeto 	 */
46298fe96085Stim szeto 	free(fVeList);
4630fcf3ce44SJohn Forte 	return (ret);
4631fcf3ce44SJohn Forte }
4632fcf3ce44SJohn Forte 
46338fe96085Stim szeto 
4634fcf3ce44SJohn Forte /*
4635fcf3ce44SJohn Forte  * loadHostGroups
4636fcf3ce44SJohn Forte  *
4637fcf3ce44SJohn Forte  * Purpose - issues the ioctl to load the host groups into stmf
4638fcf3ce44SJohn Forte  *
4639fcf3ce44SJohn Forte  * fd - file descriptor for the control node of stmf.
4640fcf3ce44SJohn Forte  * groupList - populated host group list
4641fcf3ce44SJohn Forte  */
4642fcf3ce44SJohn Forte static int
4643fcf3ce44SJohn Forte loadHostGroups(int fd, stmfGroupList *groupList)
4644fcf3ce44SJohn Forte {
4645fcf3ce44SJohn Forte 	int i, j;
4646fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4647fcf3ce44SJohn Forte 	stmfGroupProperties *groupProps = NULL;
4648fcf3ce44SJohn Forte 
4649fcf3ce44SJohn Forte 	for (i = 0; i < groupList->cnt; i++) {
4650fcf3ce44SJohn Forte 		if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_HOST_GROUP,
4651fcf3ce44SJohn Forte 		    &(groupList->name[i]))) != STMF_STATUS_SUCCESS) {
4652fcf3ce44SJohn Forte 			goto out;
4653fcf3ce44SJohn Forte 		}
46548fe96085Stim szeto 		ret = iLoadGroupMembersFromPs(&(groupList->name[i]),
46558fe96085Stim szeto 		    &groupProps, HOST_GROUP);
4656fcf3ce44SJohn Forte 		for (j = 0; j < groupProps->cnt; j++) {
4657fcf3ce44SJohn Forte 			if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_HG_ENTRY,
4658fcf3ce44SJohn Forte 			    &(groupList->name[i]), &(groupProps->name[j])))
4659fcf3ce44SJohn Forte 			    != STMF_STATUS_SUCCESS) {
4660fcf3ce44SJohn Forte 				goto out;
4661fcf3ce44SJohn Forte 			}
4662fcf3ce44SJohn Forte 		}
4663fcf3ce44SJohn Forte 	}
4664fcf3ce44SJohn Forte 
4665fcf3ce44SJohn Forte 
4666fcf3ce44SJohn Forte out:
4667fcf3ce44SJohn Forte 	stmfFreeMemory(groupProps);
4668fcf3ce44SJohn Forte 	return (ret);
4669fcf3ce44SJohn Forte }
4670fcf3ce44SJohn Forte 
4671fcf3ce44SJohn Forte /*
4672fcf3ce44SJohn Forte  * loadTargetGroups
4673fcf3ce44SJohn Forte  *
4674fcf3ce44SJohn Forte  * Purpose - issues the ioctl to load the target groups into stmf
4675fcf3ce44SJohn Forte  *
4676fcf3ce44SJohn Forte  * fd - file descriptor for the control node of stmf.
4677fcf3ce44SJohn Forte  * groupList - populated target group list.
4678fcf3ce44SJohn Forte  */
4679fcf3ce44SJohn Forte static int
4680fcf3ce44SJohn Forte loadTargetGroups(int fd, stmfGroupList *groupList)
4681fcf3ce44SJohn Forte {
4682fcf3ce44SJohn Forte 	int i, j;
4683fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4684fcf3ce44SJohn Forte 	stmfGroupProperties *groupProps = NULL;
4685fcf3ce44SJohn Forte 
4686fcf3ce44SJohn Forte 	for (i = 0; i < groupList->cnt; i++) {
4687fcf3ce44SJohn Forte 		if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_TARGET_GROUP,
4688fcf3ce44SJohn Forte 		    &(groupList->name[i]))) != STMF_STATUS_SUCCESS) {
4689fcf3ce44SJohn Forte 			goto out;
4690fcf3ce44SJohn Forte 		}
46918fe96085Stim szeto 		ret = iLoadGroupMembersFromPs(&(groupList->name[i]),
46928fe96085Stim szeto 		    &groupProps, TARGET_GROUP);
4693fcf3ce44SJohn Forte 		for (j = 0; j < groupProps->cnt; j++) {
4694fcf3ce44SJohn Forte 			if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_TG_ENTRY,
4695fcf3ce44SJohn Forte 			    &(groupList->name[i]), &(groupProps->name[j])))
4696fcf3ce44SJohn Forte 			    != STMF_STATUS_SUCCESS) {
4697fcf3ce44SJohn Forte 				goto out;
4698fcf3ce44SJohn Forte 			}
4699fcf3ce44SJohn Forte 		}
4700fcf3ce44SJohn Forte 	}
4701fcf3ce44SJohn Forte 
4702fcf3ce44SJohn Forte 
4703fcf3ce44SJohn Forte out:
4704fcf3ce44SJohn Forte 	stmfFreeMemory(groupProps);
4705fcf3ce44SJohn Forte 	return (ret);
4706fcf3ce44SJohn Forte }
4707fcf3ce44SJohn Forte 
4708fcf3ce44SJohn Forte 
4709fcf3ce44SJohn Forte /*
4710fcf3ce44SJohn Forte  * loadStore
4711fcf3ce44SJohn Forte  *
4712fcf3ce44SJohn Forte  * Purpose: Load the configuration data from the store
4713fcf3ce44SJohn Forte  *
4714fcf3ce44SJohn Forte  * First load the host groups and target groups, then the view entries
4715fcf3ce44SJohn Forte  * and finally the provider data
4716fcf3ce44SJohn Forte  *
4717fcf3ce44SJohn Forte  * fd - file descriptor of control node for stmf.
4718fcf3ce44SJohn Forte  */
4719fcf3ce44SJohn Forte static int
4720fcf3ce44SJohn Forte loadStore(int fd)
4721fcf3ce44SJohn Forte {
4722fcf3ce44SJohn Forte 	int ret;
4723fcf3ce44SJohn Forte 	int i, j;
4724fcf3ce44SJohn Forte 	stmfGroupList *groupList = NULL;
4725fcf3ce44SJohn Forte 	stmfGuidList *guidList = NULL;
4726fcf3ce44SJohn Forte 	stmfViewEntryList *viewEntryList = NULL;
4727fcf3ce44SJohn Forte 	stmfProviderList *providerList = NULL;
4728fcf3ce44SJohn Forte 	int providerType;
4729fcf3ce44SJohn Forte 	nvlist_t *nvl = NULL;
4730fcf3ce44SJohn Forte 
4731fcf3ce44SJohn Forte 
4732fcf3ce44SJohn Forte 
4733fcf3ce44SJohn Forte 	/* load host groups */
47348fe96085Stim szeto 	ret = iLoadGroupFromPs(&groupList, HOST_GROUP);
4735fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4736fcf3ce44SJohn Forte 		return (ret);
4737fcf3ce44SJohn Forte 	}
4738fcf3ce44SJohn Forte 	ret = loadHostGroups(fd, groupList);
4739fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4740fcf3ce44SJohn Forte 		goto out;
4741fcf3ce44SJohn Forte 	}
4742fcf3ce44SJohn Forte 
4743fcf3ce44SJohn Forte 	stmfFreeMemory(groupList);
4744fcf3ce44SJohn Forte 	groupList = NULL;
4745fcf3ce44SJohn Forte 
4746fcf3ce44SJohn Forte 	/* load target groups */
47478fe96085Stim szeto 	ret = iLoadGroupFromPs(&groupList, TARGET_GROUP);
4748fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4749fcf3ce44SJohn Forte 		goto out;
4750fcf3ce44SJohn Forte 	}
4751fcf3ce44SJohn Forte 	ret = loadTargetGroups(fd, groupList);
4752fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4753fcf3ce44SJohn Forte 		goto out;
4754fcf3ce44SJohn Forte 	}
4755fcf3ce44SJohn Forte 
4756fcf3ce44SJohn Forte 	stmfFreeMemory(groupList);
4757fcf3ce44SJohn Forte 	groupList = NULL;
4758fcf3ce44SJohn Forte 
4759fcf3ce44SJohn Forte 	/* Get the guid list */
4760fcf3ce44SJohn Forte 	ret = psGetLogicalUnitList(&guidList);
4761fcf3ce44SJohn Forte 	switch (ret) {
4762fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
4763fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
4764fcf3ce44SJohn Forte 			break;
4765fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
4766fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
4767fcf3ce44SJohn Forte 			break;
4768fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
4769fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
4770fcf3ce44SJohn Forte 			break;
4771fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4772fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
4773fcf3ce44SJohn Forte 			break;
4774fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
4775fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
4776fcf3ce44SJohn Forte 			break;
4777fcf3ce44SJohn Forte 		default:
4778fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
4779fcf3ce44SJohn Forte 			break;
4780fcf3ce44SJohn Forte 	}
4781fcf3ce44SJohn Forte 
4782fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4783fcf3ce44SJohn Forte 		goto out;
4784fcf3ce44SJohn Forte 	}
4785fcf3ce44SJohn Forte 
4786fcf3ce44SJohn Forte 	/*
4787fcf3ce44SJohn Forte 	 * We have the guid list, now get the corresponding
4788fcf3ce44SJohn Forte 	 * view entries for each guid
4789fcf3ce44SJohn Forte 	 */
4790fcf3ce44SJohn Forte 	for (i = 0; i < guidList->cnt; i++) {
4791fcf3ce44SJohn Forte 		ret = psGetViewEntryList(&guidList->guid[i], &viewEntryList);
4792fcf3ce44SJohn Forte 		switch (ret) {
4793fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
4794fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
4795fcf3ce44SJohn Forte 				break;
4796fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
4797fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4798fcf3ce44SJohn Forte 				break;
4799fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
4800fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4801fcf3ce44SJohn Forte 				break;
4802fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4803fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
4804fcf3ce44SJohn Forte 				break;
4805fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
4806fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
4807fcf3ce44SJohn Forte 				break;
4808fcf3ce44SJohn Forte 			default:
4809fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4810fcf3ce44SJohn Forte 				break;
4811fcf3ce44SJohn Forte 		}
4812fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
4813fcf3ce44SJohn Forte 			goto out;
4814fcf3ce44SJohn Forte 		}
4815fcf3ce44SJohn Forte 		for (j = 0; j < viewEntryList->cnt; j++) {
4816fcf3ce44SJohn Forte 			ret = addViewEntryIoctl(fd, &guidList->guid[i],
4817fcf3ce44SJohn Forte 			    &viewEntryList->ve[j]);
4818fcf3ce44SJohn Forte 			if (ret != STMF_STATUS_SUCCESS) {
4819fcf3ce44SJohn Forte 				goto out;
4820fcf3ce44SJohn Forte 			}
4821fcf3ce44SJohn Forte 		}
4822fcf3ce44SJohn Forte 	}
4823fcf3ce44SJohn Forte 
4824fcf3ce44SJohn Forte 	/* get the list of providers that have data */
4825fcf3ce44SJohn Forte 	ret = psGetProviderDataList(&providerList);
4826fcf3ce44SJohn Forte 	switch (ret) {
4827fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
4828fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
4829fcf3ce44SJohn Forte 			break;
4830fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
4831fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
4832fcf3ce44SJohn Forte 			break;
4833fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
4834fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
4835fcf3ce44SJohn Forte 			break;
4836fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4837fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
4838fcf3ce44SJohn Forte 			break;
4839fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
4840fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
4841fcf3ce44SJohn Forte 			break;
4842fcf3ce44SJohn Forte 		default:
4843fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
4844fcf3ce44SJohn Forte 			break;
4845fcf3ce44SJohn Forte 	}
4846fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4847fcf3ce44SJohn Forte 		goto out;
4848fcf3ce44SJohn Forte 	}
4849fcf3ce44SJohn Forte 
4850fcf3ce44SJohn Forte 	for (i = 0; i < providerList->cnt; i++) {
4851fcf3ce44SJohn Forte 		providerType = providerList->provider[i].providerType;
4852fcf3ce44SJohn Forte 		ret = psGetProviderData(providerList->provider[i].name,
4853fcf3ce44SJohn Forte 		    &nvl, providerType, NULL);
4854fcf3ce44SJohn Forte 		switch (ret) {
4855fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
4856fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
4857fcf3ce44SJohn Forte 				break;
4858fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
4859fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4860fcf3ce44SJohn Forte 				break;
4861fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
4862fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4863fcf3ce44SJohn Forte 				break;
4864fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4865fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
4866fcf3ce44SJohn Forte 				break;
4867fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
4868fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
4869fcf3ce44SJohn Forte 				break;
4870fcf3ce44SJohn Forte 			default:
4871fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4872fcf3ce44SJohn Forte 				break;
4873fcf3ce44SJohn Forte 		}
4874fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
4875fcf3ce44SJohn Forte 			goto out;
4876fcf3ce44SJohn Forte 		}
4877fcf3ce44SJohn Forte 
4878fcf3ce44SJohn Forte 		/* call setProviderData */
4879fcf3ce44SJohn Forte 		ret = setProviderData(fd, providerList->provider[i].name, nvl,
48808fe96085Stim szeto 		    providerType, NULL);
4881fcf3ce44SJohn Forte 		switch (ret) {
4882fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
4883fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
4884fcf3ce44SJohn Forte 				break;
4885fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
4886fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4887fcf3ce44SJohn Forte 				break;
4888fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
4889fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4890fcf3ce44SJohn Forte 				break;
4891fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
4892fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
4893fcf3ce44SJohn Forte 				break;
4894fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
4895fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
4896fcf3ce44SJohn Forte 				break;
4897fcf3ce44SJohn Forte 			default:
4898fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4899fcf3ce44SJohn Forte 				break;
4900fcf3ce44SJohn Forte 		}
4901fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
4902fcf3ce44SJohn Forte 			goto out;
4903fcf3ce44SJohn Forte 		}
4904fcf3ce44SJohn Forte 
4905fcf3ce44SJohn Forte 		nvlist_free(nvl);
4906fcf3ce44SJohn Forte 		nvl = NULL;
4907fcf3ce44SJohn Forte 	}
4908fcf3ce44SJohn Forte out:
4909fcf3ce44SJohn Forte 	if (groupList != NULL) {
4910fcf3ce44SJohn Forte 		free(groupList);
4911fcf3ce44SJohn Forte 	}
4912fcf3ce44SJohn Forte 	if (guidList != NULL) {
4913fcf3ce44SJohn Forte 		free(guidList);
4914fcf3ce44SJohn Forte 	}
4915fcf3ce44SJohn Forte 	if (viewEntryList != NULL) {
4916fcf3ce44SJohn Forte 		free(viewEntryList);
4917fcf3ce44SJohn Forte 	}
4918fcf3ce44SJohn Forte 	if (nvl != NULL) {
4919fcf3ce44SJohn Forte 		nvlist_free(nvl);
4920fcf3ce44SJohn Forte 	}
4921fcf3ce44SJohn Forte 	return (ret);
4922fcf3ce44SJohn Forte }
4923fcf3ce44SJohn Forte 
4924fcf3ce44SJohn Forte /*
492545039663SJohn Forte  * stmfGetAluaState
492645039663SJohn Forte  *
492745039663SJohn Forte  * Purpose - Get the alua state
492845039663SJohn Forte  *
492945039663SJohn Forte  */
493045039663SJohn Forte int
493145039663SJohn Forte stmfGetAluaState(boolean_t *enabled, uint32_t *node)
493245039663SJohn Forte {
493345039663SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
493445039663SJohn Forte 	int fd;
493545039663SJohn Forte 	stmf_iocdata_t stmfIoctl = {0};
493645039663SJohn Forte 	stmf_alua_state_desc_t alua_state = {0};
493745039663SJohn Forte 	int ioctlRet;
493845039663SJohn Forte 
493945039663SJohn Forte 	if (enabled == NULL || node == NULL) {
494045039663SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
494145039663SJohn Forte 	}
494245039663SJohn Forte 
494345039663SJohn Forte 	/*
494445039663SJohn Forte 	 * Open control node for stmf
494545039663SJohn Forte 	 */
494645039663SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
494745039663SJohn Forte 		return (ret);
494845039663SJohn Forte 
494945039663SJohn Forte 	/*
495045039663SJohn Forte 	 * Issue ioctl to get the stmf state
495145039663SJohn Forte 	 */
495245039663SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
495345039663SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (alua_state);
495445039663SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&alua_state;
495545039663SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_GET_ALUA_STATE, &stmfIoctl);
495645039663SJohn Forte 
495745039663SJohn Forte 	(void) close(fd);
495845039663SJohn Forte 
495945039663SJohn Forte 	if (ioctlRet != 0) {
496045039663SJohn Forte 		switch (errno) {
496145039663SJohn Forte 			case EBUSY:
496245039663SJohn Forte 				ret = STMF_ERROR_BUSY;
496345039663SJohn Forte 				break;
496445039663SJohn Forte 			case EPERM:
496545039663SJohn Forte 			case EACCES:
496645039663SJohn Forte 				ret = STMF_ERROR_PERM;
496745039663SJohn Forte 				break;
496845039663SJohn Forte 			default:
496945039663SJohn Forte 				syslog(LOG_DEBUG,
497045039663SJohn Forte 				    "getStmfState:ioctl errno(%d)", errno);
497145039663SJohn Forte 				ret = STMF_STATUS_ERROR;
497245039663SJohn Forte 				break;
497345039663SJohn Forte 		}
497445039663SJohn Forte 	} else {
497545039663SJohn Forte 		if (alua_state.alua_state == 1) {
497645039663SJohn Forte 			*enabled = B_TRUE;
497745039663SJohn Forte 		} else {
497845039663SJohn Forte 			*enabled = B_FALSE;
497945039663SJohn Forte 		}
498045039663SJohn Forte 		*node = alua_state.alua_node;
498145039663SJohn Forte 	}
498245039663SJohn Forte 
498345039663SJohn Forte 	return (ret);
498445039663SJohn Forte }
498545039663SJohn Forte 
498645039663SJohn Forte /*
498745039663SJohn Forte  * stmfSetAluaState
498845039663SJohn Forte  *
498945039663SJohn Forte  * Purpose - set the alua state to enabled/disabled
499045039663SJohn Forte  *
499145039663SJohn Forte  */
499245039663SJohn Forte int
499345039663SJohn Forte stmfSetAluaState(boolean_t enabled, uint32_t node)
499445039663SJohn Forte {
499545039663SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
499645039663SJohn Forte 	int fd;
499745039663SJohn Forte 	stmf_iocdata_t stmfIoctl = {0};
499845039663SJohn Forte 	stmf_alua_state_desc_t alua_state = {0};
499945039663SJohn Forte 	int ioctlRet;
500045039663SJohn Forte 
500145039663SJohn Forte 	if ((enabled != B_TRUE && enabled != B_FALSE) || (node > 1)) {
500245039663SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
500345039663SJohn Forte 	}
500445039663SJohn Forte 
500545039663SJohn Forte 	if (enabled) {
500645039663SJohn Forte 		alua_state.alua_state = 1;
500745039663SJohn Forte 	}
500845039663SJohn Forte 
500945039663SJohn Forte 	alua_state.alua_node = node;
501045039663SJohn Forte 
501145039663SJohn Forte 	/*
501245039663SJohn Forte 	 * Open control node for stmf
501345039663SJohn Forte 	 */
501445039663SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
501545039663SJohn Forte 		return (ret);
501645039663SJohn Forte 
501745039663SJohn Forte 	/*
501845039663SJohn Forte 	 * Issue ioctl to get the stmf state
501945039663SJohn Forte 	 */
502045039663SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
502145039663SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (alua_state);
502245039663SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&alua_state;
502345039663SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_SET_ALUA_STATE, &stmfIoctl);
502445039663SJohn Forte 
502545039663SJohn Forte 	(void) close(fd);
502645039663SJohn Forte 
502745039663SJohn Forte 	if (ioctlRet != 0) {
502845039663SJohn Forte 		switch (errno) {
502945039663SJohn Forte 			case EBUSY:
503045039663SJohn Forte 				ret = STMF_ERROR_BUSY;
503145039663SJohn Forte 				break;
503245039663SJohn Forte 			case EPERM:
503345039663SJohn Forte 			case EACCES:
503445039663SJohn Forte 				ret = STMF_ERROR_PERM;
503545039663SJohn Forte 				break;
503645039663SJohn Forte 			default:
503745039663SJohn Forte 				syslog(LOG_DEBUG,
503845039663SJohn Forte 				    "getStmfState:ioctl errno(%d)", errno);
503945039663SJohn Forte 				ret = STMF_STATUS_ERROR;
504045039663SJohn Forte 				break;
504145039663SJohn Forte 		}
504245039663SJohn Forte 	}
504345039663SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
504445039663SJohn Forte 		deleteNonActiveLus();
504545039663SJohn Forte 	}
504645039663SJohn Forte 
504745039663SJohn Forte 	return (ret);
504845039663SJohn Forte }
504945039663SJohn Forte 
505045039663SJohn Forte static void
505145039663SJohn Forte deleteNonActiveLus()
505245039663SJohn Forte {
505345039663SJohn Forte 	int stmfRet;
505445039663SJohn Forte 	int i;
505545039663SJohn Forte 	stmfGuidList *luList;
505645039663SJohn Forte 	luResource hdl = NULL;
505745039663SJohn Forte 	char propVal[10];
505845039663SJohn Forte 	size_t propValSize = sizeof (propVal);
505945039663SJohn Forte 
506045039663SJohn Forte 	stmfRet = stmfGetLogicalUnitList(&luList);
506145039663SJohn Forte 	if (stmfRet != STMF_STATUS_SUCCESS) {
506245039663SJohn Forte 		return;
506345039663SJohn Forte 	}
506445039663SJohn Forte 
506545039663SJohn Forte 	for (i = 0; i < luList->cnt; i++) {
506645039663SJohn Forte 		stmfRet = stmfGetLuResource(&luList->guid[i], &hdl);
506745039663SJohn Forte 		if (stmfRet != STMF_STATUS_SUCCESS) {
506845039663SJohn Forte 			goto err;
506945039663SJohn Forte 		}
507045039663SJohn Forte 		stmfRet = stmfGetLuProp(hdl, STMF_LU_PROP_ACCESS_STATE, propVal,
507145039663SJohn Forte 		    &propValSize);
507245039663SJohn Forte 		if (stmfRet != STMF_STATUS_SUCCESS) {
507345039663SJohn Forte 			goto err;
507445039663SJohn Forte 		}
507545039663SJohn Forte 		if (propVal[0] == '0') {
507645039663SJohn Forte 			(void) stmfFreeLuResource(hdl);
507745039663SJohn Forte 			hdl = NULL;
507845039663SJohn Forte 			continue;
507945039663SJohn Forte 		}
508045039663SJohn Forte 		(void) stmfDeleteLu(&luList->guid[i]);
508145039663SJohn Forte 		(void) stmfFreeLuResource(hdl);
508245039663SJohn Forte 		hdl = NULL;
508345039663SJohn Forte 	}
508445039663SJohn Forte 
508545039663SJohn Forte err:
508645039663SJohn Forte 	stmfFreeMemory(luList);
508745039663SJohn Forte 	(void) stmfFreeLuResource(hdl);
508845039663SJohn Forte }
508945039663SJohn Forte 
509045039663SJohn Forte /*
5091fcf3ce44SJohn Forte  * stmfLoadConfig
5092fcf3ce44SJohn Forte  *
5093fcf3ce44SJohn Forte  * Purpose - load the configuration data from smf into stmf
5094fcf3ce44SJohn Forte  *
5095fcf3ce44SJohn Forte  */
5096fcf3ce44SJohn Forte int
5097fcf3ce44SJohn Forte stmfLoadConfig(void)
5098fcf3ce44SJohn Forte {
50998fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
5100fcf3ce44SJohn Forte 	int fd;
5101fcf3ce44SJohn Forte 	stmf_state_desc_t stmfStateSet;
5102fcf3ce44SJohn Forte 	stmfState state;
5103fcf3ce44SJohn Forte 
51048fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
51058fe96085Stim szeto 		stmfStateSet.state = STMF_STATE_OFFLINE;
5106640428aeSSue Gleeson 
51078fe96085Stim szeto 		if ((ret = openStmf(OPEN_EXCL_STMF, &fd))
51088fe96085Stim szeto 		    != STMF_STATUS_SUCCESS) {
51098fe96085Stim szeto 			return (ret);
51108fe96085Stim szeto 		}
5111640428aeSSue Gleeson 		/*
5112640428aeSSue Gleeson 		 * Configuration not stored persistently; nothing to
5113640428aeSSue Gleeson 		 * initialize so do not set to STMF_CONFIG_INIT.
5114640428aeSSue Gleeson 		 */
51158fe96085Stim szeto 		stmfStateSet.config_state = STMF_CONFIG_INIT_DONE;
51168fe96085Stim szeto 		goto done;
51178fe96085Stim szeto 	}
5118fcf3ce44SJohn Forte 
5119fcf3ce44SJohn Forte 	/* Check to ensure service exists */
5120fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
5121fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
5122fcf3ce44SJohn Forte 	}
5123fcf3ce44SJohn Forte 
5124fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
5125fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
5126fcf3ce44SJohn Forte 		if (state.operationalState != STMF_SERVICE_STATE_OFFLINE) {
5127fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_ONLINE);
5128fcf3ce44SJohn Forte 		}
5129fcf3ce44SJohn Forte 	} else {
5130fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
5131fcf3ce44SJohn Forte 	}
5132fcf3ce44SJohn Forte 
5133fcf3ce44SJohn Forte 
5134fcf3ce44SJohn Forte 	stmfStateSet.state = STMF_STATE_OFFLINE;
5135fcf3ce44SJohn Forte 	stmfStateSet.config_state = STMF_CONFIG_INIT;
5136fcf3ce44SJohn Forte 
5137fcf3ce44SJohn Forte 	/*
5138fcf3ce44SJohn Forte 	 * Open control node for stmf
5139fcf3ce44SJohn Forte 	 */
5140fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5141fcf3ce44SJohn Forte 		return (ret);
5142fcf3ce44SJohn Forte 
5143fcf3ce44SJohn Forte 	ret = setStmfState(fd, &stmfStateSet, STMF_SERVICE_TYPE);
5144fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5145fcf3ce44SJohn Forte 		goto done;
5146fcf3ce44SJohn Forte 	}
5147fcf3ce44SJohn Forte 
5148fcf3ce44SJohn Forte 	/* Load the persistent configuration data */
5149fcf3ce44SJohn Forte 	ret = loadStore(fd);
5150fcf3ce44SJohn Forte 	if (ret != 0) {
5151fcf3ce44SJohn Forte 		goto done;
5152fcf3ce44SJohn Forte 	}
5153fcf3ce44SJohn Forte 
5154fcf3ce44SJohn Forte 	stmfStateSet.state = STMF_STATE_OFFLINE;
5155fcf3ce44SJohn Forte 	stmfStateSet.config_state = STMF_CONFIG_INIT_DONE;
5156fcf3ce44SJohn Forte 
5157fcf3ce44SJohn Forte done:
5158fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
5159fcf3ce44SJohn Forte 		ret = setStmfState(fd, &stmfStateSet, STMF_SERVICE_TYPE);
5160fcf3ce44SJohn Forte 	}
5161fcf3ce44SJohn Forte 	(void) close(fd);
5162fcf3ce44SJohn Forte 	return (ret);
5163fcf3ce44SJohn Forte }
5164fcf3ce44SJohn Forte 
51658fe96085Stim szeto 
5166fcf3ce44SJohn Forte /*
5167fcf3ce44SJohn Forte  * getStmfState
5168fcf3ce44SJohn Forte  *
5169fcf3ce44SJohn Forte  * stmfState - pointer to stmf_state_desc_t structure. Will contain the state
5170fcf3ce44SJohn Forte  *             information of the stmf service on success.
5171fcf3ce44SJohn Forte  */
5172fcf3ce44SJohn Forte static int
5173fcf3ce44SJohn Forte getStmfState(stmf_state_desc_t *stmfState)
5174fcf3ce44SJohn Forte {
5175fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5176fcf3ce44SJohn Forte 	int fd;
5177fcf3ce44SJohn Forte 	int ioctlRet;
5178fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
5179fcf3ce44SJohn Forte 
5180fcf3ce44SJohn Forte 	/*
5181fcf3ce44SJohn Forte 	 * Open control node for stmf
5182fcf3ce44SJohn Forte 	 */
5183fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5184fcf3ce44SJohn Forte 		return (ret);
5185fcf3ce44SJohn Forte 
5186fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
5187fcf3ce44SJohn Forte 	/*
5188fcf3ce44SJohn Forte 	 * Issue ioctl to get the stmf state
5189fcf3ce44SJohn Forte 	 */
5190fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
5191fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_state_desc_t);
5192fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)stmfState;
5193fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (stmf_state_desc_t);
5194fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)stmfState;
5195fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_GET_STMF_STATE, &stmfIoctl);
5196fcf3ce44SJohn Forte 
5197fcf3ce44SJohn Forte 	(void) close(fd);
5198fcf3ce44SJohn Forte 
5199fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
5200fcf3ce44SJohn Forte 		switch (errno) {
5201fcf3ce44SJohn Forte 			case EBUSY:
5202fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
5203fcf3ce44SJohn Forte 				break;
5204fcf3ce44SJohn Forte 			case EPERM:
5205fcf3ce44SJohn Forte 			case EACCES:
5206fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
5207fcf3ce44SJohn Forte 				break;
5208fcf3ce44SJohn Forte 			default:
5209fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
5210fcf3ce44SJohn Forte 				    "getStmfState:ioctl errno(%d)", errno);
5211fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
5212fcf3ce44SJohn Forte 				break;
5213fcf3ce44SJohn Forte 		}
5214fcf3ce44SJohn Forte 	}
5215fcf3ce44SJohn Forte 	return (ret);
5216fcf3ce44SJohn Forte }
5217fcf3ce44SJohn Forte 
5218fcf3ce44SJohn Forte 
5219fcf3ce44SJohn Forte /*
5220fcf3ce44SJohn Forte  * setStmfState
5221fcf3ce44SJohn Forte  *
5222fcf3ce44SJohn Forte  * stmfState - pointer to caller set state structure
5223fcf3ce44SJohn Forte  * objectType - one of:
5224fcf3ce44SJohn Forte  *		LOGICAL_UNIT_TYPE
5225fcf3ce44SJohn Forte  *		TARGET_TYPE
5226fcf3ce44SJohn Forte  *		STMF_SERVICE_TYPE
5227fcf3ce44SJohn Forte  */
5228fcf3ce44SJohn Forte static int
5229fcf3ce44SJohn Forte setStmfState(int fd, stmf_state_desc_t *stmfState, int objectType)
5230fcf3ce44SJohn Forte {
5231fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5232fcf3ce44SJohn Forte 	int ioctlRet;
5233fcf3ce44SJohn Forte 	int cmd;
5234fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
5235fcf3ce44SJohn Forte 
5236fcf3ce44SJohn Forte 	switch (objectType) {
5237fcf3ce44SJohn Forte 		case LOGICAL_UNIT_TYPE:
5238fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_LU_STATE;
5239fcf3ce44SJohn Forte 			break;
5240fcf3ce44SJohn Forte 		case TARGET_TYPE:
5241fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_TARGET_PORT_STATE;
5242fcf3ce44SJohn Forte 			break;
5243fcf3ce44SJohn Forte 		case STMF_SERVICE_TYPE:
5244fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_STMF_STATE;
5245fcf3ce44SJohn Forte 			break;
5246fcf3ce44SJohn Forte 		default:
5247fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5248fcf3ce44SJohn Forte 			goto done;
5249fcf3ce44SJohn Forte 	}
5250fcf3ce44SJohn Forte 
5251fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
5252fcf3ce44SJohn Forte 	/*
5253fcf3ce44SJohn Forte 	 * Issue ioctl to set the stmf state
5254fcf3ce44SJohn Forte 	 */
5255fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
5256fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_state_desc_t);
5257fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)stmfState;
5258fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
5259fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
5260fcf3ce44SJohn Forte 		switch (errno) {
5261fcf3ce44SJohn Forte 			case EBUSY:
5262fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
5263fcf3ce44SJohn Forte 				break;
52648fe96085Stim szeto 			case EPERM:
5265fcf3ce44SJohn Forte 			case EACCES:
5266fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
5267fcf3ce44SJohn Forte 				break;
5268fcf3ce44SJohn Forte 			case ENOENT:
5269fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
5270fcf3ce44SJohn Forte 				break;
5271fcf3ce44SJohn Forte 			default:
5272fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
5273fcf3ce44SJohn Forte 				    "setStmfState:ioctl errno(%d)", errno);
5274fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
5275fcf3ce44SJohn Forte 				break;
5276fcf3ce44SJohn Forte 		}
5277fcf3ce44SJohn Forte 	}
5278fcf3ce44SJohn Forte done:
5279fcf3ce44SJohn Forte 	return (ret);
5280fcf3ce44SJohn Forte }
5281fcf3ce44SJohn Forte 
5282fcf3ce44SJohn Forte /*
5283fcf3ce44SJohn Forte  * stmfOnline
5284fcf3ce44SJohn Forte  *
5285fcf3ce44SJohn Forte  * Purpose: Online stmf service
5286fcf3ce44SJohn Forte  *
5287fcf3ce44SJohn Forte  */
5288fcf3ce44SJohn Forte int
5289fcf3ce44SJohn Forte stmfOnline(void)
5290fcf3ce44SJohn Forte {
5291fcf3ce44SJohn Forte 	int ret;
5292fcf3ce44SJohn Forte 	int fd;
5293fcf3ce44SJohn Forte 	stmfState state;
5294fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
5295fcf3ce44SJohn Forte 
5296fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
5297fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
5298fcf3ce44SJohn Forte 		if (state.operationalState == STMF_SERVICE_STATE_ONLINE) {
5299fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_ONLINE);
5300fcf3ce44SJohn Forte 		}
5301fcf3ce44SJohn Forte 	} else {
5302fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
5303fcf3ce44SJohn Forte 	}
5304fcf3ce44SJohn Forte 	iState.state = STMF_STATE_ONLINE;
5305fcf3ce44SJohn Forte 	iState.config_state = STMF_CONFIG_NONE;
5306fcf3ce44SJohn Forte 	/*
5307fcf3ce44SJohn Forte 	 * Open control node for stmf
5308fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5309fcf3ce44SJohn Forte 	 */
5310fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5311fcf3ce44SJohn Forte 		return (ret);
5312fcf3ce44SJohn Forte 	ret = setStmfState(fd, &iState, STMF_SERVICE_TYPE);
5313fcf3ce44SJohn Forte 	(void) close(fd);
5314fcf3ce44SJohn Forte 	return (ret);
5315fcf3ce44SJohn Forte }
5316fcf3ce44SJohn Forte 
5317fcf3ce44SJohn Forte /*
5318fcf3ce44SJohn Forte  * stmfOffline
5319fcf3ce44SJohn Forte  *
5320fcf3ce44SJohn Forte  * Purpose: Offline stmf service
5321fcf3ce44SJohn Forte  *
5322fcf3ce44SJohn Forte  */
5323fcf3ce44SJohn Forte int
5324fcf3ce44SJohn Forte stmfOffline(void)
5325fcf3ce44SJohn Forte {
5326fcf3ce44SJohn Forte 	int ret;
5327fcf3ce44SJohn Forte 	int fd;
5328fcf3ce44SJohn Forte 	stmfState state;
5329fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
5330fcf3ce44SJohn Forte 
5331fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
5332fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
5333fcf3ce44SJohn Forte 		if (state.operationalState == STMF_SERVICE_STATE_OFFLINE) {
5334fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_OFFLINE);
5335fcf3ce44SJohn Forte 		}
5336fcf3ce44SJohn Forte 	} else {
5337fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
5338fcf3ce44SJohn Forte 	}
5339fcf3ce44SJohn Forte 	iState.state = STMF_STATE_OFFLINE;
5340fcf3ce44SJohn Forte 	iState.config_state = STMF_CONFIG_NONE;
5341fcf3ce44SJohn Forte 
5342fcf3ce44SJohn Forte 	/*
5343fcf3ce44SJohn Forte 	 * Open control node for stmf
5344fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5345fcf3ce44SJohn Forte 	 */
5346fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5347fcf3ce44SJohn Forte 		return (ret);
5348fcf3ce44SJohn Forte 	ret = setStmfState(fd, &iState, STMF_SERVICE_TYPE);
5349fcf3ce44SJohn Forte 	(void) close(fd);
5350fcf3ce44SJohn Forte 	return (ret);
5351fcf3ce44SJohn Forte }
5352fcf3ce44SJohn Forte 
5353fcf3ce44SJohn Forte 
5354fcf3ce44SJohn Forte /*
5355fcf3ce44SJohn Forte  * stmfOfflineTarget
5356fcf3ce44SJohn Forte  *
5357fcf3ce44SJohn Forte  * Purpose: Change state of target to offline
5358fcf3ce44SJohn Forte  *
5359fcf3ce44SJohn Forte  * devid - devid of the target to offline
5360fcf3ce44SJohn Forte  */
5361fcf3ce44SJohn Forte int
5362fcf3ce44SJohn Forte stmfOfflineTarget(stmfDevid *devid)
5363fcf3ce44SJohn Forte {
5364fcf3ce44SJohn Forte 	stmf_state_desc_t targetState;
5365fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5366fcf3ce44SJohn Forte 	int fd;
5367fcf3ce44SJohn Forte 
5368fcf3ce44SJohn Forte 	if (devid == NULL) {
5369fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5370fcf3ce44SJohn Forte 	}
5371fcf3ce44SJohn Forte 	bzero(&targetState, sizeof (targetState));
5372fcf3ce44SJohn Forte 
5373fcf3ce44SJohn Forte 	targetState.state = STMF_STATE_OFFLINE;
5374fcf3ce44SJohn Forte 	targetState.ident[IDENT_LENGTH_BYTE] = devid->identLength;
5375fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetState.ident[IDENT_LENGTH_BYTE + 1],
5376fcf3ce44SJohn Forte 	    devid->identLength);
5377fcf3ce44SJohn Forte 	/*
5378fcf3ce44SJohn Forte 	 * Open control node for stmf
5379fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5380fcf3ce44SJohn Forte 	 */
5381fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5382fcf3ce44SJohn Forte 		return (ret);
5383fcf3ce44SJohn Forte 	ret = setStmfState(fd, &targetState, TARGET_TYPE);
5384fcf3ce44SJohn Forte 	(void) close(fd);
5385fcf3ce44SJohn Forte 	return (ret);
5386fcf3ce44SJohn Forte }
5387fcf3ce44SJohn Forte 
5388fcf3ce44SJohn Forte /*
5389fcf3ce44SJohn Forte  * stmfOfflineLogicalUnit
5390fcf3ce44SJohn Forte  *
5391fcf3ce44SJohn Forte  * Purpose: Change state of logical unit to offline
5392fcf3ce44SJohn Forte  *
5393fcf3ce44SJohn Forte  * lu - guid of the logical unit to offline
5394fcf3ce44SJohn Forte  */
5395fcf3ce44SJohn Forte int
5396fcf3ce44SJohn Forte stmfOfflineLogicalUnit(stmfGuid *lu)
5397fcf3ce44SJohn Forte {
5398fcf3ce44SJohn Forte 	stmf_state_desc_t luState;
5399fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5400fcf3ce44SJohn Forte 	int fd;
5401fcf3ce44SJohn Forte 
5402fcf3ce44SJohn Forte 	if (lu == NULL) {
5403fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5404fcf3ce44SJohn Forte 	}
5405fcf3ce44SJohn Forte 
5406fcf3ce44SJohn Forte 	bzero(&luState, sizeof (luState));
5407fcf3ce44SJohn Forte 
5408fcf3ce44SJohn Forte 	luState.state = STMF_STATE_OFFLINE;
5409fcf3ce44SJohn Forte 	bcopy(lu, &luState.ident, sizeof (stmfGuid));
5410fcf3ce44SJohn Forte 	/*
5411fcf3ce44SJohn Forte 	 * Open control node for stmf
5412fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5413fcf3ce44SJohn Forte 	 */
5414fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5415fcf3ce44SJohn Forte 		return (ret);
5416fcf3ce44SJohn Forte 	ret = setStmfState(fd, &luState, LOGICAL_UNIT_TYPE);
5417fcf3ce44SJohn Forte 	(void) close(fd);
5418fcf3ce44SJohn Forte 	return (ret);
5419fcf3ce44SJohn Forte }
5420fcf3ce44SJohn Forte 
5421fcf3ce44SJohn Forte /*
5422fcf3ce44SJohn Forte  * stmfOnlineTarget
5423fcf3ce44SJohn Forte  *
5424fcf3ce44SJohn Forte  * Purpose: Change state of target to online
5425fcf3ce44SJohn Forte  *
5426fcf3ce44SJohn Forte  * devid - devid of the target to online
5427fcf3ce44SJohn Forte  */
5428fcf3ce44SJohn Forte int
5429fcf3ce44SJohn Forte stmfOnlineTarget(stmfDevid *devid)
5430fcf3ce44SJohn Forte {
5431fcf3ce44SJohn Forte 	stmf_state_desc_t targetState;
5432fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5433fcf3ce44SJohn Forte 	int fd;
5434fcf3ce44SJohn Forte 
5435fcf3ce44SJohn Forte 	if (devid == NULL) {
5436fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5437fcf3ce44SJohn Forte 	}
5438fcf3ce44SJohn Forte 	bzero(&targetState, sizeof (targetState));
5439fcf3ce44SJohn Forte 
5440fcf3ce44SJohn Forte 	targetState.state = STMF_STATE_ONLINE;
5441fcf3ce44SJohn Forte 	targetState.ident[IDENT_LENGTH_BYTE] = devid->identLength;
5442fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetState.ident[IDENT_LENGTH_BYTE + 1],
5443fcf3ce44SJohn Forte 	    devid->identLength);
5444fcf3ce44SJohn Forte 	/*
5445fcf3ce44SJohn Forte 	 * Open control node for stmf
5446fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5447fcf3ce44SJohn Forte 	 */
5448fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5449fcf3ce44SJohn Forte 		return (ret);
5450fcf3ce44SJohn Forte 	ret = setStmfState(fd, &targetState, TARGET_TYPE);
5451fcf3ce44SJohn Forte 	(void) close(fd);
5452fcf3ce44SJohn Forte 	return (ret);
5453fcf3ce44SJohn Forte }
5454fcf3ce44SJohn Forte 
5455fcf3ce44SJohn Forte /*
5456fcf3ce44SJohn Forte  * stmfOnlineLogicalUnit
5457fcf3ce44SJohn Forte  *
5458fcf3ce44SJohn Forte  * Purpose: Change state of logical unit to online
5459fcf3ce44SJohn Forte  *
5460fcf3ce44SJohn Forte  * lu - guid of the logical unit to online
5461fcf3ce44SJohn Forte  */
5462fcf3ce44SJohn Forte int
5463fcf3ce44SJohn Forte stmfOnlineLogicalUnit(stmfGuid *lu)
5464fcf3ce44SJohn Forte {
5465fcf3ce44SJohn Forte 	stmf_state_desc_t luState;
5466fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5467fcf3ce44SJohn Forte 	int fd;
5468fcf3ce44SJohn Forte 
5469fcf3ce44SJohn Forte 	if (lu == NULL) {
5470fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5471fcf3ce44SJohn Forte 	}
5472fcf3ce44SJohn Forte 
5473fcf3ce44SJohn Forte 	bzero(&luState, sizeof (luState));
5474fcf3ce44SJohn Forte 
5475fcf3ce44SJohn Forte 	luState.state = STMF_STATE_ONLINE;
5476fcf3ce44SJohn Forte 	bcopy(lu, &luState.ident, sizeof (stmfGuid));
5477fcf3ce44SJohn Forte 	/*
5478fcf3ce44SJohn Forte 	 * Open control node for stmf
5479fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5480fcf3ce44SJohn Forte 	 */
5481fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5482fcf3ce44SJohn Forte 		return (ret);
5483fcf3ce44SJohn Forte 	ret = setStmfState(fd, &luState, LOGICAL_UNIT_TYPE);
5484fcf3ce44SJohn Forte 	(void) close(fd);
5485fcf3ce44SJohn Forte 	return (ret);
5486fcf3ce44SJohn Forte }
5487fcf3ce44SJohn Forte 
5488fcf3ce44SJohn Forte /*
5489fcf3ce44SJohn Forte  * stmfRemoveFromHostGroup
5490fcf3ce44SJohn Forte  *
5491fcf3ce44SJohn Forte  * Purpose: Removes an initiator from an initiator group
5492fcf3ce44SJohn Forte  *
5493fcf3ce44SJohn Forte  * hostGroupName - name of an initiator group
5494fcf3ce44SJohn Forte  * hostName - name of host group member to remove
5495fcf3ce44SJohn Forte  */
5496fcf3ce44SJohn Forte int
5497fcf3ce44SJohn Forte stmfRemoveFromHostGroup(stmfGroupName *hostGroupName, stmfDevid *hostName)
5498fcf3ce44SJohn Forte {
5499fcf3ce44SJohn Forte 	int ret;
5500fcf3ce44SJohn Forte 	int fd;
5501fcf3ce44SJohn Forte 
5502fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
5503fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
5504fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || hostName == NULL) {
5505fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5506fcf3ce44SJohn Forte 	}
5507fcf3ce44SJohn Forte 
5508fcf3ce44SJohn Forte 	/* call init */
5509fcf3ce44SJohn Forte 	ret = initializeConfig();
5510fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5511fcf3ce44SJohn Forte 		return (ret);
5512fcf3ce44SJohn Forte 	}
5513fcf3ce44SJohn Forte 
5514fcf3ce44SJohn Forte 	/*
5515fcf3ce44SJohn Forte 	 * Open control node for stmf
5516fcf3ce44SJohn Forte 	 */
5517fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5518fcf3ce44SJohn Forte 		return (ret);
5519fcf3ce44SJohn Forte 
5520fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_REMOVE_HG_ENTRY,
5521fcf3ce44SJohn Forte 	    hostGroupName, hostName)) != STMF_STATUS_SUCCESS) {
5522fcf3ce44SJohn Forte 		goto done;
5523fcf3ce44SJohn Forte 	}
5524fcf3ce44SJohn Forte 
55258fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
55268fe96085Stim szeto 		goto done;
55278fe96085Stim szeto 	}
55288fe96085Stim szeto 
5529fcf3ce44SJohn Forte 	ret = psRemoveHostGroupMember((char *)hostGroupName,
5530fcf3ce44SJohn Forte 	    (char *)hostName->ident);
5531fcf3ce44SJohn Forte 	switch (ret) {
5532fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5533fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5534fcf3ce44SJohn Forte 			break;
5535fcf3ce44SJohn Forte 		case STMF_PS_ERROR_MEMBER_NOT_FOUND:
5536fcf3ce44SJohn Forte 			ret = STMF_ERROR_MEMBER_NOT_FOUND;
5537fcf3ce44SJohn Forte 			break;
5538fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
5539fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
5540fcf3ce44SJohn Forte 			break;
5541fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5542fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5543fcf3ce44SJohn Forte 			break;
5544fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5545fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5546fcf3ce44SJohn Forte 			break;
5547fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5548fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5549fcf3ce44SJohn Forte 			break;
5550fcf3ce44SJohn Forte 		default:
5551fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
5552fcf3ce44SJohn Forte 			    "stmfRemoveFromHostGroup"
5553fcf3ce44SJohn Forte 			    "psRemoveHostGroupMember:error(%d)", ret);
5554fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5555fcf3ce44SJohn Forte 			break;
5556fcf3ce44SJohn Forte 	}
5557fcf3ce44SJohn Forte 
5558fcf3ce44SJohn Forte done:
5559fcf3ce44SJohn Forte 	(void) close(fd);
5560fcf3ce44SJohn Forte 	return (ret);
5561fcf3ce44SJohn Forte }
5562fcf3ce44SJohn Forte 
5563fcf3ce44SJohn Forte /*
5564fcf3ce44SJohn Forte  * stmfRemoveFromTargetGroup
5565fcf3ce44SJohn Forte  *
5566fcf3ce44SJohn Forte  * Purpose: Removes a local port from a local port group
5567fcf3ce44SJohn Forte  *
5568fcf3ce44SJohn Forte  * targetGroupName - name of a target group
5569fcf3ce44SJohn Forte  * targetName - name of target to remove
5570fcf3ce44SJohn Forte  */
5571fcf3ce44SJohn Forte int
5572fcf3ce44SJohn Forte stmfRemoveFromTargetGroup(stmfGroupName *targetGroupName, stmfDevid *targetName)
5573fcf3ce44SJohn Forte {
5574fcf3ce44SJohn Forte 	int ret;
5575fcf3ce44SJohn Forte 	int fd;
5576fcf3ce44SJohn Forte 
5577fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
5578fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
5579fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || targetName == NULL) {
5580fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5581fcf3ce44SJohn Forte 	}
5582fcf3ce44SJohn Forte 
5583fcf3ce44SJohn Forte 	/* call init */
5584fcf3ce44SJohn Forte 	ret = initializeConfig();
5585fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5586fcf3ce44SJohn Forte 		return (ret);
5587fcf3ce44SJohn Forte 	}
5588fcf3ce44SJohn Forte 
5589fcf3ce44SJohn Forte 	/*
5590fcf3ce44SJohn Forte 	 * Open control node for stmf
5591fcf3ce44SJohn Forte 	 */
5592fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5593fcf3ce44SJohn Forte 		return (ret);
5594fcf3ce44SJohn Forte 
5595fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_REMOVE_TG_ENTRY,
5596fcf3ce44SJohn Forte 	    targetGroupName, targetName)) != STMF_STATUS_SUCCESS) {
5597fcf3ce44SJohn Forte 		goto done;
5598fcf3ce44SJohn Forte 	}
5599fcf3ce44SJohn Forte 
56008fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
56018fe96085Stim szeto 		goto done;
56028fe96085Stim szeto 	}
56038fe96085Stim szeto 
5604fcf3ce44SJohn Forte 	ret = psRemoveTargetGroupMember((char *)targetGroupName,
5605fcf3ce44SJohn Forte 	    (char *)targetName->ident);
5606fcf3ce44SJohn Forte 	switch (ret) {
5607fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5608fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5609fcf3ce44SJohn Forte 			break;
5610fcf3ce44SJohn Forte 		case STMF_PS_ERROR_MEMBER_NOT_FOUND:
5611fcf3ce44SJohn Forte 			ret = STMF_ERROR_MEMBER_NOT_FOUND;
5612fcf3ce44SJohn Forte 			break;
5613fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
5614fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
5615fcf3ce44SJohn Forte 			break;
5616fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5617fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5618fcf3ce44SJohn Forte 			break;
5619fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5620fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5621fcf3ce44SJohn Forte 			break;
5622fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5623fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5624fcf3ce44SJohn Forte 			break;
5625fcf3ce44SJohn Forte 		default:
5626fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
5627fcf3ce44SJohn Forte 			    "stmfRemoveFromTargetGroup"
5628fcf3ce44SJohn Forte 			    "psRemoveTargetGroupMember:error(%d)", ret);
5629fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5630fcf3ce44SJohn Forte 			break;
5631fcf3ce44SJohn Forte 	}
5632fcf3ce44SJohn Forte 
5633fcf3ce44SJohn Forte done:
5634fcf3ce44SJohn Forte 	(void) close(fd);
5635fcf3ce44SJohn Forte 	return (ret);
5636fcf3ce44SJohn Forte }
5637fcf3ce44SJohn Forte 
5638fcf3ce44SJohn Forte /*
5639fcf3ce44SJohn Forte  * stmfRemoveViewEntry
5640fcf3ce44SJohn Forte  *
5641fcf3ce44SJohn Forte  * Purpose: Removes a view entry from a logical unit
5642fcf3ce44SJohn Forte  *
5643fcf3ce44SJohn Forte  * lu - guid of lu for which view entry is being removed
5644fcf3ce44SJohn Forte  * viewEntryIndex - index of view entry to remove
5645fcf3ce44SJohn Forte  *
5646fcf3ce44SJohn Forte  */
5647fcf3ce44SJohn Forte int
5648fcf3ce44SJohn Forte stmfRemoveViewEntry(stmfGuid *lu, uint32_t viewEntryIndex)
5649fcf3ce44SJohn Forte {
5650fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5651fcf3ce44SJohn Forte 	int fd;
5652fcf3ce44SJohn Forte 	int ioctlRet;
5653fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
5654fcf3ce44SJohn Forte 	stmf_view_op_entry_t ioctlViewEntry;
5655fcf3ce44SJohn Forte 
5656fcf3ce44SJohn Forte 	if (lu == NULL) {
5657fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5658fcf3ce44SJohn Forte 	}
5659fcf3ce44SJohn Forte 
5660fcf3ce44SJohn Forte 	/* call init */
5661fcf3ce44SJohn Forte 	ret = initializeConfig();
5662fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5663fcf3ce44SJohn Forte 		return (ret);
5664fcf3ce44SJohn Forte 	}
5665fcf3ce44SJohn Forte 
5666fcf3ce44SJohn Forte 	/*
5667fcf3ce44SJohn Forte 	 * Open control node for stmf
5668fcf3ce44SJohn Forte 	 */
5669fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5670fcf3ce44SJohn Forte 		return (ret);
5671fcf3ce44SJohn Forte 
5672fcf3ce44SJohn Forte 	bzero(&ioctlViewEntry, sizeof (ioctlViewEntry));
5673fcf3ce44SJohn Forte 	ioctlViewEntry.ve_ndx_valid = B_TRUE;
5674fcf3ce44SJohn Forte 	ioctlViewEntry.ve_ndx = viewEntryIndex;
5675fcf3ce44SJohn Forte 	bcopy(lu, &ioctlViewEntry.ve_guid, sizeof (stmfGuid));
5676fcf3ce44SJohn Forte 
5677fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
5678fcf3ce44SJohn Forte 	/*
5679fcf3ce44SJohn Forte 	 * Issue ioctl to add to the view entry
5680fcf3ce44SJohn Forte 	 */
5681fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
5682fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ioctlViewEntry);
5683fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ioctlViewEntry;
5684fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_REMOVE_VIEW_ENTRY, &stmfIoctl);
5685fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
5686fcf3ce44SJohn Forte 		switch (errno) {
5687fcf3ce44SJohn Forte 			case EBUSY:
5688fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
5689fcf3ce44SJohn Forte 				break;
56908fe96085Stim szeto 			case EPERM:
56918fe96085Stim szeto 				ret = STMF_ERROR_PERM;
56928fe96085Stim szeto 				break;
5693fcf3ce44SJohn Forte 			case EACCES:
5694fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
5695fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
5696fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
5697fcf3ce44SJohn Forte 						break;
5698fcf3ce44SJohn Forte 					default:
5699fcf3ce44SJohn Forte 						ret = STMF_ERROR_PERM;
5700fcf3ce44SJohn Forte 						break;
5701fcf3ce44SJohn Forte 				}
5702fcf3ce44SJohn Forte 				break;
5703fcf3ce44SJohn Forte 			case ENODEV:
5704fcf3ce44SJohn Forte 			case ENOENT:
5705fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
5706fcf3ce44SJohn Forte 				break;
5707fcf3ce44SJohn Forte 			default:
5708fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
5709fcf3ce44SJohn Forte 				    "stmfRemoveViewEntry:ioctl errno(%d)",
5710fcf3ce44SJohn Forte 				    errno);
5711fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
5712fcf3ce44SJohn Forte 				break;
5713fcf3ce44SJohn Forte 		}
5714fcf3ce44SJohn Forte 		goto done;
5715fcf3ce44SJohn Forte 	}
5716fcf3ce44SJohn Forte 
57178fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
57188fe96085Stim szeto 		goto done;
57198fe96085Stim szeto 	}
57208fe96085Stim szeto 
5721fcf3ce44SJohn Forte 	ret = psRemoveViewEntry(lu, viewEntryIndex);
5722fcf3ce44SJohn Forte 	switch (ret) {
5723fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5724fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5725fcf3ce44SJohn Forte 			break;
5726fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
5727fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
5728fcf3ce44SJohn Forte 			break;
5729fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5730fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5731fcf3ce44SJohn Forte 			break;
5732fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5733fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5734fcf3ce44SJohn Forte 			break;
5735fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5736fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5737fcf3ce44SJohn Forte 			break;
5738fcf3ce44SJohn Forte 		default:
5739fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
5740fcf3ce44SJohn Forte 			    "stmfRemoveViewEntry" "psRemoveViewEntry:error(%d)",
5741fcf3ce44SJohn Forte 			    ret);
5742fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5743fcf3ce44SJohn Forte 			break;
5744fcf3ce44SJohn Forte 	}
5745fcf3ce44SJohn Forte 
5746fcf3ce44SJohn Forte done:
5747fcf3ce44SJohn Forte 	(void) close(fd);
5748fcf3ce44SJohn Forte 	return (ret);
5749fcf3ce44SJohn Forte }
5750fcf3ce44SJohn Forte 
5751fcf3ce44SJohn Forte /*
5752fcf3ce44SJohn Forte  * stmfSetProviderData
5753fcf3ce44SJohn Forte  *
5754fcf3ce44SJohn Forte  * Purpose: set the provider data
5755fcf3ce44SJohn Forte  *
5756fcf3ce44SJohn Forte  * providerName - unique name of provider
5757fcf3ce44SJohn Forte  * nvl - nvlist to set
5758fcf3ce44SJohn Forte  * providerType - type of provider for which to set data
5759fcf3ce44SJohn Forte  *		STMF_LU_PROVIDER_TYPE
5760fcf3ce44SJohn Forte  *		STMF_PORT_PROVIDER_TYPE
5761fcf3ce44SJohn Forte  */
5762fcf3ce44SJohn Forte int
5763fcf3ce44SJohn Forte stmfSetProviderData(char *providerName, nvlist_t *nvl, int providerType)
5764fcf3ce44SJohn Forte {
5765fcf3ce44SJohn Forte 	return (stmfSetProviderDataProt(providerName, nvl, providerType,
5766fcf3ce44SJohn Forte 	    NULL));
5767fcf3ce44SJohn Forte }
5768fcf3ce44SJohn Forte 
5769fcf3ce44SJohn Forte /*
5770fcf3ce44SJohn Forte  * stmfSetProviderDataProt
5771fcf3ce44SJohn Forte  *
5772fcf3ce44SJohn Forte  * Purpose: set the provider data
5773fcf3ce44SJohn Forte  *
5774fcf3ce44SJohn Forte  * providerName - unique name of provider
5775fcf3ce44SJohn Forte  * nvl - nvlist to set
5776fcf3ce44SJohn Forte  * providerType - type of provider for which to set data
5777fcf3ce44SJohn Forte  *		STMF_LU_PROVIDER_TYPE
5778fcf3ce44SJohn Forte  *		STMF_PORT_PROVIDER_TYPE
5779fcf3ce44SJohn Forte  * setToken - Stale data token returned in the stmfGetProviderDataProt()
5780fcf3ce44SJohn Forte  *	      call or NULL.
5781fcf3ce44SJohn Forte  */
5782fcf3ce44SJohn Forte int
5783fcf3ce44SJohn Forte stmfSetProviderDataProt(char *providerName, nvlist_t *nvl, int providerType,
5784fcf3ce44SJohn Forte     uint64_t *setToken)
5785fcf3ce44SJohn Forte {
5786fcf3ce44SJohn Forte 	int ret;
5787fcf3ce44SJohn Forte 	int fd;
5788fcf3ce44SJohn Forte 
5789fcf3ce44SJohn Forte 	if (providerName == NULL || nvl == NULL) {
5790fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5791fcf3ce44SJohn Forte 	}
5792fcf3ce44SJohn Forte 
5793fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
5794fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
5795fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5796fcf3ce44SJohn Forte 	}
5797fcf3ce44SJohn Forte 
5798fcf3ce44SJohn Forte 	/* call init */
5799fcf3ce44SJohn Forte 	ret = initializeConfig();
5800fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5801fcf3ce44SJohn Forte 		return (ret);
5802fcf3ce44SJohn Forte 	}
5803fcf3ce44SJohn Forte 
5804fcf3ce44SJohn Forte 	/*
5805fcf3ce44SJohn Forte 	 * Open control node for stmf
5806fcf3ce44SJohn Forte 	 */
5807fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5808fcf3ce44SJohn Forte 		return (ret);
5809fcf3ce44SJohn Forte 
58108fe96085Stim szeto 	ret = setProviderData(fd, providerName, nvl, providerType, setToken);
5811fcf3ce44SJohn Forte 
5812fcf3ce44SJohn Forte 	(void) close(fd);
5813fcf3ce44SJohn Forte 
5814fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5815fcf3ce44SJohn Forte 		goto done;
5816fcf3ce44SJohn Forte 	}
5817fcf3ce44SJohn Forte 
58188fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
58198fe96085Stim szeto 		goto done;
58208fe96085Stim szeto 	}
58218fe96085Stim szeto 
5822fcf3ce44SJohn Forte 	/* setting driver provider data successful. Now persist it */
58238fe96085Stim szeto 	ret = psSetProviderData(providerName, nvl, providerType, NULL);
5824fcf3ce44SJohn Forte 	switch (ret) {
5825fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5826fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5827fcf3ce44SJohn Forte 			break;
5828fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
5829fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
5830fcf3ce44SJohn Forte 			break;
5831fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5832fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5833fcf3ce44SJohn Forte 			break;
5834fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5835fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5836fcf3ce44SJohn Forte 			break;
5837fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5838fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5839fcf3ce44SJohn Forte 			break;
5840fcf3ce44SJohn Forte 		case STMF_PS_ERROR_PROV_DATA_STALE:
5841fcf3ce44SJohn Forte 			ret = STMF_ERROR_PROV_DATA_STALE;
5842fcf3ce44SJohn Forte 			break;
5843fcf3ce44SJohn Forte 		default:
5844fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
5845fcf3ce44SJohn Forte 			    "stmfSetProviderData"
5846fcf3ce44SJohn Forte 			    "psSetProviderData:error(%d)", ret);
5847fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5848fcf3ce44SJohn Forte 			break;
5849fcf3ce44SJohn Forte 	}
5850fcf3ce44SJohn Forte 
5851fcf3ce44SJohn Forte done:
5852fcf3ce44SJohn Forte 	return (ret);
5853fcf3ce44SJohn Forte }
5854fcf3ce44SJohn Forte 
5855fcf3ce44SJohn Forte /*
58568fe96085Stim szeto  * getProviderData
58578fe96085Stim szeto  *
58588fe96085Stim szeto  * Purpose: set the provider data from stmf
58598fe96085Stim szeto  *
58608fe96085Stim szeto  * providerName - unique name of provider
58618fe96085Stim szeto  * nvl - nvlist to load/retrieve
58628fe96085Stim szeto  * providerType - logical unit or port provider
58638fe96085Stim szeto  * setToken - returned stale data token
58648fe96085Stim szeto  */
58658fe96085Stim szeto int
58668fe96085Stim szeto getProviderData(char *providerName, nvlist_t **nvl, int providerType,
58678fe96085Stim szeto     uint64_t *setToken)
58688fe96085Stim szeto {
58698fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
58708fe96085Stim szeto 	int fd;
58718fe96085Stim szeto 	int ioctlRet;
58728fe96085Stim szeto 	size_t nvlistSize = ALLOC_PP_DATA_SIZE;
58738fe96085Stim szeto 	int retryCnt = 0;
58748fe96085Stim szeto 	int retryCntMax = MAX_PROVIDER_RETRY;
58758fe96085Stim szeto 	stmf_ppioctl_data_t ppi = {0}, *ppi_out = NULL;
58768fe96085Stim szeto 	boolean_t retry = B_TRUE;
58778fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
58788fe96085Stim szeto 
58798fe96085Stim szeto 	if (providerName == NULL) {
58808fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
58818fe96085Stim szeto 	}
58828fe96085Stim szeto 
58838fe96085Stim szeto 	/*
58848fe96085Stim szeto 	 * Open control node for stmf
58858fe96085Stim szeto 	 */
58868fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
58878fe96085Stim szeto 		return (ret);
58888fe96085Stim szeto 
58898fe96085Stim szeto 	/* set provider name and provider type */
58908fe96085Stim szeto 	if (strlcpy(ppi.ppi_name, providerName,
58918fe96085Stim szeto 	    sizeof (ppi.ppi_name)) >=
58928fe96085Stim szeto 	    sizeof (ppi.ppi_name)) {
58938fe96085Stim szeto 		ret = STMF_ERROR_INVALID_ARG;
58948fe96085Stim szeto 		goto done;
58958fe96085Stim szeto 	}
58968fe96085Stim szeto 	switch (providerType) {
58978fe96085Stim szeto 		case STMF_LU_PROVIDER_TYPE:
58988fe96085Stim szeto 			ppi.ppi_lu_provider = 1;
58998fe96085Stim szeto 			break;
59008fe96085Stim szeto 		case STMF_PORT_PROVIDER_TYPE:
59018fe96085Stim szeto 			ppi.ppi_port_provider = 1;
59028fe96085Stim szeto 			break;
59038fe96085Stim szeto 		default:
59048fe96085Stim szeto 			ret = STMF_ERROR_INVALID_ARG;
59058fe96085Stim szeto 			goto done;
59068fe96085Stim szeto 	}
59078fe96085Stim szeto 
59088fe96085Stim szeto 	do {
59098fe96085Stim szeto 		/* allocate memory for ioctl */
59108fe96085Stim szeto 		ppi_out = (stmf_ppioctl_data_t *)calloc(1, nvlistSize +
59118fe96085Stim szeto 		    sizeof (stmf_ppioctl_data_t));
59128fe96085Stim szeto 		if (ppi_out == NULL) {
59138fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
59148fe96085Stim szeto 			goto done;
59158fe96085Stim szeto 
59168fe96085Stim szeto 		}
59178fe96085Stim szeto 
59188fe96085Stim szeto 		/* set the size of the ioctl data to allocated buffer */
59198fe96085Stim szeto 		ppi.ppi_data_size = nvlistSize;
59208fe96085Stim szeto 
59218fe96085Stim szeto 		bzero(&stmfIoctl, sizeof (stmfIoctl));
59228fe96085Stim szeto 
59238fe96085Stim szeto 		stmfIoctl.stmf_version = STMF_VERSION_1;
59248fe96085Stim szeto 		stmfIoctl.stmf_ibuf_size = sizeof (stmf_ppioctl_data_t);
59258fe96085Stim szeto 		stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ppi;
59268fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = sizeof (stmf_ppioctl_data_t) +
59278fe96085Stim szeto 		    nvlistSize;
59288fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)ppi_out;
59298fe96085Stim szeto 		ioctlRet = ioctl(fd, STMF_IOCTL_GET_PP_DATA, &stmfIoctl);
59308fe96085Stim szeto 		if (ioctlRet != 0) {
59318fe96085Stim szeto 			switch (errno) {
59328fe96085Stim szeto 				case EBUSY:
59338fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
59348fe96085Stim szeto 					break;
59358fe96085Stim szeto 				case EPERM:
59368fe96085Stim szeto 				case EACCES:
59378fe96085Stim szeto 					ret = STMF_ERROR_PERM;
59388fe96085Stim szeto 					break;
59398fe96085Stim szeto 				case EINVAL:
59408fe96085Stim szeto 					if (stmfIoctl.stmf_error ==
59418fe96085Stim szeto 					    STMF_IOCERR_INSUFFICIENT_BUF) {
59428fe96085Stim szeto 						nvlistSize =
59438fe96085Stim szeto 						    ppi_out->ppi_data_size;
59448fe96085Stim szeto 						free(ppi_out);
59458fe96085Stim szeto 						ppi_out = NULL;
59468fe96085Stim szeto 						if (retryCnt++ > retryCntMax) {
59478fe96085Stim szeto 							retry = B_FALSE;
59488fe96085Stim szeto 							ret = STMF_ERROR_BUSY;
59498fe96085Stim szeto 						} else {
59508fe96085Stim szeto 							ret =
59518fe96085Stim szeto 							    STMF_STATUS_SUCCESS;
59528fe96085Stim szeto 						}
59538fe96085Stim szeto 					} else {
59548fe96085Stim szeto 						syslog(LOG_DEBUG,
59558fe96085Stim szeto 						    "getProviderData:ioctl"
59568fe96085Stim szeto 						    "unable to retrieve "
59578fe96085Stim szeto 						    "nvlist");
59588fe96085Stim szeto 						ret = STMF_STATUS_ERROR;
59598fe96085Stim szeto 					}
59608fe96085Stim szeto 					break;
59618fe96085Stim szeto 				case ENOENT:
59628fe96085Stim szeto 					ret = STMF_ERROR_NOT_FOUND;
59638fe96085Stim szeto 					break;
59648fe96085Stim szeto 				default:
59658fe96085Stim szeto 					syslog(LOG_DEBUG,
59668fe96085Stim szeto 					    "getProviderData:ioctl errno(%d)",
59678fe96085Stim szeto 					    errno);
59688fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
59698fe96085Stim szeto 					break;
59708fe96085Stim szeto 			}
59718fe96085Stim szeto 			if (ret != STMF_STATUS_SUCCESS)
59728fe96085Stim szeto 				goto done;
59738fe96085Stim szeto 		}
59748fe96085Stim szeto 	} while (retry && stmfIoctl.stmf_error == STMF_IOCERR_INSUFFICIENT_BUF);
59758fe96085Stim szeto 
59768fe96085Stim szeto 	if ((ret = nvlist_unpack((char *)ppi_out->ppi_data,
59778fe96085Stim szeto 	    ppi_out->ppi_data_size, nvl, 0)) != 0) {
59788fe96085Stim szeto 		ret = STMF_STATUS_ERROR;
59798fe96085Stim szeto 		goto done;
59808fe96085Stim szeto 	}
59818fe96085Stim szeto 
59828fe96085Stim szeto 	/* caller has asked for new token */
59838fe96085Stim szeto 	if (setToken) {
59848fe96085Stim szeto 		*setToken = ppi_out->ppi_token;
59858fe96085Stim szeto 	}
59868fe96085Stim szeto done:
59878fe96085Stim szeto 	free(ppi_out);
59888fe96085Stim szeto 	(void) close(fd);
59898fe96085Stim szeto 	return (ret);
59908fe96085Stim szeto }
59918fe96085Stim szeto 
59928fe96085Stim szeto /*
5993fcf3ce44SJohn Forte  * setProviderData
5994fcf3ce44SJohn Forte  *
59958fe96085Stim szeto  * Purpose: set the provider data in stmf
5996fcf3ce44SJohn Forte  *
5997fcf3ce44SJohn Forte  * providerName - unique name of provider
5998fcf3ce44SJohn Forte  * nvl - nvlist to set
5999fcf3ce44SJohn Forte  * providerType - logical unit or port provider
60008fe96085Stim szeto  * setToken - stale data token to check if not NULL
6001fcf3ce44SJohn Forte  */
6002fcf3ce44SJohn Forte static int
60038fe96085Stim szeto setProviderData(int fd, char *providerName, nvlist_t *nvl, int providerType,
60048fe96085Stim szeto     uint64_t *setToken)
6005fcf3ce44SJohn Forte {
6006fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
6007fcf3ce44SJohn Forte 	int ioctlRet;
6008fcf3ce44SJohn Forte 	size_t nvlistEncodedSize;
6009fcf3ce44SJohn Forte 	stmf_ppioctl_data_t *ppi = NULL;
60108fe96085Stim szeto 	uint64_t outToken;
6011fcf3ce44SJohn Forte 	char *allocatedNvBuffer;
6012fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
6013fcf3ce44SJohn Forte 
6014fcf3ce44SJohn Forte 	if (providerName == NULL) {
6015fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
6016fcf3ce44SJohn Forte 	}
6017fcf3ce44SJohn Forte 
6018fcf3ce44SJohn Forte 	/* get size of encoded nvlist */
6019fcf3ce44SJohn Forte 	if (nvlist_size(nvl, &nvlistEncodedSize, NV_ENCODE_XDR) != 0) {
6020fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
6021fcf3ce44SJohn Forte 	}
6022fcf3ce44SJohn Forte 
6023fcf3ce44SJohn Forte 	/* allocate memory for ioctl */
6024fcf3ce44SJohn Forte 	ppi = (stmf_ppioctl_data_t *)calloc(1, nvlistEncodedSize +
6025fcf3ce44SJohn Forte 	    sizeof (stmf_ppioctl_data_t));
6026fcf3ce44SJohn Forte 	if (ppi == NULL) {
6027fcf3ce44SJohn Forte 		return (STMF_ERROR_NOMEM);
6028fcf3ce44SJohn Forte 	}
6029fcf3ce44SJohn Forte 
60308fe96085Stim szeto 	if (setToken) {
60318fe96085Stim szeto 		ppi->ppi_token_valid = 1;
60328fe96085Stim szeto 		ppi->ppi_token = *setToken;
60338fe96085Stim szeto 	}
60348fe96085Stim szeto 
6035fcf3ce44SJohn Forte 	allocatedNvBuffer = (char *)&ppi->ppi_data;
6036fcf3ce44SJohn Forte 	if (nvlist_pack(nvl, &allocatedNvBuffer, &nvlistEncodedSize,
6037fcf3ce44SJohn Forte 	    NV_ENCODE_XDR, 0) != 0) {
6038fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
6039fcf3ce44SJohn Forte 	}
6040fcf3ce44SJohn Forte 
6041fcf3ce44SJohn Forte 	/* set provider name and provider type */
6042fcf3ce44SJohn Forte 	(void) strncpy(ppi->ppi_name, providerName, sizeof (ppi->ppi_name));
6043fcf3ce44SJohn Forte 	switch (providerType) {
6044fcf3ce44SJohn Forte 		case STMF_LU_PROVIDER_TYPE:
6045fcf3ce44SJohn Forte 			ppi->ppi_lu_provider = 1;
6046fcf3ce44SJohn Forte 			break;
6047fcf3ce44SJohn Forte 		case STMF_PORT_PROVIDER_TYPE:
6048fcf3ce44SJohn Forte 			ppi->ppi_port_provider = 1;
6049fcf3ce44SJohn Forte 			break;
6050fcf3ce44SJohn Forte 		default:
6051fcf3ce44SJohn Forte 			return (STMF_ERROR_INVALID_ARG);
6052fcf3ce44SJohn Forte 	}
6053fcf3ce44SJohn Forte 
6054fcf3ce44SJohn Forte 	/* set the size of the ioctl data to packed data size */
6055fcf3ce44SJohn Forte 	ppi->ppi_data_size = nvlistEncodedSize;
6056fcf3ce44SJohn Forte 
6057fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
6058fcf3ce44SJohn Forte 
6059fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
6060fcf3ce44SJohn Forte 	/*
6061fcf3ce44SJohn Forte 	 * Subtracting 8 from the size as that is the size of the last member
6062fcf3ce44SJohn Forte 	 * of the structure where the packed data resides
6063fcf3ce44SJohn Forte 	 */
6064fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = nvlistEncodedSize +
6065fcf3ce44SJohn Forte 	    sizeof (stmf_ppioctl_data_t) - 8;
6066fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)ppi;
60678fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = sizeof (uint64_t);
60688fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&outToken;
6069fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_LOAD_PP_DATA, &stmfIoctl);
6070fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
6071fcf3ce44SJohn Forte 		switch (errno) {
6072fcf3ce44SJohn Forte 			case EBUSY:
6073fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
6074fcf3ce44SJohn Forte 				break;
60758fe96085Stim szeto 			case EPERM:
6076fcf3ce44SJohn Forte 			case EACCES:
6077fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
6078fcf3ce44SJohn Forte 				break;
60798fe96085Stim szeto 			case EINVAL:
60808fe96085Stim szeto 				if (stmfIoctl.stmf_error ==
60818fe96085Stim szeto 				    STMF_IOCERR_PPD_UPDATED) {
60828fe96085Stim szeto 					ret = STMF_ERROR_PROV_DATA_STALE;
60838fe96085Stim szeto 				} else {
60848fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
60858fe96085Stim szeto 				}
60868fe96085Stim szeto 				break;
6087fcf3ce44SJohn Forte 			default:
6088fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
6089fcf3ce44SJohn Forte 				    "setProviderData:ioctl errno(%d)", errno);
6090fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
6091fcf3ce44SJohn Forte 				break;
6092fcf3ce44SJohn Forte 		}
6093fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS)
6094fcf3ce44SJohn Forte 			goto done;
6095fcf3ce44SJohn Forte 	}
6096fcf3ce44SJohn Forte 
60978fe96085Stim szeto 	/* caller has asked for new token */
60988fe96085Stim szeto 	if (setToken) {
60998fe96085Stim szeto 		*setToken = outToken;
61008fe96085Stim szeto 	}
6101fcf3ce44SJohn Forte done:
6102fcf3ce44SJohn Forte 	free(ppi);
6103fcf3ce44SJohn Forte 	return (ret);
6104fcf3ce44SJohn Forte }
61058fe96085Stim szeto 
61068fe96085Stim szeto /*
61078fe96085Stim szeto  * set the persistence method in the library only or library and service
61088fe96085Stim szeto  */
61098fe96085Stim szeto int
61108fe96085Stim szeto stmfSetPersistMethod(uint8_t persistType, boolean_t serviceSet)
61118fe96085Stim szeto {
61128fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
61138fe96085Stim szeto 	int oldPersist;
61148fe96085Stim szeto 
61158fe96085Stim szeto 	(void) pthread_mutex_lock(&persistenceTypeLock);
61168fe96085Stim szeto 	oldPersist = iPersistType;
61178fe96085Stim szeto 	if (persistType == STMF_PERSIST_NONE ||
61188fe96085Stim szeto 	    persistType == STMF_PERSIST_SMF) {
61198fe96085Stim szeto 		iLibSetPersist = B_TRUE;
61208fe96085Stim szeto 		iPersistType = persistType;
61218fe96085Stim szeto 	} else {
61228fe96085Stim szeto 		(void) pthread_mutex_unlock(&persistenceTypeLock);
61238fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
61248fe96085Stim szeto 	}
61258fe96085Stim szeto 	/* Is this for this library open or in SMF */
61268fe96085Stim szeto 	if (serviceSet == B_TRUE) {
61278fe96085Stim szeto 		ret = psSetServicePersist(persistType);
61288fe96085Stim szeto 		if (ret != STMF_PS_SUCCESS) {
61298fe96085Stim szeto 			ret = STMF_ERROR_PERSIST_TYPE;
61308fe96085Stim szeto 			/* Set to old value */
61318fe96085Stim szeto 			iPersistType = oldPersist;
61328fe96085Stim szeto 		}
61338fe96085Stim szeto 	}
61348fe96085Stim szeto 	(void) pthread_mutex_unlock(&persistenceTypeLock);
61358fe96085Stim szeto 
61368fe96085Stim szeto 	return (ret);
61378fe96085Stim szeto }
61388fe96085Stim szeto 
61398fe96085Stim szeto /*
61408fe96085Stim szeto  * Only returns internal state for persist. If unset, goes to ps. If that
61418fe96085Stim szeto  * fails, returns default setting
61428fe96085Stim szeto  */
61438fe96085Stim szeto static uint8_t
61448fe96085Stim szeto iGetPersistMethod()
61458fe96085Stim szeto {
61468fe96085Stim szeto 
61478fe96085Stim szeto 	uint8_t persistType = 0;
61488fe96085Stim szeto 
61498fe96085Stim szeto 	(void) pthread_mutex_lock(&persistenceTypeLock);
61508fe96085Stim szeto 	if (iLibSetPersist) {
61518fe96085Stim szeto 		persistType = iPersistType;
61528fe96085Stim szeto 	} else {
61538fe96085Stim szeto 		int ret;
61548fe96085Stim szeto 		ret = psGetServicePersist(&persistType);
61558fe96085Stim szeto 		if (ret != STMF_PS_SUCCESS) {
61568fe96085Stim szeto 			/* set to default */
61578fe96085Stim szeto 			persistType = STMF_DEFAULT_PERSIST;
61588fe96085Stim szeto 		}
61598fe96085Stim szeto 	}
61608fe96085Stim szeto 	(void) pthread_mutex_unlock(&persistenceTypeLock);
61618fe96085Stim szeto 	return (persistType);
61628fe96085Stim szeto }
61638fe96085Stim szeto 
61648fe96085Stim szeto /*
61658fe96085Stim szeto  * Returns either library state or persistent config state depending on
61668fe96085Stim szeto  * serviceState
61678fe96085Stim szeto  */
61688fe96085Stim szeto int
61698fe96085Stim szeto stmfGetPersistMethod(uint8_t *persistType, boolean_t serviceState)
61708fe96085Stim szeto {
61718fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
61728fe96085Stim szeto 
61738fe96085Stim szeto 	if (persistType == NULL) {
61748fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
61758fe96085Stim szeto 	}
61768fe96085Stim szeto 	if (serviceState) {
61778fe96085Stim szeto 		ret = psGetServicePersist(persistType);
61788fe96085Stim szeto 		if (ret != STMF_PS_SUCCESS) {
61798fe96085Stim szeto 			ret = STMF_ERROR_PERSIST_TYPE;
61808fe96085Stim szeto 		}
61818fe96085Stim szeto 	} else {
61828fe96085Stim szeto 		(void) pthread_mutex_lock(&persistenceTypeLock);
61838fe96085Stim szeto 		if (iLibSetPersist) {
61848fe96085Stim szeto 			*persistType = iPersistType;
61858fe96085Stim szeto 		} else {
61868fe96085Stim szeto 			*persistType = STMF_DEFAULT_PERSIST;
61878fe96085Stim szeto 		}
61888fe96085Stim szeto 		(void) pthread_mutex_unlock(&persistenceTypeLock);
61898fe96085Stim szeto 	}
61908fe96085Stim szeto 
61918fe96085Stim szeto 	return (ret);
61928fe96085Stim szeto }
619363ed874aStim szeto 
619463ed874aStim szeto /*
619545039663SJohn Forte  * stmfPostProxyMsg
619645039663SJohn Forte  *
619745039663SJohn Forte  * Purpose: Post a message to the proxy port provider
619845039663SJohn Forte  *
619945039663SJohn Forte  * buf - buffer containing message to post
620045039663SJohn Forte  * buflen - buffer length
620145039663SJohn Forte  */
620245039663SJohn Forte int
620345039663SJohn Forte stmfPostProxyMsg(int hdl, void *buf, uint32_t buflen)
620445039663SJohn Forte {
620545039663SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
620645039663SJohn Forte 	int ioctlRet;
620745039663SJohn Forte 	pppt_iocdata_t ppptIoctl = {0};
620845039663SJohn Forte 
620945039663SJohn Forte 	if (buf == NULL) {
621045039663SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
621145039663SJohn Forte 	}
621245039663SJohn Forte 
621345039663SJohn Forte 	/*
621445039663SJohn Forte 	 * Issue ioctl to post the message
621545039663SJohn Forte 	 */
621645039663SJohn Forte 	ppptIoctl.pppt_version = PPPT_VERSION_1;
621745039663SJohn Forte 	ppptIoctl.pppt_buf_size = buflen;
621845039663SJohn Forte 	ppptIoctl.pppt_buf = (uint64_t)(unsigned long)buf;
621945039663SJohn Forte 	ioctlRet = ioctl(hdl, PPPT_MESSAGE, &ppptIoctl);
622045039663SJohn Forte 	if (ioctlRet != 0) {
622145039663SJohn Forte 		switch (errno) {
622245039663SJohn Forte 			case EPERM:
622345039663SJohn Forte 			case EACCES:
622445039663SJohn Forte 				ret = STMF_ERROR_PERM;
622545039663SJohn Forte 				break;
622645039663SJohn Forte 			default:
622745039663SJohn Forte 				ret = STMF_ERROR_POST_MSG_FAILED;
622845039663SJohn Forte 				break;
622945039663SJohn Forte 		}
623045039663SJohn Forte 	}
623145039663SJohn Forte 
623245039663SJohn Forte 	return (ret);
623345039663SJohn Forte }
623445039663SJohn Forte 
623545039663SJohn Forte /*
623645039663SJohn Forte  * stmfInitProxyDoor
623745039663SJohn Forte  *
623845039663SJohn Forte  * Purpose: Install door in proxy
623945039663SJohn Forte  *
624045039663SJohn Forte  * hdl - pointer to returned handle
624145039663SJohn Forte  * fd - door from door_create()
624245039663SJohn Forte  */
624345039663SJohn Forte int
624445039663SJohn Forte stmfInitProxyDoor(int *hdl, int door)
624545039663SJohn Forte {
624645039663SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
624745039663SJohn Forte 	int ioctlRet;
624845039663SJohn Forte 	int fd;
624945039663SJohn Forte 	pppt_iocdata_t ppptIoctl = {0};
625045039663SJohn Forte 
625145039663SJohn Forte 	if (hdl == NULL) {
625245039663SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
625345039663SJohn Forte 	}
625445039663SJohn Forte 
625545039663SJohn Forte 	/*
625645039663SJohn Forte 	 * Open control node for pppt
625745039663SJohn Forte 	 */
625845039663SJohn Forte 	if ((ret = openPppt(OPEN_PPPT, &fd)) != STMF_STATUS_SUCCESS) {
625945039663SJohn Forte 		return (ret);
626045039663SJohn Forte 	}
626145039663SJohn Forte 
626245039663SJohn Forte 	/*
626345039663SJohn Forte 	 * Issue ioctl to install the door
626445039663SJohn Forte 	 */
626545039663SJohn Forte 	ppptIoctl.pppt_version = PPPT_VERSION_1;
626645039663SJohn Forte 	ppptIoctl.pppt_door_fd = (uint32_t)door;
626745039663SJohn Forte 	ioctlRet = ioctl(fd, PPPT_INSTALL_DOOR, &ppptIoctl);
626845039663SJohn Forte 	if (ioctlRet != 0) {
626945039663SJohn Forte 		switch (errno) {
627045039663SJohn Forte 			case EPERM:
627145039663SJohn Forte 			case EACCES:
627245039663SJohn Forte 				ret = STMF_ERROR_PERM;
627345039663SJohn Forte 				break;
627445039663SJohn Forte 			case EINVAL:
627545039663SJohn Forte 				ret = STMF_ERROR_INVALID_ARG;
627645039663SJohn Forte 				break;
627745039663SJohn Forte 			case EBUSY:
627845039663SJohn Forte 				ret = STMF_ERROR_DOOR_INSTALLED;
627945039663SJohn Forte 				break;
628045039663SJohn Forte 			default:
628145039663SJohn Forte 				ret = STMF_STATUS_ERROR;
628245039663SJohn Forte 				break;
628345039663SJohn Forte 		}
628445039663SJohn Forte 	}
628545039663SJohn Forte 
628645039663SJohn Forte 	/* return driver fd to caller */
628745039663SJohn Forte 	*hdl = fd;
628845039663SJohn Forte 	return (ret);
628945039663SJohn Forte }
629045039663SJohn Forte 
629145039663SJohn Forte void
629245039663SJohn Forte stmfDestroyProxyDoor(int hdl)
629345039663SJohn Forte {
629445039663SJohn Forte 	(void) close(hdl);
629545039663SJohn Forte }
629645039663SJohn Forte 
629745039663SJohn Forte /*
629863ed874aStim szeto  * validateLunNumIoctl
629963ed874aStim szeto  *
630063ed874aStim szeto  * Purpose: Issues ioctl to check and get available lun# in view entry
630163ed874aStim szeto  *
630263ed874aStim szeto  * viewEntry - view entry to use
630363ed874aStim szeto  */
630463ed874aStim szeto static int
630563ed874aStim szeto validateLunNumIoctl(int fd, stmfViewEntry *viewEntry)
630663ed874aStim szeto {
630763ed874aStim szeto 	int ret = STMF_STATUS_SUCCESS;
630863ed874aStim szeto 	int ioctlRet;
630963ed874aStim szeto 	stmf_iocdata_t stmfIoctl;
631063ed874aStim szeto 	stmf_view_op_entry_t ioctlViewEntry;
631163ed874aStim szeto 
631263ed874aStim szeto 	bzero(&ioctlViewEntry, sizeof (ioctlViewEntry));
631363ed874aStim szeto 	/*
631463ed874aStim szeto 	 * don't set ve_ndx or ve_ndx_valid as ve_ndx_valid should be
631563ed874aStim szeto 	 * false on input
631663ed874aStim szeto 	 */
631763ed874aStim szeto 	ioctlViewEntry.ve_lu_number_valid = viewEntry->luNbrValid;
631863ed874aStim szeto 	ioctlViewEntry.ve_all_hosts = viewEntry->allHosts;
631963ed874aStim szeto 	ioctlViewEntry.ve_all_targets = viewEntry->allTargets;
632063ed874aStim szeto 
632163ed874aStim szeto 	if (viewEntry->allHosts == B_FALSE) {
632263ed874aStim szeto 		bcopy(viewEntry->hostGroup, &ioctlViewEntry.ve_host_group.name,
632363ed874aStim szeto 		    sizeof (stmfGroupName));
632463ed874aStim szeto 		ioctlViewEntry.ve_host_group.name_size =
632563ed874aStim szeto 		    strlen((char *)viewEntry->hostGroup);
632663ed874aStim szeto 	}
632763ed874aStim szeto 	if (viewEntry->allTargets == B_FALSE) {
632863ed874aStim szeto 		bcopy(viewEntry->targetGroup,
632963ed874aStim szeto 		    &ioctlViewEntry.ve_target_group.name,
633063ed874aStim szeto 		    sizeof (stmfGroupName));
633163ed874aStim szeto 		ioctlViewEntry.ve_target_group.name_size =
633263ed874aStim szeto 		    strlen((char *)viewEntry->targetGroup);
633363ed874aStim szeto 	}
633463ed874aStim szeto 	/* Validating the lun number */
633563ed874aStim szeto 	if (viewEntry->luNbrValid) {
633663ed874aStim szeto 		bcopy(viewEntry->luNbr, &ioctlViewEntry.ve_lu_nbr,
633763ed874aStim szeto 		    sizeof (ioctlViewEntry.ve_lu_nbr));
633863ed874aStim szeto 	}
633963ed874aStim szeto 
634063ed874aStim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
634163ed874aStim szeto 	/*
634263ed874aStim szeto 	 * Issue ioctl to validate lun# in the view entry
634363ed874aStim szeto 	 */
634463ed874aStim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
634563ed874aStim szeto 	stmfIoctl.stmf_ibuf_size = sizeof (ioctlViewEntry);
634663ed874aStim szeto 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ioctlViewEntry;
634763ed874aStim szeto 	stmfIoctl.stmf_obuf_size = sizeof (ioctlViewEntry);
634863ed874aStim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&ioctlViewEntry;
634963ed874aStim szeto 	ioctlRet = ioctl(fd, STMF_IOCTL_VALIDATE_VIEW, &stmfIoctl);
635063ed874aStim szeto 
635163ed874aStim szeto 	/* save available lun number */
635263ed874aStim szeto 	if (!viewEntry->luNbrValid) {
635363ed874aStim szeto 		bcopy(ioctlViewEntry.ve_lu_nbr, viewEntry->luNbr,
635463ed874aStim szeto 		    sizeof (ioctlViewEntry.ve_lu_nbr));
635563ed874aStim szeto 	}
635663ed874aStim szeto 	if (ioctlRet != 0) {
635763ed874aStim szeto 		switch (errno) {
635863ed874aStim szeto 			case EBUSY:
635963ed874aStim szeto 				ret = STMF_ERROR_BUSY;
636063ed874aStim szeto 				break;
636163ed874aStim szeto 			case EPERM:
636263ed874aStim szeto 				ret = STMF_ERROR_PERM;
636363ed874aStim szeto 				break;
636463ed874aStim szeto 			case EACCES:
636563ed874aStim szeto 				switch (stmfIoctl.stmf_error) {
636663ed874aStim szeto 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
636763ed874aStim szeto 						ret = STMF_ERROR_CONFIG_NONE;
636863ed874aStim szeto 						break;
636963ed874aStim szeto 					default:
637063ed874aStim szeto 						ret = STMF_ERROR_PERM;
637163ed874aStim szeto 						break;
637263ed874aStim szeto 				}
637363ed874aStim szeto 				break;
637463ed874aStim szeto 			default:
637563ed874aStim szeto 				switch (stmfIoctl.stmf_error) {
637663ed874aStim szeto 					case STMF_IOCERR_LU_NUMBER_IN_USE:
637763ed874aStim szeto 						ret = STMF_ERROR_LUN_IN_USE;
637863ed874aStim szeto 						break;
637963ed874aStim szeto 					case STMF_IOCERR_VIEW_ENTRY_CONFLICT:
638063ed874aStim szeto 						ret = STMF_ERROR_VE_CONFLICT;
638163ed874aStim szeto 						break;
638263ed874aStim szeto 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
638363ed874aStim szeto 						ret = STMF_ERROR_CONFIG_NONE;
638463ed874aStim szeto 						break;
638563ed874aStim szeto 					case STMF_IOCERR_INVALID_HG:
638663ed874aStim szeto 						ret = STMF_ERROR_INVALID_HG;
638763ed874aStim szeto 						break;
638863ed874aStim szeto 					case STMF_IOCERR_INVALID_TG:
638963ed874aStim szeto 						ret = STMF_ERROR_INVALID_TG;
639063ed874aStim szeto 						break;
639163ed874aStim szeto 					default:
639263ed874aStim szeto 						syslog(LOG_DEBUG,
639363ed874aStim szeto 						    "addViewEntryIoctl"
639463ed874aStim szeto 						    ":error(%d)",
639563ed874aStim szeto 						    stmfIoctl.stmf_error);
639663ed874aStim szeto 						ret = STMF_STATUS_ERROR;
639763ed874aStim szeto 						break;
639863ed874aStim szeto 				}
639963ed874aStim szeto 				break;
640063ed874aStim szeto 		}
640163ed874aStim szeto 	}
640263ed874aStim szeto 	return (ret);
640363ed874aStim szeto }
640463ed874aStim szeto 
640563ed874aStim szeto /*
640663ed874aStim szeto  * stmfValidateView
640763ed874aStim szeto  *
640863ed874aStim szeto  * Purpose: Validate or get lun # base on TG, HG of view entry
640963ed874aStim szeto  *
641063ed874aStim szeto  * viewEntry - view entry structure to use
641163ed874aStim szeto  */
641263ed874aStim szeto int
641363ed874aStim szeto stmfValidateView(stmfViewEntry *viewEntry)
641463ed874aStim szeto {
641563ed874aStim szeto 	int ret;
641663ed874aStim szeto 	int fd;
641763ed874aStim szeto 	stmfViewEntry iViewEntry;
641863ed874aStim szeto 
641963ed874aStim szeto 	if (viewEntry == NULL) {
642063ed874aStim szeto 		return (STMF_ERROR_INVALID_ARG);
642163ed874aStim szeto 	}
642263ed874aStim szeto 
642363ed874aStim szeto 	/* initialize and set internal view entry */
642463ed874aStim szeto 	bzero(&iViewEntry, sizeof (iViewEntry));
642563ed874aStim szeto 
642663ed874aStim szeto 	if (!viewEntry->allHosts) {
642763ed874aStim szeto 		bcopy(viewEntry->hostGroup, iViewEntry.hostGroup,
642863ed874aStim szeto 		    sizeof (iViewEntry.hostGroup));
642963ed874aStim szeto 	} else {
643063ed874aStim szeto 		iViewEntry.allHosts = B_TRUE;
643163ed874aStim szeto 	}
643263ed874aStim szeto 
643363ed874aStim szeto 	if (!viewEntry->allTargets) {
643463ed874aStim szeto 		bcopy(viewEntry->targetGroup, iViewEntry.targetGroup,
643563ed874aStim szeto 		    sizeof (iViewEntry.targetGroup));
643663ed874aStim szeto 	} else {
643763ed874aStim szeto 		iViewEntry.allTargets = B_TRUE;
643863ed874aStim szeto 	}
643963ed874aStim szeto 
644063ed874aStim szeto 	if (viewEntry->luNbrValid) {
644163ed874aStim szeto 		iViewEntry.luNbrValid = B_TRUE;
644263ed874aStim szeto 		bcopy(viewEntry->luNbr, iViewEntry.luNbr,
644363ed874aStim szeto 		    sizeof (iViewEntry.luNbr));
644463ed874aStim szeto 	}
644563ed874aStim szeto 
644663ed874aStim szeto 	/*
644763ed874aStim szeto 	 * set users return view entry index valid flag to false
644863ed874aStim szeto 	 * in case of failure
644963ed874aStim szeto 	 */
645063ed874aStim szeto 	viewEntry->veIndexValid = B_FALSE;
645163ed874aStim szeto 
645263ed874aStim szeto 	/* Check to ensure service exists */
645363ed874aStim szeto 	if (psCheckService() != STMF_STATUS_SUCCESS) {
645463ed874aStim szeto 		return (STMF_ERROR_SERVICE_NOT_FOUND);
645563ed874aStim szeto 	}
645663ed874aStim szeto 
645763ed874aStim szeto 	/* call init */
645863ed874aStim szeto 	ret = initializeConfig();
645963ed874aStim szeto 	if (ret != STMF_STATUS_SUCCESS) {
646063ed874aStim szeto 		return (ret);
646163ed874aStim szeto 	}
646263ed874aStim szeto 
646363ed874aStim szeto 	/*
646463ed874aStim szeto 	 * Open control node for stmf
646563ed874aStim szeto 	 */
646663ed874aStim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
646763ed874aStim szeto 		return (ret);
646863ed874aStim szeto 
646963ed874aStim szeto 	/*
647063ed874aStim szeto 	 * Validate lun# in the view entry from the driver
647163ed874aStim szeto 	 */
647263ed874aStim szeto 	ret = validateLunNumIoctl(fd, &iViewEntry);
647363ed874aStim szeto 	(void) close(fd);
647463ed874aStim szeto 
647563ed874aStim szeto 	/* save available lun number */
647663ed874aStim szeto 	if (!viewEntry->luNbrValid) {
647763ed874aStim szeto 		bcopy(iViewEntry.luNbr, viewEntry->luNbr,
647863ed874aStim szeto 		    sizeof (iViewEntry.luNbr));
647963ed874aStim szeto 	}
648063ed874aStim szeto 
648163ed874aStim szeto 	return (ret);
648263ed874aStim szeto }
6483