xref: /titanic_51/usr/src/lib/libstmf/common/stmf.c (revision 5b0f0869cc90cee54c7aaef2233f2743c456a6c3)
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 /*
22a1837c6dSsrivijitha dugganapalli  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23fcf3ce44SJohn Forte  */
24fcf3ce44SJohn Forte 
25fcf3ce44SJohn Forte #include <stdlib.h>
26fcf3ce44SJohn Forte #include <stdio.h>
27fcf3ce44SJohn Forte #include <wchar.h>
28fcf3ce44SJohn Forte #include <strings.h>
29fcf3ce44SJohn Forte #include <sys/types.h>
30fcf3ce44SJohn Forte #include <sys/stat.h>
31fcf3ce44SJohn Forte #include <fcntl.h>
32fcf3ce44SJohn Forte #include <unistd.h>
33fcf3ce44SJohn Forte #include <libintl.h>
34fcf3ce44SJohn Forte #include <errno.h>
35fcf3ce44SJohn Forte #include <string.h>
36fcf3ce44SJohn Forte #include <assert.h>
37fcf3ce44SJohn Forte #include <libnvpair.h>
38fcf3ce44SJohn Forte #include <pthread.h>
39fcf3ce44SJohn Forte #include <syslog.h>
40fcf3ce44SJohn Forte #include <libstmf.h>
41fcf3ce44SJohn Forte #include <netinet/in.h>
42fcf3ce44SJohn Forte #include <inttypes.h>
43fcf3ce44SJohn Forte #include <store.h>
44fcf3ce44SJohn Forte #include <locale.h>
458fe96085Stim szeto #include <math.h>
468fe96085Stim szeto #include <libstmf_impl.h>
47fcf3ce44SJohn Forte #include <sys/stmf_ioctl.h>
488fe96085Stim szeto #include <sys/stmf_sbd_ioctl.h>
4945039663SJohn Forte #include <sys/pppt_ioctl.h>
507beff157SJohn Forte #include <macros.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
62fdcc480aSJohn Forte #define	HOST_ID_ASCII_SIZE 8
638fe96085Stim szeto #define	OUI_SIZE 3
64fdcc480aSJohn 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 *);
1277beff157SJohn Forte static int getDiskGlobalProp(uint32_t prop, char *propVal, size_t *propLen);
1288fe96085Stim szeto static int checkHexUpper(char *);
1298fe96085Stim szeto static int strToShift(const char *);
1308fe96085Stim szeto static int niceStrToNum(const char *, uint64_t *);
1318fe96085Stim szeto static void diskError(uint32_t, int *);
1328fe96085Stim szeto static int importDiskLu(char *fname, stmfGuid *);
1338fe96085Stim szeto static int modifyDiskLu(diskResource *, stmfGuid *, const char *);
1348fe96085Stim szeto static int modifyDiskLuProp(stmfGuid *, const char *, uint32_t, const char *);
1358fe96085Stim szeto static int validateModifyDiskProp(uint32_t);
1368fe96085Stim szeto static uint8_t iGetPersistMethod();
1378fe96085Stim szeto static int groupListIoctl(stmfGroupList **, int);
1388fe96085Stim szeto static int iLoadGroupFromPs(stmfGroupList **, int);
1398fe96085Stim szeto static int groupMemberListIoctl(stmfGroupName *, stmfGroupProperties **, int);
1408fe96085Stim szeto static int getProviderData(char *, nvlist_t **, int, uint64_t *);
14145039663SJohn Forte static int setDiskStandby(stmfGuid *luGuid);
1427beff157SJohn Forte static int setDiskGlobalProp(uint32_t, const char *);
1438fe96085Stim szeto static int viewEntryCompare(const void *, const void *);
14445039663SJohn Forte static void deleteNonActiveLus();
1454b31676fSsrivijitha dugganapalli static int loadStmfProp(int fd);
1468fe96085Stim szeto 
1478fe96085Stim szeto static pthread_mutex_t persistenceTypeLock = PTHREAD_MUTEX_INITIALIZER;
1488fe96085Stim szeto static int iPersistType = 0;
1498fe96085Stim szeto /* when B_TRUE, no need to access SMF anymore. Just use iPersistType */
1508fe96085Stim szeto static boolean_t iLibSetPersist = B_FALSE;
151fcf3ce44SJohn Forte 
152fcf3ce44SJohn Forte /*
153fcf3ce44SJohn Forte  * Open for stmf module
154fcf3ce44SJohn Forte  *
155fcf3ce44SJohn Forte  * flag - open flag (OPEN_STMF, OPEN_EXCL_STMF)
156fcf3ce44SJohn Forte  * fd - pointer to integer. On success, contains the stmf file descriptor
157fcf3ce44SJohn Forte  */
158fcf3ce44SJohn Forte static int
159fcf3ce44SJohn Forte openStmf(int flag, int *fd)
160fcf3ce44SJohn Forte {
161fcf3ce44SJohn Forte 	int ret = STMF_STATUS_ERROR;
162fcf3ce44SJohn Forte 
163fcf3ce44SJohn Forte 	if ((*fd = open(STMF_PATH, O_NDELAY | O_RDONLY | flag)) != -1) {
164fcf3ce44SJohn Forte 		ret = STMF_STATUS_SUCCESS;
165fcf3ce44SJohn Forte 	} else {
166fcf3ce44SJohn Forte 		if (errno == EBUSY) {
167fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1688fe96085Stim szeto 		} else if (errno == EACCES) {
1698fe96085Stim szeto 			ret = STMF_ERROR_PERM;
170fcf3ce44SJohn Forte 		} else {
171fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
172fcf3ce44SJohn Forte 		}
173fcf3ce44SJohn Forte 		syslog(LOG_DEBUG, "openStmf:open failure:%s:errno(%d)",
174fcf3ce44SJohn Forte 		    STMF_PATH, errno);
175fcf3ce44SJohn Forte 	}
176fcf3ce44SJohn Forte 
177fcf3ce44SJohn Forte 	return (ret);
178fcf3ce44SJohn Forte }
179fcf3ce44SJohn Forte 
180fcf3ce44SJohn Forte /*
1818fe96085Stim szeto  * Open for sbd module
1828fe96085Stim szeto  *
18345039663SJohn Forte  * flag - open flag (OPEN_SBD, OPEN_EXCL_SBD)
1848fe96085Stim szeto  * fd - pointer to integer. On success, contains the stmf file descriptor
1858fe96085Stim szeto  */
1868fe96085Stim szeto static int
1878fe96085Stim szeto openSbd(int flag, int *fd)
1888fe96085Stim szeto {
1898fe96085Stim szeto 	int ret = STMF_STATUS_ERROR;
1908fe96085Stim szeto 
1918fe96085Stim szeto 	if ((*fd = open(SBD_PATH, O_NDELAY | O_RDONLY | flag)) != -1) {
1928fe96085Stim szeto 		ret = STMF_STATUS_SUCCESS;
1938fe96085Stim szeto 	} else {
1948fe96085Stim szeto 		if (errno == EBUSY) {
1958fe96085Stim szeto 			ret = STMF_ERROR_BUSY;
1968fe96085Stim szeto 		} else if (errno == EACCES) {
1978fe96085Stim szeto 			ret = STMF_ERROR_PERM;
1988fe96085Stim szeto 		} else {
1998fe96085Stim szeto 			ret = STMF_STATUS_ERROR;
2008fe96085Stim szeto 		}
2018fe96085Stim szeto 		syslog(LOG_DEBUG, "openSbd:open failure:%s:errno(%d)",
2028fe96085Stim szeto 		    SBD_PATH, errno);
2038fe96085Stim szeto 	}
2048fe96085Stim szeto 
2058fe96085Stim szeto 	return (ret);
2068fe96085Stim szeto }
2078fe96085Stim szeto 
2088fe96085Stim szeto /*
20945039663SJohn Forte  * Open for pppt module
21045039663SJohn Forte  *
21145039663SJohn Forte  * flag - open flag (OPEN_PPPT, OPEN_EXCL_PPPT)
21245039663SJohn Forte  * fd - pointer to integer. On success, contains the stmf file descriptor
21345039663SJohn Forte  */
21445039663SJohn Forte static int
21545039663SJohn Forte openPppt(int flag, int *fd)
21645039663SJohn Forte {
21745039663SJohn Forte 	int ret = STMF_STATUS_ERROR;
21845039663SJohn Forte 
21945039663SJohn Forte 	if ((*fd = open(PPPT_PATH, O_RDONLY | flag)) != -1) {
22045039663SJohn Forte 		ret = STMF_STATUS_SUCCESS;
22145039663SJohn Forte 	} else {
22245039663SJohn Forte 		if (errno == EBUSY) {
22345039663SJohn Forte 			ret = STMF_ERROR_BUSY;
22445039663SJohn Forte 		} else if (errno == EACCES) {
22545039663SJohn Forte 			ret = STMF_ERROR_PERM;
22645039663SJohn Forte 		} else {
22745039663SJohn Forte 			ret = STMF_STATUS_ERROR;
22845039663SJohn Forte 		}
22945039663SJohn Forte 		syslog(LOG_DEBUG, "openPppt:open failure:%s:errno(%d)",
23045039663SJohn Forte 		    PPPT_PATH, errno);
23145039663SJohn Forte 	}
23245039663SJohn Forte 
23345039663SJohn Forte 	return (ret);
23445039663SJohn Forte }
23545039663SJohn Forte 
23645039663SJohn Forte /*
237fcf3ce44SJohn Forte  * initializeConfig
238fcf3ce44SJohn Forte  *
239fcf3ce44SJohn Forte  * This routine should be called before any ioctl requiring initialization
240fcf3ce44SJohn Forte  * which is basically everything except stmfGetState(), setStmfState() and
241fcf3ce44SJohn Forte  * stmfLoadConfig().
242fcf3ce44SJohn Forte  */
243fcf3ce44SJohn Forte static int
244fcf3ce44SJohn Forte initializeConfig()
245fcf3ce44SJohn Forte {
246fcf3ce44SJohn Forte 	int ret;
247fcf3ce44SJohn Forte 	stmfState state;
248fcf3ce44SJohn Forte 
249fcf3ce44SJohn Forte 
250fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
251fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
252fcf3ce44SJohn Forte 		return (ret);
253fcf3ce44SJohn Forte 	}
254fcf3ce44SJohn Forte 
255fcf3ce44SJohn Forte 	/* if we've already initialized or in the process, return success */
256fcf3ce44SJohn Forte 	if (state.configState == STMF_CONFIG_STATE_INIT_DONE ||
257fcf3ce44SJohn Forte 	    state.configState == STMF_CONFIG_STATE_INIT) {
258fcf3ce44SJohn Forte 		return (STMF_STATUS_SUCCESS);
259fcf3ce44SJohn Forte 	}
260fcf3ce44SJohn Forte 
261fcf3ce44SJohn Forte 	ret = stmfLoadConfig();
262fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
263fcf3ce44SJohn Forte 		syslog(LOG_DEBUG,
264fcf3ce44SJohn Forte 		    "initializeConfig:stmfLoadConfig:error(%d)", ret);
265fcf3ce44SJohn Forte 		return (ret);
266fcf3ce44SJohn Forte 	}
267fcf3ce44SJohn Forte 
268fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
269fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
270fcf3ce44SJohn Forte 		syslog(LOG_DEBUG,
271fcf3ce44SJohn Forte 		    "initializeConfig:stmfGetState:error(%d)", ret);
272fcf3ce44SJohn Forte 		return (ret);
273fcf3ce44SJohn Forte 	}
274fcf3ce44SJohn Forte 
275fcf3ce44SJohn Forte 	if (state.configState != STMF_CONFIG_STATE_INIT_DONE) {
276fcf3ce44SJohn Forte 		syslog(LOG_DEBUG, "initializeConfig:state.configState(%d)",
277fcf3ce44SJohn Forte 		    state.configState);
278fcf3ce44SJohn Forte 		ret = STMF_STATUS_ERROR;
279fcf3ce44SJohn Forte 	}
280fcf3ce44SJohn Forte 
281fcf3ce44SJohn Forte 	return (ret);
282fcf3ce44SJohn Forte }
283fcf3ce44SJohn Forte 
284fcf3ce44SJohn Forte 
285fcf3ce44SJohn Forte /*
286fcf3ce44SJohn Forte  * groupIoctl
287fcf3ce44SJohn Forte  *
288fcf3ce44SJohn Forte  * Purpose: issue ioctl for create/delete on group
289fcf3ce44SJohn Forte  *
290fcf3ce44SJohn Forte  * cmd - valid STMF ioctl group cmd
291fcf3ce44SJohn Forte  * groupName - groupName to create or delete
292fcf3ce44SJohn Forte  */
293fcf3ce44SJohn Forte static int
294fcf3ce44SJohn Forte groupIoctl(int fd, int cmd, stmfGroupName *groupName)
295fcf3ce44SJohn Forte {
296fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
297fcf3ce44SJohn Forte 	int ioctlRet;
298fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
299fcf3ce44SJohn Forte 	stmf_group_name_t iGroupName;
300fcf3ce44SJohn Forte 
301fcf3ce44SJohn Forte 	bzero(&iGroupName, sizeof (iGroupName));
302fcf3ce44SJohn Forte 
303fcf3ce44SJohn Forte 	bcopy(groupName, &iGroupName.name, strlen((char *)groupName));
304fcf3ce44SJohn Forte 
305fcf3ce44SJohn Forte 	iGroupName.name_size = strlen((char *)groupName);
306fcf3ce44SJohn Forte 
307fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
308fcf3ce44SJohn Forte 	/*
309fcf3ce44SJohn Forte 	 * Issue ioctl to create the host group
310fcf3ce44SJohn Forte 	 */
311fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
312fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (iGroupName);
313fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&iGroupName;
314fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
315fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
316fcf3ce44SJohn Forte 		switch (errno) {
3178fe96085Stim szeto 			case EPERM:
318fcf3ce44SJohn Forte 			case EACCES:
319fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
320fcf3ce44SJohn Forte 				break;
321fcf3ce44SJohn Forte 			default:
322fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
323fcf3ce44SJohn Forte 					case STMF_IOCERR_TG_EXISTS:
324fcf3ce44SJohn Forte 					case STMF_IOCERR_HG_EXISTS:
325fcf3ce44SJohn Forte 						ret = STMF_ERROR_EXISTS;
326fcf3ce44SJohn Forte 						break;
327fcf3ce44SJohn Forte 					case STMF_IOCERR_TG_IN_USE:
328fcf3ce44SJohn Forte 					case STMF_IOCERR_HG_IN_USE:
329fcf3ce44SJohn Forte 						ret = STMF_ERROR_GROUP_IN_USE;
330fcf3ce44SJohn Forte 						break;
331fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG:
332fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG:
333fcf3ce44SJohn Forte 						ret = STMF_ERROR_NOT_FOUND;
334fcf3ce44SJohn Forte 						break;
335fcf3ce44SJohn Forte 					default:
336fcf3ce44SJohn Forte 						syslog(LOG_DEBUG,
337fcf3ce44SJohn Forte 						    "groupIoctl:error(%d)",
338fcf3ce44SJohn Forte 						    stmfIoctl.stmf_error);
339fcf3ce44SJohn Forte 						ret = STMF_STATUS_ERROR;
340fcf3ce44SJohn Forte 						break;
341fcf3ce44SJohn Forte 				}
342fcf3ce44SJohn Forte 				break;
343fcf3ce44SJohn Forte 		}
344fcf3ce44SJohn Forte 	}
345fcf3ce44SJohn Forte done:
346fcf3ce44SJohn Forte 	return (ret);
347fcf3ce44SJohn Forte }
348fcf3ce44SJohn Forte 
349fcf3ce44SJohn Forte /*
3508fe96085Stim szeto  * groupMemberIoctl
351fcf3ce44SJohn Forte  *
352fcf3ce44SJohn Forte  * Purpose: issue ioctl for add/remove member on group
353fcf3ce44SJohn Forte  *
354fcf3ce44SJohn Forte  * cmd - valid STMF ioctl group member cmd
355fcf3ce44SJohn Forte  * groupName - groupName to add to or remove from
356fcf3ce44SJohn Forte  * devid - group member to add or remove
357fcf3ce44SJohn Forte  */
358fcf3ce44SJohn Forte static int
359fcf3ce44SJohn Forte groupMemberIoctl(int fd, int cmd, stmfGroupName *groupName, stmfDevid *devid)
360fcf3ce44SJohn Forte {
361fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
362fcf3ce44SJohn Forte 	int ioctlRet;
363fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
364fcf3ce44SJohn Forte 	stmf_group_op_data_t stmfGroupData;
365fcf3ce44SJohn Forte 
366fcf3ce44SJohn Forte 	bzero(&stmfGroupData, sizeof (stmfGroupData));
367fcf3ce44SJohn Forte 
368fcf3ce44SJohn Forte 	bcopy(groupName, &stmfGroupData.group.name, strlen((char *)groupName));
369fcf3ce44SJohn Forte 
370fcf3ce44SJohn Forte 	stmfGroupData.group.name_size = strlen((char *)groupName);
371fcf3ce44SJohn Forte 	stmfGroupData.ident[IDENT_LENGTH_BYTE] = devid->identLength;
372fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &stmfGroupData.ident[IDENT_LENGTH_BYTE + 1],
373fcf3ce44SJohn Forte 	    devid->identLength);
374fcf3ce44SJohn Forte 
375fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
376fcf3ce44SJohn Forte 	/*
377fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
378fcf3ce44SJohn Forte 	 */
379fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
380fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGroupData);
381fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&stmfGroupData;
382fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
383fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
384fcf3ce44SJohn Forte 		switch (errno) {
385fcf3ce44SJohn Forte 			case EBUSY:
3865c8cac22Stim szeto 				switch (stmfIoctl.stmf_error) {
3875c8cac22Stim szeto 					case STMF_IOCERR_TG_NEED_TG_OFFLINE:
3885c8cac22Stim szeto 						ret = STMF_ERROR_TG_ONLINE;
3895c8cac22Stim szeto 						break;
3905c8cac22Stim szeto 					default:
391fcf3ce44SJohn Forte 						ret = STMF_ERROR_BUSY;
392fcf3ce44SJohn Forte 						break;
3935c8cac22Stim szeto 				}
3945c8cac22Stim szeto 				break;
3958fe96085Stim szeto 			case EPERM:
396fcf3ce44SJohn Forte 			case EACCES:
397fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
398fcf3ce44SJohn Forte 				break;
399fcf3ce44SJohn Forte 			default:
400fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
401fcf3ce44SJohn Forte 					case STMF_IOCERR_TG_ENTRY_EXISTS:
402fcf3ce44SJohn Forte 					case STMF_IOCERR_HG_ENTRY_EXISTS:
403fcf3ce44SJohn Forte 						ret = STMF_ERROR_EXISTS;
404fcf3ce44SJohn Forte 						break;
405fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG_ENTRY:
406fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG_ENTRY:
407fcf3ce44SJohn Forte 						ret =
408fcf3ce44SJohn Forte 						    STMF_ERROR_MEMBER_NOT_FOUND;
409fcf3ce44SJohn Forte 						break;
410fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG:
411fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG:
412fcf3ce44SJohn Forte 						ret =
413fcf3ce44SJohn Forte 						    STMF_ERROR_GROUP_NOT_FOUND;
414fcf3ce44SJohn Forte 						break;
415fcf3ce44SJohn Forte 					default:
416fcf3ce44SJohn Forte 						syslog(LOG_DEBUG,
417fcf3ce44SJohn Forte 						    "groupMemberIoctl:error"
418fcf3ce44SJohn Forte 						    "(%d)",
419fcf3ce44SJohn Forte 						    stmfIoctl.stmf_error);
420fcf3ce44SJohn Forte 						ret = STMF_STATUS_ERROR;
421fcf3ce44SJohn Forte 						break;
422fcf3ce44SJohn Forte 				}
423fcf3ce44SJohn Forte 				break;
424fcf3ce44SJohn Forte 		}
425fcf3ce44SJohn Forte 	}
426fcf3ce44SJohn Forte done:
427fcf3ce44SJohn Forte 	return (ret);
428fcf3ce44SJohn Forte }
429fcf3ce44SJohn Forte 
430fcf3ce44SJohn Forte /*
4318fe96085Stim szeto  * qsort function
4328fe96085Stim szeto  * sort on veIndex
4338fe96085Stim szeto  */
4348fe96085Stim szeto static int
4358fe96085Stim szeto viewEntryCompare(const void *p1, const void *p2)
4368fe96085Stim szeto {
4378fe96085Stim szeto 
4388fe96085Stim szeto 	stmfViewEntry *v1 = (stmfViewEntry *)p1, *v2 = (stmfViewEntry *)p2;
4398fe96085Stim szeto 	if (v1->veIndex > v2->veIndex)
4408fe96085Stim szeto 		return (1);
4418fe96085Stim szeto 	if (v1->veIndex < v2->veIndex)
4428fe96085Stim szeto 		return (-1);
4438fe96085Stim szeto 	return (0);
4448fe96085Stim szeto }
4458fe96085Stim szeto 
4468fe96085Stim szeto /*
447fcf3ce44SJohn Forte  * guidCompare
448fcf3ce44SJohn Forte  *
449fcf3ce44SJohn Forte  * qsort function
450fcf3ce44SJohn Forte  * sort on guid
451fcf3ce44SJohn Forte  */
452fcf3ce44SJohn Forte static int
453fcf3ce44SJohn Forte guidCompare(const void *p1, const void *p2)
454fcf3ce44SJohn Forte {
455fcf3ce44SJohn Forte 
456fcf3ce44SJohn Forte 	stmfGuid *g1 = (stmfGuid *)p1, *g2 = (stmfGuid *)p2;
457fcf3ce44SJohn Forte 	int i;
458fcf3ce44SJohn Forte 
459fcf3ce44SJohn Forte 	for (i = 0; i < sizeof (stmfGuid); i++) {
460fcf3ce44SJohn Forte 		if (g1->guid[i] > g2->guid[i])
461fcf3ce44SJohn Forte 			return (1);
462fcf3ce44SJohn Forte 		if (g1->guid[i] < g2->guid[i])
463fcf3ce44SJohn Forte 			return (-1);
464fcf3ce44SJohn Forte 	}
465fcf3ce44SJohn Forte 
466fcf3ce44SJohn Forte 	return (0);
467fcf3ce44SJohn Forte }
468fcf3ce44SJohn Forte 
469fcf3ce44SJohn Forte /*
470fcf3ce44SJohn Forte  * stmfAddToHostGroup
471fcf3ce44SJohn Forte  *
472fcf3ce44SJohn Forte  * Purpose: Adds an initiator to an existing host group
473fcf3ce44SJohn Forte  *
474fcf3ce44SJohn Forte  * hostGroupName - name of an existing host group
475fcf3ce44SJohn Forte  * hostName - name of initiator to add
476fcf3ce44SJohn Forte  */
477fcf3ce44SJohn Forte int
478fcf3ce44SJohn Forte stmfAddToHostGroup(stmfGroupName *hostGroupName, stmfDevid *hostName)
479fcf3ce44SJohn Forte {
480fcf3ce44SJohn Forte 	int ret;
481fcf3ce44SJohn Forte 	int fd;
482fcf3ce44SJohn Forte 
483fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
484fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
485fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || hostName == NULL) {
486fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
487fcf3ce44SJohn Forte 	}
488fcf3ce44SJohn Forte 
489fcf3ce44SJohn Forte 	/* call init */
490fcf3ce44SJohn Forte 	ret = initializeConfig();
491fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
492fcf3ce44SJohn Forte 		return (ret);
493fcf3ce44SJohn Forte 	}
494fcf3ce44SJohn Forte 
495fcf3ce44SJohn Forte 	/*
496fcf3ce44SJohn Forte 	 * Open control node for stmf
497fcf3ce44SJohn Forte 	 */
498fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
499fcf3ce44SJohn Forte 		return (ret);
500fcf3ce44SJohn Forte 
501fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_HG_ENTRY, hostGroupName,
502fcf3ce44SJohn Forte 	    hostName)) != STMF_STATUS_SUCCESS) {
503fcf3ce44SJohn Forte 		goto done;
504fcf3ce44SJohn Forte 	}
505fcf3ce44SJohn Forte 
5068fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
5078fe96085Stim szeto 		goto done;
5088fe96085Stim szeto 	}
5098fe96085Stim szeto 
510fcf3ce44SJohn Forte 	ret = psAddHostGroupMember((char *)hostGroupName,
511fcf3ce44SJohn Forte 	    (char *)hostName->ident);
512fcf3ce44SJohn Forte 	switch (ret) {
513fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
514fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
515fcf3ce44SJohn Forte 			break;
516fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
517fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
518fcf3ce44SJohn Forte 			break;
519fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
520fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
521fcf3ce44SJohn Forte 			break;
522fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
523fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
524fcf3ce44SJohn Forte 			break;
525fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
526fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
527fcf3ce44SJohn Forte 			break;
528fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
529fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
530fcf3ce44SJohn Forte 			break;
531fcf3ce44SJohn Forte 		default:
532fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
533fcf3ce44SJohn Forte 			    "stmfAddToHostGroup:psAddHostGroupMember:error(%d)",
534fcf3ce44SJohn Forte 			    ret);
535fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
536fcf3ce44SJohn Forte 			break;
537fcf3ce44SJohn Forte 	}
538fcf3ce44SJohn Forte 
539fcf3ce44SJohn Forte done:
540fcf3ce44SJohn Forte 	(void) close(fd);
541fcf3ce44SJohn Forte 	return (ret);
542fcf3ce44SJohn Forte }
543fcf3ce44SJohn Forte 
544fcf3ce44SJohn Forte /*
545fcf3ce44SJohn Forte  * stmfAddToTargetGroup
546fcf3ce44SJohn Forte  *
547fcf3ce44SJohn Forte  * Purpose: Adds a local port to an existing target group
548fcf3ce44SJohn Forte  *
549fcf3ce44SJohn Forte  * targetGroupName - name of an existing target group
550fcf3ce44SJohn Forte  * targetName - name of target to add
551fcf3ce44SJohn Forte  */
552fcf3ce44SJohn Forte int
553fcf3ce44SJohn Forte stmfAddToTargetGroup(stmfGroupName *targetGroupName, stmfDevid *targetName)
554fcf3ce44SJohn Forte {
555fcf3ce44SJohn Forte 	int ret;
556fcf3ce44SJohn Forte 	int fd;
557fcf3ce44SJohn Forte 
558fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
559fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
560fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || targetName == NULL) {
561fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
562fcf3ce44SJohn Forte 	}
563fcf3ce44SJohn Forte 
564fcf3ce44SJohn Forte 	/* call init */
565fcf3ce44SJohn Forte 	ret = initializeConfig();
566fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
567fcf3ce44SJohn Forte 		return (ret);
568fcf3ce44SJohn Forte 	}
569fcf3ce44SJohn Forte 
570fcf3ce44SJohn Forte 	/*
571fcf3ce44SJohn Forte 	 * Open control node for stmf
572fcf3ce44SJohn Forte 	 */
573fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
574fcf3ce44SJohn Forte 		return (ret);
575fcf3ce44SJohn Forte 
576fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_TG_ENTRY,
577fcf3ce44SJohn Forte 	    targetGroupName, targetName)) != STMF_STATUS_SUCCESS) {
578fcf3ce44SJohn Forte 		goto done;
579fcf3ce44SJohn Forte 	}
580fcf3ce44SJohn Forte 
5818fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
5828fe96085Stim szeto 		goto done;
5838fe96085Stim szeto 	}
5848fe96085Stim szeto 
585fcf3ce44SJohn Forte 	ret = psAddTargetGroupMember((char *)targetGroupName,
586fcf3ce44SJohn Forte 	    (char *)targetName->ident);
587fcf3ce44SJohn Forte 	switch (ret) {
588fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
589fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
590fcf3ce44SJohn Forte 			break;
591fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
592fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
593fcf3ce44SJohn Forte 			break;
594fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
595fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
596fcf3ce44SJohn Forte 			break;
597fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
598fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
599fcf3ce44SJohn Forte 			break;
600fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
601fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
602fcf3ce44SJohn Forte 			break;
603fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
604fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
605fcf3ce44SJohn Forte 			break;
606fcf3ce44SJohn Forte 		default:
607fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
608fcf3ce44SJohn Forte 			    "stmfAddToTargetGroup:psAddTargetGroupMember:"
609fcf3ce44SJohn Forte 			    "error(%d)", ret);
610fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
611fcf3ce44SJohn Forte 			break;
612fcf3ce44SJohn Forte 	}
613fcf3ce44SJohn Forte 
614fcf3ce44SJohn Forte done:
615fcf3ce44SJohn Forte 	(void) close(fd);
616fcf3ce44SJohn Forte 	return (ret);
617fcf3ce44SJohn Forte }
618fcf3ce44SJohn Forte 
619fcf3ce44SJohn Forte /*
620fcf3ce44SJohn Forte  * addViewEntryIoctl
621fcf3ce44SJohn Forte  *
622fcf3ce44SJohn Forte  * Purpose: Issues ioctl to add a view entry
623fcf3ce44SJohn Forte  *
624fcf3ce44SJohn Forte  * lu - Logical Unit identifier to which the view entry is added
625fcf3ce44SJohn Forte  * viewEntry - view entry to add
626fcf3ce44SJohn Forte  * init - When set to B_TRUE, we are in the init state, i.e. don't call open
627fcf3ce44SJohn Forte  */
628fcf3ce44SJohn Forte static int
629fcf3ce44SJohn Forte addViewEntryIoctl(int fd, stmfGuid *lu, stmfViewEntry *viewEntry)
630fcf3ce44SJohn Forte {
631fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
632fcf3ce44SJohn Forte 	int ioctlRet;
633fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
634fcf3ce44SJohn Forte 	stmf_view_op_entry_t ioctlViewEntry;
635fcf3ce44SJohn Forte 
636fcf3ce44SJohn Forte 	bzero(&ioctlViewEntry, sizeof (ioctlViewEntry));
637fcf3ce44SJohn Forte 	/*
638fcf3ce44SJohn Forte 	 * don't set ve_ndx or ve_ndx_valid as ve_ndx_valid should be
639fcf3ce44SJohn Forte 	 * false on input
640fcf3ce44SJohn Forte 	 */
641fcf3ce44SJohn Forte 	ioctlViewEntry.ve_lu_number_valid = viewEntry->luNbrValid;
642fcf3ce44SJohn Forte 	ioctlViewEntry.ve_all_hosts = viewEntry->allHosts;
643fcf3ce44SJohn Forte 	ioctlViewEntry.ve_all_targets = viewEntry->allTargets;
644fcf3ce44SJohn Forte 
645fcf3ce44SJohn Forte 	if (viewEntry->allHosts == B_FALSE) {
646fcf3ce44SJohn Forte 		bcopy(viewEntry->hostGroup, &ioctlViewEntry.ve_host_group.name,
647fcf3ce44SJohn Forte 		    sizeof (stmfGroupName));
648fcf3ce44SJohn Forte 		ioctlViewEntry.ve_host_group.name_size =
649fcf3ce44SJohn Forte 		    strlen((char *)viewEntry->hostGroup);
650fcf3ce44SJohn Forte 	}
651fcf3ce44SJohn Forte 	if (viewEntry->allTargets == B_FALSE) {
652fcf3ce44SJohn Forte 		bcopy(viewEntry->targetGroup,
653fcf3ce44SJohn Forte 		    &ioctlViewEntry.ve_target_group.name,
654fcf3ce44SJohn Forte 		    sizeof (stmfGroupName));
655fcf3ce44SJohn Forte 		ioctlViewEntry.ve_target_group.name_size =
656fcf3ce44SJohn Forte 		    strlen((char *)viewEntry->targetGroup);
657fcf3ce44SJohn Forte 	}
658fcf3ce44SJohn Forte 	if (viewEntry->luNbrValid) {
659fcf3ce44SJohn Forte 		bcopy(viewEntry->luNbr, &ioctlViewEntry.ve_lu_nbr,
660fcf3ce44SJohn Forte 		    sizeof (ioctlViewEntry.ve_lu_nbr));
661fcf3ce44SJohn Forte 	}
662fcf3ce44SJohn Forte 	bcopy(lu, &ioctlViewEntry.ve_guid, sizeof (stmfGuid));
663fcf3ce44SJohn Forte 
664fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
665fcf3ce44SJohn Forte 	/*
666fcf3ce44SJohn Forte 	 * Issue ioctl to add to the view entry
667fcf3ce44SJohn Forte 	 */
668fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
669fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ioctlViewEntry);
670fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ioctlViewEntry;
671fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (ioctlViewEntry);
672fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&ioctlViewEntry;
673fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_ADD_VIEW_ENTRY, &stmfIoctl);
674fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
675fcf3ce44SJohn Forte 		switch (errno) {
676fcf3ce44SJohn Forte 			case EBUSY:
677fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
678fcf3ce44SJohn Forte 				break;
6798fe96085Stim szeto 			case EPERM:
6808fe96085Stim szeto 				ret = STMF_ERROR_PERM;
6818fe96085Stim szeto 				break;
682fcf3ce44SJohn Forte 			case EACCES:
683fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
684fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
685fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
686fcf3ce44SJohn Forte 						break;
687fcf3ce44SJohn Forte 					default:
688fcf3ce44SJohn Forte 						ret = STMF_ERROR_PERM;
689fcf3ce44SJohn Forte 						break;
690fcf3ce44SJohn Forte 				}
691fcf3ce44SJohn Forte 				break;
692fcf3ce44SJohn Forte 			default:
693fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
694fcf3ce44SJohn Forte 					case STMF_IOCERR_LU_NUMBER_IN_USE:
695fcf3ce44SJohn Forte 						ret = STMF_ERROR_LUN_IN_USE;
696fcf3ce44SJohn Forte 						break;
697fcf3ce44SJohn Forte 					case STMF_IOCERR_VIEW_ENTRY_CONFLICT:
698fcf3ce44SJohn Forte 						ret = STMF_ERROR_VE_CONFLICT;
699fcf3ce44SJohn Forte 						break;
700fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
701fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
702fcf3ce44SJohn Forte 						break;
703fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_HG:
704fcf3ce44SJohn Forte 						ret = STMF_ERROR_INVALID_HG;
705fcf3ce44SJohn Forte 						break;
706fcf3ce44SJohn Forte 					case STMF_IOCERR_INVALID_TG:
707fcf3ce44SJohn Forte 						ret = STMF_ERROR_INVALID_TG;
708fcf3ce44SJohn Forte 						break;
709fcf3ce44SJohn Forte 					default:
710fcf3ce44SJohn Forte 						syslog(LOG_DEBUG,
711fcf3ce44SJohn Forte 						    "addViewEntryIoctl"
712fcf3ce44SJohn Forte 						    ":error(%d)",
713fcf3ce44SJohn Forte 						    stmfIoctl.stmf_error);
714fcf3ce44SJohn Forte 						ret = STMF_STATUS_ERROR;
715fcf3ce44SJohn Forte 						break;
716fcf3ce44SJohn Forte 				}
717fcf3ce44SJohn Forte 				break;
718fcf3ce44SJohn Forte 		}
719fcf3ce44SJohn Forte 		goto done;
720fcf3ce44SJohn Forte 	}
721fcf3ce44SJohn Forte 
722fcf3ce44SJohn Forte 	/* copy lu nbr back to caller's view entry on success */
723fcf3ce44SJohn Forte 	viewEntry->veIndex = ioctlViewEntry.ve_ndx;
724fcf3ce44SJohn Forte 	if (ioctlViewEntry.ve_lu_number_valid) {
725fcf3ce44SJohn Forte 		bcopy(&ioctlViewEntry.ve_lu_nbr, viewEntry->luNbr,
726fcf3ce44SJohn Forte 		    sizeof (ioctlViewEntry.ve_lu_nbr));
727fcf3ce44SJohn Forte 	}
728fcf3ce44SJohn Forte 	viewEntry->luNbrValid = B_TRUE;
729fcf3ce44SJohn Forte 
730fcf3ce44SJohn Forte done:
731fcf3ce44SJohn Forte 	return (ret);
732fcf3ce44SJohn Forte }
733fcf3ce44SJohn Forte 
734fcf3ce44SJohn Forte /*
735fcf3ce44SJohn Forte  * stmfAddViewEntry
736fcf3ce44SJohn Forte  *
737fcf3ce44SJohn Forte  * Purpose: Adds a view entry to a logical unit
738fcf3ce44SJohn Forte  *
739fcf3ce44SJohn Forte  * lu - guid of the logical unit to which the view entry is added
740fcf3ce44SJohn Forte  * viewEntry - view entry structure to add
741fcf3ce44SJohn Forte  */
742fcf3ce44SJohn Forte int
743fcf3ce44SJohn Forte stmfAddViewEntry(stmfGuid *lu, stmfViewEntry *viewEntry)
744fcf3ce44SJohn Forte {
745fcf3ce44SJohn Forte 	int ret;
746fcf3ce44SJohn Forte 	int fd;
747fcf3ce44SJohn Forte 	stmfViewEntry iViewEntry;
748fcf3ce44SJohn Forte 
749fcf3ce44SJohn Forte 	if (lu == NULL || viewEntry == NULL) {
750fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
751fcf3ce44SJohn Forte 	}
752fcf3ce44SJohn Forte 
753fcf3ce44SJohn Forte 	/* initialize and set internal view entry */
754fcf3ce44SJohn Forte 	bzero(&iViewEntry, sizeof (iViewEntry));
755fcf3ce44SJohn Forte 
756fcf3ce44SJohn Forte 	if (!viewEntry->allHosts) {
757fcf3ce44SJohn Forte 		bcopy(viewEntry->hostGroup, iViewEntry.hostGroup,
758fcf3ce44SJohn Forte 		    sizeof (iViewEntry.hostGroup));
759fcf3ce44SJohn Forte 	} else {
760fcf3ce44SJohn Forte 		iViewEntry.allHosts = B_TRUE;
761fcf3ce44SJohn Forte 	}
762fcf3ce44SJohn Forte 
763fcf3ce44SJohn Forte 	if (!viewEntry->allTargets) {
764fcf3ce44SJohn Forte 		bcopy(viewEntry->targetGroup, iViewEntry.targetGroup,
765fcf3ce44SJohn Forte 		    sizeof (iViewEntry.targetGroup));
766fcf3ce44SJohn Forte 	} else {
767fcf3ce44SJohn Forte 		iViewEntry.allTargets = B_TRUE;
768fcf3ce44SJohn Forte 	}
769fcf3ce44SJohn Forte 
770fcf3ce44SJohn Forte 	if (viewEntry->luNbrValid) {
771fcf3ce44SJohn Forte 		iViewEntry.luNbrValid = B_TRUE;
772fcf3ce44SJohn Forte 		bcopy(viewEntry->luNbr, iViewEntry.luNbr,
773fcf3ce44SJohn Forte 		    sizeof (iViewEntry.luNbr));
774fcf3ce44SJohn Forte 	}
775fcf3ce44SJohn Forte 
776fcf3ce44SJohn Forte 	/*
777fcf3ce44SJohn Forte 	 * set users return view entry index valid flag to false
778fcf3ce44SJohn Forte 	 * in case of failure
779fcf3ce44SJohn Forte 	 */
780fcf3ce44SJohn Forte 	viewEntry->veIndexValid = B_FALSE;
781fcf3ce44SJohn Forte 
782fcf3ce44SJohn Forte 	/* Check to ensure service exists */
783fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
784fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
785fcf3ce44SJohn Forte 	}
786fcf3ce44SJohn Forte 
787fcf3ce44SJohn Forte 	/* call init */
788fcf3ce44SJohn Forte 	ret = initializeConfig();
789fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
790fcf3ce44SJohn Forte 		return (ret);
791fcf3ce44SJohn Forte 	}
792fcf3ce44SJohn Forte 
793fcf3ce44SJohn Forte 	/*
794fcf3ce44SJohn Forte 	 * Open control node for stmf
795fcf3ce44SJohn Forte 	 */
796fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
797fcf3ce44SJohn Forte 		return (ret);
798fcf3ce44SJohn Forte 
799fcf3ce44SJohn Forte 	/*
800fcf3ce44SJohn Forte 	 * First add the view entry to the driver
801fcf3ce44SJohn Forte 	 */
802fcf3ce44SJohn Forte 	ret = addViewEntryIoctl(fd, lu, &iViewEntry);
803fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
804fcf3ce44SJohn Forte 		goto done;
805fcf3ce44SJohn Forte 	}
806fcf3ce44SJohn Forte 
8078fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
8088fe96085Stim szeto 		goto done;
8098fe96085Stim szeto 	}
8108fe96085Stim szeto 
811fcf3ce44SJohn Forte 	/*
812fcf3ce44SJohn Forte 	 * If the add to driver was successful, add it to the persistent
813fcf3ce44SJohn Forte 	 * store.
814fcf3ce44SJohn Forte 	 */
815fcf3ce44SJohn Forte 	ret = psAddViewEntry(lu, &iViewEntry);
816fcf3ce44SJohn Forte 	switch (ret) {
817fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
818fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
819fcf3ce44SJohn Forte 			break;
820fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
821fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
822fcf3ce44SJohn Forte 			break;
823fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
824fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
825fcf3ce44SJohn Forte 			break;
826fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
827fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
828fcf3ce44SJohn Forte 			break;
829fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
830fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
831fcf3ce44SJohn Forte 			break;
832fcf3ce44SJohn Forte 		default:
833fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
834fcf3ce44SJohn Forte 			    "stmfAddViewEntry:psAddViewEntry:error(%d)", ret);
835fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
836fcf3ce44SJohn Forte 			break;
837fcf3ce44SJohn Forte 	}
838fcf3ce44SJohn Forte 
839fcf3ce44SJohn Forte done:
840fcf3ce44SJohn Forte 	(void) close(fd);
841fcf3ce44SJohn Forte 
842fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
843fcf3ce44SJohn Forte 		/* set caller's view entry on success */
844fcf3ce44SJohn Forte 		viewEntry->veIndexValid = iViewEntry.veIndexValid;
845fcf3ce44SJohn Forte 		viewEntry->veIndex = iViewEntry.veIndex;
846fcf3ce44SJohn Forte 		viewEntry->luNbrValid = B_TRUE;
847fcf3ce44SJohn Forte 		bcopy(iViewEntry.luNbr, viewEntry->luNbr,
848fcf3ce44SJohn Forte 		    sizeof (iViewEntry.luNbr));
849fcf3ce44SJohn Forte 	}
850fcf3ce44SJohn Forte 	return (ret);
851fcf3ce44SJohn Forte }
852fcf3ce44SJohn Forte 
853fcf3ce44SJohn Forte /*
854fcf3ce44SJohn Forte  * stmfClearProviderData
855fcf3ce44SJohn Forte  *
856fcf3ce44SJohn Forte  * Purpose: delete all provider data for specified provider
857fcf3ce44SJohn Forte  *
858fcf3ce44SJohn Forte  * providerName - name of provider for which data should be deleted
859fcf3ce44SJohn Forte  */
860fcf3ce44SJohn Forte int
861fcf3ce44SJohn Forte stmfClearProviderData(char *providerName, int providerType)
862fcf3ce44SJohn Forte {
863fcf3ce44SJohn Forte 	int ret;
864fcf3ce44SJohn Forte 	int fd;
865fcf3ce44SJohn Forte 	int ioctlRet;
866fcf3ce44SJohn Forte 	int savedErrno;
867fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
868fcf3ce44SJohn Forte 	stmf_ppioctl_data_t ppi;
869fcf3ce44SJohn Forte 
870fcf3ce44SJohn Forte 	/* call init */
871fcf3ce44SJohn Forte 	ret = initializeConfig();
872fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
873fcf3ce44SJohn Forte 		return (ret);
874fcf3ce44SJohn Forte 	}
875fcf3ce44SJohn Forte 
876fcf3ce44SJohn Forte 	if (providerName == NULL) {
877fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
878fcf3ce44SJohn Forte 	}
879fcf3ce44SJohn Forte 
880fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
881fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
882fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
883fcf3ce44SJohn Forte 	}
884fcf3ce44SJohn Forte 
885fcf3ce44SJohn Forte 	/*
886fcf3ce44SJohn Forte 	 * Open control node for stmf
887fcf3ce44SJohn Forte 	 */
888fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
889fcf3ce44SJohn Forte 		return (ret);
890fcf3ce44SJohn Forte 
891fcf3ce44SJohn Forte 	bzero(&ppi, sizeof (ppi));
892fcf3ce44SJohn Forte 
893fcf3ce44SJohn Forte 	(void) strncpy(ppi.ppi_name, providerName, sizeof (ppi.ppi_name));
894fcf3ce44SJohn Forte 
895fcf3ce44SJohn Forte 	switch (providerType) {
896fcf3ce44SJohn Forte 		case STMF_LU_PROVIDER_TYPE:
897fcf3ce44SJohn Forte 			ppi.ppi_lu_provider = 1;
898fcf3ce44SJohn Forte 			break;
899fcf3ce44SJohn Forte 		case STMF_PORT_PROVIDER_TYPE:
900fcf3ce44SJohn Forte 			ppi.ppi_port_provider = 1;
901fcf3ce44SJohn Forte 			break;
902fcf3ce44SJohn Forte 		default:
903fcf3ce44SJohn Forte 			ret = STMF_ERROR_INVALID_ARG;
904fcf3ce44SJohn Forte 			goto done;
905fcf3ce44SJohn Forte 	}
906fcf3ce44SJohn Forte 
907fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
908fcf3ce44SJohn Forte 
909fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
910fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_ppioctl_data_t);
911fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ppi;
912fcf3ce44SJohn Forte 
913fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_CLEAR_PP_DATA, &stmfIoctl);
914fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
915fcf3ce44SJohn Forte 		savedErrno = errno;
916fcf3ce44SJohn Forte 		switch (savedErrno) {
917fcf3ce44SJohn Forte 			case EBUSY:
918fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
919fcf3ce44SJohn Forte 				break;
9208fe96085Stim szeto 			case EPERM:
921fcf3ce44SJohn Forte 			case EACCES:
922fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
923fcf3ce44SJohn Forte 				break;
924fcf3ce44SJohn Forte 			default:
925fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
926fcf3ce44SJohn Forte 				    "stmfClearProviderData:ioctl error(%d)",
927fcf3ce44SJohn Forte 				    ioctlRet);
928fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
929fcf3ce44SJohn Forte 				break;
930fcf3ce44SJohn Forte 		}
931fcf3ce44SJohn Forte 		if (savedErrno != ENOENT) {
932fcf3ce44SJohn Forte 			goto done;
933fcf3ce44SJohn Forte 		}
934fcf3ce44SJohn Forte 	}
935fcf3ce44SJohn Forte 
9368fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
9378fe96085Stim szeto 		goto done;
9388fe96085Stim szeto 	}
9398fe96085Stim szeto 
940fcf3ce44SJohn Forte 	ret = psClearProviderData(providerName, providerType);
941fcf3ce44SJohn Forte 	switch (ret) {
942fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
943fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
944fcf3ce44SJohn Forte 			break;
945fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
946fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
947fcf3ce44SJohn Forte 			break;
948fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
949fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
950fcf3ce44SJohn Forte 			break;
951fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
952fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
953fcf3ce44SJohn Forte 			break;
954fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
955fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
956fcf3ce44SJohn Forte 			break;
957fcf3ce44SJohn Forte 		default:
958fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
959fcf3ce44SJohn Forte 			    "stmfClearProviderData:psClearProviderData"
960fcf3ce44SJohn Forte 			    ":error(%d)", ret);
961fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
962fcf3ce44SJohn Forte 			break;
963fcf3ce44SJohn Forte 	}
964fcf3ce44SJohn Forte 
965fcf3ce44SJohn Forte done:
966fcf3ce44SJohn Forte 	(void) close(fd);
967fcf3ce44SJohn Forte 	return (ret);
968fcf3ce44SJohn Forte }
969fcf3ce44SJohn Forte 
970fcf3ce44SJohn Forte /*
971fcf3ce44SJohn Forte  * stmfCreateHostGroup
972fcf3ce44SJohn Forte  *
973fcf3ce44SJohn Forte  * Purpose: Create a new initiator group
974fcf3ce44SJohn Forte  *
975fcf3ce44SJohn Forte  * hostGroupName - name of host group to create
976fcf3ce44SJohn Forte  */
977fcf3ce44SJohn Forte int
978fcf3ce44SJohn Forte stmfCreateHostGroup(stmfGroupName *hostGroupName)
979fcf3ce44SJohn Forte {
980fcf3ce44SJohn Forte 	int ret;
981fcf3ce44SJohn Forte 	int fd;
982fcf3ce44SJohn Forte 
983fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
984fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
985fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName))) {
986fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
987fcf3ce44SJohn Forte 	}
988fcf3ce44SJohn Forte 
989fcf3ce44SJohn Forte 	/* Check to ensure service exists */
990fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
991fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
992fcf3ce44SJohn Forte 	}
993fcf3ce44SJohn Forte 
994fcf3ce44SJohn Forte 	/* call init */
995fcf3ce44SJohn Forte 	ret = initializeConfig();
996fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
997fcf3ce44SJohn Forte 		return (ret);
998fcf3ce44SJohn Forte 	}
999fcf3ce44SJohn Forte 
1000fcf3ce44SJohn Forte 	/*
1001fcf3ce44SJohn Forte 	 * Open control node for stmf
1002fcf3ce44SJohn Forte 	 */
1003fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
1004fcf3ce44SJohn Forte 		return (ret);
1005fcf3ce44SJohn Forte 
1006fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_HOST_GROUP,
1007fcf3ce44SJohn Forte 	    hostGroupName)) != STMF_STATUS_SUCCESS) {
1008fcf3ce44SJohn Forte 		goto done;
1009fcf3ce44SJohn Forte 	}
1010fcf3ce44SJohn Forte 
10118fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
10128fe96085Stim szeto 		goto done;
10138fe96085Stim szeto 	}
10148fe96085Stim szeto 
1015fcf3ce44SJohn Forte 	ret = psCreateHostGroup((char *)hostGroupName);
1016fcf3ce44SJohn Forte 	switch (ret) {
1017fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
1018fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
1019fcf3ce44SJohn Forte 			break;
1020fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
1021fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
1022fcf3ce44SJohn Forte 			break;
1023fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
1024fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
1025fcf3ce44SJohn Forte 			break;
1026fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
1027fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
1028fcf3ce44SJohn Forte 			break;
1029fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
1030fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
1031fcf3ce44SJohn Forte 			break;
1032fcf3ce44SJohn Forte 		default:
1033fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
1034fcf3ce44SJohn Forte 			    "stmfCreateHostGroup:psCreateHostGroup:error(%d)",
1035fcf3ce44SJohn Forte 			    ret);
1036fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
1037fcf3ce44SJohn Forte 			break;
1038fcf3ce44SJohn Forte 	}
1039fcf3ce44SJohn Forte 
1040fcf3ce44SJohn Forte done:
1041fcf3ce44SJohn Forte 	(void) close(fd);
1042fcf3ce44SJohn Forte 	return (ret);
1043fcf3ce44SJohn Forte }
1044fcf3ce44SJohn Forte 
1045fcf3ce44SJohn Forte /*
10468fe96085Stim szeto  * stmfCreateLu
10478fe96085Stim szeto  *
10488fe96085Stim szeto  * Purpose: Create a logical unit
10498fe96085Stim szeto  *
10508fe96085Stim szeto  * hdl - handle to logical unit resource created via stmfCreateLuResource
10518fe96085Stim szeto  *
10528fe96085Stim szeto  * luGuid - If non-NULL, on success, contains the guid of the created logical
10538fe96085Stim szeto  *	    unit
10548fe96085Stim szeto  */
10558fe96085Stim szeto int
10568fe96085Stim szeto stmfCreateLu(luResource hdl, stmfGuid *luGuid)
10578fe96085Stim szeto {
10588fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
10598fe96085Stim szeto 	luResourceImpl *luPropsHdl = hdl;
10608fe96085Stim szeto 
10618fe96085Stim szeto 	if (hdl == NULL) {
10628fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
10638fe96085Stim szeto 	}
10648fe96085Stim szeto 
10658fe96085Stim szeto 	if (luPropsHdl->type == STMF_DISK) {
10668fe96085Stim szeto 		ret = createDiskLu((diskResource *)luPropsHdl->resource,
10678fe96085Stim szeto 		    luGuid);
10688fe96085Stim szeto 	} else {
10698fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
10708fe96085Stim szeto 	}
10718fe96085Stim szeto 
10728fe96085Stim szeto 	return (ret);
10738fe96085Stim szeto }
10748fe96085Stim szeto 
10758fe96085Stim szeto /*
10768fe96085Stim szeto  * stmfCreateLuResource
10778fe96085Stim szeto  *
10788fe96085Stim szeto  * Purpose: Create resource handle for a logical unit
10798fe96085Stim szeto  *
10808fe96085Stim szeto  * dType - Type of logical unit resource to create
10818fe96085Stim szeto  *	   Can be: STMF_DISK
10828fe96085Stim szeto  *
10838fe96085Stim szeto  * hdl - pointer to luResource
10848fe96085Stim szeto  */
10858fe96085Stim szeto int
10868fe96085Stim szeto stmfCreateLuResource(uint16_t dType, luResource *hdl)
10878fe96085Stim szeto {
10888fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
10898fe96085Stim szeto 
10908fe96085Stim szeto 	if (dType != STMF_DISK || hdl == NULL) {
10918fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
10928fe96085Stim szeto 	}
10938fe96085Stim szeto 
10948fe96085Stim szeto 	*hdl = calloc(1, sizeof (luResourceImpl));
10958fe96085Stim szeto 	if (*hdl == NULL) {
10968fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
10978fe96085Stim szeto 	}
10988fe96085Stim szeto 
10998fe96085Stim szeto 	ret = createDiskResource((luResourceImpl *)*hdl);
11008fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
11018fe96085Stim szeto 		free(*hdl);
11028fe96085Stim szeto 		return (ret);
11038fe96085Stim szeto 	}
11048fe96085Stim szeto 
11058fe96085Stim szeto 	return (STMF_STATUS_SUCCESS);
11068fe96085Stim szeto }
11078fe96085Stim szeto 
11088fe96085Stim szeto /*
11098fe96085Stim szeto  * Creates a disk logical unit
11108fe96085Stim szeto  *
11118fe96085Stim szeto  * disk - pointer to diskResource structure that represents the properties
11128fe96085Stim szeto  *        for the disk logical unit to be created.
11138fe96085Stim szeto  */
11148fe96085Stim szeto static int
11158fe96085Stim szeto createDiskLu(diskResource *disk, stmfGuid *createdGuid)
11168fe96085Stim szeto {
11178fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
11188fe96085Stim szeto 	int dataFileNameLen = 0;
11198fe96085Stim szeto 	int metaFileNameLen = 0;
11208fe96085Stim szeto 	int serialNumLen = 0;
11218fe96085Stim szeto 	int luAliasLen = 0;
11222f624233SNattuvetty Bhavyan 	int luMgmtUrlLen = 0;
11238fe96085Stim szeto 	int sluBufSize = 0;
11248fe96085Stim szeto 	int bufOffset = 0;
11258fe96085Stim szeto 	int fd = 0;
11268fe96085Stim szeto 	int ioctlRet;
11278fe96085Stim szeto 	int savedErrno;
11288fe96085Stim szeto 	stmfGuid guid;
11298fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
11308fe96085Stim szeto 
11318fe96085Stim szeto 	sbd_create_and_reg_lu_t *sbdLu = NULL;
11328fe96085Stim szeto 
11338fe96085Stim szeto 	/*
11348fe96085Stim szeto 	 * Open control node for sbd
11358fe96085Stim szeto 	 */
11368fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
11378fe96085Stim szeto 		return (ret);
11388fe96085Stim szeto 
11398fe96085Stim szeto 	/* data file name must be specified */
11408fe96085Stim szeto 	if (disk->luDataFileNameValid) {
11418fe96085Stim szeto 		dataFileNameLen = strlen(disk->luDataFileName);
11428fe96085Stim szeto 	} else {
11438fe96085Stim szeto 		(void) close(fd);
11448fe96085Stim szeto 		return (STMF_ERROR_MISSING_PROP_VAL);
11458fe96085Stim szeto 	}
11468fe96085Stim szeto 
11478fe96085Stim szeto 	sluBufSize += dataFileNameLen + 1;
11488fe96085Stim szeto 
11498fe96085Stim szeto 	if (disk->luMetaFileNameValid) {
11508fe96085Stim szeto 		metaFileNameLen = strlen(disk->luMetaFileName);
11518fe96085Stim szeto 		sluBufSize += metaFileNameLen + 1;
11528fe96085Stim szeto 	}
11538fe96085Stim szeto 
11548fe96085Stim szeto 	serialNumLen = strlen(disk->serialNum);
11558fe96085Stim szeto 	sluBufSize += serialNumLen;
11568fe96085Stim szeto 
11578fe96085Stim szeto 	if (disk->luAliasValid) {
11588fe96085Stim szeto 		luAliasLen = strlen(disk->luAlias);
11598fe96085Stim szeto 		sluBufSize += luAliasLen + 1;
11608fe96085Stim szeto 	}
11618fe96085Stim szeto 
11622f624233SNattuvetty Bhavyan 	if (disk->luMgmtUrlValid) {
11632f624233SNattuvetty Bhavyan 		luMgmtUrlLen = strlen(disk->luMgmtUrl);
11642f624233SNattuvetty Bhavyan 		sluBufSize += luMgmtUrlLen + 1;
11652f624233SNattuvetty Bhavyan 	}
11662f624233SNattuvetty Bhavyan 
11678fe96085Stim szeto 	/*
11688fe96085Stim szeto 	 * 8 is the size of the buffer set aside for
11698fe96085Stim szeto 	 * concatenation of variable length fields
11708fe96085Stim szeto 	 */
11718fe96085Stim szeto 	sbdLu = (sbd_create_and_reg_lu_t *)calloc(1,
11728fe96085Stim szeto 	    sizeof (sbd_create_and_reg_lu_t) + sluBufSize - 8);
11738fe96085Stim szeto 	if (sbdLu == NULL) {
11748fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
11758fe96085Stim szeto 	}
11768fe96085Stim szeto 
11778fe96085Stim szeto 	sbdLu->slu_struct_size = sizeof (sbd_create_and_reg_lu_t) +
11788fe96085Stim szeto 	    sluBufSize - 8;
11798fe96085Stim szeto 
11808fe96085Stim szeto 	if (metaFileNameLen) {
11818fe96085Stim szeto 		sbdLu->slu_meta_fname_valid = 1;
11828fe96085Stim szeto 		sbdLu->slu_meta_fname_off = bufOffset;
11838fe96085Stim szeto 		bcopy(disk->luMetaFileName, &(sbdLu->slu_buf[bufOffset]),
11848fe96085Stim szeto 		    metaFileNameLen + 1);
11858fe96085Stim szeto 		bufOffset += metaFileNameLen + 1;
11868fe96085Stim szeto 	}
11878fe96085Stim szeto 
11888fe96085Stim szeto 	bcopy(disk->luDataFileName, &(sbdLu->slu_buf[bufOffset]),
11898fe96085Stim szeto 	    dataFileNameLen + 1);
11908fe96085Stim szeto 	sbdLu->slu_data_fname_off = bufOffset;
11918fe96085Stim szeto 	bufOffset += dataFileNameLen + 1;
11928fe96085Stim szeto 
11938fe96085Stim szeto 	/* currently, serial # is not passed null terminated to the driver */
11948fe96085Stim szeto 	if (disk->serialNumValid) {
11958fe96085Stim szeto 		sbdLu->slu_serial_valid = 1;
11968fe96085Stim szeto 		sbdLu->slu_serial_off = bufOffset;
11978fe96085Stim szeto 		sbdLu->slu_serial_size = serialNumLen;
11988fe96085Stim szeto 		bcopy(disk->serialNum, &(sbdLu->slu_buf[bufOffset]),
11998fe96085Stim szeto 		    serialNumLen);
12008fe96085Stim szeto 		bufOffset += serialNumLen;
12018fe96085Stim szeto 	}
12028fe96085Stim szeto 
12038fe96085Stim szeto 	if (disk->luAliasValid) {
12048fe96085Stim szeto 		sbdLu->slu_alias_valid = 1;
12058fe96085Stim szeto 		sbdLu->slu_alias_off = bufOffset;
12068fe96085Stim szeto 		bcopy(disk->luAlias, &(sbdLu->slu_buf[bufOffset]),
12078fe96085Stim szeto 		    luAliasLen + 1);
12088fe96085Stim szeto 		bufOffset += luAliasLen + 1;
12098fe96085Stim szeto 	}
12108fe96085Stim szeto 
12112f624233SNattuvetty Bhavyan 	if (disk->luMgmtUrlValid) {
12122f624233SNattuvetty Bhavyan 		sbdLu->slu_mgmt_url_valid = 1;
12132f624233SNattuvetty Bhavyan 		sbdLu->slu_mgmt_url_off = bufOffset;
12142f624233SNattuvetty Bhavyan 		bcopy(disk->luMgmtUrl, &(sbdLu->slu_buf[bufOffset]),
12152f624233SNattuvetty Bhavyan 		    luMgmtUrlLen + 1);
12162f624233SNattuvetty Bhavyan 		bufOffset += luMgmtUrlLen + 1;
12172f624233SNattuvetty Bhavyan 	}
12182f624233SNattuvetty Bhavyan 
12198fe96085Stim szeto 	if (disk->luSizeValid) {
12208fe96085Stim szeto 		sbdLu->slu_lu_size_valid = 1;
12218fe96085Stim szeto 		sbdLu->slu_lu_size = disk->luSize;
12228fe96085Stim szeto 	}
12238fe96085Stim szeto 
12248fe96085Stim szeto 	if (disk->luGuidValid) {
12258fe96085Stim szeto 		sbdLu->slu_guid_valid = 1;
12268fe96085Stim szeto 		bcopy(disk->luGuid, sbdLu->slu_guid, sizeof (disk->luGuid));
12278fe96085Stim szeto 	}
12288fe96085Stim szeto 
12298fe96085Stim szeto 	if (disk->vidValid) {
12308fe96085Stim szeto 		sbdLu->slu_vid_valid = 1;
12318fe96085Stim szeto 		bcopy(disk->vid, sbdLu->slu_vid, sizeof (disk->vid));
12328fe96085Stim szeto 	}
12338fe96085Stim szeto 
12348fe96085Stim szeto 	if (disk->pidValid) {
12358fe96085Stim szeto 		sbdLu->slu_pid_valid = 1;
12368fe96085Stim szeto 		bcopy(disk->pid, sbdLu->slu_pid, sizeof (disk->pid));
12378fe96085Stim szeto 	}
12388fe96085Stim szeto 
12398fe96085Stim szeto 	if (disk->revValid) {
12408fe96085Stim szeto 		sbdLu->slu_rev_valid = 1;
12418fe96085Stim szeto 		bcopy(disk->rev, sbdLu->slu_rev, sizeof (disk->rev));
12428fe96085Stim szeto 	}
12438fe96085Stim szeto 
12448fe96085Stim szeto 	if (disk->companyIdValid) {
12458fe96085Stim szeto 		sbdLu->slu_company_id_valid = 1;
12468fe96085Stim szeto 		sbdLu->slu_company_id = disk->companyId;
12478fe96085Stim szeto 	}
12488fe96085Stim szeto 
1249fdcc480aSJohn Forte 	if (disk->hostIdValid) {
1250fdcc480aSJohn Forte 		sbdLu->slu_host_id_valid = 1;
1251fdcc480aSJohn Forte 		sbdLu->slu_host_id = disk->hostId;
1252fdcc480aSJohn Forte 	}
1253fdcc480aSJohn Forte 
12548fe96085Stim szeto 	if (disk->blkSizeValid) {
12558fe96085Stim szeto 		sbdLu->slu_blksize_valid = 1;
12568fe96085Stim szeto 		sbdLu->slu_blksize = disk->blkSize;
12578fe96085Stim szeto 	}
12588fe96085Stim szeto 
12598fe96085Stim szeto 	if (disk->writeProtectEnableValid) {
12608fe96085Stim szeto 		if (disk->writeProtectEnable) {
12618fe96085Stim szeto 			sbdLu->slu_write_protected = 1;
12628fe96085Stim szeto 		}
12638fe96085Stim szeto 	}
12648fe96085Stim szeto 
12658fe96085Stim szeto 	if (disk->writebackCacheDisableValid) {
12668fe96085Stim szeto 		sbdLu->slu_writeback_cache_disable_valid = 1;
12678fe96085Stim szeto 		if (disk->writebackCacheDisable) {
12688fe96085Stim szeto 			sbdLu->slu_writeback_cache_disable = 1;
12698fe96085Stim szeto 		}
12708fe96085Stim szeto 	}
12718fe96085Stim szeto 
12728fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
12738fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdLu->slu_struct_size;
12748fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdLu;
12758fe96085Stim szeto 	sbdIoctl.stmf_obuf_size = sbdLu->slu_struct_size;
12768fe96085Stim szeto 	sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdLu;
12778fe96085Stim szeto 
12788fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_CREATE_AND_REGISTER_LU, &sbdIoctl);
12798fe96085Stim szeto 	if (ioctlRet != 0) {
12808fe96085Stim szeto 		savedErrno = errno;
12818fe96085Stim szeto 		switch (savedErrno) {
12828fe96085Stim szeto 			case EBUSY:
12838fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
12848fe96085Stim szeto 				break;
12858fe96085Stim szeto 			case EPERM:
12868fe96085Stim szeto 			case EACCES:
12878fe96085Stim szeto 				ret = STMF_ERROR_PERM;
12888fe96085Stim szeto 				break;
12898fe96085Stim szeto 			default:
12908fe96085Stim szeto 				diskError(sbdIoctl.stmf_error, &ret);
12918fe96085Stim szeto 				if (ret == STMF_STATUS_ERROR) {
12928fe96085Stim szeto 					syslog(LOG_DEBUG,
12938fe96085Stim szeto 					"createDiskLu:ioctl "
12948fe96085Stim szeto 					"error(%d) (%d) (%d)", ioctlRet,
12958fe96085Stim szeto 					    sbdIoctl.stmf_error, savedErrno);
12968fe96085Stim szeto 				}
12978fe96085Stim szeto 				break;
12988fe96085Stim szeto 		}
12998fe96085Stim szeto 	}
13008fe96085Stim szeto 
13018fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
13028fe96085Stim szeto 		goto done;
13038fe96085Stim szeto 	}
13048fe96085Stim szeto 
13058fe96085Stim szeto 	/*
13068fe96085Stim szeto 	 * on success, copy the resulting guid into the caller's guid if not
13078fe96085Stim szeto 	 * NULL
13088fe96085Stim szeto 	 */
13098fe96085Stim szeto 	if (createdGuid) {
13108fe96085Stim szeto 		bcopy(sbdLu->slu_guid, createdGuid->guid,
13118fe96085Stim szeto 		    sizeof (sbdLu->slu_guid));
13128fe96085Stim szeto 	}
13138fe96085Stim szeto 
13148fe96085Stim szeto 	bcopy(sbdLu->slu_guid, guid.guid, sizeof (sbdLu->slu_guid));
13158fe96085Stim szeto 	if (disk->luMetaFileNameValid) {
13168fe96085Stim szeto 		ret = addGuidToDiskStore(&guid, disk->luMetaFileName);
13178fe96085Stim szeto 	} else {
13188fe96085Stim szeto 		ret = addGuidToDiskStore(&guid, disk->luDataFileName);
13198fe96085Stim szeto 	}
13208fe96085Stim szeto done:
13218fe96085Stim szeto 	free(sbdLu);
13228fe96085Stim szeto 	(void) close(fd);
13238fe96085Stim szeto 	return (ret);
13248fe96085Stim szeto }
13258fe96085Stim szeto 
13268fe96085Stim szeto 
13278fe96085Stim szeto /*
13288fe96085Stim szeto  * stmfImportLu
13298fe96085Stim szeto  *
13308fe96085Stim szeto  * Purpose: Import a previously created logical unit
13318fe96085Stim szeto  *
13328fe96085Stim szeto  * dType - Type of logical unit
13338fe96085Stim szeto  *         Can be: STMF_DISK
13348fe96085Stim szeto  *
13358fe96085Stim szeto  * luGuid - If non-NULL, on success, contains the guid of the imported logical
13368fe96085Stim szeto  *	    unit
13378fe96085Stim szeto  *
13388fe96085Stim szeto  * fname - A file name where the metadata resides
13398fe96085Stim szeto  *
13408fe96085Stim szeto  */
13418fe96085Stim szeto int
13428fe96085Stim szeto stmfImportLu(uint16_t dType, char *fname, stmfGuid *luGuid)
13438fe96085Stim szeto {
13448fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
13458fe96085Stim szeto 
13468fe96085Stim szeto 	if (dType == STMF_DISK) {
13478fe96085Stim szeto 		ret = importDiskLu(fname, luGuid);
13488fe96085Stim szeto 	} else {
13498fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
13508fe96085Stim szeto 	}
13518fe96085Stim szeto 
13528fe96085Stim szeto 	return (ret);
13538fe96085Stim szeto }
13548fe96085Stim szeto 
13558fe96085Stim szeto /*
13568fe96085Stim szeto  * importDiskLu
13578fe96085Stim szeto  *
13588fe96085Stim szeto  * filename - filename to import
13598fe96085Stim szeto  * createdGuid - if not NULL, on success contains the imported guid
13608fe96085Stim szeto  *
13618fe96085Stim szeto  */
13628fe96085Stim szeto static int
13638fe96085Stim szeto importDiskLu(char *fname, stmfGuid *createdGuid)
13648fe96085Stim szeto {
13658fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
13668fe96085Stim szeto 	int fd = 0;
13678fe96085Stim szeto 	int ioctlRet;
13688fe96085Stim szeto 	int savedErrno;
13698fe96085Stim szeto 	int metaFileNameLen;
13708fe96085Stim szeto 	stmfGuid iGuid;
13718fe96085Stim szeto 	int iluBufSize = 0;
13728fe96085Stim szeto 	sbd_import_lu_t *sbdLu = NULL;
13738fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
13748fe96085Stim szeto 
13758fe96085Stim szeto 	if (fname == NULL) {
13768fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
13778fe96085Stim szeto 	}
13788fe96085Stim szeto 
13798fe96085Stim szeto 	/*
13808fe96085Stim szeto 	 * Open control node for sbd
13818fe96085Stim szeto 	 */
13828fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
13838fe96085Stim szeto 		return (ret);
13848fe96085Stim szeto 
13858fe96085Stim szeto 	metaFileNameLen = strlen(fname);
13868fe96085Stim szeto 	iluBufSize += metaFileNameLen + 1;
13878fe96085Stim szeto 
13888fe96085Stim szeto 	/*
13898fe96085Stim szeto 	 * 8 is the size of the buffer set aside for
13908fe96085Stim szeto 	 * concatenation of variable length fields
13918fe96085Stim szeto 	 */
13928fe96085Stim szeto 	sbdLu = (sbd_import_lu_t *)calloc(1,
13938fe96085Stim szeto 	    sizeof (sbd_import_lu_t) + iluBufSize - 8);
13948fe96085Stim szeto 	if (sbdLu == NULL) {
13958fe96085Stim szeto 		(void) close(fd);
13968fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
13978fe96085Stim szeto 	}
13988fe96085Stim szeto 
13998fe96085Stim szeto 	/*
14008fe96085Stim szeto 	 * Accept either a data file or meta data file.
14018fe96085Stim szeto 	 * sbd will do the right thing here either way.
14028fe96085Stim szeto 	 * i.e. if it's a data file, it assumes that the
14038fe96085Stim szeto 	 * meta data is shared with the data.
14048fe96085Stim szeto 	 */
14058fe96085Stim szeto 	(void) strncpy(sbdLu->ilu_meta_fname, fname, metaFileNameLen);
14068fe96085Stim szeto 
14078fe96085Stim szeto 	sbdLu->ilu_struct_size = sizeof (sbd_import_lu_t) + iluBufSize - 8;
14088fe96085Stim szeto 
14098fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
14108fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdLu->ilu_struct_size;
14118fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdLu;
14128fe96085Stim szeto 	sbdIoctl.stmf_obuf_size = sbdLu->ilu_struct_size;
14138fe96085Stim szeto 	sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdLu;
14148fe96085Stim szeto 
14158fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_IMPORT_LU, &sbdIoctl);
14168fe96085Stim szeto 	if (ioctlRet != 0) {
1417a1837c6dSsrivijitha dugganapalli 
1418a1837c6dSsrivijitha dugganapalli 		if (createdGuid && sbdIoctl.stmf_error ==
1419a1837c6dSsrivijitha dugganapalli 		    SBD_RET_FILE_ALREADY_REGISTERED) {
1420a1837c6dSsrivijitha dugganapalli 			bcopy(sbdLu->ilu_ret_guid, createdGuid->guid,
1421a1837c6dSsrivijitha dugganapalli 			    sizeof (sbdLu->ilu_ret_guid));
1422a1837c6dSsrivijitha dugganapalli 		}
1423a1837c6dSsrivijitha dugganapalli 
14248fe96085Stim szeto 		savedErrno = errno;
14258fe96085Stim szeto 		switch (savedErrno) {
14268fe96085Stim szeto 			case EBUSY:
14278fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
14288fe96085Stim szeto 				break;
14298fe96085Stim szeto 			case EPERM:
14308fe96085Stim szeto 			case EACCES:
14318fe96085Stim szeto 				ret = STMF_ERROR_PERM;
14328fe96085Stim szeto 				break;
14338fe96085Stim szeto 			default:
14348fe96085Stim szeto 				diskError(sbdIoctl.stmf_error, &ret);
14358fe96085Stim szeto 				if (ret == STMF_STATUS_ERROR) {
14368fe96085Stim szeto 					syslog(LOG_DEBUG,
14378fe96085Stim szeto 					"importDiskLu:ioctl "
14388fe96085Stim szeto 					"error(%d) (%d) (%d)", ioctlRet,
14398fe96085Stim szeto 					    sbdIoctl.stmf_error, savedErrno);
14408fe96085Stim szeto 				}
14418fe96085Stim szeto 				break;
14428fe96085Stim szeto 		}
14438fe96085Stim szeto 	}
14448fe96085Stim szeto 
1445a1837c6dSsrivijitha dugganapalli 
14468fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
14478fe96085Stim szeto 		goto done;
14488fe96085Stim szeto 	}
14498fe96085Stim szeto 
14508fe96085Stim szeto 	/*
14518fe96085Stim szeto 	 * on success, copy the resulting guid into the caller's guid if not
14528fe96085Stim szeto 	 * NULL and add it to the persistent store for sbd
14538fe96085Stim szeto 	 */
14548fe96085Stim szeto 	if (createdGuid) {
14558fe96085Stim szeto 		bcopy(sbdLu->ilu_ret_guid, createdGuid->guid,
14568fe96085Stim szeto 		    sizeof (sbdLu->ilu_ret_guid));
14578fe96085Stim szeto 		ret = addGuidToDiskStore(createdGuid, fname);
14588fe96085Stim szeto 	} else {
14598fe96085Stim szeto 		bcopy(sbdLu->ilu_ret_guid, iGuid.guid,
14608fe96085Stim szeto 		    sizeof (sbdLu->ilu_ret_guid));
14618fe96085Stim szeto 		ret = addGuidToDiskStore(&iGuid, fname);
14628fe96085Stim szeto 	}
14638fe96085Stim szeto done:
14648fe96085Stim szeto 	free(sbdLu);
14658fe96085Stim szeto 	(void) close(fd);
14668fe96085Stim szeto 	return (ret);
14678fe96085Stim szeto }
14688fe96085Stim szeto 
14698fe96085Stim szeto /*
14708fe96085Stim szeto  * diskError
14718fe96085Stim szeto  *
14728fe96085Stim szeto  * Purpose: Translate sbd driver error
14738fe96085Stim szeto  */
14748fe96085Stim szeto static void
14758fe96085Stim szeto diskError(uint32_t stmfError, int *ret)
14768fe96085Stim szeto {
14778fe96085Stim szeto 	switch (stmfError) {
14788fe96085Stim szeto 		case SBD_RET_META_CREATION_FAILED:
14798fe96085Stim szeto 		case SBD_RET_ZFS_META_CREATE_FAILED:
14808fe96085Stim szeto 			*ret = STMF_ERROR_META_CREATION;
14818fe96085Stim szeto 			break;
14828fe96085Stim szeto 		case SBD_RET_INVALID_BLKSIZE:
14838fe96085Stim szeto 			*ret = STMF_ERROR_INVALID_BLKSIZE;
14848fe96085Stim szeto 			break;
14858fe96085Stim szeto 		case SBD_RET_FILE_ALREADY_REGISTERED:
14868fe96085Stim szeto 			*ret = STMF_ERROR_FILE_IN_USE;
14878fe96085Stim szeto 			break;
14888fe96085Stim szeto 		case SBD_RET_GUID_ALREADY_REGISTERED:
14898fe96085Stim szeto 			*ret = STMF_ERROR_GUID_IN_USE;
14908fe96085Stim szeto 			break;
14918fe96085Stim szeto 		case SBD_RET_META_PATH_NOT_ABSOLUTE:
14928fe96085Stim szeto 		case SBD_RET_META_FILE_LOOKUP_FAILED:
14938fe96085Stim szeto 		case SBD_RET_META_FILE_OPEN_FAILED:
14948fe96085Stim szeto 		case SBD_RET_META_FILE_GETATTR_FAILED:
14958fe96085Stim szeto 		case SBD_RET_NO_META:
14968fe96085Stim szeto 			*ret = STMF_ERROR_META_FILE_NAME;
14978fe96085Stim szeto 			break;
14988fe96085Stim szeto 		case SBD_RET_DATA_PATH_NOT_ABSOLUTE:
14998fe96085Stim szeto 		case SBD_RET_DATA_FILE_LOOKUP_FAILED:
15008fe96085Stim szeto 		case SBD_RET_DATA_FILE_OPEN_FAILED:
15018fe96085Stim szeto 		case SBD_RET_DATA_FILE_GETATTR_FAILED:
15028fe96085Stim szeto 			*ret = STMF_ERROR_DATA_FILE_NAME;
15038fe96085Stim szeto 			break;
15048fe96085Stim szeto 		case SBD_RET_FILE_SIZE_ERROR:
15058fe96085Stim szeto 			*ret = STMF_ERROR_FILE_SIZE_INVALID;
15068fe96085Stim szeto 			break;
15078fe96085Stim szeto 		case SBD_RET_SIZE_OUT_OF_RANGE:
15088fe96085Stim szeto 			*ret = STMF_ERROR_SIZE_OUT_OF_RANGE;
15098fe96085Stim szeto 			break;
15108fe96085Stim szeto 		case SBD_RET_LU_BUSY:
15118fe96085Stim szeto 			*ret = STMF_ERROR_LU_BUSY;
15128fe96085Stim szeto 			break;
15138fe96085Stim szeto 		case SBD_RET_WRITE_CACHE_SET_FAILED:
15148fe96085Stim szeto 			*ret = STMF_ERROR_WRITE_CACHE_SET;
15158fe96085Stim szeto 			break;
151645039663SJohn Forte 		case SBD_RET_ACCESS_STATE_FAILED:
151745039663SJohn Forte 			*ret = STMF_ERROR_ACCESS_STATE_SET;
151845039663SJohn Forte 			break;
15198fe96085Stim szeto 		default:
15208fe96085Stim szeto 			*ret = STMF_STATUS_ERROR;
15218fe96085Stim szeto 			break;
15228fe96085Stim szeto 	}
15238fe96085Stim szeto }
15248fe96085Stim szeto 
15258fe96085Stim szeto /*
15268fe96085Stim szeto  * Creates a logical unit resource of type STMF_DISK.
15278fe96085Stim szeto  *
15288fe96085Stim szeto  * No defaults should be set here as all defaults are derived from the
15298fe96085Stim szeto  * driver's default settings.
15308fe96085Stim szeto  */
15318fe96085Stim szeto static int
15328fe96085Stim szeto createDiskResource(luResourceImpl *hdl)
15338fe96085Stim szeto {
15348fe96085Stim szeto 	hdl->type = STMF_DISK;
15358fe96085Stim szeto 
15368fe96085Stim szeto 	hdl->resource = calloc(1, sizeof (diskResource));
15378fe96085Stim szeto 	if (hdl->resource == NULL) {
15388fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
15398fe96085Stim szeto 	}
15408fe96085Stim szeto 
15418fe96085Stim szeto 	return (STMF_STATUS_SUCCESS);
15428fe96085Stim szeto }
15438fe96085Stim szeto 
15448fe96085Stim szeto /*
15458fe96085Stim szeto  * stmfDeleteLu
15468fe96085Stim szeto  *
15478fe96085Stim szeto  * Purpose: Delete a logical unit
15488fe96085Stim szeto  *
15498fe96085Stim szeto  * hdl - handle to logical unit resource created via stmfCreateLuResource
15508fe96085Stim szeto  *
15518fe96085Stim szeto  * luGuid - If non-NULL, on success, contains the guid of the created logical
15528fe96085Stim szeto  *	    unit
15538fe96085Stim szeto  */
15548fe96085Stim szeto int
15558fe96085Stim szeto stmfDeleteLu(stmfGuid *luGuid)
15568fe96085Stim szeto {
15578fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
15588fe96085Stim szeto 	stmfLogicalUnitProperties luProps;
15598fe96085Stim szeto 
15608fe96085Stim szeto 	if (luGuid == NULL) {
15618fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
15628fe96085Stim szeto 	}
15638fe96085Stim szeto 
15648fe96085Stim szeto 	/* Check logical unit provider name to call correct dtype function */
15658fe96085Stim szeto 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
15668fe96085Stim szeto 	    != STMF_STATUS_SUCCESS) {
15678fe96085Stim szeto 		return (ret);
15688fe96085Stim szeto 	} else {
15698fe96085Stim szeto 		if (strcmp(luProps.providerName, "sbd") == 0) {
15708fe96085Stim szeto 			ret = deleteDiskLu(luGuid);
15718fe96085Stim szeto 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
15728fe96085Stim szeto 			return (STMF_ERROR_NOT_FOUND);
15738fe96085Stim szeto 		} else {
15748fe96085Stim szeto 			return (STMF_ERROR_INVALID_ARG);
15758fe96085Stim szeto 		}
15768fe96085Stim szeto 	}
15778fe96085Stim szeto 
15788fe96085Stim szeto 	return (ret);
15798fe96085Stim szeto }
15808fe96085Stim szeto 
15818fe96085Stim szeto static int
15828fe96085Stim szeto deleteDiskLu(stmfGuid *luGuid)
15838fe96085Stim szeto {
15848fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
15858fe96085Stim szeto 	int fd;
15868fe96085Stim szeto 	int savedErrno;
15878fe96085Stim szeto 	int ioctlRet;
15888fe96085Stim szeto 	sbd_delete_lu_t deleteLu = {0};
15898fe96085Stim szeto 
15908fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
15918fe96085Stim szeto 
15928fe96085Stim szeto 	/*
15938fe96085Stim szeto 	 * Open control node for sbd
15948fe96085Stim szeto 	 */
15958fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
15968fe96085Stim szeto 		return (ret);
15978fe96085Stim szeto 
15988fe96085Stim szeto 	ret = removeGuidFromDiskStore(luGuid);
15998fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
16008fe96085Stim szeto 		goto done;
16018fe96085Stim szeto 	}
16028fe96085Stim szeto 
16038fe96085Stim szeto 	bcopy(luGuid, deleteLu.dlu_guid, sizeof (deleteLu.dlu_guid));
16048fe96085Stim szeto 	deleteLu.dlu_by_guid = 1;
16058fe96085Stim szeto 
16068fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
16078fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sizeof (deleteLu);
16088fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)&deleteLu;
16098fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_DELETE_LU, &sbdIoctl);
16108fe96085Stim szeto 	if (ioctlRet != 0) {
16118fe96085Stim szeto 		savedErrno = errno;
16128fe96085Stim szeto 		switch (savedErrno) {
16138fe96085Stim szeto 			case EBUSY:
16148fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
16158fe96085Stim szeto 				break;
16168fe96085Stim szeto 			case EPERM:
16178fe96085Stim szeto 			case EACCES:
16188fe96085Stim szeto 				ret = STMF_ERROR_PERM;
16198fe96085Stim szeto 				break;
16208fe96085Stim szeto 			case ENOENT:
16218fe96085Stim szeto 				ret = STMF_ERROR_NOT_FOUND;
16228fe96085Stim szeto 				break;
16238fe96085Stim szeto 			default:
16248fe96085Stim szeto 				syslog(LOG_DEBUG,
16258fe96085Stim szeto 				    "deleteDiskLu:ioctl error(%d) (%d) (%d)",
16268fe96085Stim szeto 				    ioctlRet, sbdIoctl.stmf_error, savedErrno);
16278fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
16288fe96085Stim szeto 				break;
16298fe96085Stim szeto 		}
16308fe96085Stim szeto 	}
16318fe96085Stim szeto 
16328fe96085Stim szeto done:
16338fe96085Stim szeto 	(void) close(fd);
16348fe96085Stim szeto 	return (ret);
16358fe96085Stim szeto }
16368fe96085Stim szeto 
16378fe96085Stim szeto /*
163845039663SJohn Forte  * stmfLuStandby
163945039663SJohn Forte  *
164045039663SJohn Forte  * Purpose: Sets access state to standby
164145039663SJohn Forte  *
164245039663SJohn Forte  * luGuid - guid of registered logical unit
164345039663SJohn Forte  *
164445039663SJohn Forte  */
164545039663SJohn Forte int
164645039663SJohn Forte stmfLuStandby(stmfGuid *luGuid)
164745039663SJohn Forte {
164845039663SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
164945039663SJohn Forte 	stmfLogicalUnitProperties luProps;
165045039663SJohn Forte 
165145039663SJohn Forte 	if (luGuid == NULL) {
165245039663SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
165345039663SJohn Forte 	}
165445039663SJohn Forte 
165545039663SJohn Forte 	/* Check logical unit provider name to call correct dtype function */
165645039663SJohn Forte 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
165745039663SJohn Forte 	    != STMF_STATUS_SUCCESS) {
165845039663SJohn Forte 		return (ret);
165945039663SJohn Forte 	} else {
166045039663SJohn Forte 		if (strcmp(luProps.providerName, "sbd") == 0) {
166145039663SJohn Forte 			ret = setDiskStandby(luGuid);
166245039663SJohn Forte 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
166345039663SJohn Forte 			return (STMF_ERROR_NOT_FOUND);
166445039663SJohn Forte 		} else {
166545039663SJohn Forte 			return (STMF_ERROR_INVALID_ARG);
166645039663SJohn Forte 		}
166745039663SJohn Forte 	}
166845039663SJohn Forte 
166945039663SJohn Forte 	return (ret);
167045039663SJohn Forte }
167145039663SJohn Forte 
167245039663SJohn Forte static int
167345039663SJohn Forte setDiskStandby(stmfGuid *luGuid)
167445039663SJohn Forte {
167545039663SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
167645039663SJohn Forte 	stmf_iocdata_t sbdIoctl = {0};
167745039663SJohn Forte 	sbd_set_lu_standby_t sbdLu = {0};
167845039663SJohn Forte 	int ioctlRet;
167945039663SJohn Forte 	int savedErrno;
168045039663SJohn Forte 	int fd = 0;
168145039663SJohn Forte 
168245039663SJohn Forte 	/*
168345039663SJohn Forte 	 * Open control node for sbd
168445039663SJohn Forte 	 */
168545039663SJohn Forte 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
168645039663SJohn Forte 		return (ret);
168745039663SJohn Forte 
168845039663SJohn Forte 	bcopy(luGuid, &sbdLu.stlu_guid, sizeof (stmfGuid));
168945039663SJohn Forte 
169045039663SJohn Forte 	sbdIoctl.stmf_version = STMF_VERSION_1;
169145039663SJohn Forte 	sbdIoctl.stmf_ibuf_size = sizeof (sbd_set_lu_standby_t);
169245039663SJohn Forte 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)&sbdLu;
169345039663SJohn Forte 
169445039663SJohn Forte 	ioctlRet = ioctl(fd, SBD_IOCTL_SET_LU_STANDBY, &sbdIoctl);
169545039663SJohn Forte 	if (ioctlRet != 0) {
169645039663SJohn Forte 		savedErrno = errno;
169745039663SJohn Forte 		switch (savedErrno) {
169845039663SJohn Forte 			case EBUSY:
169945039663SJohn Forte 				ret = STMF_ERROR_BUSY;
170045039663SJohn Forte 				break;
170145039663SJohn Forte 			case EPERM:
170245039663SJohn Forte 			case EACCES:
170345039663SJohn Forte 				ret = STMF_ERROR_PERM;
170445039663SJohn Forte 				break;
170545039663SJohn Forte 			default:
170645039663SJohn Forte 				diskError(sbdIoctl.stmf_error, &ret);
170745039663SJohn Forte 				if (ret == STMF_STATUS_ERROR) {
170845039663SJohn Forte 					syslog(LOG_DEBUG,
170945039663SJohn Forte 					"setDiskStandby:ioctl "
171045039663SJohn Forte 					"error(%d) (%d) (%d)", ioctlRet,
171145039663SJohn Forte 					    sbdIoctl.stmf_error, savedErrno);
171245039663SJohn Forte 				}
171345039663SJohn Forte 				break;
171445039663SJohn Forte 		}
171545039663SJohn Forte 	}
1716*5b0f0869SJohn Forte 	(void) close(fd);
171745039663SJohn Forte 	return (ret);
171845039663SJohn Forte }
171945039663SJohn Forte 
172045039663SJohn Forte /*
17218fe96085Stim szeto  * stmfModifyLu
17228fe96085Stim szeto  *
17238fe96085Stim szeto  * Purpose: Modify properties of a logical unit
17248fe96085Stim szeto  *
17258fe96085Stim szeto  * luGuid - guid of registered logical unit
17268fe96085Stim szeto  * prop - property to modify
17278fe96085Stim szeto  * propVal - property value to set
17288fe96085Stim szeto  *
17298fe96085Stim szeto  */
17308fe96085Stim szeto int
17318fe96085Stim szeto stmfModifyLu(stmfGuid *luGuid, uint32_t prop, const char *propVal)
17328fe96085Stim szeto {
17338fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
17348fe96085Stim szeto 	stmfLogicalUnitProperties luProps;
17358fe96085Stim szeto 
17368fe96085Stim szeto 	if (luGuid == NULL) {
17378fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
17388fe96085Stim szeto 	}
17398fe96085Stim szeto 
17408fe96085Stim szeto 	/* Check logical unit provider name to call correct dtype function */
17418fe96085Stim szeto 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
17428fe96085Stim szeto 	    != STMF_STATUS_SUCCESS) {
17438fe96085Stim szeto 		return (ret);
17448fe96085Stim szeto 	} else {
17458fe96085Stim szeto 		if (strcmp(luProps.providerName, "sbd") == 0) {
17468fe96085Stim szeto 			ret = modifyDiskLuProp(luGuid, NULL, prop, propVal);
17478fe96085Stim szeto 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
17488fe96085Stim szeto 			return (STMF_ERROR_NOT_FOUND);
17498fe96085Stim szeto 		} else {
17508fe96085Stim szeto 			return (STMF_ERROR_INVALID_ARG);
17518fe96085Stim szeto 		}
17528fe96085Stim szeto 	}
17538fe96085Stim szeto 
17548fe96085Stim szeto 	return (ret);
17558fe96085Stim szeto }
17568fe96085Stim szeto 
17578fe96085Stim szeto /*
17588fe96085Stim szeto  * stmfModifyLuByFname
17598fe96085Stim szeto  *
17608fe96085Stim szeto  * Purpose: Modify a device by filename. Device does not need to be registered.
17618fe96085Stim szeto  *
17628fe96085Stim szeto  * dType - type of device to modify
17638fe96085Stim szeto  *         STMF_DISK
17648fe96085Stim szeto  *
17658fe96085Stim szeto  * fname - filename or meta filename
17668fe96085Stim szeto  * prop - valid property identifier
17678fe96085Stim szeto  * propVal - property value
17688fe96085Stim szeto  *
17698fe96085Stim szeto  */
17708fe96085Stim szeto int
17718fe96085Stim szeto stmfModifyLuByFname(uint16_t dType, const char *fname, uint32_t prop,
17728fe96085Stim szeto     const char *propVal)
17738fe96085Stim szeto {
17748fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
17758fe96085Stim szeto 	if (fname == NULL) {
17768fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
17778fe96085Stim szeto 	}
17788fe96085Stim szeto 
17798fe96085Stim szeto 	if (dType == STMF_DISK) {
17808fe96085Stim szeto 		ret = modifyDiskLuProp(NULL, fname, prop, propVal);
17818fe96085Stim szeto 	} else {
17828fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
17838fe96085Stim szeto 	}
17848fe96085Stim szeto 
17858fe96085Stim szeto 	return (ret);
17868fe96085Stim szeto }
17878fe96085Stim szeto 
17888fe96085Stim szeto static int
17898fe96085Stim szeto modifyDiskLuProp(stmfGuid *luGuid, const char *fname, uint32_t prop,
17908fe96085Stim szeto     const char *propVal)
17918fe96085Stim szeto {
17928fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
17938fe96085Stim szeto 	luResource hdl = NULL;
17948fe96085Stim szeto 	luResourceImpl *luPropsHdl;
17958fe96085Stim szeto 
17968fe96085Stim szeto 	ret = stmfCreateLuResource(STMF_DISK, &hdl);
17978fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
17988fe96085Stim szeto 		return (ret);
17998fe96085Stim szeto 	}
18008fe96085Stim szeto 	ret = validateModifyDiskProp(prop);
18018fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
18028fe96085Stim szeto 		(void) stmfFreeLuResource(hdl);
18038fe96085Stim szeto 		return (STMF_ERROR_INVALID_PROP);
18048fe96085Stim szeto 	}
18058fe96085Stim szeto 	ret = stmfSetLuProp(hdl, prop, propVal);
18068fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
18078fe96085Stim szeto 		(void) stmfFreeLuResource(hdl);
18088fe96085Stim szeto 		return (ret);
18098fe96085Stim szeto 	}
18108fe96085Stim szeto 	luPropsHdl = hdl;
18118fe96085Stim szeto 	ret = modifyDiskLu((diskResource *)luPropsHdl->resource, luGuid, fname);
18128fe96085Stim szeto 	(void) stmfFreeLuResource(hdl);
18138fe96085Stim szeto 	return (ret);
18148fe96085Stim szeto }
18158fe96085Stim szeto 
18168fe96085Stim szeto static int
18178fe96085Stim szeto validateModifyDiskProp(uint32_t prop)
18188fe96085Stim szeto {
18198fe96085Stim szeto 	switch (prop) {
18208fe96085Stim szeto 		case STMF_LU_PROP_ALIAS:
18218fe96085Stim szeto 		case STMF_LU_PROP_SIZE:
18222f624233SNattuvetty Bhavyan 		case STMF_LU_PROP_MGMT_URL:
18238fe96085Stim szeto 		case STMF_LU_PROP_WRITE_PROTECT:
18248fe96085Stim szeto 		case STMF_LU_PROP_WRITE_CACHE_DISABLE:
18258fe96085Stim szeto 			return (STMF_STATUS_SUCCESS);
18268fe96085Stim szeto 			break;
18278fe96085Stim szeto 		default:
18288fe96085Stim szeto 			return (STMF_STATUS_ERROR);
18298fe96085Stim szeto 			break;
18308fe96085Stim szeto 	}
18318fe96085Stim szeto }
18328fe96085Stim szeto 
18338fe96085Stim szeto static int
18348fe96085Stim szeto modifyDiskLu(diskResource *disk, stmfGuid *luGuid, const char *fname)
18358fe96085Stim szeto {
18368fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
18378fe96085Stim szeto 	int luAliasLen = 0;
18382f624233SNattuvetty Bhavyan 	int luMgmtUrlLen = 0;
18398fe96085Stim szeto 	int mluBufSize = 0;
18408fe96085Stim szeto 	int bufOffset = 0;
18418fe96085Stim szeto 	int fd = 0;
18428fe96085Stim szeto 	int ioctlRet;
18438fe96085Stim szeto 	int savedErrno;
18448fe96085Stim szeto 	int fnameSize = 0;
18458fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
18468fe96085Stim szeto 
18478fe96085Stim szeto 	sbd_modify_lu_t *sbdLu = NULL;
18488fe96085Stim szeto 
18498fe96085Stim szeto 	if (luGuid == NULL && fname == NULL) {
18508fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
18518fe96085Stim szeto 	}
18528fe96085Stim szeto 
18538fe96085Stim szeto 	if (fname) {
18548fe96085Stim szeto 		fnameSize = strlen(fname) + 1;
18558fe96085Stim szeto 		mluBufSize += fnameSize;
18568fe96085Stim szeto 	}
18578fe96085Stim szeto 
18588fe96085Stim szeto 	/*
18598fe96085Stim szeto 	 * Open control node for sbd
18608fe96085Stim szeto 	 */
18618fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
18628fe96085Stim szeto 		return (ret);
18638fe96085Stim szeto 
18648fe96085Stim szeto 	if (disk->luAliasValid) {
18658fe96085Stim szeto 		luAliasLen = strlen(disk->luAlias);
18668fe96085Stim szeto 		mluBufSize += luAliasLen + 1;
18678fe96085Stim szeto 	}
18688fe96085Stim szeto 
18692f624233SNattuvetty Bhavyan 	if (disk->luMgmtUrlValid) {
18702f624233SNattuvetty Bhavyan 		luMgmtUrlLen = strlen(disk->luMgmtUrl);
18712f624233SNattuvetty Bhavyan 		mluBufSize += luMgmtUrlLen + 1;
18722f624233SNattuvetty Bhavyan 	}
18732f624233SNattuvetty Bhavyan 
18748fe96085Stim szeto 	/*
18758fe96085Stim szeto 	 * 8 is the size of the buffer set aside for
18768fe96085Stim szeto 	 * concatenation of variable length fields
18778fe96085Stim szeto 	 */
18788fe96085Stim szeto 	sbdLu = (sbd_modify_lu_t *)calloc(1,
18798fe96085Stim szeto 	    sizeof (sbd_modify_lu_t) + mluBufSize - 8 + fnameSize);
18808fe96085Stim szeto 	if (sbdLu == NULL) {
18818fe96085Stim szeto 		(void) close(fd);
18828fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
18838fe96085Stim szeto 	}
18848fe96085Stim szeto 
18858fe96085Stim szeto 	sbdLu->mlu_struct_size = sizeof (sbd_modify_lu_t) +
18868fe96085Stim szeto 	    mluBufSize - 8 + fnameSize;
18878fe96085Stim szeto 
18888fe96085Stim szeto 	if (disk->luAliasValid) {
18898fe96085Stim szeto 		sbdLu->mlu_alias_valid = 1;
18908fe96085Stim szeto 		sbdLu->mlu_alias_off = bufOffset;
18918fe96085Stim szeto 		bcopy(disk->luAlias, &(sbdLu->mlu_buf[bufOffset]),
18928fe96085Stim szeto 		    luAliasLen + 1);
18938fe96085Stim szeto 		bufOffset += luAliasLen + 1;
18948fe96085Stim szeto 	}
18958fe96085Stim szeto 
18962f624233SNattuvetty Bhavyan 	if (disk->luMgmtUrlValid) {
18972f624233SNattuvetty Bhavyan 		sbdLu->mlu_mgmt_url_valid = 1;
18982f624233SNattuvetty Bhavyan 		sbdLu->mlu_mgmt_url_off = bufOffset;
18992f624233SNattuvetty Bhavyan 		bcopy(disk->luMgmtUrl, &(sbdLu->mlu_buf[bufOffset]),
19002f624233SNattuvetty Bhavyan 		    luMgmtUrlLen + 1);
19012f624233SNattuvetty Bhavyan 		bufOffset += luMgmtUrlLen + 1;
19022f624233SNattuvetty Bhavyan 	}
19032f624233SNattuvetty Bhavyan 
19048fe96085Stim szeto 	if (disk->luSizeValid) {
19058fe96085Stim szeto 		sbdLu->mlu_lu_size_valid = 1;
19068fe96085Stim szeto 		sbdLu->mlu_lu_size = disk->luSize;
19078fe96085Stim szeto 	}
19088fe96085Stim szeto 
19098fe96085Stim szeto 	if (disk->writeProtectEnableValid) {
19108fe96085Stim szeto 		sbdLu->mlu_write_protected_valid = 1;
19118fe96085Stim szeto 		if (disk->writeProtectEnable) {
19128fe96085Stim szeto 			sbdLu->mlu_write_protected = 1;
19138fe96085Stim szeto 		}
19148fe96085Stim szeto 	}
19158fe96085Stim szeto 
19168fe96085Stim szeto 	if (disk->writebackCacheDisableValid) {
19178fe96085Stim szeto 		sbdLu->mlu_writeback_cache_disable_valid = 1;
19188fe96085Stim szeto 		if (disk->writebackCacheDisable) {
19198fe96085Stim szeto 			sbdLu->mlu_writeback_cache_disable = 1;
19208fe96085Stim szeto 		}
19218fe96085Stim szeto 	}
19228fe96085Stim szeto 
19238fe96085Stim szeto 	if (luGuid) {
19248fe96085Stim szeto 		bcopy(luGuid, sbdLu->mlu_input_guid, sizeof (stmfGuid));
19258fe96085Stim szeto 		sbdLu->mlu_by_guid = 1;
19268fe96085Stim szeto 	} else {
19278fe96085Stim szeto 		sbdLu->mlu_fname_off = bufOffset;
19288fe96085Stim szeto 		bcopy(fname, &(sbdLu->mlu_buf[bufOffset]), fnameSize + 1);
19298fe96085Stim szeto 		sbdLu->mlu_by_fname = 1;
19308fe96085Stim szeto 	}
19318fe96085Stim szeto 
19328fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
19338fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdLu->mlu_struct_size;
19348fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdLu;
19358fe96085Stim szeto 
19368fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_MODIFY_LU, &sbdIoctl);
19378fe96085Stim szeto 	if (ioctlRet != 0) {
19388fe96085Stim szeto 		savedErrno = errno;
19398fe96085Stim szeto 		switch (savedErrno) {
19408fe96085Stim szeto 			case EBUSY:
19418fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
19428fe96085Stim szeto 				break;
19438fe96085Stim szeto 			case EPERM:
19448fe96085Stim szeto 			case EACCES:
19458fe96085Stim szeto 				ret = STMF_ERROR_PERM;
19468fe96085Stim szeto 				break;
19478fe96085Stim szeto 			default:
19488fe96085Stim szeto 				diskError(sbdIoctl.stmf_error, &ret);
19498fe96085Stim szeto 				if (ret == STMF_STATUS_ERROR) {
19508fe96085Stim szeto 					syslog(LOG_DEBUG,
19518fe96085Stim szeto 					"modifyDiskLu:ioctl "
19528fe96085Stim szeto 					"error(%d) (%d) (%d)", ioctlRet,
19538fe96085Stim szeto 					    sbdIoctl.stmf_error, savedErrno);
19548fe96085Stim szeto 				}
19558fe96085Stim szeto 				break;
19568fe96085Stim szeto 		}
19578fe96085Stim szeto 	}
19588fe96085Stim szeto 
19598fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
19608fe96085Stim szeto 		goto done;
19618fe96085Stim szeto 	}
19628fe96085Stim szeto 
19638fe96085Stim szeto done:
19648fe96085Stim szeto 	free(sbdLu);
19658fe96085Stim szeto 	(void) close(fd);
19668fe96085Stim szeto 	return (ret);
19678fe96085Stim szeto }
19688fe96085Stim szeto 
19698fe96085Stim szeto /*
19708fe96085Stim szeto  * removeGuidFromDiskStore
19718fe96085Stim szeto  *
19728fe96085Stim szeto  * Purpose: delete a logical unit from the sbd provider data
19738fe96085Stim szeto  */
19748fe96085Stim szeto static int
19758fe96085Stim szeto removeGuidFromDiskStore(stmfGuid *guid)
19768fe96085Stim szeto {
19778fe96085Stim szeto 	return (persistDiskGuid(guid, NULL, B_FALSE));
19788fe96085Stim szeto }
19798fe96085Stim szeto 
19808fe96085Stim szeto 
19818fe96085Stim szeto /*
19828fe96085Stim szeto  * addGuidToDiskStore
19838fe96085Stim szeto  *
19848fe96085Stim szeto  * Purpose: add a logical unit to the sbd provider data
19858fe96085Stim szeto  */
19868fe96085Stim szeto static int
19878fe96085Stim szeto addGuidToDiskStore(stmfGuid *guid, char *filename)
19888fe96085Stim szeto {
19898fe96085Stim szeto 	return (persistDiskGuid(guid, filename, B_TRUE));
19908fe96085Stim szeto }
19918fe96085Stim szeto 
19928fe96085Stim szeto 
19938fe96085Stim szeto /*
19948fe96085Stim szeto  * persistDiskGuid
19958fe96085Stim szeto  *
19968fe96085Stim szeto  * Purpose: Persist or unpersist a guid for the sbd provider data
19978fe96085Stim szeto  *
19988fe96085Stim szeto  */
19998fe96085Stim szeto static int
20008fe96085Stim szeto persistDiskGuid(stmfGuid *guid, char *filename, boolean_t persist)
20018fe96085Stim szeto {
20028fe96085Stim szeto 	char	    guidAsciiBuf[LU_ASCII_GUID_SIZE + 1] = {0};
20038fe96085Stim szeto 	nvlist_t    *nvl = NULL;
20048fe96085Stim szeto 
20058fe96085Stim szeto 	uint64_t    setToken;
20068fe96085Stim szeto 	boolean_t   retryGetProviderData = B_FALSE;
20078fe96085Stim szeto 	boolean_t   newData = B_FALSE;
20088fe96085Stim szeto 	int	    ret = STMF_STATUS_SUCCESS;
20098fe96085Stim szeto 	int	    retryCnt = 0;
20108fe96085Stim szeto 	int	    stmfRet;
20118fe96085Stim szeto 
20128fe96085Stim szeto 	/* if we're persisting a guid, there must be a filename */
20138fe96085Stim szeto 	if (persist && !filename) {
20148fe96085Stim szeto 		return (1);
20158fe96085Stim szeto 	}
20168fe96085Stim szeto 
20178fe96085Stim szeto 	/* guid is stored in lowercase ascii hex */
20188fe96085Stim szeto 	(void) snprintf(guidAsciiBuf, sizeof (guidAsciiBuf),
20198fe96085Stim szeto 	    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
20208fe96085Stim szeto 	    "%02x%02x%02x%02x%02x%02x",
20218fe96085Stim szeto 	    guid->guid[0], guid->guid[1], guid->guid[2], guid->guid[3],
20228fe96085Stim szeto 	    guid->guid[4], guid->guid[5], guid->guid[6], guid->guid[7],
20238fe96085Stim szeto 	    guid->guid[8], guid->guid[9], guid->guid[10], guid->guid[11],
20248fe96085Stim szeto 	    guid->guid[12], guid->guid[13], guid->guid[14], guid->guid[15]);
20258fe96085Stim szeto 
20268fe96085Stim szeto 
20278fe96085Stim szeto 	do {
20288fe96085Stim szeto 		retryGetProviderData = B_FALSE;
20298fe96085Stim szeto 		stmfRet = stmfGetProviderDataProt("sbd", &nvl,
20308fe96085Stim szeto 		    STMF_LU_PROVIDER_TYPE, &setToken);
20318fe96085Stim szeto 		if (stmfRet != STMF_STATUS_SUCCESS) {
20328fe96085Stim szeto 			if (persist && stmfRet == STMF_ERROR_NOT_FOUND) {
20338fe96085Stim szeto 				ret = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
20348fe96085Stim szeto 				if (ret != 0) {
20358fe96085Stim szeto 					syslog(LOG_DEBUG,
20368fe96085Stim szeto 					    "unpersistGuid:nvlist_alloc(%d)",
20378fe96085Stim szeto 					    ret);
20388fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
20398fe96085Stim szeto 					goto done;
20408fe96085Stim szeto 				}
20418fe96085Stim szeto 				newData = B_TRUE;
20428fe96085Stim szeto 			} else {
204345039663SJohn Forte 				/*
204445039663SJohn Forte 				 * if we're persisting the data, it's
204545039663SJohn Forte 				 * an error. Otherwise, just return
204645039663SJohn Forte 				 */
204745039663SJohn Forte 				if (persist) {
20488fe96085Stim szeto 					ret = stmfRet;
204945039663SJohn Forte 				}
20508fe96085Stim szeto 				goto done;
20518fe96085Stim szeto 			}
20528fe96085Stim szeto 		}
20538fe96085Stim szeto 		if (persist) {
20548fe96085Stim szeto 			ret = nvlist_add_string(nvl, guidAsciiBuf, filename);
20558fe96085Stim szeto 		} else {
20568fe96085Stim szeto 			ret = nvlist_remove(nvl, guidAsciiBuf,
20578fe96085Stim szeto 			    DATA_TYPE_STRING);
20588fe96085Stim szeto 			if (ret == ENOENT) {
20598fe96085Stim szeto 				ret = 0;
20608fe96085Stim szeto 			}
20618fe96085Stim szeto 		}
20628fe96085Stim szeto 		if (ret == 0) {
20638fe96085Stim szeto 			if (newData) {
20648fe96085Stim szeto 				stmfRet = stmfSetProviderDataProt("sbd", nvl,
20658fe96085Stim szeto 				    STMF_LU_PROVIDER_TYPE, NULL);
20668fe96085Stim szeto 			} else {
20678fe96085Stim szeto 				stmfRet = stmfSetProviderDataProt("sbd", nvl,
20688fe96085Stim szeto 				    STMF_LU_PROVIDER_TYPE, &setToken);
20698fe96085Stim szeto 			}
20708fe96085Stim szeto 			if (stmfRet != STMF_STATUS_SUCCESS) {
20718fe96085Stim szeto 				if (stmfRet == STMF_ERROR_BUSY) {
20728fe96085Stim szeto 					/* get/set failed, try again */
20738fe96085Stim szeto 					retryGetProviderData = B_TRUE;
20748fe96085Stim szeto 					if (retryCnt++ > MAX_PROVIDER_RETRY) {
20758fe96085Stim szeto 						ret = stmfRet;
20768fe96085Stim szeto 						break;
20778fe96085Stim szeto 					}
20788fe96085Stim szeto 					continue;
20798fe96085Stim szeto 				} else if (stmfRet ==
20808fe96085Stim szeto 				    STMF_ERROR_PROV_DATA_STALE) {
20818fe96085Stim szeto 					/* update failed, try again */
20828fe96085Stim szeto 					nvlist_free(nvl);
20838fe96085Stim szeto 					nvl = NULL;
20848fe96085Stim szeto 					retryGetProviderData = B_TRUE;
20858fe96085Stim szeto 					if (retryCnt++ > MAX_PROVIDER_RETRY) {
20868fe96085Stim szeto 						ret = stmfRet;
20878fe96085Stim szeto 						break;
20888fe96085Stim szeto 					}
20898fe96085Stim szeto 					continue;
20908fe96085Stim szeto 				} else {
20918fe96085Stim szeto 					syslog(LOG_DEBUG,
20928fe96085Stim szeto 					    "unpersistGuid:error(%x)", stmfRet);
20938fe96085Stim szeto 					ret = stmfRet;
20948fe96085Stim szeto 				}
20958fe96085Stim szeto 				break;
20968fe96085Stim szeto 			}
20978fe96085Stim szeto 		} else {
20988fe96085Stim szeto 			syslog(LOG_DEBUG,
20998fe96085Stim szeto 			    "unpersistGuid:error nvlist_add/remove(%d)",
21008fe96085Stim szeto 			    ret);
21018fe96085Stim szeto 			ret = STMF_STATUS_ERROR;
21028fe96085Stim szeto 		}
21038fe96085Stim szeto 	} while (retryGetProviderData);
21048fe96085Stim szeto 
21058fe96085Stim szeto done:
21068fe96085Stim szeto 	nvlist_free(nvl);
21078fe96085Stim szeto 	return (ret);
21088fe96085Stim szeto }
21098fe96085Stim szeto 
21108fe96085Stim szeto 
21118fe96085Stim szeto /*
21128fe96085Stim szeto  * stmfGetLuProp
21138fe96085Stim szeto  *
21148fe96085Stim szeto  * Purpose: Get current value for a resource property
21158fe96085Stim szeto  *
21168fe96085Stim szeto  * hdl - luResource from a previous call to stmfCreateLuResource
21178fe96085Stim szeto  *
21188fe96085Stim szeto  * resourceProp - a valid resource property type
21198fe96085Stim szeto  *
21208fe96085Stim szeto  * propVal - void pointer to a pointer of the value to be retrieved
21218fe96085Stim szeto  */
21228fe96085Stim szeto int
21238fe96085Stim szeto stmfGetLuProp(luResource hdl, uint32_t prop, char *propVal, size_t *propLen)
21248fe96085Stim szeto {
21258fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
21268fe96085Stim szeto 	luResourceImpl *luPropsHdl = hdl;
21278fe96085Stim szeto 	if (hdl == NULL || propLen == NULL || propVal == NULL) {
21288fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
21298fe96085Stim szeto 	}
21308fe96085Stim szeto 
21318fe96085Stim szeto 	if (luPropsHdl->type == STMF_DISK) {
21328fe96085Stim szeto 		ret = getDiskProp(luPropsHdl, prop, propVal, propLen);
21338fe96085Stim szeto 	} else {
21348fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
21358fe96085Stim szeto 	}
21368fe96085Stim szeto 
21378fe96085Stim szeto 	return (ret);
21388fe96085Stim szeto }
21398fe96085Stim szeto 
21408fe96085Stim szeto /*
21418fe96085Stim szeto  * stmfGetLuResource
21428fe96085Stim szeto  *
21438fe96085Stim szeto  * Purpose: Get a logical unit resource handle for a given logical unit.
21448fe96085Stim szeto  *
21458fe96085Stim szeto  * hdl - pointer to luResource
21468fe96085Stim szeto  */
21478fe96085Stim szeto int
21488fe96085Stim szeto stmfGetLuResource(stmfGuid *luGuid, luResource *hdl)
21498fe96085Stim szeto {
21508fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
21518fe96085Stim szeto 	stmfLogicalUnitProperties luProps;
21528fe96085Stim szeto 
21537b506e25Ssrivijitha dugganapalli 	if (hdl == NULL) {
21547b506e25Ssrivijitha dugganapalli 		return (STMF_ERROR_INVALID_ARG);
21557b506e25Ssrivijitha dugganapalli 	}
21568fe96085Stim szeto 
21578fe96085Stim szeto 	/* Check logical unit provider name to call correct dtype function */
21588fe96085Stim szeto 	if ((ret = stmfGetLogicalUnitProperties(luGuid, &luProps))
21598fe96085Stim szeto 	    != STMF_STATUS_SUCCESS) {
21608fe96085Stim szeto 		return (ret);
21618fe96085Stim szeto 	} else {
21628fe96085Stim szeto 		if (strcmp(luProps.providerName, "sbd") == 0) {
21638fe96085Stim szeto 			ret = getDiskAllProps(luGuid, hdl);
21648fe96085Stim szeto 		} else if (luProps.status == STMF_LOGICAL_UNIT_UNREGISTERED) {
21658fe96085Stim szeto 			return (STMF_ERROR_NOT_FOUND);
21668fe96085Stim szeto 		} else {
21678fe96085Stim szeto 			return (STMF_ERROR_INVALID_ARG);
21688fe96085Stim szeto 		}
21698fe96085Stim szeto 	}
21708fe96085Stim szeto 
21718fe96085Stim szeto 	return (ret);
21728fe96085Stim szeto }
21738fe96085Stim szeto 
21748fe96085Stim szeto /*
21758fe96085Stim szeto  * getDiskAllProps
21768fe96085Stim szeto  *
21778fe96085Stim szeto  * Purpose: load all disk properties from sbd driver
21788fe96085Stim szeto  *
21798fe96085Stim szeto  * luGuid - guid of disk device for which properties are to be retrieved
21808fe96085Stim szeto  * hdl - allocated luResource into which properties are to be copied
21818fe96085Stim szeto  *
21828fe96085Stim szeto  */
21838fe96085Stim szeto static int
21848fe96085Stim szeto getDiskAllProps(stmfGuid *luGuid, luResource *hdl)
21858fe96085Stim szeto {
21868fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
21878fe96085Stim szeto 	int fd;
21888fe96085Stim szeto 	sbd_lu_props_t *sbdProps;
21898fe96085Stim szeto 	int ioctlRet;
21908fe96085Stim szeto 	int savedErrno;
21918fe96085Stim szeto 	int sbdPropsSize = sizeof (*sbdProps) + MAX_SBD_PROPS;
21928fe96085Stim szeto 	stmf_iocdata_t sbdIoctl = {0};
21938fe96085Stim szeto 
21948fe96085Stim szeto 	/*
21958fe96085Stim szeto 	 * Open control node for sbd
21968fe96085Stim szeto 	 */
21978fe96085Stim szeto 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
21988fe96085Stim szeto 		return (ret);
21998fe96085Stim szeto 
22008fe96085Stim szeto 
22018fe96085Stim szeto 	*hdl = calloc(1, sizeof (luResourceImpl));
22028fe96085Stim szeto 	if (*hdl == NULL) {
22038fe96085Stim szeto 		(void) close(fd);
22048fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
22058fe96085Stim szeto 	}
22068fe96085Stim szeto 
22078fe96085Stim szeto 	sbdProps = calloc(1, sbdPropsSize);
22088fe96085Stim szeto 	if (sbdProps == NULL) {
22098fe96085Stim szeto 		free(*hdl);
22108fe96085Stim szeto 		(void) close(fd);
22118fe96085Stim szeto 		return (STMF_ERROR_NOMEM);
22128fe96085Stim szeto 	}
22138fe96085Stim szeto 
22148fe96085Stim szeto 	ret = createDiskResource((luResourceImpl *)*hdl);
22158fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
22168fe96085Stim szeto 		free(*hdl);
221745039663SJohn Forte 		free(sbdProps);
22188fe96085Stim szeto 		(void) close(fd);
22198fe96085Stim szeto 		return (ret);
22208fe96085Stim szeto 	}
22218fe96085Stim szeto 
22228fe96085Stim szeto 	sbdProps->slp_input_guid = 1;
22238fe96085Stim szeto 	bcopy(luGuid, sbdProps->slp_guid, sizeof (sbdProps->slp_guid));
22248fe96085Stim szeto 
22258fe96085Stim szeto 	sbdIoctl.stmf_version = STMF_VERSION_1;
22268fe96085Stim szeto 	sbdIoctl.stmf_ibuf_size = sbdPropsSize;
22278fe96085Stim szeto 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdProps;
22288fe96085Stim szeto 	sbdIoctl.stmf_obuf_size = sbdPropsSize;
22298fe96085Stim szeto 	sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdProps;
22308fe96085Stim szeto 	ioctlRet = ioctl(fd, SBD_IOCTL_GET_LU_PROPS, &sbdIoctl);
22318fe96085Stim szeto 	if (ioctlRet != 0) {
22328fe96085Stim szeto 		savedErrno = errno;
22338fe96085Stim szeto 		switch (savedErrno) {
22348fe96085Stim szeto 			case EBUSY:
22358fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
22368fe96085Stim szeto 				break;
22378fe96085Stim szeto 			case EPERM:
22388fe96085Stim szeto 			case EACCES:
22398fe96085Stim szeto 				ret = STMF_ERROR_PERM;
22408fe96085Stim szeto 				break;
22418fe96085Stim szeto 			case ENOENT:
22428fe96085Stim szeto 				ret = STMF_ERROR_NOT_FOUND;
22438fe96085Stim szeto 				break;
22448fe96085Stim szeto 			default:
22458fe96085Stim szeto 				syslog(LOG_DEBUG,
22468fe96085Stim szeto 				    "getDiskAllProps:ioctl error(%d) (%d) (%d)",
22478fe96085Stim szeto 				    ioctlRet, sbdIoctl.stmf_error, savedErrno);
22488fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
22498fe96085Stim szeto 				break;
22508fe96085Stim szeto 		}
22518fe96085Stim szeto 	}
22528fe96085Stim szeto 
22538fe96085Stim szeto 	if (ret == STMF_STATUS_SUCCESS) {
22548fe96085Stim szeto 		ret = loadDiskPropsFromDriver((luResourceImpl *)*hdl, sbdProps);
22558fe96085Stim szeto 	}
22568fe96085Stim szeto 
225745039663SJohn Forte 	free(sbdProps);
22588fe96085Stim szeto 	(void) close(fd);
22598fe96085Stim szeto 	return (ret);
22608fe96085Stim szeto }
22618fe96085Stim szeto 
22628fe96085Stim szeto /*
22638fe96085Stim szeto  * loadDiskPropsFromDriver
22648fe96085Stim szeto  *
22658fe96085Stim szeto  * Purpose: Retrieve all disk type properties from sbd driver
22668fe96085Stim szeto  *
22678fe96085Stim szeto  * hdl - Allocated luResourceImpl
22688fe96085Stim szeto  * sbdProps - sbd_lu_props_t structure returned from sbd driver
22698fe96085Stim szeto  *
22708fe96085Stim szeto  */
22718fe96085Stim szeto static int
22728fe96085Stim szeto loadDiskPropsFromDriver(luResourceImpl *hdl, sbd_lu_props_t *sbdProps)
22738fe96085Stim szeto {
22748fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
22758fe96085Stim szeto 	diskResource *diskLu = hdl->resource;
22768fe96085Stim szeto 	/* copy guid */
22778fe96085Stim szeto 	diskLu->luGuidValid = B_TRUE;
22788fe96085Stim szeto 	bcopy(sbdProps->slp_guid, diskLu->luGuid, sizeof (sbdProps->slp_guid));
22798fe96085Stim szeto 
22808fe96085Stim szeto 	if (sbdProps->slp_separate_meta && sbdProps->slp_meta_fname_valid) {
22818fe96085Stim szeto 		diskLu->luMetaFileNameValid = B_TRUE;
22828fe96085Stim szeto 		if (strlcpy(diskLu->luMetaFileName,
22838fe96085Stim szeto 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_meta_fname_off]),
22848fe96085Stim szeto 		    sizeof (diskLu->luMetaFileName)) >=
22858fe96085Stim szeto 		    sizeof (diskLu->luMetaFileName)) {
22868fe96085Stim szeto 			return (STMF_STATUS_ERROR);
22878fe96085Stim szeto 		}
22888fe96085Stim szeto 	}
22898fe96085Stim szeto 
22908fe96085Stim szeto 	if (sbdProps->slp_data_fname_valid) {
22918fe96085Stim szeto 		diskLu->luDataFileNameValid = B_TRUE;
22928fe96085Stim szeto 		if (strlcpy(diskLu->luDataFileName,
22938fe96085Stim szeto 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_data_fname_off]),
22948fe96085Stim szeto 		    sizeof (diskLu->luDataFileName)) >=
22958fe96085Stim szeto 		    sizeof (diskLu->luDataFileName)) {
22968fe96085Stim szeto 			return (STMF_STATUS_ERROR);
22978fe96085Stim szeto 		}
22988fe96085Stim szeto 	}
22998fe96085Stim szeto 
23008fe96085Stim szeto 	if (sbdProps->slp_serial_valid) {
23018fe96085Stim szeto 		diskLu->serialNumValid = B_TRUE;
23028fe96085Stim szeto 		bcopy(&(sbdProps->slp_buf[sbdProps->slp_serial_off]),
23038fe96085Stim szeto 		    diskLu->serialNum, sbdProps->slp_serial_size);
23048fe96085Stim szeto 	}
23058fe96085Stim szeto 
23062f624233SNattuvetty Bhavyan 	if (sbdProps->slp_mgmt_url_valid) {
23072f624233SNattuvetty Bhavyan 		diskLu->luMgmtUrlValid = B_TRUE;
23082f624233SNattuvetty Bhavyan 		if (strlcpy(diskLu->luMgmtUrl,
23092f624233SNattuvetty Bhavyan 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_mgmt_url_off]),
23102f624233SNattuvetty Bhavyan 		    sizeof (diskLu->luMgmtUrl)) >=
23112f624233SNattuvetty Bhavyan 		    sizeof (diskLu->luMgmtUrl)) {
23122f624233SNattuvetty Bhavyan 			return (STMF_STATUS_ERROR);
23132f624233SNattuvetty Bhavyan 		}
23142f624233SNattuvetty Bhavyan 	}
23152f624233SNattuvetty Bhavyan 
23168fe96085Stim szeto 	if (sbdProps->slp_alias_valid) {
23178fe96085Stim szeto 		diskLu->luAliasValid = B_TRUE;
23188fe96085Stim szeto 		if (strlcpy(diskLu->luAlias,
23198fe96085Stim szeto 		    (char *)&(sbdProps->slp_buf[sbdProps->slp_alias_off]),
23208fe96085Stim szeto 		    sizeof (diskLu->luAlias)) >=
23218fe96085Stim szeto 		    sizeof (diskLu->luAlias)) {
23228fe96085Stim szeto 			return (STMF_STATUS_ERROR);
23238fe96085Stim szeto 		}
23248fe96085Stim szeto 	} else { /* set alias to data filename if not set */
23258fe96085Stim szeto 		if (sbdProps->slp_data_fname_valid) {
23268fe96085Stim szeto 			diskLu->luAliasValid = B_TRUE;
23278fe96085Stim szeto 			if (strlcpy(diskLu->luAlias,
23288fe96085Stim szeto 			    (char *)&(sbdProps->slp_buf[
23298fe96085Stim szeto 			    sbdProps->slp_data_fname_off]),
23308fe96085Stim szeto 			    sizeof (diskLu->luAlias)) >=
23318fe96085Stim szeto 			    sizeof (diskLu->luAlias)) {
23328fe96085Stim szeto 				return (STMF_STATUS_ERROR);
23338fe96085Stim szeto 			}
23348fe96085Stim szeto 		}
23358fe96085Stim szeto 	}
23368fe96085Stim szeto 
23378fe96085Stim szeto 	diskLu->vidValid = B_TRUE;
23388fe96085Stim szeto 	bcopy(sbdProps->slp_vid, diskLu->vid, sizeof (diskLu->vid));
23398fe96085Stim szeto 
23408fe96085Stim szeto 	diskLu->pidValid = B_TRUE;
23418fe96085Stim szeto 	bcopy(sbdProps->slp_pid, diskLu->pid, sizeof (diskLu->pid));
23428fe96085Stim szeto 
23438fe96085Stim szeto 	diskLu->revValid = B_TRUE;
23448fe96085Stim szeto 	bcopy(sbdProps->slp_rev, diskLu->rev, sizeof (diskLu->rev));
23458fe96085Stim szeto 
23468fe96085Stim szeto 	diskLu->writeProtectEnableValid = B_TRUE;
23478fe96085Stim szeto 	if (sbdProps->slp_write_protected) {
23488fe96085Stim szeto 		diskLu->writeProtectEnable = B_TRUE;
23498fe96085Stim szeto 	}
23508fe96085Stim szeto 
23518fe96085Stim szeto 	diskLu->writebackCacheDisableValid = B_TRUE;
23528fe96085Stim szeto 	if (sbdProps->slp_writeback_cache_disable_cur) {
23538fe96085Stim szeto 		diskLu->writebackCacheDisable = B_TRUE;
23548fe96085Stim szeto 	}
23558fe96085Stim szeto 
23568fe96085Stim szeto 	diskLu->blkSizeValid = B_TRUE;
23578fe96085Stim szeto 	diskLu->blkSize = sbdProps->slp_blksize;
23588fe96085Stim szeto 
23598fe96085Stim szeto 	diskLu->luSizeValid = B_TRUE;
23608fe96085Stim szeto 	diskLu->luSize = sbdProps->slp_lu_size;
23618fe96085Stim szeto 
236245039663SJohn Forte 	diskLu->accessState = sbdProps->slp_access_state;
236345039663SJohn Forte 
23648fe96085Stim szeto 	return (ret);
23658fe96085Stim szeto }
23668fe96085Stim szeto 
23677beff157SJohn Forte /*
23687beff157SJohn Forte  * stmfGetGlobalLuProp
23697beff157SJohn Forte  *
23707beff157SJohn Forte  * Purpose: get a global property for a device type
23717beff157SJohn Forte  *
23727beff157SJohn Forte  */
23737beff157SJohn Forte int
23747beff157SJohn Forte stmfGetGlobalLuProp(uint16_t dType, uint32_t prop, char *propVal,
23757beff157SJohn Forte     size_t *propLen)
23767beff157SJohn Forte {
23777beff157SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
23787beff157SJohn Forte 	if (dType != STMF_DISK || propVal == NULL) {
23797beff157SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
23807beff157SJohn Forte 	}
23817beff157SJohn Forte 
23827beff157SJohn Forte 	ret = getDiskGlobalProp(prop, propVal, propLen);
23837beff157SJohn Forte 
23847beff157SJohn Forte 	return (ret);
23857beff157SJohn Forte }
23867beff157SJohn Forte 
23877beff157SJohn Forte /*
23887beff157SJohn Forte  * getDiskGlobalProp
23897beff157SJohn Forte  *
23907beff157SJohn Forte  * Purpose: get global property from sbd driver
23917beff157SJohn Forte  *
23927beff157SJohn Forte  */
23937beff157SJohn Forte static int
23947beff157SJohn Forte getDiskGlobalProp(uint32_t prop, char *propVal, size_t *propLen)
23957beff157SJohn Forte {
23967beff157SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
23977beff157SJohn Forte 	int fd;
23987beff157SJohn Forte 	sbd_global_props_t *sbdProps;
23997beff157SJohn Forte 	void *sbd_realloc;
24007beff157SJohn Forte 	int retryCnt = 0;
24017beff157SJohn Forte 	boolean_t retry;
24027beff157SJohn Forte 	int ioctlRet;
24037beff157SJohn Forte 	int savedErrno;
24047beff157SJohn Forte 	int sbdPropsSize = sizeof (*sbdProps) + MAX_SBD_PROPS;
24057beff157SJohn Forte 	stmf_iocdata_t sbdIoctl = {0};
24067beff157SJohn Forte 	size_t reqLen;
24077beff157SJohn Forte 
24087beff157SJohn Forte 	switch (prop) {
24097beff157SJohn Forte 		case STMF_LU_PROP_MGMT_URL:
24107beff157SJohn Forte 			break;
24117beff157SJohn Forte 		default:
24127beff157SJohn Forte 			return (STMF_ERROR_INVALID_PROP);
24137beff157SJohn Forte 	}
24147beff157SJohn Forte 
24157beff157SJohn Forte 	/*
24167beff157SJohn Forte 	 * Open control node for sbd
24177beff157SJohn Forte 	 */
24187beff157SJohn Forte 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
24197beff157SJohn Forte 		return (ret);
24207beff157SJohn Forte 
24217beff157SJohn Forte 	sbdProps = calloc(1, sbdPropsSize);
24227beff157SJohn Forte 	if (sbdProps == NULL) {
24237beff157SJohn Forte 		(void) close(fd);
24247beff157SJohn Forte 		return (STMF_ERROR_NOMEM);
24257beff157SJohn Forte 	}
24267beff157SJohn Forte 
24277beff157SJohn Forte 	do {
24287beff157SJohn Forte 		retry = B_FALSE;
24297beff157SJohn Forte 		sbdIoctl.stmf_version = STMF_VERSION_1;
24307beff157SJohn Forte 		sbdIoctl.stmf_obuf_size = sbdPropsSize;
24317beff157SJohn Forte 		sbdIoctl.stmf_obuf = (uint64_t)(unsigned long)sbdProps;
24327beff157SJohn Forte 		ioctlRet = ioctl(fd, SBD_IOCTL_GET_GLOBAL_LU, &sbdIoctl);
24337beff157SJohn Forte 		if (ioctlRet != 0) {
24347beff157SJohn Forte 			savedErrno = errno;
24357beff157SJohn Forte 			switch (savedErrno) {
24367beff157SJohn Forte 				case EBUSY:
24377beff157SJohn Forte 					ret = STMF_ERROR_BUSY;
24387beff157SJohn Forte 					break;
24397beff157SJohn Forte 				case EPERM:
24407beff157SJohn Forte 				case EACCES:
24417beff157SJohn Forte 					ret = STMF_ERROR_PERM;
24427beff157SJohn Forte 					break;
24437beff157SJohn Forte 				case ENOMEM:
24447beff157SJohn Forte 					if (sbdIoctl.stmf_error ==
24457beff157SJohn Forte 					    SBD_RET_INSUFFICIENT_BUF_SPACE &&
24467beff157SJohn Forte 					    retryCnt++ < 3) {
24477beff157SJohn Forte 						sbdPropsSize =
24487beff157SJohn Forte 						    sizeof (*sbdProps) +
24497beff157SJohn Forte 						    sbdProps->
24507beff157SJohn Forte 						    mlu_buf_size_needed;
24517beff157SJohn Forte 
24527beff157SJohn Forte 						sbd_realloc = sbdProps;
24537beff157SJohn Forte 						sbdProps = realloc(sbdProps,
24547beff157SJohn Forte 						    sbdPropsSize);
24557beff157SJohn Forte 						if (sbdProps == NULL) {
24567beff157SJohn Forte 							free(sbd_realloc);
24577beff157SJohn Forte 							ret = STMF_ERROR_NOMEM;
24587beff157SJohn Forte 							break;
24597beff157SJohn Forte 						}
24607beff157SJohn Forte 						retry = B_TRUE;
24617beff157SJohn Forte 					} else {
24627beff157SJohn Forte 						ret = STMF_ERROR_NOMEM;
24637beff157SJohn Forte 					}
24647beff157SJohn Forte 					break;
24657beff157SJohn Forte 				default:
24667beff157SJohn Forte 					syslog(LOG_DEBUG,
24677beff157SJohn Forte 					    "getDiskGlobalProp:ioctl error(%d)"
24687beff157SJohn Forte 					    "(%d)(%d)", ioctlRet,
24697beff157SJohn Forte 					    sbdIoctl.stmf_error, savedErrno);
24707beff157SJohn Forte 					ret = STMF_STATUS_ERROR;
24717beff157SJohn Forte 					break;
24727beff157SJohn Forte 			}
24737beff157SJohn Forte 
24747beff157SJohn Forte 		}
24757beff157SJohn Forte 	} while (retry);
24767beff157SJohn Forte 
24777beff157SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
24787beff157SJohn Forte 		goto done;
24797beff157SJohn Forte 	}
24807beff157SJohn Forte 
24817beff157SJohn Forte 	switch (prop) {
24827beff157SJohn Forte 		case STMF_LU_PROP_MGMT_URL:
24837beff157SJohn Forte 			if (sbdProps->mlu_mgmt_url_valid == 0) {
24847beff157SJohn Forte 				ret = STMF_ERROR_NO_PROP;
24857beff157SJohn Forte 				goto done;
24867beff157SJohn Forte 			}
24877beff157SJohn Forte 			if ((reqLen = strlcpy(propVal, (char *)&(
24887beff157SJohn Forte 			    sbdProps->mlu_buf[sbdProps->mlu_mgmt_url_off]),
24897beff157SJohn Forte 			    *propLen)) >= *propLen) {
24907beff157SJohn Forte 				*propLen = reqLen + 1;
24917beff157SJohn Forte 				ret = STMF_ERROR_INVALID_ARG;
24927beff157SJohn Forte 				goto done;
24937beff157SJohn Forte 			}
24947beff157SJohn Forte 			break;
24957beff157SJohn Forte 	}
24967beff157SJohn Forte 
24977beff157SJohn Forte done:
24987beff157SJohn Forte 	free(sbdProps);
24997beff157SJohn Forte 	(void) close(fd);
25007beff157SJohn Forte 	return (ret);
25017beff157SJohn Forte }
25027beff157SJohn Forte 
25037beff157SJohn Forte /*
25047beff157SJohn Forte  * stmfSetGlobalLuProp
25057beff157SJohn Forte  *
25067beff157SJohn Forte  * Purpose: set a global property for a device type
25077beff157SJohn Forte  *
25087beff157SJohn Forte  */
25097beff157SJohn Forte int
25107beff157SJohn Forte stmfSetGlobalLuProp(uint16_t dType, uint32_t prop, const char *propVal)
25117beff157SJohn Forte {
25127beff157SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
25137beff157SJohn Forte 	if (dType != STMF_DISK || propVal == NULL) {
25147beff157SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
25157beff157SJohn Forte 	}
25167beff157SJohn Forte 
25177beff157SJohn Forte 	ret = setDiskGlobalProp(prop, propVal);
25187beff157SJohn Forte 
25197beff157SJohn Forte 	return (ret);
25207beff157SJohn Forte }
25217beff157SJohn Forte 
25227beff157SJohn Forte /*
25237beff157SJohn Forte  * setDiskGlobalProp
25247beff157SJohn Forte  *
25257beff157SJohn Forte  * Purpose: set properties for resource of type disk
25267beff157SJohn Forte  *
25277beff157SJohn Forte  * resourceProp - valid resource identifier
25287beff157SJohn Forte  * propVal - valid resource value
25297beff157SJohn Forte  */
25307beff157SJohn Forte static int
25317beff157SJohn Forte setDiskGlobalProp(uint32_t resourceProp, const char *propVal)
25327beff157SJohn Forte {
25337beff157SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
25347beff157SJohn Forte 	sbd_global_props_t *sbdGlobalProps = NULL;
25357beff157SJohn Forte 	int sbdGlobalPropsSize = 0;
25367beff157SJohn Forte 	int propLen;
25377beff157SJohn Forte 	int mluBufSize = 0;
25387beff157SJohn Forte 	int fd;
25397beff157SJohn Forte 	int savedErrno;
25407beff157SJohn Forte 	int ioctlRet;
25417beff157SJohn Forte 	stmf_iocdata_t sbdIoctl = {0};
25427beff157SJohn Forte 
25437beff157SJohn Forte 	switch (resourceProp) {
25447beff157SJohn Forte 		case STMF_LU_PROP_MGMT_URL:
25457beff157SJohn Forte 			break;
25467beff157SJohn Forte 		default:
25477beff157SJohn Forte 			return (STMF_ERROR_INVALID_PROP);
25487beff157SJohn Forte 			break;
25497beff157SJohn Forte 	}
25507beff157SJohn Forte 
25517beff157SJohn Forte 	/*
25527beff157SJohn Forte 	 * Open control node for sbd
25537beff157SJohn Forte 	 */
25547beff157SJohn Forte 	if ((ret = openSbd(OPEN_SBD, &fd)) != STMF_STATUS_SUCCESS)
25557beff157SJohn Forte 		return (ret);
25567beff157SJohn Forte 
25577beff157SJohn Forte 	propLen = strlen(propVal);
25587beff157SJohn Forte 	mluBufSize += propLen + 1;
25597beff157SJohn Forte 	sbdGlobalPropsSize += sizeof (sbd_global_props_t) - 8 +
25607beff157SJohn Forte 	    max(8, mluBufSize);
25617beff157SJohn Forte 	/*
25627beff157SJohn Forte 	 * 8 is the size of the buffer set aside for
25637beff157SJohn Forte 	 * concatenation of variable length fields
25647beff157SJohn Forte 	 */
25657beff157SJohn Forte 	sbdGlobalProps = (sbd_global_props_t *)calloc(1, sbdGlobalPropsSize);
25667beff157SJohn Forte 	if (sbdGlobalProps == NULL) {
25677beff157SJohn Forte 		(void) close(fd);
25687beff157SJohn Forte 		return (STMF_ERROR_NOMEM);
25697beff157SJohn Forte 	}
25707beff157SJohn Forte 
25717beff157SJohn Forte 	sbdGlobalProps->mlu_struct_size = sbdGlobalPropsSize;
25727beff157SJohn Forte 
25737beff157SJohn Forte 	switch (resourceProp) {
25747beff157SJohn Forte 		case STMF_LU_PROP_MGMT_URL:
25757beff157SJohn Forte 			sbdGlobalProps->mlu_mgmt_url_valid = 1;
25767beff157SJohn Forte 			bcopy(propVal, &(sbdGlobalProps->mlu_buf),
25777beff157SJohn Forte 			    propLen + 1);
25787beff157SJohn Forte 			break;
25797beff157SJohn Forte 		default:
25807beff157SJohn Forte 			ret = STMF_ERROR_NO_PROP;
25817beff157SJohn Forte 			goto done;
25827beff157SJohn Forte 	}
25837beff157SJohn Forte 
25847beff157SJohn Forte 	sbdIoctl.stmf_version = STMF_VERSION_1;
25857beff157SJohn Forte 	sbdIoctl.stmf_ibuf_size = sbdGlobalProps->mlu_struct_size;
25867beff157SJohn Forte 	sbdIoctl.stmf_ibuf = (uint64_t)(unsigned long)sbdGlobalProps;
25877beff157SJohn Forte 
25887beff157SJohn Forte 	ioctlRet = ioctl(fd, SBD_IOCTL_SET_GLOBAL_LU, &sbdIoctl);
25897beff157SJohn Forte 	if (ioctlRet != 0) {
25907beff157SJohn Forte 		savedErrno = errno;
25917beff157SJohn Forte 		switch (savedErrno) {
25927beff157SJohn Forte 			case EBUSY:
25937beff157SJohn Forte 				ret = STMF_ERROR_BUSY;
25947beff157SJohn Forte 				break;
25957beff157SJohn Forte 			case EPERM:
25967beff157SJohn Forte 			case EACCES:
25977beff157SJohn Forte 				ret = STMF_ERROR_PERM;
25987beff157SJohn Forte 				break;
25997beff157SJohn Forte 			default:
26007beff157SJohn Forte 				diskError(sbdIoctl.stmf_error, &ret);
26017beff157SJohn Forte 				if (ret == STMF_STATUS_ERROR) {
26027beff157SJohn Forte 					syslog(LOG_DEBUG,
26037beff157SJohn Forte 					"modifyDiskLu:ioctl "
26047beff157SJohn Forte 					"error(%d) (%d) (%d)", ioctlRet,
26057beff157SJohn Forte 					    sbdIoctl.stmf_error, savedErrno);
26067beff157SJohn Forte 				}
26077beff157SJohn Forte 				break;
26087beff157SJohn Forte 		}
26097beff157SJohn Forte 	}
26107beff157SJohn Forte 
26117beff157SJohn Forte done:
26127beff157SJohn Forte 	free(sbdGlobalProps);
26137beff157SJohn Forte 	(void) close(fd);
26147beff157SJohn Forte 	return (ret);
26157beff157SJohn Forte }
26167beff157SJohn Forte 
26178fe96085Stim szeto 
26188fe96085Stim szeto /*
26198fe96085Stim szeto  * stmfSetLuProp
26208fe96085Stim szeto  *
26218fe96085Stim szeto  * Purpose: set a property on an luResource
26228fe96085Stim szeto  *
26238fe96085Stim szeto  * hdl - allocated luResource
26248fe96085Stim szeto  * prop - property identifier
26258fe96085Stim szeto  * propVal - property value to be set
26268fe96085Stim szeto  */
26278fe96085Stim szeto int
26288fe96085Stim szeto stmfSetLuProp(luResource hdl, uint32_t prop, const char *propVal)
26298fe96085Stim szeto {
26308fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
26318fe96085Stim szeto 	luResourceImpl *luPropsHdl = hdl;
26328fe96085Stim szeto 	if (hdl == NULL) {
26338fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
26348fe96085Stim szeto 	}
26358fe96085Stim szeto 
26368fe96085Stim szeto 	if (luPropsHdl->type == STMF_DISK) {
26378fe96085Stim szeto 		ret = setDiskProp(luPropsHdl, prop, propVal);
26388fe96085Stim szeto 	} else {
26398fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
26408fe96085Stim szeto 	}
26418fe96085Stim szeto 
26428fe96085Stim szeto 	return (ret);
26438fe96085Stim szeto }
26448fe96085Stim szeto 
26458fe96085Stim szeto /*
26468fe96085Stim szeto  * getDiskProp
26478fe96085Stim szeto  *
26488fe96085Stim szeto  * Purpose: retrieve a given property from a logical unit resource of type disk
26498fe96085Stim szeto  *
26508fe96085Stim szeto  * hdl - allocated luResourceImpl
26518fe96085Stim szeto  * prop - property identifier
26528fe96085Stim szeto  * propVal - pointer to character to contain the retrieved property value
26538fe96085Stim szeto  * propLen - On input this is the length of propVal. On failure, it contains the
26548fe96085Stim szeto  *           number of bytes required for propVal
26558fe96085Stim szeto  */
26568fe96085Stim szeto static int
26578fe96085Stim szeto getDiskProp(luResourceImpl *hdl, uint32_t prop, char *propVal, size_t *propLen)
26588fe96085Stim szeto {
26598fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
26608fe96085Stim szeto 	diskResource *diskLu = hdl->resource;
266145039663SJohn Forte 	char accessState[20];
26628fe96085Stim szeto 	size_t reqLen;
26638fe96085Stim szeto 
266445039663SJohn Forte 	if (prop == STMF_LU_PROP_ACCESS_STATE) {
266545039663SJohn Forte 		if (diskLu->accessState == SBD_LU_ACTIVE) {
266645039663SJohn Forte 			(void) strlcpy(accessState, STMF_ACCESS_ACTIVE,
266745039663SJohn Forte 			    sizeof (accessState));
266845039663SJohn Forte 		} else if (diskLu->accessState == SBD_LU_TRANSITION_TO_ACTIVE) {
266945039663SJohn Forte 			(void) strlcpy(accessState,
267045039663SJohn Forte 			    STMF_ACCESS_STANDBY_TO_ACTIVE,
267145039663SJohn Forte 			    sizeof (accessState));
267245039663SJohn Forte 		} else if (diskLu->accessState == SBD_LU_STANDBY) {
267345039663SJohn Forte 			(void) strlcpy(accessState, STMF_ACCESS_STANDBY,
267445039663SJohn Forte 			    sizeof (accessState));
267545039663SJohn Forte 		} else if (diskLu->accessState ==
267645039663SJohn Forte 		    SBD_LU_TRANSITION_TO_STANDBY) {
267745039663SJohn Forte 			(void) strlcpy(accessState,
267845039663SJohn Forte 			    STMF_ACCESS_ACTIVE_TO_STANDBY,
267945039663SJohn Forte 			    sizeof (accessState));
268045039663SJohn Forte 		}
268145039663SJohn Forte 		if ((reqLen = strlcpy(propVal, accessState,
268245039663SJohn Forte 		    *propLen)) >= *propLen) {
268345039663SJohn Forte 			*propLen = reqLen + 1;
268445039663SJohn Forte 			return (STMF_ERROR_INVALID_ARG);
268545039663SJohn Forte 		}
268645039663SJohn Forte 		return (0);
268745039663SJohn Forte 	}
268845039663SJohn Forte 
268945039663SJohn Forte 	if (diskLu->accessState != SBD_LU_ACTIVE) {
269045039663SJohn Forte 		return (STMF_ERROR_NO_PROP_STANDBY);
269145039663SJohn Forte 	}
269245039663SJohn Forte 
26938fe96085Stim szeto 	switch (prop) {
26948fe96085Stim szeto 		case STMF_LU_PROP_BLOCK_SIZE:
26958fe96085Stim szeto 			if (diskLu->blkSizeValid == B_FALSE) {
26968fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
26978fe96085Stim szeto 			}
26988fe96085Stim szeto 			reqLen = snprintf(propVal, *propLen, "%llu",
26998fe96085Stim szeto 			    (u_longlong_t)diskLu->blkSize);
27008fe96085Stim szeto 			if (reqLen >= *propLen) {
27018fe96085Stim szeto 				*propLen = reqLen + 1;
27028fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
27038fe96085Stim szeto 			}
27048fe96085Stim szeto 			break;
27058fe96085Stim szeto 		case STMF_LU_PROP_FILENAME:
27068fe96085Stim szeto 			if (diskLu->luDataFileNameValid == B_FALSE) {
27078fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
27088fe96085Stim szeto 			}
27098fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->luDataFileName,
27108fe96085Stim szeto 			    *propLen)) >= *propLen) {
27118fe96085Stim szeto 				*propLen = reqLen + 1;
27128fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
27138fe96085Stim szeto 			}
27148fe96085Stim szeto 			break;
27158fe96085Stim szeto 		case STMF_LU_PROP_META_FILENAME:
27168fe96085Stim szeto 			if (diskLu->luMetaFileNameValid == B_FALSE) {
27178fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
27188fe96085Stim szeto 			}
27198fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->luMetaFileName,
27208fe96085Stim szeto 			    *propLen)) >= *propLen) {
27218fe96085Stim szeto 				*propLen = reqLen + 1;
27228fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
27238fe96085Stim szeto 			}
27248fe96085Stim szeto 			break;
27252f624233SNattuvetty Bhavyan 		case STMF_LU_PROP_MGMT_URL:
27262f624233SNattuvetty Bhavyan 			if (diskLu->luMgmtUrlValid == B_FALSE) {
27272f624233SNattuvetty Bhavyan 				return (STMF_ERROR_NO_PROP);
27282f624233SNattuvetty Bhavyan 			}
27292f624233SNattuvetty Bhavyan 			if ((reqLen = strlcpy(propVal, diskLu->luMgmtUrl,
27302f624233SNattuvetty Bhavyan 			    *propLen)) >= *propLen) {
27312f624233SNattuvetty Bhavyan 				*propLen = reqLen + 1;
27322f624233SNattuvetty Bhavyan 				return (STMF_ERROR_INVALID_ARG);
27332f624233SNattuvetty Bhavyan 			}
27342f624233SNattuvetty Bhavyan 			break;
27358fe96085Stim szeto 		case STMF_LU_PROP_GUID:
27368fe96085Stim szeto 			if (diskLu->luGuidValid == B_FALSE) {
27378fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
27388fe96085Stim szeto 			}
27398fe96085Stim szeto 			reqLen = snprintf(propVal, *propLen,
27408fe96085Stim szeto 			    "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X"
27418fe96085Stim szeto 			    "%02X%02X%02X%02X",
27428fe96085Stim szeto 			    diskLu->luGuid[0], diskLu->luGuid[1],
27438fe96085Stim szeto 			    diskLu->luGuid[2], diskLu->luGuid[3],
27448fe96085Stim szeto 			    diskLu->luGuid[4], diskLu->luGuid[5],
27458fe96085Stim szeto 			    diskLu->luGuid[6], diskLu->luGuid[7],
27468fe96085Stim szeto 			    diskLu->luGuid[8], diskLu->luGuid[9],
27478fe96085Stim szeto 			    diskLu->luGuid[10], diskLu->luGuid[11],
27488fe96085Stim szeto 			    diskLu->luGuid[12], diskLu->luGuid[13],
27498fe96085Stim szeto 			    diskLu->luGuid[14], diskLu->luGuid[15]);
27508fe96085Stim szeto 			if (reqLen >= *propLen) {
27518fe96085Stim szeto 				*propLen = reqLen + 1;
27528fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
27538fe96085Stim szeto 			}
27548fe96085Stim szeto 			break;
27558fe96085Stim szeto 		case STMF_LU_PROP_SERIAL_NUM:
27568fe96085Stim szeto 			if (diskLu->serialNumValid == B_FALSE) {
27578fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
27588fe96085Stim szeto 			}
27598fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->serialNum,
27608fe96085Stim szeto 			    *propLen)) >= *propLen) {
27618fe96085Stim szeto 				*propLen = reqLen + 1;
27628fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
27638fe96085Stim szeto 			}
27648fe96085Stim szeto 			break;
27658fe96085Stim szeto 		case STMF_LU_PROP_SIZE:
27668fe96085Stim szeto 			if (diskLu->luSizeValid == B_FALSE) {
27678fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
27688fe96085Stim szeto 			}
27698fe96085Stim szeto 			(void) snprintf(propVal, *propLen, "%llu",
27708fe96085Stim szeto 			    (u_longlong_t)diskLu->luSize);
27718fe96085Stim szeto 			break;
27728fe96085Stim szeto 		case STMF_LU_PROP_ALIAS:
27738fe96085Stim szeto 			if (diskLu->luAliasValid == B_FALSE) {
27748fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
27758fe96085Stim szeto 			}
27768fe96085Stim szeto 			if ((reqLen = strlcpy(propVal, diskLu->luAlias,
27778fe96085Stim szeto 			    *propLen)) >= *propLen) {
27788fe96085Stim szeto 				*propLen = reqLen + 1;
27798fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
27808fe96085Stim szeto 			}
27818fe96085Stim szeto 			break;
27828fe96085Stim szeto 		case STMF_LU_PROP_VID:
27838fe96085Stim szeto 			if (diskLu->vidValid == B_FALSE) {
27848fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
27858fe96085Stim szeto 			}
27868fe96085Stim szeto 			if (*propLen <= sizeof (diskLu->vid)) {
27878fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
27888fe96085Stim szeto 			}
27898fe96085Stim szeto 			bcopy(diskLu->vid, propVal, sizeof (diskLu->vid));
27908fe96085Stim szeto 			propVal[sizeof (diskLu->vid)] = 0;
27918fe96085Stim szeto 			break;
27928fe96085Stim szeto 		case STMF_LU_PROP_PID:
27938fe96085Stim szeto 			if (diskLu->pidValid == B_FALSE) {
27948fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
27958fe96085Stim szeto 			}
27968fe96085Stim szeto 			if (*propLen <= sizeof (diskLu->pid)) {
27978fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
27988fe96085Stim szeto 			}
27998fe96085Stim szeto 			bcopy(diskLu->pid, propVal, sizeof (diskLu->pid));
28008fe96085Stim szeto 			propVal[sizeof (diskLu->pid)] = 0;
28018fe96085Stim szeto 			break;
28028fe96085Stim szeto 		case STMF_LU_PROP_WRITE_PROTECT:
28038fe96085Stim szeto 			if (diskLu->writeProtectEnableValid == B_FALSE) {
28048fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
28058fe96085Stim szeto 			}
28068fe96085Stim szeto 			if (diskLu->writeProtectEnable) {
28078fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "true",
28088fe96085Stim szeto 				    *propLen)) >= *propLen) {
28098fe96085Stim szeto 					*propLen = reqLen + 1;
28108fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
28118fe96085Stim szeto 				}
28128fe96085Stim szeto 			} else {
28138fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "false",
28148fe96085Stim szeto 				    *propLen)) >= *propLen) {
28158fe96085Stim szeto 					*propLen = reqLen + 1;
28168fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
28178fe96085Stim szeto 				}
28188fe96085Stim szeto 			}
28198fe96085Stim szeto 			break;
28208fe96085Stim szeto 		case STMF_LU_PROP_WRITE_CACHE_DISABLE:
28218fe96085Stim szeto 			if (diskLu->writebackCacheDisableValid == B_FALSE) {
28228fe96085Stim szeto 				return (STMF_ERROR_NO_PROP);
28238fe96085Stim szeto 			}
28248fe96085Stim szeto 			if (diskLu->writebackCacheDisable) {
28258fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "true",
28268fe96085Stim szeto 				    *propLen)) >= *propLen) {
28278fe96085Stim szeto 					*propLen = reqLen + 1;
28288fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
28298fe96085Stim szeto 				}
28308fe96085Stim szeto 			} else {
28318fe96085Stim szeto 				if ((reqLen = strlcpy(propVal, "false",
28328fe96085Stim szeto 				    *propLen)) >= *propLen) {
28338fe96085Stim szeto 					*propLen = reqLen + 1;
28348fe96085Stim szeto 					return (STMF_ERROR_INVALID_ARG);
28358fe96085Stim szeto 				}
28368fe96085Stim szeto 			}
28378fe96085Stim szeto 			break;
28388fe96085Stim szeto 		default:
28397b506e25Ssrivijitha dugganapalli 			ret = STMF_ERROR_INVALID_PROP;
28408fe96085Stim szeto 			break;
28418fe96085Stim szeto 	}
28428fe96085Stim szeto 
28438fe96085Stim szeto 	return (ret);
28448fe96085Stim szeto }
28458fe96085Stim szeto 
28468fe96085Stim szeto /*
28478fe96085Stim szeto  * setDiskProp
28488fe96085Stim szeto  *
28498fe96085Stim szeto  * Purpose: set properties for resource of type disk
28508fe96085Stim szeto  *
28518fe96085Stim szeto  * hdl - allocated luResourceImpl
28528fe96085Stim szeto  * resourceProp - valid resource identifier
28538fe96085Stim szeto  * propVal - valid resource value
28548fe96085Stim szeto  */
28558fe96085Stim szeto static int
28568fe96085Stim szeto setDiskProp(luResourceImpl *hdl, uint32_t resourceProp, const char *propVal)
28578fe96085Stim szeto {
28588fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
28598fe96085Stim szeto 	int i;
28608fe96085Stim szeto 	diskResource *diskLu = hdl->resource;
28618fe96085Stim szeto 	unsigned long long numericProp = 0;
28628fe96085Stim szeto 	char guidProp[LU_ASCII_GUID_SIZE + 1];
28638fe96085Stim szeto 	char ouiProp[OUI_ASCII_SIZE + 1];
2864fdcc480aSJohn Forte 	char hostIdProp[HOST_ID_ASCII_SIZE + 1];
28658fe96085Stim szeto 	unsigned int oui[OUI_SIZE];
2866fdcc480aSJohn Forte 	unsigned int hostId[HOST_ID_SIZE];
28678fe96085Stim szeto 	unsigned int guid[LU_GUID_SIZE];
28688fe96085Stim szeto 	int propSize;
28698fe96085Stim szeto 
28708fe96085Stim szeto 
28718fe96085Stim szeto 	if (propVal == NULL) {
28728fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
28738fe96085Stim szeto 	}
28748fe96085Stim szeto 
28758fe96085Stim szeto 	switch (resourceProp) {
28768fe96085Stim szeto 		case STMF_LU_PROP_ALIAS:
28778fe96085Stim szeto 			if (strlcpy(diskLu->luAlias, propVal,
28788fe96085Stim szeto 			    sizeof (diskLu->luAlias)) >=
28798fe96085Stim szeto 			    sizeof (diskLu->luAlias)) {
28808fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
28818fe96085Stim szeto 			}
28828fe96085Stim szeto 			diskLu->luAliasValid = B_TRUE;
28838fe96085Stim szeto 			break;
288426717e15Ssrivijitha dugganapalli 		case STMF_LU_PROP_BLOCK_SIZE: {
288526717e15Ssrivijitha dugganapalli 			const char *tmp = propVal;
288626717e15Ssrivijitha dugganapalli 			while (*tmp) {
288726717e15Ssrivijitha dugganapalli 				if (!isdigit(*tmp++)) {
288826717e15Ssrivijitha dugganapalli 					return (STMF_ERROR_INVALID_ARG);
288926717e15Ssrivijitha dugganapalli 				}
289026717e15Ssrivijitha dugganapalli 			}
28918fe96085Stim szeto 			(void) sscanf(propVal, "%llu", &numericProp);
28928fe96085Stim szeto 			if (numericProp > UINT16_MAX) {
28938fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
28948fe96085Stim szeto 			}
28958fe96085Stim szeto 			diskLu->blkSize = numericProp;
28968fe96085Stim szeto 			diskLu->blkSizeValid = B_TRUE;
28978fe96085Stim szeto 			break;
289826717e15Ssrivijitha dugganapalli 		}
28998fe96085Stim szeto 		case STMF_LU_PROP_COMPANY_ID:
29008fe96085Stim szeto 			if ((strlcpy(ouiProp, propVal, sizeof (ouiProp))) >=
29018fe96085Stim szeto 			    sizeof (ouiProp)) {
29028fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
29038fe96085Stim szeto 			}
29048fe96085Stim szeto 			if (checkHexUpper(ouiProp) != 0) {
29058fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
29068fe96085Stim szeto 			}
29078fe96085Stim szeto 			(void) sscanf(ouiProp, "%2X%2X%2X",
29088fe96085Stim szeto 			    &oui[0], &oui[1], &oui[2]);
29098fe96085Stim szeto 
29108fe96085Stim szeto 			diskLu->companyId = 0;
29118fe96085Stim szeto 			diskLu->companyId += oui[0] << 16;
29128fe96085Stim szeto 			diskLu->companyId += oui[1] << 8;
29138fe96085Stim szeto 			diskLu->companyId += oui[2];
2914fdcc480aSJohn Forte 			if (diskLu->companyId == 0) {
2915fdcc480aSJohn Forte 				return (STMF_ERROR_INVALID_ARG);
2916fdcc480aSJohn Forte 			}
29178fe96085Stim szeto 			diskLu->companyIdValid = B_TRUE;
29188fe96085Stim szeto 			break;
2919fdcc480aSJohn Forte 		case STMF_LU_PROP_HOST_ID:
2920fdcc480aSJohn Forte 			if ((strlcpy(hostIdProp, propVal,
2921fdcc480aSJohn Forte 			    sizeof (hostIdProp))) >= sizeof (hostIdProp)) {
2922fdcc480aSJohn Forte 				return (STMF_ERROR_INVALID_ARG);
2923fdcc480aSJohn Forte 			}
2924fdcc480aSJohn Forte 			if (checkHexUpper(hostIdProp) != 0) {
2925fdcc480aSJohn Forte 				return (STMF_ERROR_INVALID_ARG);
2926fdcc480aSJohn Forte 			}
2927fdcc480aSJohn Forte 			(void) sscanf(hostIdProp, "%2X%2X%2X%2X",
2928fdcc480aSJohn Forte 			    &hostId[0], &hostId[1], &hostId[2], &hostId[3]);
2929fdcc480aSJohn Forte 
2930fdcc480aSJohn Forte 			diskLu->hostId = 0;
2931fdcc480aSJohn Forte 			diskLu->hostId += hostId[0] << 24;
2932fdcc480aSJohn Forte 			diskLu->hostId += hostId[1] << 16;
2933fdcc480aSJohn Forte 			diskLu->hostId += hostId[2] << 8;
2934fdcc480aSJohn Forte 			diskLu->hostId += hostId[3];
2935fdcc480aSJohn Forte 			if (diskLu->hostId == 0) {
2936fdcc480aSJohn Forte 				return (STMF_ERROR_INVALID_ARG);
2937fdcc480aSJohn Forte 			}
2938fdcc480aSJohn Forte 			diskLu->hostIdValid = B_TRUE;
2939fdcc480aSJohn Forte 			break;
29408fe96085Stim szeto 		case STMF_LU_PROP_GUID:
29418fe96085Stim szeto 			if (strlen(propVal) != LU_ASCII_GUID_SIZE) {
29428fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
29438fe96085Stim szeto 			}
29448fe96085Stim szeto 
29458fe96085Stim szeto 			if ((strlcpy(guidProp, propVal, sizeof (guidProp))) >=
29468fe96085Stim szeto 			    sizeof (guidProp)) {
29478fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
29488fe96085Stim szeto 			}
29498fe96085Stim szeto 
29508fe96085Stim szeto 			if (checkHexUpper(guidProp) != 0) {
29518fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
29528fe96085Stim szeto 			}
29538fe96085Stim szeto 
29548fe96085Stim szeto 			(void) sscanf(guidProp,
29558fe96085Stim szeto 			    "%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X%2X",
29568fe96085Stim szeto 			    &guid[0], &guid[1], &guid[2], &guid[3], &guid[4],
29578fe96085Stim szeto 			    &guid[5], &guid[6], &guid[7], &guid[8], &guid[9],
29588fe96085Stim szeto 			    &guid[10], &guid[11], &guid[12], &guid[13],
29598fe96085Stim szeto 			    &guid[14], &guid[15]);
29608fe96085Stim szeto 			for (i = 0; i < sizeof (diskLu->luGuid); i++) {
29618fe96085Stim szeto 				diskLu->luGuid[i] = guid[i];
29628fe96085Stim szeto 			}
29638fe96085Stim szeto 			diskLu->luGuidValid = B_TRUE;
29648fe96085Stim szeto 			break;
29658fe96085Stim szeto 		case STMF_LU_PROP_FILENAME:
29668fe96085Stim szeto 			if ((strlcpy(diskLu->luDataFileName, propVal,
29678fe96085Stim szeto 			    sizeof (diskLu->luDataFileName))) >=
29688fe96085Stim szeto 			    sizeof (diskLu->luDataFileName)) {
29698fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
29708fe96085Stim szeto 			}
29718fe96085Stim szeto 			diskLu->luDataFileNameValid = B_TRUE;
29728fe96085Stim szeto 			break;
29738fe96085Stim szeto 		case STMF_LU_PROP_META_FILENAME:
29748fe96085Stim szeto 			if ((strlcpy(diskLu->luMetaFileName, propVal,
29758fe96085Stim szeto 			    sizeof (diskLu->luMetaFileName))) >=
29768fe96085Stim szeto 			    sizeof (diskLu->luMetaFileName)) {
29778fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
29788fe96085Stim szeto 			}
29798fe96085Stim szeto 			diskLu->luMetaFileNameValid = B_TRUE;
29808fe96085Stim szeto 			break;
29812f624233SNattuvetty Bhavyan 		case STMF_LU_PROP_MGMT_URL:
29822f624233SNattuvetty Bhavyan 			if ((strlcpy(diskLu->luMgmtUrl, propVal,
29832f624233SNattuvetty Bhavyan 			    sizeof (diskLu->luMgmtUrl))) >=
29842f624233SNattuvetty Bhavyan 			    sizeof (diskLu->luMgmtUrl)) {
29852f624233SNattuvetty Bhavyan 				return (STMF_ERROR_INVALID_PROPSIZE);
29862f624233SNattuvetty Bhavyan 			}
29872f624233SNattuvetty Bhavyan 			diskLu->luMgmtUrlValid = B_TRUE;
29882f624233SNattuvetty Bhavyan 			break;
29898fe96085Stim szeto 		case STMF_LU_PROP_PID:
29908fe96085Stim szeto 			if ((propSize = strlen(propVal)) >
29918fe96085Stim szeto 			    sizeof (diskLu->pid)) {
29928fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
29938fe96085Stim szeto 			}
29948fe96085Stim szeto 			(void) strncpy(diskLu->pid, propVal, propSize);
29958fe96085Stim szeto 			diskLu->pidValid = B_TRUE;
29968fe96085Stim szeto 			break;
29978fe96085Stim szeto 		case STMF_LU_PROP_SERIAL_NUM:
29988fe96085Stim szeto 			if ((propSize = strlen(propVal)) >
29998fe96085Stim szeto 			    (sizeof (diskLu->serialNum) - 1)) {
30008fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
30018fe96085Stim szeto 			}
30028fe96085Stim szeto 			(void) strncpy(diskLu->serialNum, propVal, propSize);
30038fe96085Stim szeto 			diskLu->serialNumValid = B_TRUE;
30048fe96085Stim szeto 			break;
30058fe96085Stim szeto 		case STMF_LU_PROP_SIZE:
30068fe96085Stim szeto 			if ((niceStrToNum(propVal, &diskLu->luSize) != 0)) {
30078fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
30088fe96085Stim szeto 			}
30098fe96085Stim szeto 			diskLu->luSizeValid = B_TRUE;
30108fe96085Stim szeto 			break;
30118fe96085Stim szeto 		case STMF_LU_PROP_VID:
30128fe96085Stim szeto 			if ((propSize = strlen(propVal)) >
30138fe96085Stim szeto 			    sizeof (diskLu->vid)) {
30148fe96085Stim szeto 				return (STMF_ERROR_INVALID_PROPSIZE);
30158fe96085Stim szeto 			}
30168fe96085Stim szeto 			(void) strncpy(diskLu->vid, propVal, propSize);
30178fe96085Stim szeto 			diskLu->vidValid = B_TRUE;
30188fe96085Stim szeto 			break;
30198fe96085Stim szeto 		case STMF_LU_PROP_WRITE_PROTECT:
30208fe96085Stim szeto 			if (strcasecmp(propVal, "TRUE") == 0) {
30218fe96085Stim szeto 				diskLu->writeProtectEnable = B_TRUE;
30228fe96085Stim szeto 			} else if (strcasecmp(propVal, "FALSE") == 0) {
30238fe96085Stim szeto 				diskLu->writeProtectEnable = B_FALSE;
30248fe96085Stim szeto 			} else {
30258fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
30268fe96085Stim szeto 			}
30278fe96085Stim szeto 			diskLu->writeProtectEnableValid = B_TRUE;
30288fe96085Stim szeto 			break;
30298fe96085Stim szeto 		case STMF_LU_PROP_WRITE_CACHE_DISABLE:
30308fe96085Stim szeto 			if (strcasecmp(propVal, "TRUE") == 0) {
30318fe96085Stim szeto 				diskLu->writebackCacheDisable = B_TRUE;
30328fe96085Stim szeto 			} else if (strcasecmp(propVal, "FALSE") == 0) {
30338fe96085Stim szeto 				diskLu->writebackCacheDisable = B_FALSE;
30348fe96085Stim szeto 			} else {
30358fe96085Stim szeto 				return (STMF_ERROR_INVALID_ARG);
30368fe96085Stim szeto 			}
30378fe96085Stim szeto 			diskLu->writebackCacheDisableValid = B_TRUE;
30388fe96085Stim szeto 			break;
303945039663SJohn Forte 		case STMF_LU_PROP_ACCESS_STATE:
304045039663SJohn Forte 			ret = STMF_ERROR_INVALID_PROP;
304145039663SJohn Forte 			break;
30428fe96085Stim szeto 		default:
30437b506e25Ssrivijitha dugganapalli 			ret = STMF_ERROR_INVALID_PROP;
30448fe96085Stim szeto 			break;
30458fe96085Stim szeto 	}
30468fe96085Stim szeto 	return (ret);
30478fe96085Stim szeto }
30488fe96085Stim szeto 
30498fe96085Stim szeto static int
30508fe96085Stim szeto checkHexUpper(char *buf)
30518fe96085Stim szeto {
30528fe96085Stim szeto 	int i;
30538fe96085Stim szeto 
30548fe96085Stim szeto 	for (i = 0; i < strlen(buf); i++) {
30558fe96085Stim szeto 		if (isxdigit(buf[i])) {
30568fe96085Stim szeto 			buf[i] = toupper(buf[i]);
30578fe96085Stim szeto 			continue;
30588fe96085Stim szeto 		}
30598fe96085Stim szeto 		return (-1);
30608fe96085Stim szeto 	}
30618fe96085Stim szeto 
30628fe96085Stim szeto 	return (0);
30638fe96085Stim szeto }
30648fe96085Stim szeto 
30658fe96085Stim szeto /*
30668fe96085Stim szeto  * Given a numeric suffix, convert the value into a number of bits that the
30678fe96085Stim szeto  * resulting value must be shifted.
30688fe96085Stim szeto  * Code lifted from libzfs_util.c
30698fe96085Stim szeto  */
30708fe96085Stim szeto static int
30718fe96085Stim szeto strToShift(const char *buf)
30728fe96085Stim szeto {
30738fe96085Stim szeto 	const char *ends = "BKMGTPE";
30748fe96085Stim szeto 	int i;
30758fe96085Stim szeto 
30768fe96085Stim szeto 	if (buf[0] == '\0')
30778fe96085Stim szeto 		return (0);
30788fe96085Stim szeto 
30798fe96085Stim szeto 	for (i = 0; i < strlen(ends); i++) {
30808fe96085Stim szeto 		if (toupper(buf[0]) == ends[i])
30818fe96085Stim szeto 			return (10*i);
30828fe96085Stim szeto 	}
30838fe96085Stim szeto 
30848fe96085Stim szeto 	return (-1);
30858fe96085Stim szeto }
30868fe96085Stim szeto 
30878fe96085Stim szeto int
30888fe96085Stim szeto stmfFreeLuResource(luResource hdl)
30898fe96085Stim szeto {
30908fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
30918fe96085Stim szeto 	if (hdl == NULL) {
30928fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
30938fe96085Stim szeto 	}
30948fe96085Stim szeto 
30958fe96085Stim szeto 	luResourceImpl *hdlImpl = hdl;
30968fe96085Stim szeto 	free(hdlImpl->resource);
30978fe96085Stim szeto 	free(hdlImpl);
30988fe96085Stim szeto 	return (ret);
30998fe96085Stim szeto }
31008fe96085Stim szeto 
31018fe96085Stim szeto /*
31028fe96085Stim szeto  * Convert a string of the form '100G' into a real number. Used when setting
31038fe96085Stim szeto  * the size of a logical unit.
31048fe96085Stim szeto  * Code lifted from libzfs_util.c
31058fe96085Stim szeto  */
31068fe96085Stim szeto static int
31078fe96085Stim szeto niceStrToNum(const char *value, uint64_t *num)
31088fe96085Stim szeto {
31098fe96085Stim szeto 	char *end;
31108fe96085Stim szeto 	int shift;
31118fe96085Stim szeto 
31128fe96085Stim szeto 	*num = 0;
31138fe96085Stim szeto 
31148fe96085Stim szeto 	/* Check to see if this looks like a number.  */
31158fe96085Stim szeto 	if ((value[0] < '0' || value[0] > '9') && value[0] != '.') {
31168fe96085Stim szeto 		return (-1);
31178fe96085Stim szeto 	}
31188fe96085Stim szeto 
31198fe96085Stim szeto 	/* Rely on stroull() to process the numeric portion.  */
31208fe96085Stim szeto 	errno = 0;
31218fe96085Stim szeto 	*num = strtoull(value, &end, 10);
31228fe96085Stim szeto 
31238fe96085Stim szeto 	/*
31248fe96085Stim szeto 	 * Check for ERANGE, which indicates that the value is too large to fit
31258fe96085Stim szeto 	 * in a 64-bit value.
31268fe96085Stim szeto 	 */
31278fe96085Stim szeto 	if (errno == ERANGE) {
31288fe96085Stim szeto 		return (-1);
31298fe96085Stim szeto 	}
31308fe96085Stim szeto 
31318fe96085Stim szeto 	/*
31328fe96085Stim szeto 	 * If we have a decimal value, then do the computation with floating
31338fe96085Stim szeto 	 * point arithmetic.  Otherwise, use standard arithmetic.
31348fe96085Stim szeto 	 */
31358fe96085Stim szeto 	if (*end == '.') {
31368fe96085Stim szeto 		double fval = strtod(value, &end);
31378fe96085Stim szeto 
31388fe96085Stim szeto 		if ((shift = strToShift(end)) == -1) {
31398fe96085Stim szeto 			return (-1);
31408fe96085Stim szeto 		}
31418fe96085Stim szeto 
31428fe96085Stim szeto 		fval *= pow(2, shift);
31438fe96085Stim szeto 
31448fe96085Stim szeto 		if (fval > UINT64_MAX) {
31458fe96085Stim szeto 			return (-1);
31468fe96085Stim szeto 		}
31478fe96085Stim szeto 
31488fe96085Stim szeto 		*num = (uint64_t)fval;
31498fe96085Stim szeto 	} else {
31508fe96085Stim szeto 		if ((shift = strToShift(end)) == -1) {
31518fe96085Stim szeto 			return (-1);
31528fe96085Stim szeto 		}
31538fe96085Stim szeto 
31548fe96085Stim szeto 		/* Check for overflow */
31558fe96085Stim szeto 		if (shift >= 64 || (*num << shift) >> shift != *num) {
31568fe96085Stim szeto 			return (-1);
31578fe96085Stim szeto 		}
31588fe96085Stim szeto 
31598fe96085Stim szeto 		*num <<= shift;
31608fe96085Stim szeto 	}
31618fe96085Stim szeto 
31628fe96085Stim szeto 	return (0);
31638fe96085Stim szeto }
31648fe96085Stim szeto 
31658fe96085Stim szeto /*
3166fcf3ce44SJohn Forte  * stmfCreateTargetGroup
3167fcf3ce44SJohn Forte  *
3168fcf3ce44SJohn Forte  * Purpose: Create a local port group
3169fcf3ce44SJohn Forte  *
3170fcf3ce44SJohn Forte  * targetGroupName - name of local port group to create
3171fcf3ce44SJohn Forte  */
3172fcf3ce44SJohn Forte int
3173fcf3ce44SJohn Forte stmfCreateTargetGroup(stmfGroupName *targetGroupName)
3174fcf3ce44SJohn Forte {
3175fcf3ce44SJohn Forte 	int ret;
3176fcf3ce44SJohn Forte 	int fd;
3177fcf3ce44SJohn Forte 
3178fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
3179fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
3180fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName))) {
3181fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3182fcf3ce44SJohn Forte 	}
3183fcf3ce44SJohn Forte 
3184fcf3ce44SJohn Forte 	/* Check to ensure service exists */
3185fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
3186fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
3187fcf3ce44SJohn Forte 	}
3188fcf3ce44SJohn Forte 
3189fcf3ce44SJohn Forte 	/* call init */
3190fcf3ce44SJohn Forte 	ret = initializeConfig();
3191fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3192fcf3ce44SJohn Forte 		return (ret);
3193fcf3ce44SJohn Forte 	}
3194fcf3ce44SJohn Forte 
3195fcf3ce44SJohn Forte 	/*
3196fcf3ce44SJohn Forte 	 * Open control node for stmf
3197fcf3ce44SJohn Forte 	 */
3198fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3199fcf3ce44SJohn Forte 		return (ret);
3200fcf3ce44SJohn Forte 
3201fcf3ce44SJohn Forte 	/*
3202fcf3ce44SJohn Forte 	 * Add the group to the driver
3203fcf3ce44SJohn Forte 	 */
3204fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_TARGET_GROUP,
3205fcf3ce44SJohn Forte 	    targetGroupName)) != STMF_STATUS_SUCCESS) {
3206fcf3ce44SJohn Forte 		goto done;
3207fcf3ce44SJohn Forte 	}
3208fcf3ce44SJohn Forte 
32098fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
32108fe96085Stim szeto 		goto done;
32118fe96085Stim szeto 	}
32128fe96085Stim szeto 
3213fcf3ce44SJohn Forte 	/*
3214fcf3ce44SJohn Forte 	 * If the add to the driver was successful, add it to the persistent
3215fcf3ce44SJohn Forte 	 * store.
3216fcf3ce44SJohn Forte 	 */
3217fcf3ce44SJohn Forte 	ret = psCreateTargetGroup((char *)targetGroupName);
3218fcf3ce44SJohn Forte 	switch (ret) {
3219fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3220fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3221fcf3ce44SJohn Forte 			break;
3222fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
3223fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
3224fcf3ce44SJohn Forte 			break;
3225fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3226fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3227fcf3ce44SJohn Forte 			break;
3228fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3229fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3230fcf3ce44SJohn Forte 			break;
3231fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3232fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3233fcf3ce44SJohn Forte 			break;
3234fcf3ce44SJohn Forte 		default:
3235fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3236fcf3ce44SJohn Forte 			    "stmfCreateTargetGroup:psCreateTargetGroup"
3237fcf3ce44SJohn Forte 			    ":error(%d)", ret);
3238fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3239fcf3ce44SJohn Forte 			break;
3240fcf3ce44SJohn Forte 	}
3241fcf3ce44SJohn Forte 
3242fcf3ce44SJohn Forte done:
3243fcf3ce44SJohn Forte 	(void) close(fd);
3244fcf3ce44SJohn Forte 	return (ret);
3245fcf3ce44SJohn Forte }
3246fcf3ce44SJohn Forte 
3247fcf3ce44SJohn Forte /*
3248fcf3ce44SJohn Forte  * stmfDeleteHostGroup
3249fcf3ce44SJohn Forte  *
3250fcf3ce44SJohn Forte  * Purpose: Delete an initiator or local port group
3251fcf3ce44SJohn Forte  *
3252fcf3ce44SJohn Forte  * hostGroupName - group to delete
3253fcf3ce44SJohn Forte  */
3254fcf3ce44SJohn Forte int
3255fcf3ce44SJohn Forte stmfDeleteHostGroup(stmfGroupName *hostGroupName)
3256fcf3ce44SJohn Forte {
3257fcf3ce44SJohn Forte 	int ret;
3258fcf3ce44SJohn Forte 	int fd;
3259fcf3ce44SJohn Forte 
3260fcf3ce44SJohn Forte 	if (hostGroupName == NULL) {
3261fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3262fcf3ce44SJohn Forte 	}
3263fcf3ce44SJohn Forte 
3264fcf3ce44SJohn Forte 	/* Check to ensure service exists */
3265fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
3266fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
3267fcf3ce44SJohn Forte 	}
3268fcf3ce44SJohn Forte 
3269fcf3ce44SJohn Forte 	/* call init */
3270fcf3ce44SJohn Forte 	ret = initializeConfig();
3271fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3272fcf3ce44SJohn Forte 		return (ret);
3273fcf3ce44SJohn Forte 	}
3274fcf3ce44SJohn Forte 
3275fcf3ce44SJohn Forte 	/*
3276fcf3ce44SJohn Forte 	 * Open control node for stmf
3277fcf3ce44SJohn Forte 	 */
3278fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3279fcf3ce44SJohn Forte 		return (ret);
3280fcf3ce44SJohn Forte 
3281fcf3ce44SJohn Forte 	/*
3282fcf3ce44SJohn Forte 	 * Remove the group from the driver
3283fcf3ce44SJohn Forte 	 */
3284fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_REMOVE_HOST_GROUP,
3285fcf3ce44SJohn Forte 	    hostGroupName)) != STMF_STATUS_SUCCESS) {
3286fcf3ce44SJohn Forte 		goto done;
3287fcf3ce44SJohn Forte 	}
3288fcf3ce44SJohn Forte 
32898fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
32908fe96085Stim szeto 		goto done;
32918fe96085Stim szeto 	}
32928fe96085Stim szeto 
3293fcf3ce44SJohn Forte 	/*
3294fcf3ce44SJohn Forte 	 * If the remove from the driver was successful, remove it from the
3295fcf3ce44SJohn Forte 	 * persistent store.
3296fcf3ce44SJohn Forte 	 */
3297fcf3ce44SJohn Forte 	ret = psDeleteHostGroup((char *)hostGroupName);
3298fcf3ce44SJohn Forte 	switch (ret) {
3299fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3300fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3301fcf3ce44SJohn Forte 			break;
3302fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3303fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3304fcf3ce44SJohn Forte 			break;
3305fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3306fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3307fcf3ce44SJohn Forte 			break;
3308fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3309fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3310fcf3ce44SJohn Forte 			break;
3311fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3312fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3313fcf3ce44SJohn Forte 			break;
3314fcf3ce44SJohn Forte 		default:
3315fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3316fcf3ce44SJohn Forte 			    "stmfDeleteHostGroup:psDeleteHostGroup:error(%d)",
3317fcf3ce44SJohn Forte 			    ret);
3318fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3319fcf3ce44SJohn Forte 			break;
3320fcf3ce44SJohn Forte 	}
3321fcf3ce44SJohn Forte 
3322fcf3ce44SJohn Forte done:
3323fcf3ce44SJohn Forte 	(void) close(fd);
3324fcf3ce44SJohn Forte 	return (ret);
3325fcf3ce44SJohn Forte }
3326fcf3ce44SJohn Forte 
3327fcf3ce44SJohn Forte /*
3328fcf3ce44SJohn Forte  * stmfDeleteTargetGroup
3329fcf3ce44SJohn Forte  *
3330fcf3ce44SJohn Forte  * Purpose: Delete an initiator or local port group
3331fcf3ce44SJohn Forte  *
3332fcf3ce44SJohn Forte  * targetGroupName - group to delete
3333fcf3ce44SJohn Forte  */
3334fcf3ce44SJohn Forte int
3335fcf3ce44SJohn Forte stmfDeleteTargetGroup(stmfGroupName *targetGroupName)
3336fcf3ce44SJohn Forte {
3337fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
3338fcf3ce44SJohn Forte 	int fd;
3339fcf3ce44SJohn Forte 
3340fcf3ce44SJohn Forte 	if (targetGroupName == NULL) {
3341fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3342fcf3ce44SJohn Forte 	}
3343fcf3ce44SJohn Forte 
3344fcf3ce44SJohn Forte 	/* Check to ensure service exists */
3345fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
3346fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
3347fcf3ce44SJohn Forte 	}
3348fcf3ce44SJohn Forte 
3349fcf3ce44SJohn Forte 	/* call init */
3350fcf3ce44SJohn Forte 	ret = initializeConfig();
3351fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3352fcf3ce44SJohn Forte 		return (ret);
3353fcf3ce44SJohn Forte 	}
3354fcf3ce44SJohn Forte 
3355fcf3ce44SJohn Forte 	/*
3356fcf3ce44SJohn Forte 	 * Open control node for stmf
3357fcf3ce44SJohn Forte 	 */
3358fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
3359fcf3ce44SJohn Forte 		return (ret);
3360fcf3ce44SJohn Forte 
3361fcf3ce44SJohn Forte 	/*
3362fcf3ce44SJohn Forte 	 * Remove the group from the driver
3363fcf3ce44SJohn Forte 	 */
3364fcf3ce44SJohn Forte 	if ((ret = groupIoctl(fd, STMF_IOCTL_REMOVE_TARGET_GROUP,
3365fcf3ce44SJohn Forte 	    targetGroupName)) != STMF_STATUS_SUCCESS) {
3366fcf3ce44SJohn Forte 		goto done;
3367fcf3ce44SJohn Forte 	}
3368fcf3ce44SJohn Forte 
33698fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
33708fe96085Stim szeto 		goto done;
33718fe96085Stim szeto 	}
33728fe96085Stim szeto 
3373fcf3ce44SJohn Forte 	/*
3374fcf3ce44SJohn Forte 	 * If the remove from the driver was successful, remove it from the
3375fcf3ce44SJohn Forte 	 * persistent store.
3376fcf3ce44SJohn Forte 	 */
3377fcf3ce44SJohn Forte 	ret = psDeleteTargetGroup((char *)targetGroupName);
3378fcf3ce44SJohn Forte 	switch (ret) {
3379fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3380fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3381fcf3ce44SJohn Forte 			break;
3382fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3383fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3384fcf3ce44SJohn Forte 			break;
3385fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3386fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3387fcf3ce44SJohn Forte 			break;
3388fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3389fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3390fcf3ce44SJohn Forte 			break;
3391fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3392fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3393fcf3ce44SJohn Forte 			break;
3394fcf3ce44SJohn Forte 		default:
3395fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3396fcf3ce44SJohn Forte 			    "stmfDeleteTargetGroup:psDeleteTargetGroup"
3397fcf3ce44SJohn Forte 			    ":error(%d)", ret);
3398fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3399fcf3ce44SJohn Forte 			break;
3400fcf3ce44SJohn Forte 	}
3401fcf3ce44SJohn Forte 
3402fcf3ce44SJohn Forte done:
3403fcf3ce44SJohn Forte 	(void) close(fd);
3404fcf3ce44SJohn Forte 	return (ret);
3405fcf3ce44SJohn Forte }
3406fcf3ce44SJohn Forte 
3407fcf3ce44SJohn Forte /*
3408fcf3ce44SJohn Forte  * stmfDevidFromIscsiName
3409fcf3ce44SJohn Forte  *
3410fcf3ce44SJohn Forte  * Purpose: convert an iSCSI name to an stmf devid
3411fcf3ce44SJohn Forte  *
3412fcf3ce44SJohn Forte  * iscsiName - unicode nul terminated utf-8 encoded iSCSI name
3413fcf3ce44SJohn Forte  * devid - on success, contains the converted iscsi name
3414fcf3ce44SJohn Forte  */
3415fcf3ce44SJohn Forte int
3416fcf3ce44SJohn Forte stmfDevidFromIscsiName(char *iscsiName, stmfDevid *devid)
3417fcf3ce44SJohn Forte {
3418fcf3ce44SJohn Forte 	if (devid == NULL || iscsiName == NULL)
3419fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3420fcf3ce44SJohn Forte 
3421fcf3ce44SJohn Forte 	bzero(devid, sizeof (stmfDevid));
3422fcf3ce44SJohn Forte 
3423fcf3ce44SJohn Forte 	/* Validate size of target */
3424fcf3ce44SJohn Forte 	if ((devid->identLength = strlen(iscsiName)) > MAX_ISCSI_NAME ||
3425fcf3ce44SJohn Forte 	    devid->identLength < strlen(EUI) ||
3426fcf3ce44SJohn Forte 	    devid->identLength < strlen(IQN)) {
3427fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3428fcf3ce44SJohn Forte 	}
3429fcf3ce44SJohn Forte 
3430fcf3ce44SJohn Forte 	if ((strncmp(iscsiName, EUI, strlen(EUI)) != 0) &&
3431fcf3ce44SJohn Forte 	    strncmp(iscsiName, IQN, strlen(IQN)) != 0) {
3432fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3433fcf3ce44SJohn Forte 	}
3434fcf3ce44SJohn Forte 
3435fcf3ce44SJohn Forte 	/* copy UTF-8 bytes to ident */
3436fcf3ce44SJohn Forte 	bcopy(iscsiName, devid->ident, devid->identLength);
3437fcf3ce44SJohn Forte 
3438fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
3439fcf3ce44SJohn Forte }
3440fcf3ce44SJohn Forte 
3441fcf3ce44SJohn Forte /*
3442fcf3ce44SJohn Forte  * stmfDevidFromWwn
3443fcf3ce44SJohn Forte  *
3444fcf3ce44SJohn Forte  * Purpose: convert a WWN to an stmf devid
3445fcf3ce44SJohn Forte  *
3446fcf3ce44SJohn Forte  * wwn - 8-byte wwn identifier
3447fcf3ce44SJohn Forte  * devid - on success, contains the converted wwn
3448fcf3ce44SJohn Forte  */
3449fcf3ce44SJohn Forte int
3450fcf3ce44SJohn Forte stmfDevidFromWwn(uchar_t *wwn, stmfDevid *devid)
3451fcf3ce44SJohn Forte {
3452fcf3ce44SJohn Forte 	if (wwn == NULL || devid == NULL)
3453fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3454fcf3ce44SJohn Forte 
3455fcf3ce44SJohn Forte 	bzero(devid, sizeof (stmfDevid));
3456fcf3ce44SJohn Forte 
3457fcf3ce44SJohn Forte 	/* Copy eui prefix */
3458fcf3ce44SJohn Forte 	(void) bcopy(WWN, devid->ident, strlen(WWN));
3459fcf3ce44SJohn Forte 
3460fcf3ce44SJohn Forte 	/* Convert to ASCII uppercase hexadecimal string */
3461fcf3ce44SJohn Forte 	(void) snprintf((char *)&devid->ident[strlen(WWN)],
3462fcf3ce44SJohn Forte 	    sizeof (devid->ident), "%02X%02X%02X%02X%02X%02X%02X%02X",
3463fcf3ce44SJohn Forte 	    wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
3464fcf3ce44SJohn Forte 
3465fcf3ce44SJohn Forte 	devid->identLength = strlen((char *)devid->ident);
3466fcf3ce44SJohn Forte 
3467fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
3468fcf3ce44SJohn Forte }
3469fcf3ce44SJohn Forte 
3470fcf3ce44SJohn Forte /*
3471fcf3ce44SJohn Forte  * stmfFreeMemory
3472fcf3ce44SJohn Forte  *
3473fcf3ce44SJohn Forte  * Purpose: Free memory allocated by this library
3474fcf3ce44SJohn Forte  *
3475fcf3ce44SJohn Forte  * memory - previously allocated pointer of memory managed by library
3476fcf3ce44SJohn Forte  */
3477fcf3ce44SJohn Forte void
3478fcf3ce44SJohn Forte stmfFreeMemory(void *memory)
3479fcf3ce44SJohn Forte {
3480fcf3ce44SJohn Forte 	free(memory);
3481fcf3ce44SJohn Forte }
3482fcf3ce44SJohn Forte 
3483fcf3ce44SJohn Forte /*
34848fe96085Stim szeto  * get host group, target group list from stmf
3485fcf3ce44SJohn Forte  *
34868fe96085Stim szeto  * groupType - HOST_GROUP, TARGET_GROUP
3487fcf3ce44SJohn Forte  */
34888fe96085Stim szeto static int
34898fe96085Stim szeto groupListIoctl(stmfGroupList **groupList, int groupType)
3490fcf3ce44SJohn Forte {
3491fcf3ce44SJohn Forte 	int ret;
34928fe96085Stim szeto 	int fd;
34938fe96085Stim szeto 	int ioctlRet;
34948fe96085Stim szeto 	int i;
34958fe96085Stim szeto 	int cmd;
34968fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
34978fe96085Stim szeto 	/* framework group list */
34988fe96085Stim szeto 	stmf_group_name_t *iGroupList = NULL;
34998fe96085Stim szeto 	uint32_t groupListSize;
3500fcf3ce44SJohn Forte 
35018fe96085Stim szeto 	if (groupList == NULL) {
3502fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3503fcf3ce44SJohn Forte 	}
3504fcf3ce44SJohn Forte 
35058fe96085Stim szeto 	if (groupType == HOST_GROUP) {
35068fe96085Stim szeto 		cmd = STMF_IOCTL_GET_HG_LIST;
35078fe96085Stim szeto 	} else if (groupType == TARGET_GROUP) {
35088fe96085Stim szeto 		cmd = STMF_IOCTL_GET_TG_LIST;
35098fe96085Stim szeto 	} else {
35108fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
35118fe96085Stim szeto 	}
35128fe96085Stim szeto 
35138fe96085Stim szeto 	/* call init */
35148fe96085Stim szeto 	ret = initializeConfig();
35158fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
35168fe96085Stim szeto 		return (ret);
35178fe96085Stim szeto 	}
35188fe96085Stim szeto 
35198fe96085Stim szeto 	/*
35208fe96085Stim szeto 	 * Open control node for stmf
35218fe96085Stim szeto 	 */
35228fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
35238fe96085Stim szeto 		return (ret);
35248fe96085Stim szeto 
35258fe96085Stim szeto 	/*
35268fe96085Stim szeto 	 * Allocate ioctl input buffer
35278fe96085Stim szeto 	 */
35288fe96085Stim szeto 	groupListSize = ALLOC_GROUP;
35298fe96085Stim szeto 	groupListSize = groupListSize * (sizeof (stmf_group_name_t));
35308fe96085Stim szeto 	iGroupList = (stmf_group_name_t *)calloc(1, groupListSize);
35318fe96085Stim szeto 	if (iGroupList == NULL) {
35328fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
35338fe96085Stim szeto 		goto done;
35348fe96085Stim szeto 	}
35358fe96085Stim szeto 
35368fe96085Stim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
35378fe96085Stim szeto 	/*
35388fe96085Stim szeto 	 * Issue ioctl to get the group list
35398fe96085Stim szeto 	 */
35408fe96085Stim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
35418fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = groupListSize;
35428fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupList;
35438fe96085Stim szeto 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
35448fe96085Stim szeto 	if (ioctlRet != 0) {
35458fe96085Stim szeto 		switch (errno) {
35468fe96085Stim szeto 			case EBUSY:
35478fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
35488fe96085Stim szeto 				break;
35498fe96085Stim szeto 			case EPERM:
35508fe96085Stim szeto 			case EACCES:
35518fe96085Stim szeto 				ret = STMF_ERROR_PERM;
35528fe96085Stim szeto 				break;
35538fe96085Stim szeto 			default:
35548fe96085Stim szeto 				syslog(LOG_DEBUG,
35558fe96085Stim szeto 				    "groupListIoctl:ioctl errno(%d)",
35568fe96085Stim szeto 				    errno);
35578fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
35588fe96085Stim szeto 				break;
35598fe96085Stim szeto 		}
35608fe96085Stim szeto 		goto done;
35618fe96085Stim szeto 	}
35628fe96085Stim szeto 	/*
35638fe96085Stim szeto 	 * Check whether input buffer was large enough
35648fe96085Stim szeto 	 */
35658fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_GROUP) {
35668fe96085Stim szeto 		groupListSize = stmfIoctl.stmf_obuf_max_nentries *
35678fe96085Stim szeto 		    sizeof (stmf_group_name_t);
35688fe96085Stim szeto 		iGroupList = realloc(iGroupList, groupListSize);
35698fe96085Stim szeto 		if (iGroupList == NULL) {
35708fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
35718fe96085Stim szeto 			goto done;
35728fe96085Stim szeto 		}
35738fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = groupListSize;
35748fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupList;
35758fe96085Stim szeto 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
35768fe96085Stim szeto 		if (ioctlRet != 0) {
35778fe96085Stim szeto 			switch (errno) {
35788fe96085Stim szeto 				case EBUSY:
35798fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
35808fe96085Stim szeto 					break;
35818fe96085Stim szeto 				case EPERM:
35828fe96085Stim szeto 				case EACCES:
35838fe96085Stim szeto 					ret = STMF_ERROR_PERM;
35848fe96085Stim szeto 					break;
35858fe96085Stim szeto 				default:
35868fe96085Stim szeto 					syslog(LOG_DEBUG,
35878fe96085Stim szeto 					    "groupListIoctl:ioctl errno(%d)",
35888fe96085Stim szeto 					    errno);
35898fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
35908fe96085Stim szeto 					break;
35918fe96085Stim szeto 			}
35928fe96085Stim szeto 			goto done;
35938fe96085Stim szeto 		}
35948fe96085Stim szeto 	}
35958fe96085Stim szeto 
35968fe96085Stim szeto 	/* allocate and copy to caller's buffer */
35974f2997b2Ssrivijitha dugganapalli 	*groupList = (stmfGroupList *)calloc(1, sizeof (stmfGroupList) +
35984f2997b2Ssrivijitha dugganapalli 	    sizeof (stmfGroupName) * stmfIoctl.stmf_obuf_nentries);
35998fe96085Stim szeto 	if (*groupList == NULL) {
36008fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
36018fe96085Stim szeto 		goto done;
36028fe96085Stim szeto 	}
36038fe96085Stim szeto 	(*groupList)->cnt = stmfIoctl.stmf_obuf_nentries;
36048fe96085Stim szeto 	for (i = 0; i < stmfIoctl.stmf_obuf_nentries; i++) {
36052f624233SNattuvetty Bhavyan 		bcopy(iGroupList[i].name, (*groupList)->name[i],
36068fe96085Stim szeto 		    sizeof (stmfGroupName));
36078fe96085Stim szeto 	}
36088fe96085Stim szeto 
36098fe96085Stim szeto done:
36108fe96085Stim szeto 	free(iGroupList);
36118fe96085Stim szeto 	(void) close(fd);
36128fe96085Stim szeto 	return (ret);
36138fe96085Stim szeto }
36148fe96085Stim szeto 
36158fe96085Stim szeto /*
36168fe96085Stim szeto  * get host group members, target group members from stmf
36178fe96085Stim szeto  *
36188fe96085Stim szeto  * groupProps - allocated on success
36198fe96085Stim szeto  *
36208fe96085Stim szeto  * groupType - HOST_GROUP, TARGET_GROUP
36218fe96085Stim szeto  */
36228fe96085Stim szeto static int
36238fe96085Stim szeto groupMemberListIoctl(stmfGroupName *groupName, stmfGroupProperties **groupProps,
36248fe96085Stim szeto     int groupType)
36258fe96085Stim szeto {
36268fe96085Stim szeto 	int ret;
36278fe96085Stim szeto 	int fd;
36288fe96085Stim szeto 	int ioctlRet;
36298fe96085Stim szeto 	int i;
36308fe96085Stim szeto 	int cmd;
36318fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
36328fe96085Stim szeto 	/* framework group list */
36338fe96085Stim szeto 	stmf_group_name_t iGroupName;
36348fe96085Stim szeto 	stmf_ge_ident_t *iGroupMembers;
36358fe96085Stim szeto 	uint32_t groupListSize;
36368fe96085Stim szeto 
36378fe96085Stim szeto 	if (groupName == NULL) {
36388fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
36398fe96085Stim szeto 	}
36408fe96085Stim szeto 
36418fe96085Stim szeto 	if (groupType == HOST_GROUP) {
36428fe96085Stim szeto 		cmd = STMF_IOCTL_GET_HG_ENTRIES;
36438fe96085Stim szeto 	} else if (groupType == TARGET_GROUP) {
36448fe96085Stim szeto 		cmd = STMF_IOCTL_GET_TG_ENTRIES;
36458fe96085Stim szeto 	} else {
36468fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
36478fe96085Stim szeto 	}
36488fe96085Stim szeto 
36498fe96085Stim szeto 	/* call init */
36508fe96085Stim szeto 	ret = initializeConfig();
36518fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
36528fe96085Stim szeto 		return (ret);
36538fe96085Stim szeto 	}
36548fe96085Stim szeto 
36558fe96085Stim szeto 	/*
36568fe96085Stim szeto 	 * Open control node for stmf
36578fe96085Stim szeto 	 */
36588fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
36598fe96085Stim szeto 		return (ret);
36608fe96085Stim szeto 
36618fe96085Stim szeto 	bzero(&iGroupName, sizeof (iGroupName));
36628fe96085Stim szeto 
36638fe96085Stim szeto 	bcopy(groupName, &iGroupName.name, strlen((char *)groupName));
36648fe96085Stim szeto 
36658fe96085Stim szeto 	iGroupName.name_size = strlen((char *)groupName);
36668fe96085Stim szeto 
36678fe96085Stim szeto 	/*
36688fe96085Stim szeto 	 * Allocate ioctl input buffer
36698fe96085Stim szeto 	 */
36708fe96085Stim szeto 	groupListSize = ALLOC_GRP_MEMBER;
36718fe96085Stim szeto 	groupListSize = groupListSize * (sizeof (stmf_ge_ident_t));
36728fe96085Stim szeto 	iGroupMembers = (stmf_ge_ident_t *)calloc(1, groupListSize);
36738fe96085Stim szeto 	if (iGroupMembers == NULL) {
36748fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
36758fe96085Stim szeto 		goto done;
36768fe96085Stim szeto 	}
36778fe96085Stim szeto 
36788fe96085Stim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
36798fe96085Stim szeto 	/*
36808fe96085Stim szeto 	 * Issue ioctl to get the group list
36818fe96085Stim szeto 	 */
36828fe96085Stim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
36838fe96085Stim szeto 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&iGroupName;
36848fe96085Stim szeto 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_group_name_t);
36858fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = groupListSize;
36868fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupMembers;
36878fe96085Stim szeto 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
36888fe96085Stim szeto 	if (ioctlRet != 0) {
36898fe96085Stim szeto 		switch (errno) {
36908fe96085Stim szeto 			case EBUSY:
36918fe96085Stim szeto 				ret = STMF_ERROR_BUSY;
36928fe96085Stim szeto 				break;
36938fe96085Stim szeto 			case EPERM:
36948fe96085Stim szeto 			case EACCES:
36958fe96085Stim szeto 				ret = STMF_ERROR_PERM;
36968fe96085Stim szeto 				break;
36978fe96085Stim szeto 			default:
36988fe96085Stim szeto 				syslog(LOG_DEBUG,
36998fe96085Stim szeto 				    "groupListIoctl:ioctl errno(%d)",
37008fe96085Stim szeto 				    errno);
37018fe96085Stim szeto 				ret = STMF_STATUS_ERROR;
37028fe96085Stim szeto 				break;
37038fe96085Stim szeto 		}
37048fe96085Stim szeto 		goto done;
37058fe96085Stim szeto 	}
37068fe96085Stim szeto 	/*
37078fe96085Stim szeto 	 * Check whether input buffer was large enough
37088fe96085Stim szeto 	 */
37098fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_GRP_MEMBER) {
37108fe96085Stim szeto 		groupListSize = stmfIoctl.stmf_obuf_max_nentries *
37118fe96085Stim szeto 		    sizeof (stmf_ge_ident_t);
37128fe96085Stim szeto 		iGroupMembers = realloc(iGroupMembers, groupListSize);
37138fe96085Stim szeto 		if (iGroupMembers == NULL) {
37148fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
37158fe96085Stim szeto 			goto done;
37168fe96085Stim szeto 		}
37178fe96085Stim szeto 		stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&iGroupName;
37188fe96085Stim szeto 		stmfIoctl.stmf_ibuf_size = sizeof (stmf_group_name_t);
37198fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = groupListSize;
37208fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)iGroupMembers;
37218fe96085Stim szeto 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
37228fe96085Stim szeto 		if (ioctlRet != 0) {
37238fe96085Stim szeto 			switch (errno) {
37248fe96085Stim szeto 				case EBUSY:
37258fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
37268fe96085Stim szeto 					break;
37278fe96085Stim szeto 				case EPERM:
37288fe96085Stim szeto 				case EACCES:
37298fe96085Stim szeto 					ret = STMF_ERROR_PERM;
37308fe96085Stim szeto 					break;
37318fe96085Stim szeto 				default:
37328fe96085Stim szeto 					syslog(LOG_DEBUG,
37338fe96085Stim szeto 					    "groupListIoctl:ioctl errno(%d)",
37348fe96085Stim szeto 					    errno);
37358fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
37368fe96085Stim szeto 					break;
37378fe96085Stim szeto 			}
37388fe96085Stim szeto 			goto done;
37398fe96085Stim szeto 		}
37408fe96085Stim szeto 	}
37418fe96085Stim szeto 
37428fe96085Stim szeto 	/* allocate and copy to caller's buffer */
37438fe96085Stim szeto 	*groupProps = (stmfGroupProperties *)calloc(1,
37444f2997b2Ssrivijitha dugganapalli 	    sizeof (stmfGroupProperties) +
37454f2997b2Ssrivijitha dugganapalli 	    sizeof (stmfDevid) * stmfIoctl.stmf_obuf_nentries);
37468fe96085Stim szeto 	if (*groupProps == NULL) {
37478fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
37488fe96085Stim szeto 		goto done;
37498fe96085Stim szeto 	}
37508fe96085Stim szeto 	(*groupProps)->cnt = stmfIoctl.stmf_obuf_nentries;
37518fe96085Stim szeto 	for (i = 0; i < stmfIoctl.stmf_obuf_nentries; i++) {
37528fe96085Stim szeto 		(*groupProps)->name[i].identLength =
37532f624233SNattuvetty Bhavyan 		    iGroupMembers[i].ident_size;
37542f624233SNattuvetty Bhavyan 		bcopy(iGroupMembers[i].ident, (*groupProps)->name[i].ident,
37552f624233SNattuvetty Bhavyan 		    iGroupMembers[i].ident_size);
37568fe96085Stim szeto 	}
37578fe96085Stim szeto 
37588fe96085Stim szeto done:
37598fe96085Stim szeto 	free(iGroupMembers);
37608fe96085Stim szeto 	(void) close(fd);
37618fe96085Stim szeto 	return (ret);
37628fe96085Stim szeto }
37638fe96085Stim szeto 
37648fe96085Stim szeto /*
37658fe96085Stim szeto  * Purpose: access persistent config data for host groups and target groups
37668fe96085Stim szeto  */
37678fe96085Stim szeto static int
37688fe96085Stim szeto iLoadGroupFromPs(stmfGroupList **groupList, int type)
37698fe96085Stim szeto {
37708fe96085Stim szeto 	int ret;
37718fe96085Stim szeto 
37728fe96085Stim szeto 	if (groupList == NULL) {
37738fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
37748fe96085Stim szeto 	}
37758fe96085Stim szeto 
37768fe96085Stim szeto 	if (type == HOST_GROUP) {
37778fe96085Stim szeto 		ret = psGetHostGroupList(groupList);
37788fe96085Stim szeto 	} else if (type == TARGET_GROUP) {
37798fe96085Stim szeto 		ret = psGetTargetGroupList(groupList);
37808fe96085Stim szeto 	} else {
37818fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
37828fe96085Stim szeto 	}
3783fcf3ce44SJohn Forte 	switch (ret) {
3784fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3785fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3786fcf3ce44SJohn Forte 			break;
3787fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3788fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3789fcf3ce44SJohn Forte 			break;
3790fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3791fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3792fcf3ce44SJohn Forte 			break;
3793fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3794fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3795fcf3ce44SJohn Forte 			break;
3796fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3797fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3798fcf3ce44SJohn Forte 			break;
3799fcf3ce44SJohn Forte 		default:
3800fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3801fcf3ce44SJohn Forte 			    "stmfGetHostGroupList:psGetHostGroupList:error(%d)",
3802fcf3ce44SJohn Forte 			    ret);
3803fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3804fcf3ce44SJohn Forte 			break;
3805fcf3ce44SJohn Forte 	}
3806fcf3ce44SJohn Forte 
3807fcf3ce44SJohn Forte 	return (ret);
3808fcf3ce44SJohn Forte }
3809fcf3ce44SJohn Forte 
3810fcf3ce44SJohn Forte /*
38118fe96085Stim szeto  * stmfGetHostGroupList
3812fcf3ce44SJohn Forte  *
38138fe96085Stim szeto  * Purpose: Retrieves the list of initiator group oids
3814fcf3ce44SJohn Forte  *
38158fe96085Stim szeto  * hostGroupList - pointer to pointer to hostGroupList structure
38168fe96085Stim szeto  *                 on success, this contains the host group list.
3817fcf3ce44SJohn Forte  */
3818fcf3ce44SJohn Forte int
38198fe96085Stim szeto stmfGetHostGroupList(stmfGroupList **hostGroupList)
3820fcf3ce44SJohn Forte {
38218fe96085Stim szeto 	int ret = STMF_STATUS_ERROR;
3822fcf3ce44SJohn Forte 
38238fe96085Stim szeto 	if (hostGroupList == NULL) {
3824fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3825fcf3ce44SJohn Forte 	}
3826fcf3ce44SJohn Forte 
38278fe96085Stim szeto 	ret = groupListIoctl(hostGroupList, HOST_GROUP);
38288fe96085Stim szeto 	return (ret);
38298fe96085Stim szeto }
38308fe96085Stim szeto 
38318fe96085Stim szeto 
38328fe96085Stim szeto /*
38338fe96085Stim szeto  * Purpose: access persistent config data for host groups and target groups
38348fe96085Stim szeto  */
38358fe96085Stim szeto static int
38368fe96085Stim szeto iLoadGroupMembersFromPs(stmfGroupName *groupName,
38378fe96085Stim szeto     stmfGroupProperties **groupProp, int type)
38388fe96085Stim szeto {
38398fe96085Stim szeto 	int ret;
38408fe96085Stim szeto 
38418fe96085Stim szeto 	if (groupName == NULL) {
38428fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
38438fe96085Stim szeto 	}
38448fe96085Stim szeto 
38458fe96085Stim szeto 	if (type == HOST_GROUP) {
3846fcf3ce44SJohn Forte 		ret = psGetHostGroupMemberList((char *)groupName, groupProp);
38478fe96085Stim szeto 	} else if (type == TARGET_GROUP) {
38488fe96085Stim szeto 		ret = psGetTargetGroupMemberList((char *)groupName, groupProp);
38498fe96085Stim szeto 	} else {
38508fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
38518fe96085Stim szeto 	}
3852fcf3ce44SJohn Forte 	switch (ret) {
3853fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3854fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3855fcf3ce44SJohn Forte 			break;
3856fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
3857fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
3858fcf3ce44SJohn Forte 			break;
3859fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3860fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3861fcf3ce44SJohn Forte 			break;
3862fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3863fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3864fcf3ce44SJohn Forte 			break;
3865fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3866fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3867fcf3ce44SJohn Forte 			break;
3868fcf3ce44SJohn Forte 		default:
3869fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
38708fe96085Stim szeto 			    "iLoadGroupMembersFromPs:psGetHostGroupList:"
38718fe96085Stim szeto 			    "error(%d)", ret);
3872fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3873fcf3ce44SJohn Forte 			break;
3874fcf3ce44SJohn Forte 	}
3875fcf3ce44SJohn Forte 
3876fcf3ce44SJohn Forte 	return (ret);
3877fcf3ce44SJohn Forte }
3878fcf3ce44SJohn Forte 
3879fcf3ce44SJohn Forte /*
38808fe96085Stim szeto  * stmfGetHostGroupMembers
38818fe96085Stim szeto  *
38828fe96085Stim szeto  * Purpose: Retrieves the group properties for a host group
38838fe96085Stim szeto  *
38848fe96085Stim szeto  * groupName - name of group for which to retrieve host group members.
38858fe96085Stim szeto  * groupProp - pointer to pointer to stmfGroupProperties structure
38868fe96085Stim szeto  *             on success, this contains the list of group members.
38878fe96085Stim szeto  */
38888fe96085Stim szeto int
38898fe96085Stim szeto stmfGetHostGroupMembers(stmfGroupName *groupName,
38908fe96085Stim szeto     stmfGroupProperties **groupProp)
38918fe96085Stim szeto {
38928fe96085Stim szeto 	int ret;
38938fe96085Stim szeto 
38948fe96085Stim szeto 	if (groupName == NULL || groupProp == NULL) {
38958fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
38968fe96085Stim szeto 	}
38978fe96085Stim szeto 
38988fe96085Stim szeto 	ret = groupMemberListIoctl(groupName, groupProp, HOST_GROUP);
38998fe96085Stim szeto 
39008fe96085Stim szeto 	return (ret);
39018fe96085Stim szeto }
39028fe96085Stim szeto 
39038fe96085Stim szeto /*
3904fcf3ce44SJohn Forte  * stmfGetProviderData
3905fcf3ce44SJohn Forte  *
3906fcf3ce44SJohn Forte  * Purpose: Get provider data list
3907fcf3ce44SJohn Forte  *
3908fcf3ce44SJohn Forte  * providerName - name of provider for which to retrieve the data
3909fcf3ce44SJohn Forte  * nvl - pointer to nvlist_t pointer which will contain the nvlist data
3910fcf3ce44SJohn Forte  *       retrieved.
3911fcf3ce44SJohn Forte  * providerType - type of provider for which to retrieve data.
3912fcf3ce44SJohn Forte  *		    STMF_LU_PROVIDER_TYPE
3913fcf3ce44SJohn Forte  *		    STMF_PORT_PROVIDER_TYPE
3914fcf3ce44SJohn Forte  */
3915fcf3ce44SJohn Forte int
3916fcf3ce44SJohn Forte stmfGetProviderData(char *providerName, nvlist_t **nvl, int providerType)
3917fcf3ce44SJohn Forte {
3918fcf3ce44SJohn Forte 	return (stmfGetProviderDataProt(providerName, nvl, providerType,
3919fcf3ce44SJohn Forte 	    NULL));
3920fcf3ce44SJohn Forte }
3921fcf3ce44SJohn Forte 
3922fcf3ce44SJohn Forte /*
3923fcf3ce44SJohn Forte  * stmfGetProviderDataProt
3924fcf3ce44SJohn Forte  *
3925fcf3ce44SJohn Forte  * Purpose: Get provider data list with token
3926fcf3ce44SJohn Forte  *
3927fcf3ce44SJohn Forte  * providerName - name of provider for which to retrieve the data
3928fcf3ce44SJohn Forte  * nvl - pointer to nvlist_t pointer which will contain the nvlist data
3929fcf3ce44SJohn Forte  *       retrieved.
3930fcf3ce44SJohn Forte  * providerType - type of provider for which to retrieve data.
3931fcf3ce44SJohn Forte  *		    STMF_LU_PROVIDER_TYPE
3932fcf3ce44SJohn Forte  *		    STMF_PORT_PROVIDER_TYPE
3933fcf3ce44SJohn Forte  * setToken - Returns the stale data token
3934fcf3ce44SJohn Forte  */
3935fcf3ce44SJohn Forte int
3936fcf3ce44SJohn Forte stmfGetProviderDataProt(char *providerName, nvlist_t **nvl, int providerType,
3937fcf3ce44SJohn Forte     uint64_t *setToken)
3938fcf3ce44SJohn Forte {
3939fcf3ce44SJohn Forte 	int ret;
3940fcf3ce44SJohn Forte 
3941fcf3ce44SJohn Forte 	if (providerName == NULL || nvl == NULL) {
3942fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3943fcf3ce44SJohn Forte 	}
3944fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
3945fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
3946fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
3947fcf3ce44SJohn Forte 	}
3948fcf3ce44SJohn Forte 	/* call init */
3949fcf3ce44SJohn Forte 	ret = initializeConfig();
3950fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
3951fcf3ce44SJohn Forte 		return (ret);
3952fcf3ce44SJohn Forte 	}
39538fe96085Stim szeto 	return (getProviderData(providerName, nvl, providerType, setToken));
3954fcf3ce44SJohn Forte }
3955fcf3ce44SJohn Forte 
3956fcf3ce44SJohn Forte /*
3957fcf3ce44SJohn Forte  * stmfGetProviderDataList
3958fcf3ce44SJohn Forte  *
3959fcf3ce44SJohn Forte  * Purpose: Get the list of providers currently persisting data
3960fcf3ce44SJohn Forte  *
3961fcf3ce44SJohn Forte  * providerList - pointer to pointer to an stmfProviderList structure allocated
3962fcf3ce44SJohn Forte  *                by the caller. Will contain the list of providers on success.
3963fcf3ce44SJohn Forte  */
3964fcf3ce44SJohn Forte int
3965fcf3ce44SJohn Forte stmfGetProviderDataList(stmfProviderList **providerList)
3966fcf3ce44SJohn Forte {
3967fcf3ce44SJohn Forte 	int ret;
3968fcf3ce44SJohn Forte 
3969fcf3ce44SJohn Forte 	ret = psGetProviderDataList(providerList);
3970fcf3ce44SJohn Forte 	switch (ret) {
3971fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
3972fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
3973fcf3ce44SJohn Forte 			break;
3974fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
3975fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
3976fcf3ce44SJohn Forte 			break;
3977fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
3978fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
3979fcf3ce44SJohn Forte 			break;
3980fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
3981fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
3982fcf3ce44SJohn Forte 			break;
3983fcf3ce44SJohn Forte 		default:
3984fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
3985fcf3ce44SJohn Forte 			    "stmfGetProviderDataList:psGetProviderDataList"
3986fcf3ce44SJohn Forte 			    ":error(%d)", ret);
3987fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
3988fcf3ce44SJohn Forte 			break;
3989fcf3ce44SJohn Forte 	}
3990fcf3ce44SJohn Forte 
3991fcf3ce44SJohn Forte 	return (ret);
3992fcf3ce44SJohn Forte }
3993fcf3ce44SJohn Forte 
3994fcf3ce44SJohn Forte 
3995fcf3ce44SJohn Forte /*
3996fcf3ce44SJohn Forte  * stmfGetSessionList
3997fcf3ce44SJohn Forte  *
3998fcf3ce44SJohn Forte  * Purpose: Retrieves the session list for a target (devid)
3999fcf3ce44SJohn Forte  *
4000fcf3ce44SJohn Forte  * devid - devid of target for which to retrieve session information.
4001fcf3ce44SJohn Forte  * sessionList - pointer to pointer to stmfSessionList structure
4002fcf3ce44SJohn Forte  *             on success, this contains the list of initiator sessions.
4003fcf3ce44SJohn Forte  */
4004fcf3ce44SJohn Forte int
4005fcf3ce44SJohn Forte stmfGetSessionList(stmfDevid *devid, stmfSessionList **sessionList)
4006fcf3ce44SJohn Forte {
4007fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4008fcf3ce44SJohn Forte 	int fd;
4009fcf3ce44SJohn Forte 	int ioctlRet;
4010fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_SESSION_LIST;
4011fcf3ce44SJohn Forte 	int i;
4012fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
401356261083SCharles Ting 	slist_scsi_session_t *fSessionList, *fSessionListP = NULL;
4014fcf3ce44SJohn Forte 	uint8_t ident[260];
4015fcf3ce44SJohn Forte 	uint32_t fSessionListSize;
4016fcf3ce44SJohn Forte 
4017fcf3ce44SJohn Forte 	if (sessionList == NULL || devid == NULL) {
4018fcf3ce44SJohn Forte 		ret = STMF_ERROR_INVALID_ARG;
4019fcf3ce44SJohn Forte 	}
4020fcf3ce44SJohn Forte 
4021fcf3ce44SJohn Forte 	/* call init */
4022fcf3ce44SJohn Forte 	ret = initializeConfig();
4023fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4024fcf3ce44SJohn Forte 		return (ret);
4025fcf3ce44SJohn Forte 	}
4026fcf3ce44SJohn Forte 
4027fcf3ce44SJohn Forte 	/*
4028fcf3ce44SJohn Forte 	 * Open control node for stmf
4029fcf3ce44SJohn Forte 	 */
4030fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
4031fcf3ce44SJohn Forte 		return (ret);
4032fcf3ce44SJohn Forte 
4033fcf3ce44SJohn Forte 	/*
4034fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
4035fcf3ce44SJohn Forte 	 */
40368fe96085Stim szeto 	fSessionListSize = ALLOC_SESSION;
4037fcf3ce44SJohn Forte 	fSessionListSize = fSessionListSize * (sizeof (slist_scsi_session_t));
4038fcf3ce44SJohn Forte 	fSessionList = (slist_scsi_session_t *)calloc(1, fSessionListSize);
403956261083SCharles Ting 	fSessionListP = fSessionList;
4040fcf3ce44SJohn Forte 	if (fSessionList == NULL) {
404156261083SCharles Ting 		ret = STMF_ERROR_NOMEM;
404256261083SCharles Ting 		goto done;
4043fcf3ce44SJohn Forte 	}
4044fcf3ce44SJohn Forte 
4045fcf3ce44SJohn Forte 	ident[IDENT_LENGTH_BYTE] = devid->identLength;
4046fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &ident[IDENT_LENGTH_BYTE + 1],
4047fcf3ce44SJohn Forte 	    devid->identLength);
4048fcf3ce44SJohn Forte 
4049fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4050fcf3ce44SJohn Forte 	/*
4051fcf3ce44SJohn Forte 	 * Issue ioctl to get the session list
4052fcf3ce44SJohn Forte 	 */
4053fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4054fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ident;
4055fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ident);
4056fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fSessionListSize;
4057fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fSessionList;
4058fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4059fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4060fcf3ce44SJohn Forte 		switch (errno) {
4061fcf3ce44SJohn Forte 			case EBUSY:
4062fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4063fcf3ce44SJohn Forte 				break;
40648fe96085Stim szeto 			case EPERM:
4065fcf3ce44SJohn Forte 			case EACCES:
4066fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4067fcf3ce44SJohn Forte 				break;
4068fcf3ce44SJohn Forte 			default:
4069fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4070fcf3ce44SJohn Forte 				    "stmfGetSessionList:ioctl errno(%d)",
4071fcf3ce44SJohn Forte 				    errno);
4072fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4073fcf3ce44SJohn Forte 				break;
4074fcf3ce44SJohn Forte 		}
4075fcf3ce44SJohn Forte 		goto done;
4076fcf3ce44SJohn Forte 	}
4077fcf3ce44SJohn Forte 	/*
4078fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
4079fcf3ce44SJohn Forte 	 */
40808fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_SESSION) {
4081fcf3ce44SJohn Forte 		fSessionListSize = stmfIoctl.stmf_obuf_max_nentries *
4082fcf3ce44SJohn Forte 		    sizeof (slist_scsi_session_t);
4083fcf3ce44SJohn Forte 		fSessionList = realloc(fSessionList, fSessionListSize);
4084fcf3ce44SJohn Forte 		if (fSessionList == NULL) {
408556261083SCharles Ting 			ret = STMF_ERROR_NOMEM;
408656261083SCharles Ting 			goto done;
4087fcf3ce44SJohn Forte 		}
408856261083SCharles Ting 		fSessionListP = fSessionList;
4089fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fSessionListSize;
4090fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fSessionList;
4091fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4092fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
4093fcf3ce44SJohn Forte 			switch (errno) {
4094fcf3ce44SJohn Forte 				case EBUSY:
4095fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
4096fcf3ce44SJohn Forte 					break;
40978fe96085Stim szeto 				case EPERM:
4098fcf3ce44SJohn Forte 				case EACCES:
4099fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
4100fcf3ce44SJohn Forte 					break;
4101fcf3ce44SJohn Forte 				default:
4102fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
4103fcf3ce44SJohn Forte 					    "stmfGetSessionList:ioctl "
4104fcf3ce44SJohn Forte 					    "errno(%d)", errno);
4105fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
4106fcf3ce44SJohn Forte 					break;
4107fcf3ce44SJohn Forte 			}
4108fcf3ce44SJohn Forte 			goto done;
4109fcf3ce44SJohn Forte 		}
4110fcf3ce44SJohn Forte 	}
4111fcf3ce44SJohn Forte 
4112fcf3ce44SJohn Forte 	/*
4113fcf3ce44SJohn Forte 	 * allocate caller's buffer with the final size
4114fcf3ce44SJohn Forte 	 */
4115fcf3ce44SJohn Forte 	*sessionList = (stmfSessionList *)calloc(1, sizeof (stmfSessionList) +
4116fcf3ce44SJohn Forte 	    stmfIoctl.stmf_obuf_max_nentries * sizeof (stmfSession));
4117fcf3ce44SJohn Forte 	if (*sessionList == NULL) {
4118fcf3ce44SJohn Forte 		ret = STMF_ERROR_NOMEM;
4119fcf3ce44SJohn Forte 		free(sessionList);
4120fcf3ce44SJohn Forte 		goto done;
4121fcf3ce44SJohn Forte 	}
4122fcf3ce44SJohn Forte 
4123fcf3ce44SJohn Forte 	(*sessionList)->cnt = stmfIoctl.stmf_obuf_max_nentries;
4124fcf3ce44SJohn Forte 
4125fcf3ce44SJohn Forte 	/*
4126fcf3ce44SJohn Forte 	 * copy session info to caller's buffer
4127fcf3ce44SJohn Forte 	 */
4128fcf3ce44SJohn Forte 	for (i = 0; i < (*sessionList)->cnt; i++) {
4129fcf3ce44SJohn Forte 		(*sessionList)->session[i].initiator.identLength =
4130fcf3ce44SJohn Forte 		    fSessionList->initiator[IDENT_LENGTH_BYTE];
4131fcf3ce44SJohn Forte 		bcopy(&(fSessionList->initiator[IDENT_LENGTH_BYTE + 1]),
4132fcf3ce44SJohn Forte 		    (*sessionList)->session[i].initiator.ident,
4133fcf3ce44SJohn Forte 		    STMF_IDENT_LENGTH);
4134fcf3ce44SJohn Forte 		bcopy(&(fSessionList->alias),
4135fcf3ce44SJohn Forte 		    &((*sessionList)->session[i].alias),
4136fcf3ce44SJohn Forte 		    sizeof ((*sessionList)->session[i].alias));
4137fcf3ce44SJohn Forte 		bcopy(&(fSessionList++->creation_time),
4138fcf3ce44SJohn Forte 		    &((*sessionList)->session[i].creationTime),
4139fcf3ce44SJohn Forte 		    sizeof (time_t));
4140fcf3ce44SJohn Forte 	}
4141fcf3ce44SJohn Forte done:
4142fcf3ce44SJohn Forte 	(void) close(fd);
414356261083SCharles Ting 	free(fSessionListP);
4144fcf3ce44SJohn Forte 	return (ret);
4145fcf3ce44SJohn Forte }
4146fcf3ce44SJohn Forte 
4147fcf3ce44SJohn Forte /*
4148fcf3ce44SJohn Forte  * stmfGetTargetGroupList
4149fcf3ce44SJohn Forte  *
4150fcf3ce44SJohn Forte  * Purpose: Retrieves the list of target groups
4151fcf3ce44SJohn Forte  *
4152fcf3ce44SJohn Forte  * targetGroupList - pointer to a pointer to an stmfGroupList structure. On
4153fcf3ce44SJohn Forte  *		     success, it contains the list of target groups.
4154fcf3ce44SJohn Forte  */
4155fcf3ce44SJohn Forte int
4156fcf3ce44SJohn Forte stmfGetTargetGroupList(stmfGroupList **targetGroupList)
4157fcf3ce44SJohn Forte {
4158fcf3ce44SJohn Forte 	int ret;
4159fcf3ce44SJohn Forte 
4160fcf3ce44SJohn Forte 	if (targetGroupList == NULL) {
4161fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4162fcf3ce44SJohn Forte 	}
4163fcf3ce44SJohn Forte 
41648fe96085Stim szeto 	ret = groupListIoctl(targetGroupList, TARGET_GROUP);
4165fcf3ce44SJohn Forte 	return (ret);
4166fcf3ce44SJohn Forte }
4167fcf3ce44SJohn Forte 
4168fcf3ce44SJohn Forte /*
4169fcf3ce44SJohn Forte  * stmfGetTargetGroupMembers
4170fcf3ce44SJohn Forte  *
4171fcf3ce44SJohn Forte  * Purpose: Retrieves the group members for a target group
4172fcf3ce44SJohn Forte  *
4173fcf3ce44SJohn Forte  * groupName - name of target group for which to retrieve members.
4174fcf3ce44SJohn Forte  * groupProp - pointer to pointer to stmfGroupProperties structure
4175fcf3ce44SJohn Forte  *             on success, this contains the list of group members.
4176fcf3ce44SJohn Forte  */
4177fcf3ce44SJohn Forte int
4178fcf3ce44SJohn Forte stmfGetTargetGroupMembers(stmfGroupName *groupName,
4179fcf3ce44SJohn Forte     stmfGroupProperties **groupProp)
4180fcf3ce44SJohn Forte {
4181fcf3ce44SJohn Forte 	int ret;
4182fcf3ce44SJohn Forte 
4183fcf3ce44SJohn Forte 	if (groupName == NULL || groupProp == NULL) {
4184fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4185fcf3ce44SJohn Forte 	}
4186fcf3ce44SJohn Forte 
41878fe96085Stim szeto 	ret = groupMemberListIoctl(groupName, groupProp, TARGET_GROUP);
4188fcf3ce44SJohn Forte 
4189fcf3ce44SJohn Forte 	return (ret);
4190fcf3ce44SJohn Forte }
4191fcf3ce44SJohn Forte 
4192fcf3ce44SJohn Forte /*
4193fcf3ce44SJohn Forte  * stmfGetTargetList
4194fcf3ce44SJohn Forte  *
4195fcf3ce44SJohn Forte  * Purpose: Retrieves the list of target ports
4196fcf3ce44SJohn Forte  *
4197fcf3ce44SJohn Forte  * targetList - pointer to a pointer to an stmfDevidList structure.
4198fcf3ce44SJohn Forte  *		    On success, it contains the list of local ports (target).
4199fcf3ce44SJohn Forte  */
4200fcf3ce44SJohn Forte int
4201fcf3ce44SJohn Forte stmfGetTargetList(stmfDevidList **targetList)
4202fcf3ce44SJohn Forte {
4203fcf3ce44SJohn Forte 	int ret;
4204fcf3ce44SJohn Forte 	int fd;
4205fcf3ce44SJohn Forte 	int ioctlRet;
4206fcf3ce44SJohn Forte 	int i;
4207fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4208fcf3ce44SJohn Forte 	/* framework target port list */
42098fe96085Stim szeto 	slist_target_port_t *fTargetList, *fTargetListP = NULL;
4210fcf3ce44SJohn Forte 	uint32_t fTargetListSize;
4211fcf3ce44SJohn Forte 
4212fcf3ce44SJohn Forte 	if (targetList == NULL) {
4213fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4214fcf3ce44SJohn Forte 	}
4215fcf3ce44SJohn Forte 
4216fcf3ce44SJohn Forte 	/* call init */
4217fcf3ce44SJohn Forte 	ret = initializeConfig();
4218fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4219fcf3ce44SJohn Forte 		return (ret);
4220fcf3ce44SJohn Forte 	}
4221fcf3ce44SJohn Forte 
4222fcf3ce44SJohn Forte 	/*
4223fcf3ce44SJohn Forte 	 * Open control node for stmf
4224fcf3ce44SJohn Forte 	 */
4225fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
4226fcf3ce44SJohn Forte 		return (ret);
4227fcf3ce44SJohn Forte 
4228fcf3ce44SJohn Forte 	/*
4229fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
4230fcf3ce44SJohn Forte 	 */
42318fe96085Stim szeto 	fTargetListSize = ALLOC_TARGET_PORT * sizeof (slist_target_port_t);
42323e7352aeSJohn Forte 	fTargetListP = fTargetList =
42333e7352aeSJohn Forte 	    (slist_target_port_t *)calloc(1, fTargetListSize);
4234fcf3ce44SJohn Forte 	if (fTargetList == NULL) {
42358fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
4236fcf3ce44SJohn Forte 		goto done;
4237fcf3ce44SJohn Forte 	}
4238fcf3ce44SJohn Forte 
4239fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4240fcf3ce44SJohn Forte 	/*
42413e7352aeSJohn Forte 	 * Issue ioctl to retrieve target list
4242fcf3ce44SJohn Forte 	 */
4243fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4244fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fTargetListSize;
4245fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fTargetList;
4246fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_TARGET_PORT_LIST, &stmfIoctl);
4247fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4248fcf3ce44SJohn Forte 		switch (errno) {
4249fcf3ce44SJohn Forte 			case EBUSY:
4250fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4251fcf3ce44SJohn Forte 				break;
42528fe96085Stim szeto 			case EPERM:
4253fcf3ce44SJohn Forte 			case EACCES:
4254fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4255fcf3ce44SJohn Forte 				break;
4256fcf3ce44SJohn Forte 			default:
4257fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4258fcf3ce44SJohn Forte 				    "stmfGetTargetList:ioctl errno(%d)", errno);
4259fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4260fcf3ce44SJohn Forte 				break;
4261fcf3ce44SJohn Forte 		}
4262fcf3ce44SJohn Forte 		goto done;
4263fcf3ce44SJohn Forte 	}
4264fcf3ce44SJohn Forte 	/*
4265fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
4266fcf3ce44SJohn Forte 	 */
42678fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_TARGET_PORT) {
4268fcf3ce44SJohn Forte 		fTargetListSize = stmfIoctl.stmf_obuf_max_nentries *
426976602b8dSJohn Forte 		    sizeof (slist_target_port_t);
42703e7352aeSJohn Forte 		fTargetListP = fTargetList =
42713e7352aeSJohn Forte 		    realloc(fTargetList, fTargetListSize);
4272fcf3ce44SJohn Forte 		if (fTargetList == NULL) {
42738fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
42748fe96085Stim szeto 			goto done;
4275fcf3ce44SJohn Forte 		}
4276fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fTargetListSize;
4277fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fTargetList;
4278fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, STMF_IOCTL_TARGET_PORT_LIST,
4279fcf3ce44SJohn Forte 		    &stmfIoctl);
4280fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
4281fcf3ce44SJohn Forte 			switch (errno) {
4282fcf3ce44SJohn Forte 				case EBUSY:
4283fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
4284fcf3ce44SJohn Forte 					break;
42858fe96085Stim szeto 				case EPERM:
4286fcf3ce44SJohn Forte 				case EACCES:
4287fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
4288fcf3ce44SJohn Forte 					break;
4289fcf3ce44SJohn Forte 				default:
4290fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
4291fcf3ce44SJohn Forte 					    "stmfGetTargetList:ioctl errno(%d)",
4292fcf3ce44SJohn Forte 					    errno);
4293fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
4294fcf3ce44SJohn Forte 					break;
4295fcf3ce44SJohn Forte 			}
4296fcf3ce44SJohn Forte 			goto done;
4297fcf3ce44SJohn Forte 		}
4298fcf3ce44SJohn Forte 	}
4299fcf3ce44SJohn Forte 
4300fcf3ce44SJohn Forte 	*targetList = (stmfDevidList *)calloc(1,
4301fcf3ce44SJohn Forte 	    stmfIoctl.stmf_obuf_max_nentries * sizeof (stmfDevid) +
4302fcf3ce44SJohn Forte 	    sizeof (stmfDevidList));
43038fe96085Stim szeto 	if (*targetList == NULL) {
43048fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
43058fe96085Stim szeto 		goto done;
43068fe96085Stim szeto 	}
4307fcf3ce44SJohn Forte 
4308fcf3ce44SJohn Forte 	(*targetList)->cnt = stmfIoctl.stmf_obuf_max_nentries;
4309fcf3ce44SJohn Forte 	for (i = 0; i < stmfIoctl.stmf_obuf_max_nentries; i++, fTargetList++) {
4310fcf3ce44SJohn Forte 		(*targetList)->devid[i].identLength =
4311fcf3ce44SJohn Forte 		    fTargetList->target[IDENT_LENGTH_BYTE];
4312fcf3ce44SJohn Forte 		bcopy(&fTargetList->target[IDENT_LENGTH_BYTE + 1],
4313fcf3ce44SJohn Forte 		    &(*targetList)->devid[i].ident,
4314fcf3ce44SJohn Forte 		    fTargetList->target[IDENT_LENGTH_BYTE]);
4315fcf3ce44SJohn Forte 	}
4316fcf3ce44SJohn Forte 
4317fcf3ce44SJohn Forte done:
4318fcf3ce44SJohn Forte 	(void) close(fd);
43193e7352aeSJohn Forte 	free(fTargetListP);
4320fcf3ce44SJohn Forte 	return (ret);
4321fcf3ce44SJohn Forte }
4322fcf3ce44SJohn Forte 
4323fcf3ce44SJohn Forte /*
4324fcf3ce44SJohn Forte  * stmfGetTargetProperties
4325fcf3ce44SJohn Forte  *
4326fcf3ce44SJohn Forte  * Purpose:  Retrieves the properties for a logical unit
4327fcf3ce44SJohn Forte  *
4328fcf3ce44SJohn Forte  * devid - devid of the target for which to retrieve properties
4329fcf3ce44SJohn Forte  * targetProps - pointer to an stmfTargetProperties structure.
4330fcf3ce44SJohn Forte  *		On success, it contains the target properties for
4331fcf3ce44SJohn Forte  *		the specified devid.
4332fcf3ce44SJohn Forte  */
4333fcf3ce44SJohn Forte int
4334fcf3ce44SJohn Forte stmfGetTargetProperties(stmfDevid *devid, stmfTargetProperties *targetProps)
4335fcf3ce44SJohn Forte {
4336fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4337fcf3ce44SJohn Forte 	int fd;
4338fcf3ce44SJohn Forte 	int ioctlRet;
4339fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4340fcf3ce44SJohn Forte 	sioc_target_port_props_t targetProperties;
434145039663SJohn Forte 	scsi_devid_desc_t *scsiDevid;
4342fcf3ce44SJohn Forte 
4343fcf3ce44SJohn Forte 	if (devid == NULL || targetProps == NULL) {
4344fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4345fcf3ce44SJohn Forte 	}
4346fcf3ce44SJohn Forte 
4347fcf3ce44SJohn Forte 	/* call init */
4348fcf3ce44SJohn Forte 	ret = initializeConfig();
4349fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4350fcf3ce44SJohn Forte 		return (ret);
4351fcf3ce44SJohn Forte 	}
4352fcf3ce44SJohn Forte 
4353fcf3ce44SJohn Forte 	/*
4354fcf3ce44SJohn Forte 	 * Open control node for stmf
4355fcf3ce44SJohn Forte 	 */
4356fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
4357fcf3ce44SJohn Forte 		return (ret);
4358fcf3ce44SJohn Forte 
4359fcf3ce44SJohn Forte 	targetProperties.tgt_id[IDENT_LENGTH_BYTE] = devid->identLength;
4360fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetProperties.tgt_id[IDENT_LENGTH_BYTE + 1],
4361fcf3ce44SJohn Forte 	    devid->identLength);
4362fcf3ce44SJohn Forte 
4363fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4364fcf3ce44SJohn Forte 	/*
4365fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
4366fcf3ce44SJohn Forte 	 */
4367fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4368fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (targetProperties.tgt_id);
4369fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&targetProperties.tgt_id;
4370fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&targetProperties;
4371fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (targetProperties);
4372fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_GET_TARGET_PORT_PROPERTIES,
4373fcf3ce44SJohn Forte 	    &stmfIoctl);
4374fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4375fcf3ce44SJohn Forte 		switch (errno) {
4376fcf3ce44SJohn Forte 			case EBUSY:
4377fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4378fcf3ce44SJohn Forte 				break;
43798fe96085Stim szeto 			case EPERM:
4380fcf3ce44SJohn Forte 			case EACCES:
4381fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4382fcf3ce44SJohn Forte 				break;
4383fcf3ce44SJohn Forte 			case ENOENT:
4384fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
4385fcf3ce44SJohn Forte 				break;
4386fcf3ce44SJohn Forte 			default:
4387fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4388fcf3ce44SJohn Forte 				    "stmfGetTargetProperties:ioctl errno(%d)",
4389fcf3ce44SJohn Forte 				    errno);
4390fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4391fcf3ce44SJohn Forte 				break;
4392fcf3ce44SJohn Forte 		}
4393fcf3ce44SJohn Forte 		goto done;
4394fcf3ce44SJohn Forte 	}
4395fcf3ce44SJohn Forte 
4396fcf3ce44SJohn Forte 	bcopy(targetProperties.tgt_provider_name, targetProps->providerName,
4397fcf3ce44SJohn Forte 	    sizeof (targetProperties.tgt_provider_name));
4398fcf3ce44SJohn Forte 	if (targetProperties.tgt_state == STMF_STATE_ONLINE) {
4399fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_ONLINE;
4400fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_OFFLINE) {
4401fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_OFFLINE;
4402fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_ONLINING) {
4403fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_ONLINING;
4404fcf3ce44SJohn Forte 	} else if (targetProperties.tgt_state == STMF_STATE_OFFLINING) {
4405fcf3ce44SJohn Forte 		targetProps->status = STMF_TARGET_PORT_OFFLINING;
4406fcf3ce44SJohn Forte 	}
4407fcf3ce44SJohn Forte 	bcopy(targetProperties.tgt_alias, targetProps->alias,
4408fcf3ce44SJohn Forte 	    sizeof (targetProps->alias));
440945039663SJohn Forte 
441045039663SJohn Forte 	scsiDevid = (scsi_devid_desc_t *)&targetProperties.tgt_id;
441145039663SJohn Forte 	targetProps->protocol = scsiDevid->protocol_id;
441245039663SJohn Forte 
4413fcf3ce44SJohn Forte done:
4414fcf3ce44SJohn Forte 	(void) close(fd);
4415fcf3ce44SJohn Forte 	return (ret);
4416fcf3ce44SJohn Forte }
4417fcf3ce44SJohn Forte 
4418fcf3ce44SJohn Forte /*
4419fcf3ce44SJohn Forte  * stmfGetLogicalUnitList
4420fcf3ce44SJohn Forte  *
4421fcf3ce44SJohn Forte  * Purpose: Retrieves list of logical unit Object IDs
4422fcf3ce44SJohn Forte  *
4423fcf3ce44SJohn Forte  * luList - pointer to a pointer to a stmfGuidList structure. On success,
4424fcf3ce44SJohn Forte  *          it contains the list of logical unit guids.
4425fcf3ce44SJohn Forte  *
4426fcf3ce44SJohn Forte  */
4427fcf3ce44SJohn Forte int
4428fcf3ce44SJohn Forte stmfGetLogicalUnitList(stmfGuidList **luList)
4429fcf3ce44SJohn Forte {
4430fcf3ce44SJohn Forte 	int ret;
4431fcf3ce44SJohn Forte 	int fd;
4432fcf3ce44SJohn Forte 	int ioctlRet;
4433fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_LU_LIST;
44348fe96085Stim szeto 	int i;
4435fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4436fcf3ce44SJohn Forte 	slist_lu_t *fLuList;
4437fcf3ce44SJohn Forte 	uint32_t fLuListSize;
44388fe96085Stim szeto 	uint32_t listCnt;
4439fcf3ce44SJohn Forte 
4440fcf3ce44SJohn Forte 	if (luList == NULL) {
4441fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4442fcf3ce44SJohn Forte 	}
4443fcf3ce44SJohn Forte 
4444fcf3ce44SJohn Forte 	/* call init */
4445fcf3ce44SJohn Forte 	ret = initializeConfig();
4446fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4447fcf3ce44SJohn Forte 		return (ret);
4448fcf3ce44SJohn Forte 	}
4449fcf3ce44SJohn Forte 
4450fcf3ce44SJohn Forte 	/*
4451fcf3ce44SJohn Forte 	 * Open control node for stmf
4452fcf3ce44SJohn Forte 	 */
4453fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
4454fcf3ce44SJohn Forte 		return (ret);
4455fcf3ce44SJohn Forte 
4456fcf3ce44SJohn Forte 	/*
4457fcf3ce44SJohn Forte 	 * Allocate ioctl input buffer
4458fcf3ce44SJohn Forte 	 */
44598fe96085Stim szeto 	fLuListSize = ALLOC_LU;
4460fcf3ce44SJohn Forte 	fLuListSize = fLuListSize * (sizeof (slist_lu_t));
4461fcf3ce44SJohn Forte 	fLuList = (slist_lu_t *)calloc(1, fLuListSize);
4462fcf3ce44SJohn Forte 	if (fLuList == NULL) {
44638fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
44648fe96085Stim szeto 		goto done;
4465fcf3ce44SJohn Forte 	}
4466fcf3ce44SJohn Forte 
4467fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4468fcf3ce44SJohn Forte 	/*
4469fcf3ce44SJohn Forte 	 * Issue ioctl to get the LU list
4470fcf3ce44SJohn Forte 	 */
4471fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4472fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = fLuListSize;
4473fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fLuList;
4474fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4475fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4476fcf3ce44SJohn Forte 		switch (errno) {
4477fcf3ce44SJohn Forte 			case EBUSY:
4478fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4479fcf3ce44SJohn Forte 				break;
44808fe96085Stim szeto 			case EPERM:
4481fcf3ce44SJohn Forte 			case EACCES:
4482fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4483fcf3ce44SJohn Forte 				break;
4484fcf3ce44SJohn Forte 			default:
4485fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4486fcf3ce44SJohn Forte 				    "stmfGetLogicalUnitList:ioctl errno(%d)",
4487fcf3ce44SJohn Forte 				    errno);
4488fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4489fcf3ce44SJohn Forte 				break;
4490fcf3ce44SJohn Forte 		}
4491fcf3ce44SJohn Forte 		goto done;
4492fcf3ce44SJohn Forte 	}
4493fcf3ce44SJohn Forte 	/*
4494fcf3ce44SJohn Forte 	 * Check whether input buffer was large enough
4495fcf3ce44SJohn Forte 	 */
44968fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_LU) {
4497fcf3ce44SJohn Forte 		fLuListSize = stmfIoctl.stmf_obuf_max_nentries *
4498fcf3ce44SJohn Forte 		    sizeof (slist_lu_t);
44998fe96085Stim szeto 		free(fLuList);
45008fe96085Stim szeto 		fLuList = (slist_lu_t *)calloc(1, fLuListSize);
4501fcf3ce44SJohn Forte 		if (fLuList == NULL) {
45028fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
45038fe96085Stim szeto 			goto done;
4504fcf3ce44SJohn Forte 		}
4505fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf_size = fLuListSize;
4506fcf3ce44SJohn Forte 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fLuList;
4507fcf3ce44SJohn Forte 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4508fcf3ce44SJohn Forte 		if (ioctlRet != 0) {
4509fcf3ce44SJohn Forte 			switch (errno) {
4510fcf3ce44SJohn Forte 				case EBUSY:
4511fcf3ce44SJohn Forte 					ret = STMF_ERROR_BUSY;
4512fcf3ce44SJohn Forte 					break;
45138fe96085Stim szeto 				case EPERM:
4514fcf3ce44SJohn Forte 				case EACCES:
4515fcf3ce44SJohn Forte 					ret = STMF_ERROR_PERM;
4516fcf3ce44SJohn Forte 					break;
4517fcf3ce44SJohn Forte 				default:
4518fcf3ce44SJohn Forte 					syslog(LOG_DEBUG,
4519fcf3ce44SJohn Forte 					    "stmfGetLogicalUnitList:"
4520fcf3ce44SJohn Forte 					    "ioctl errno(%d)", errno);
4521fcf3ce44SJohn Forte 					ret = STMF_STATUS_ERROR;
4522fcf3ce44SJohn Forte 					break;
4523fcf3ce44SJohn Forte 			}
4524fcf3ce44SJohn Forte 			goto done;
4525fcf3ce44SJohn Forte 		}
4526fcf3ce44SJohn Forte 	}
4527fcf3ce44SJohn Forte 
4528fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4529fcf3ce44SJohn Forte 		goto done;
4530fcf3ce44SJohn Forte 	}
4531fcf3ce44SJohn Forte 
45328fe96085Stim szeto 	listCnt = stmfIoctl.stmf_obuf_nentries;
4533fcf3ce44SJohn Forte 
4534fcf3ce44SJohn Forte 	/*
4535fcf3ce44SJohn Forte 	 * allocate caller's buffer with the final size
4536fcf3ce44SJohn Forte 	 */
4537fcf3ce44SJohn Forte 	*luList = (stmfGuidList *)calloc(1, sizeof (stmfGuidList) +
45388fe96085Stim szeto 	    listCnt * sizeof (stmfGuid));
4539fcf3ce44SJohn Forte 	if (*luList == NULL) {
4540fcf3ce44SJohn Forte 		ret = STMF_ERROR_NOMEM;
4541fcf3ce44SJohn Forte 		goto done;
4542fcf3ce44SJohn Forte 	}
4543fcf3ce44SJohn Forte 
45448fe96085Stim szeto 	(*luList)->cnt = listCnt;
45458fe96085Stim szeto 
45468fe96085Stim szeto 	/* copy to caller's buffer */
45478fe96085Stim szeto 	for (i = 0; i < listCnt; i++) {
45488fe96085Stim szeto 		bcopy(&fLuList[i].lu_guid, (*luList)->guid[i].guid,
4549fcf3ce44SJohn Forte 		    sizeof (stmfGuid));
4550fcf3ce44SJohn Forte 	}
4551fcf3ce44SJohn Forte 
45528fe96085Stim szeto 	/*
45538fe96085Stim szeto 	 * sort the list. This gives a consistent view across gets
45548fe96085Stim szeto 	 */
45558fe96085Stim szeto 	qsort((void *)&((*luList)->guid[0]), (*luList)->cnt,
45568fe96085Stim szeto 	    sizeof (stmfGuid), guidCompare);
4557fcf3ce44SJohn Forte 
4558fcf3ce44SJohn Forte done:
4559fcf3ce44SJohn Forte 	(void) close(fd);
4560fcf3ce44SJohn Forte 	/*
4561fcf3ce44SJohn Forte 	 * free internal buffers
4562fcf3ce44SJohn Forte 	 */
4563fcf3ce44SJohn Forte 	free(fLuList);
4564fcf3ce44SJohn Forte 	return (ret);
4565fcf3ce44SJohn Forte }
4566fcf3ce44SJohn Forte 
4567fcf3ce44SJohn Forte /*
4568fcf3ce44SJohn Forte  * stmfGetLogicalUnitProperties
4569fcf3ce44SJohn Forte  *
4570fcf3ce44SJohn Forte  * Purpose:  Retrieves the properties for a logical unit
4571fcf3ce44SJohn Forte  *
4572fcf3ce44SJohn Forte  * lu - guid of the logical unit for which to retrieve properties
4573fcf3ce44SJohn Forte  * stmfLuProps - pointer to an stmfLogicalUnitProperties structure. On success,
4574fcf3ce44SJohn Forte  *               it contains the logical unit properties for the specified guid.
4575fcf3ce44SJohn Forte  */
4576fcf3ce44SJohn Forte int
4577fcf3ce44SJohn Forte stmfGetLogicalUnitProperties(stmfGuid *lu, stmfLogicalUnitProperties *luProps)
4578fcf3ce44SJohn Forte {
4579fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4580fcf3ce44SJohn Forte 	int stmfRet;
4581fcf3ce44SJohn Forte 	int fd;
4582fcf3ce44SJohn Forte 	int ioctlRet;
4583fcf3ce44SJohn Forte 	int cmd = STMF_IOCTL_GET_LU_PROPERTIES;
4584fcf3ce44SJohn Forte 	stmfViewEntryList *viewEntryList = NULL;
4585fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
4586fcf3ce44SJohn Forte 	sioc_lu_props_t fLuProps;
4587fcf3ce44SJohn Forte 
45888fe96085Stim szeto 	if (lu == NULL || luProps == NULL) {
45898fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
4590fcf3ce44SJohn Forte 	}
4591fcf3ce44SJohn Forte 
4592fcf3ce44SJohn Forte 	bzero(luProps, sizeof (stmfLogicalUnitProperties));
4593fcf3ce44SJohn Forte 
4594fcf3ce44SJohn Forte 	/* call init */
4595fcf3ce44SJohn Forte 	ret = initializeConfig();
4596fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4597fcf3ce44SJohn Forte 		return (ret);
4598fcf3ce44SJohn Forte 	}
4599fcf3ce44SJohn Forte 
4600fcf3ce44SJohn Forte 	/*
4601fcf3ce44SJohn Forte 	 * Open control node for stmf
4602fcf3ce44SJohn Forte 	 */
4603fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
4604fcf3ce44SJohn Forte 		return (ret);
4605fcf3ce44SJohn Forte 
4606fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
4607fcf3ce44SJohn Forte 	/*
4608fcf3ce44SJohn Forte 	 * Issue ioctl to add to the host group
4609fcf3ce44SJohn Forte 	 */
4610fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
4611fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGuid);
4612fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)lu;
4613fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&fLuProps;
4614fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (fLuProps);
4615fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
4616fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
4617fcf3ce44SJohn Forte 		switch (errno) {
4618fcf3ce44SJohn Forte 			case EBUSY:
4619fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4620fcf3ce44SJohn Forte 				break;
46218fe96085Stim szeto 			case EPERM:
4622fcf3ce44SJohn Forte 			case EACCES:
4623fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
4624fcf3ce44SJohn Forte 				break;
4625fcf3ce44SJohn Forte 			case ENOENT:
4626fcf3ce44SJohn Forte 				stmfRet = stmfGetViewEntryList(lu,
4627fcf3ce44SJohn Forte 				    &viewEntryList);
4628fcf3ce44SJohn Forte 				if (stmfRet == STMF_STATUS_SUCCESS) {
4629fcf3ce44SJohn Forte 					luProps->status =
4630fcf3ce44SJohn Forte 					    STMF_LOGICAL_UNIT_UNREGISTERED;
4631fcf3ce44SJohn Forte 					if (viewEntryList->cnt > 0) {
4632fcf3ce44SJohn Forte 						ret = STMF_STATUS_SUCCESS;
4633fcf3ce44SJohn Forte 					} else {
4634fcf3ce44SJohn Forte 						ret = STMF_ERROR_NOT_FOUND;
4635fcf3ce44SJohn Forte 					}
4636fcf3ce44SJohn Forte 				} else {
4637fcf3ce44SJohn Forte 					ret = STMF_ERROR_NOT_FOUND;
4638fcf3ce44SJohn Forte 				}
4639fcf3ce44SJohn Forte 				stmfFreeMemory(viewEntryList);
4640fcf3ce44SJohn Forte 				break;
4641fcf3ce44SJohn Forte 			default:
4642fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
4643fcf3ce44SJohn Forte 				    "stmfGetLogicalUnit:ioctl errno(%d)",
4644fcf3ce44SJohn Forte 				    errno);
4645fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4646fcf3ce44SJohn Forte 				break;
4647fcf3ce44SJohn Forte 		}
4648fcf3ce44SJohn Forte 		goto done;
4649fcf3ce44SJohn Forte 	}
4650fcf3ce44SJohn Forte 
4651fcf3ce44SJohn Forte 	bcopy(fLuProps.lu_provider_name, luProps->providerName,
4652fcf3ce44SJohn Forte 	    sizeof (fLuProps.lu_provider_name));
4653fcf3ce44SJohn Forte 	if (fLuProps.lu_state == STMF_STATE_ONLINE) {
4654fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_ONLINE;
4655fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_OFFLINE) {
4656fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_OFFLINE;
4657fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_ONLINING) {
4658fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_ONLINING;
4659fcf3ce44SJohn Forte 	} else if (fLuProps.lu_state == STMF_STATE_OFFLINING) {
4660fcf3ce44SJohn Forte 		luProps->status = STMF_LOGICAL_UNIT_OFFLINING;
4661fcf3ce44SJohn Forte 	}
4662fcf3ce44SJohn Forte 	bcopy(fLuProps.lu_alias, luProps->alias, sizeof (luProps->alias));
4663fcf3ce44SJohn Forte done:
4664fcf3ce44SJohn Forte 	(void) close(fd);
4665fcf3ce44SJohn Forte 	return (ret);
4666fcf3ce44SJohn Forte }
4667fcf3ce44SJohn Forte 
4668fcf3ce44SJohn Forte /*
4669fcf3ce44SJohn Forte  * stmfGetState
4670fcf3ce44SJohn Forte  *
4671fcf3ce44SJohn Forte  * Purpose: retrieve the current state of the stmf module
4672fcf3ce44SJohn Forte  *
4673fcf3ce44SJohn Forte  * state - pointer to stmfState structure allocated by the caller
4674fcf3ce44SJohn Forte  *         On success, contains the state of stmf
4675fcf3ce44SJohn Forte  */
4676fcf3ce44SJohn Forte int
4677fcf3ce44SJohn Forte stmfGetState(stmfState *state)
4678fcf3ce44SJohn Forte {
4679fcf3ce44SJohn Forte 	int ret;
4680fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
4681fcf3ce44SJohn Forte 
4682fcf3ce44SJohn Forte 	if (state == NULL) {
4683fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4684fcf3ce44SJohn Forte 	}
4685fcf3ce44SJohn Forte 
4686fcf3ce44SJohn Forte 	ret = getStmfState(&iState);
4687fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
4688fcf3ce44SJohn Forte 		return (ret);
4689fcf3ce44SJohn Forte 	}
4690fcf3ce44SJohn Forte 	switch (iState.state) {
4691fcf3ce44SJohn Forte 		case STMF_STATE_ONLINE:
4692fcf3ce44SJohn Forte 			state->operationalState =
4693fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_ONLINE;
4694fcf3ce44SJohn Forte 			break;
4695fcf3ce44SJohn Forte 		case STMF_STATE_OFFLINE:
4696fcf3ce44SJohn Forte 			state->operationalState =
4697fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_OFFLINE;
4698fcf3ce44SJohn Forte 			break;
4699fcf3ce44SJohn Forte 		case STMF_STATE_ONLINING:
4700fcf3ce44SJohn Forte 			state->operationalState =
4701fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_ONLINING;
4702fcf3ce44SJohn Forte 			break;
4703fcf3ce44SJohn Forte 		case STMF_STATE_OFFLINING:
4704fcf3ce44SJohn Forte 			state->operationalState =
4705fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_OFFLINING;
4706fcf3ce44SJohn Forte 			break;
4707fcf3ce44SJohn Forte 		default:
4708fcf3ce44SJohn Forte 			state->operationalState =
4709fcf3ce44SJohn Forte 			    STMF_SERVICE_STATE_UNKNOWN;
4710fcf3ce44SJohn Forte 			break;
4711fcf3ce44SJohn Forte 	}
4712fcf3ce44SJohn Forte 	switch (iState.config_state) {
4713fcf3ce44SJohn Forte 		case STMF_CONFIG_NONE:
4714fcf3ce44SJohn Forte 			state->configState = STMF_CONFIG_STATE_NONE;
4715fcf3ce44SJohn Forte 			break;
4716fcf3ce44SJohn Forte 		case STMF_CONFIG_INIT:
4717fcf3ce44SJohn Forte 			state->configState = STMF_CONFIG_STATE_INIT;
4718fcf3ce44SJohn Forte 			break;
4719fcf3ce44SJohn Forte 		case STMF_CONFIG_INIT_DONE:
4720fcf3ce44SJohn Forte 			state->configState =
4721fcf3ce44SJohn Forte 			    STMF_CONFIG_STATE_INIT_DONE;
4722fcf3ce44SJohn Forte 			break;
4723fcf3ce44SJohn Forte 		default:
4724fcf3ce44SJohn Forte 			state->configState =
4725fcf3ce44SJohn Forte 			    STMF_CONFIG_STATE_UNKNOWN;
4726fcf3ce44SJohn Forte 			break;
4727fcf3ce44SJohn Forte 	}
4728fcf3ce44SJohn Forte 	return (STMF_STATUS_SUCCESS);
4729fcf3ce44SJohn Forte }
4730fcf3ce44SJohn Forte 
4731fcf3ce44SJohn Forte /*
4732fcf3ce44SJohn Forte  * stmfGetViewEntryList
4733fcf3ce44SJohn Forte  *
4734fcf3ce44SJohn Forte  * Purpose: Retrieves the list of view entries for the specified
4735fcf3ce44SJohn Forte  *          logical unit.
4736fcf3ce44SJohn Forte  *
4737fcf3ce44SJohn Forte  * lu - the guid of the logical unit for which to retrieve the view entry list
4738fcf3ce44SJohn Forte  * viewEntryList - a pointer to a pointer to a stmfViewEntryList structure. On
4739fcf3ce44SJohn Forte  *                 success, contains the list of view entries.
4740fcf3ce44SJohn Forte  */
4741fcf3ce44SJohn Forte int
4742fcf3ce44SJohn Forte stmfGetViewEntryList(stmfGuid *lu, stmfViewEntryList **viewEntryList)
4743fcf3ce44SJohn Forte {
4744fcf3ce44SJohn Forte 	int ret;
47458fe96085Stim szeto 	int fd;
47468fe96085Stim szeto 	int ioctlRet;
47478fe96085Stim szeto 	int cmd = STMF_IOCTL_LU_VE_LIST;
47488fe96085Stim szeto 	int i;
47498fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
47508fe96085Stim szeto 	stmf_view_op_entry_t *fVeList;
47518fe96085Stim szeto 	uint32_t fVeListSize;
47528fe96085Stim szeto 	uint32_t listCnt;
4753fcf3ce44SJohn Forte 
4754fcf3ce44SJohn Forte 	if (lu == NULL || viewEntryList == NULL) {
4755fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
4756fcf3ce44SJohn Forte 	}
4757fcf3ce44SJohn Forte 
47588fe96085Stim szeto 	/* call init */
47598fe96085Stim szeto 	ret = initializeConfig();
47608fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
47618fe96085Stim szeto 		return (ret);
47628fe96085Stim szeto 	}
47638fe96085Stim szeto 
47648fe96085Stim szeto 	/*
47658fe96085Stim szeto 	 * Open control node for stmf
47668fe96085Stim szeto 	 */
47678fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
47688fe96085Stim szeto 		return (ret);
47698fe96085Stim szeto 
47708fe96085Stim szeto 	/*
47718fe96085Stim szeto 	 * Allocate ioctl input buffer
47728fe96085Stim szeto 	 */
47738fe96085Stim szeto 	fVeListSize = ALLOC_VE;
47748fe96085Stim szeto 	fVeListSize = fVeListSize * (sizeof (stmf_view_op_entry_t));
47758fe96085Stim szeto 	fVeList = (stmf_view_op_entry_t *)calloc(1, fVeListSize);
47768fe96085Stim szeto 	if (fVeList == NULL) {
47778fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
47788fe96085Stim szeto 		goto done;
47798fe96085Stim szeto 	}
47808fe96085Stim szeto 
47818fe96085Stim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
47828fe96085Stim szeto 	/*
47838fe96085Stim szeto 	 * Issue ioctl to get the LU list
47848fe96085Stim szeto 	 */
47858fe96085Stim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
47868fe96085Stim szeto 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)lu;
47878fe96085Stim szeto 	stmfIoctl.stmf_ibuf_size = sizeof (stmfGuid);
47888fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = fVeListSize;
47898fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fVeList;
47908fe96085Stim szeto 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
47918fe96085Stim szeto 	if (ioctlRet != 0) {
47928fe96085Stim szeto 		switch (errno) {
47938fe96085Stim szeto 			case EBUSY:
4794fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
4795fcf3ce44SJohn Forte 				break;
47968fe96085Stim szeto 			case EPERM:
47978fe96085Stim szeto 			case EACCES:
47988fe96085Stim szeto 				ret = STMF_ERROR_PERM;
4799fcf3ce44SJohn Forte 				break;
4800fcf3ce44SJohn Forte 			default:
4801fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
48028fe96085Stim szeto 				    "stmfGetViewEntryList:ioctl errno(%d)",
48038fe96085Stim szeto 				    errno);
4804fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
4805fcf3ce44SJohn Forte 				break;
4806fcf3ce44SJohn Forte 		}
48078fe96085Stim szeto 		goto done;
48088fe96085Stim szeto 	}
48098fe96085Stim szeto 	/*
48108fe96085Stim szeto 	 * Check whether input buffer was large enough
48118fe96085Stim szeto 	 */
48128fe96085Stim szeto 	if (stmfIoctl.stmf_obuf_max_nentries > ALLOC_VE) {
48138fe96085Stim szeto 		bzero(&stmfIoctl, sizeof (stmfIoctl));
48148fe96085Stim szeto 		fVeListSize = stmfIoctl.stmf_obuf_max_nentries *
48158fe96085Stim szeto 		    sizeof (stmf_view_op_entry_t);
48168fe96085Stim szeto 		free(fVeList);
48178fe96085Stim szeto 		fVeList = (stmf_view_op_entry_t *)calloc(1, fVeListSize);
48188fe96085Stim szeto 		if (fVeList == NULL) {
48198fe96085Stim szeto 			return (STMF_ERROR_NOMEM);
48208fe96085Stim szeto 		}
48218fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = fVeListSize;
48228fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)fVeList;
48238fe96085Stim szeto 		ioctlRet = ioctl(fd, cmd, &stmfIoctl);
48248fe96085Stim szeto 		if (ioctlRet != 0) {
48258fe96085Stim szeto 			switch (errno) {
48268fe96085Stim szeto 				case EBUSY:
48278fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
48288fe96085Stim szeto 					break;
48298fe96085Stim szeto 				case EPERM:
48308fe96085Stim szeto 				case EACCES:
48318fe96085Stim szeto 					ret = STMF_ERROR_PERM;
48328fe96085Stim szeto 					break;
48338fe96085Stim szeto 				default:
48348fe96085Stim szeto 					syslog(LOG_DEBUG,
48358fe96085Stim szeto 					    "stmfGetLogicalUnitList:"
48368fe96085Stim szeto 					    "ioctl errno(%d)", errno);
48378fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
48388fe96085Stim szeto 					break;
48398fe96085Stim szeto 			}
48408fe96085Stim szeto 			goto done;
48418fe96085Stim szeto 		}
48428fe96085Stim szeto 	}
4843fcf3ce44SJohn Forte 
48448fe96085Stim szeto 	if (ret != STMF_STATUS_SUCCESS) {
48458fe96085Stim szeto 		goto done;
48468fe96085Stim szeto 	}
48478fe96085Stim szeto 
48488fe96085Stim szeto 	listCnt = stmfIoctl.stmf_obuf_nentries;
48498fe96085Stim szeto 
48508fe96085Stim szeto 	/*
48518fe96085Stim szeto 	 * allocate caller's buffer with the final size
48528fe96085Stim szeto 	 */
48538fe96085Stim szeto 	*viewEntryList = (stmfViewEntryList *)calloc(1,
48548fe96085Stim szeto 	    sizeof (stmfViewEntryList) + listCnt * sizeof (stmfViewEntry));
48558fe96085Stim szeto 	if (*viewEntryList == NULL) {
48568fe96085Stim szeto 		ret = STMF_ERROR_NOMEM;
48578fe96085Stim szeto 		goto done;
48588fe96085Stim szeto 	}
48598fe96085Stim szeto 
48608fe96085Stim szeto 	(*viewEntryList)->cnt = listCnt;
48618fe96085Stim szeto 
48628fe96085Stim szeto 	/* copy to caller's buffer */
48638fe96085Stim szeto 	for (i = 0; i < listCnt; i++) {
48648fe96085Stim szeto 		(*viewEntryList)->ve[i].veIndexValid = B_TRUE;
48658fe96085Stim szeto 		(*viewEntryList)->ve[i].veIndex = fVeList[i].ve_ndx;
48668fe96085Stim szeto 		if (fVeList[i].ve_all_hosts == 1) {
48678fe96085Stim szeto 			(*viewEntryList)->ve[i].allHosts = B_TRUE;
48688fe96085Stim szeto 		} else {
48698fe96085Stim szeto 			bcopy(fVeList[i].ve_host_group.name,
48708fe96085Stim szeto 			    (*viewEntryList)->ve[i].hostGroup,
48718fe96085Stim szeto 			    fVeList[i].ve_host_group.name_size);
48728fe96085Stim szeto 		}
48738fe96085Stim szeto 		if (fVeList[i].ve_all_targets == 1) {
48748fe96085Stim szeto 			(*viewEntryList)->ve[i].allTargets = B_TRUE;
48758fe96085Stim szeto 		} else {
48768fe96085Stim szeto 			bcopy(fVeList[i].ve_target_group.name,
48778fe96085Stim szeto 			    (*viewEntryList)->ve[i].targetGroup,
48788fe96085Stim szeto 			    fVeList[i].ve_target_group.name_size);
48798fe96085Stim szeto 		}
48808fe96085Stim szeto 		bcopy(fVeList[i].ve_lu_nbr, (*viewEntryList)->ve[i].luNbr,
48818fe96085Stim szeto 		    sizeof ((*viewEntryList)->ve[i].luNbr));
48828fe96085Stim szeto 		(*viewEntryList)->ve[i].luNbrValid = B_TRUE;
48838fe96085Stim szeto 	}
48848fe96085Stim szeto 
48858fe96085Stim szeto 	/*
48868fe96085Stim szeto 	 * sort the list. This gives a consistent view across gets
48878fe96085Stim szeto 	 */
48888fe96085Stim szeto 	qsort((void *)&((*viewEntryList)->ve[0]), (*viewEntryList)->cnt,
48898fe96085Stim szeto 	    sizeof (stmfViewEntry), viewEntryCompare);
48908fe96085Stim szeto 
48918fe96085Stim szeto done:
48928fe96085Stim szeto 	(void) close(fd);
48938fe96085Stim szeto 	/*
48948fe96085Stim szeto 	 * free internal buffers
48958fe96085Stim szeto 	 */
48968fe96085Stim szeto 	free(fVeList);
4897fcf3ce44SJohn Forte 	return (ret);
4898fcf3ce44SJohn Forte }
4899fcf3ce44SJohn Forte 
49008fe96085Stim szeto 
4901fcf3ce44SJohn Forte /*
4902fcf3ce44SJohn Forte  * loadHostGroups
4903fcf3ce44SJohn Forte  *
4904fcf3ce44SJohn Forte  * Purpose - issues the ioctl to load the host groups into stmf
4905fcf3ce44SJohn Forte  *
4906fcf3ce44SJohn Forte  * fd - file descriptor for the control node of stmf.
4907fcf3ce44SJohn Forte  * groupList - populated host group list
4908fcf3ce44SJohn Forte  */
4909fcf3ce44SJohn Forte static int
4910fcf3ce44SJohn Forte loadHostGroups(int fd, stmfGroupList *groupList)
4911fcf3ce44SJohn Forte {
4912fcf3ce44SJohn Forte 	int i, j;
4913fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4914fcf3ce44SJohn Forte 	stmfGroupProperties *groupProps = NULL;
4915fcf3ce44SJohn Forte 
4916fcf3ce44SJohn Forte 	for (i = 0; i < groupList->cnt; i++) {
4917fcf3ce44SJohn Forte 		if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_HOST_GROUP,
4918fcf3ce44SJohn Forte 		    &(groupList->name[i]))) != STMF_STATUS_SUCCESS) {
4919fcf3ce44SJohn Forte 			goto out;
4920fcf3ce44SJohn Forte 		}
49218fe96085Stim szeto 		ret = iLoadGroupMembersFromPs(&(groupList->name[i]),
49228fe96085Stim szeto 		    &groupProps, HOST_GROUP);
4923fcf3ce44SJohn Forte 		for (j = 0; j < groupProps->cnt; j++) {
4924fcf3ce44SJohn Forte 			if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_HG_ENTRY,
4925fcf3ce44SJohn Forte 			    &(groupList->name[i]), &(groupProps->name[j])))
4926fcf3ce44SJohn Forte 			    != STMF_STATUS_SUCCESS) {
4927fcf3ce44SJohn Forte 				goto out;
4928fcf3ce44SJohn Forte 			}
4929fcf3ce44SJohn Forte 		}
4930fcf3ce44SJohn Forte 	}
4931fcf3ce44SJohn Forte 
4932fcf3ce44SJohn Forte 
4933fcf3ce44SJohn Forte out:
4934fcf3ce44SJohn Forte 	stmfFreeMemory(groupProps);
4935fcf3ce44SJohn Forte 	return (ret);
4936fcf3ce44SJohn Forte }
4937fcf3ce44SJohn Forte 
4938fcf3ce44SJohn Forte /*
4939fcf3ce44SJohn Forte  * loadTargetGroups
4940fcf3ce44SJohn Forte  *
4941fcf3ce44SJohn Forte  * Purpose - issues the ioctl to load the target groups into stmf
4942fcf3ce44SJohn Forte  *
4943fcf3ce44SJohn Forte  * fd - file descriptor for the control node of stmf.
4944fcf3ce44SJohn Forte  * groupList - populated target group list.
4945fcf3ce44SJohn Forte  */
4946fcf3ce44SJohn Forte static int
4947fcf3ce44SJohn Forte loadTargetGroups(int fd, stmfGroupList *groupList)
4948fcf3ce44SJohn Forte {
4949fcf3ce44SJohn Forte 	int i, j;
4950fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
4951fcf3ce44SJohn Forte 	stmfGroupProperties *groupProps = NULL;
4952fcf3ce44SJohn Forte 
4953fcf3ce44SJohn Forte 	for (i = 0; i < groupList->cnt; i++) {
4954fcf3ce44SJohn Forte 		if ((ret = groupIoctl(fd, STMF_IOCTL_CREATE_TARGET_GROUP,
4955fcf3ce44SJohn Forte 		    &(groupList->name[i]))) != STMF_STATUS_SUCCESS) {
4956fcf3ce44SJohn Forte 			goto out;
4957fcf3ce44SJohn Forte 		}
49588fe96085Stim szeto 		ret = iLoadGroupMembersFromPs(&(groupList->name[i]),
49598fe96085Stim szeto 		    &groupProps, TARGET_GROUP);
4960fcf3ce44SJohn Forte 		for (j = 0; j < groupProps->cnt; j++) {
4961fcf3ce44SJohn Forte 			if ((ret = groupMemberIoctl(fd, STMF_IOCTL_ADD_TG_ENTRY,
4962fcf3ce44SJohn Forte 			    &(groupList->name[i]), &(groupProps->name[j])))
4963fcf3ce44SJohn Forte 			    != STMF_STATUS_SUCCESS) {
4964fcf3ce44SJohn Forte 				goto out;
4965fcf3ce44SJohn Forte 			}
4966fcf3ce44SJohn Forte 		}
4967fcf3ce44SJohn Forte 	}
4968fcf3ce44SJohn Forte 
4969fcf3ce44SJohn Forte 
4970fcf3ce44SJohn Forte out:
4971fcf3ce44SJohn Forte 	stmfFreeMemory(groupProps);
4972fcf3ce44SJohn Forte 	return (ret);
4973fcf3ce44SJohn Forte }
4974fcf3ce44SJohn Forte 
4975fcf3ce44SJohn Forte 
4976fcf3ce44SJohn Forte /*
4977fcf3ce44SJohn Forte  * loadStore
4978fcf3ce44SJohn Forte  *
4979fcf3ce44SJohn Forte  * Purpose: Load the configuration data from the store
4980fcf3ce44SJohn Forte  *
4981fcf3ce44SJohn Forte  * First load the host groups and target groups, then the view entries
4982fcf3ce44SJohn Forte  * and finally the provider data
4983fcf3ce44SJohn Forte  *
4984fcf3ce44SJohn Forte  * fd - file descriptor of control node for stmf.
4985fcf3ce44SJohn Forte  */
4986fcf3ce44SJohn Forte static int
4987fcf3ce44SJohn Forte loadStore(int fd)
4988fcf3ce44SJohn Forte {
4989fcf3ce44SJohn Forte 	int ret;
4990fcf3ce44SJohn Forte 	int i, j;
4991fcf3ce44SJohn Forte 	stmfGroupList *groupList = NULL;
4992fcf3ce44SJohn Forte 	stmfGuidList *guidList = NULL;
4993fcf3ce44SJohn Forte 	stmfViewEntryList *viewEntryList = NULL;
4994fcf3ce44SJohn Forte 	stmfProviderList *providerList = NULL;
4995fcf3ce44SJohn Forte 	int providerType;
4996fcf3ce44SJohn Forte 	nvlist_t *nvl = NULL;
4997fcf3ce44SJohn Forte 
4998fcf3ce44SJohn Forte 
4999fcf3ce44SJohn Forte 
5000fcf3ce44SJohn Forte 	/* load host groups */
50018fe96085Stim szeto 	ret = iLoadGroupFromPs(&groupList, HOST_GROUP);
5002fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5003fcf3ce44SJohn Forte 		return (ret);
5004fcf3ce44SJohn Forte 	}
5005fcf3ce44SJohn Forte 	ret = loadHostGroups(fd, groupList);
5006fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5007fcf3ce44SJohn Forte 		goto out;
5008fcf3ce44SJohn Forte 	}
5009fcf3ce44SJohn Forte 
5010fcf3ce44SJohn Forte 	stmfFreeMemory(groupList);
5011fcf3ce44SJohn Forte 	groupList = NULL;
5012fcf3ce44SJohn Forte 
5013fcf3ce44SJohn Forte 	/* load target groups */
50148fe96085Stim szeto 	ret = iLoadGroupFromPs(&groupList, TARGET_GROUP);
5015fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5016fcf3ce44SJohn Forte 		goto out;
5017fcf3ce44SJohn Forte 	}
5018fcf3ce44SJohn Forte 	ret = loadTargetGroups(fd, groupList);
5019fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5020fcf3ce44SJohn Forte 		goto out;
5021fcf3ce44SJohn Forte 	}
5022fcf3ce44SJohn Forte 
5023fcf3ce44SJohn Forte 	stmfFreeMemory(groupList);
5024fcf3ce44SJohn Forte 	groupList = NULL;
5025fcf3ce44SJohn Forte 
5026fcf3ce44SJohn Forte 	/* Get the guid list */
5027fcf3ce44SJohn Forte 	ret = psGetLogicalUnitList(&guidList);
5028fcf3ce44SJohn Forte 	switch (ret) {
5029fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5030fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5031fcf3ce44SJohn Forte 			break;
5032fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
5033fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
5034fcf3ce44SJohn Forte 			break;
5035fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5036fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5037fcf3ce44SJohn Forte 			break;
5038fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5039fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5040fcf3ce44SJohn Forte 			break;
5041fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5042fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5043fcf3ce44SJohn Forte 			break;
5044fcf3ce44SJohn Forte 		default:
5045fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5046fcf3ce44SJohn Forte 			break;
5047fcf3ce44SJohn Forte 	}
5048fcf3ce44SJohn Forte 
5049fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5050fcf3ce44SJohn Forte 		goto out;
5051fcf3ce44SJohn Forte 	}
5052fcf3ce44SJohn Forte 
5053fcf3ce44SJohn Forte 	/*
5054fcf3ce44SJohn Forte 	 * We have the guid list, now get the corresponding
5055fcf3ce44SJohn Forte 	 * view entries for each guid
5056fcf3ce44SJohn Forte 	 */
5057fcf3ce44SJohn Forte 	for (i = 0; i < guidList->cnt; i++) {
5058fcf3ce44SJohn Forte 		ret = psGetViewEntryList(&guidList->guid[i], &viewEntryList);
5059fcf3ce44SJohn Forte 		switch (ret) {
5060fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
5061fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
5062fcf3ce44SJohn Forte 				break;
5063fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
5064fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
5065fcf3ce44SJohn Forte 				break;
5066fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
5067fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
5068fcf3ce44SJohn Forte 				break;
5069fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5070fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
5071fcf3ce44SJohn Forte 				break;
5072fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
5073fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
5074fcf3ce44SJohn Forte 				break;
5075fcf3ce44SJohn Forte 			default:
5076fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
5077fcf3ce44SJohn Forte 				break;
5078fcf3ce44SJohn Forte 		}
5079fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
5080fcf3ce44SJohn Forte 			goto out;
5081fcf3ce44SJohn Forte 		}
5082fcf3ce44SJohn Forte 		for (j = 0; j < viewEntryList->cnt; j++) {
5083fcf3ce44SJohn Forte 			ret = addViewEntryIoctl(fd, &guidList->guid[i],
5084fcf3ce44SJohn Forte 			    &viewEntryList->ve[j]);
5085fcf3ce44SJohn Forte 			if (ret != STMF_STATUS_SUCCESS) {
5086fcf3ce44SJohn Forte 				goto out;
5087fcf3ce44SJohn Forte 			}
5088fcf3ce44SJohn Forte 		}
5089fcf3ce44SJohn Forte 	}
5090fcf3ce44SJohn Forte 
5091fcf3ce44SJohn Forte 	/* get the list of providers that have data */
5092fcf3ce44SJohn Forte 	ret = psGetProviderDataList(&providerList);
5093fcf3ce44SJohn Forte 	switch (ret) {
5094fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
5095fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
5096fcf3ce44SJohn Forte 			break;
5097fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
5098fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
5099fcf3ce44SJohn Forte 			break;
5100fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
5101fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
5102fcf3ce44SJohn Forte 			break;
5103fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5104fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
5105fcf3ce44SJohn Forte 			break;
5106fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
5107fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
5108fcf3ce44SJohn Forte 			break;
5109fcf3ce44SJohn Forte 		default:
5110fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5111fcf3ce44SJohn Forte 			break;
5112fcf3ce44SJohn Forte 	}
5113fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5114fcf3ce44SJohn Forte 		goto out;
5115fcf3ce44SJohn Forte 	}
5116fcf3ce44SJohn Forte 
5117fcf3ce44SJohn Forte 	for (i = 0; i < providerList->cnt; i++) {
5118fcf3ce44SJohn Forte 		providerType = providerList->provider[i].providerType;
5119fcf3ce44SJohn Forte 		ret = psGetProviderData(providerList->provider[i].name,
5120fcf3ce44SJohn Forte 		    &nvl, providerType, NULL);
5121fcf3ce44SJohn Forte 		switch (ret) {
5122fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
5123fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
5124fcf3ce44SJohn Forte 				break;
5125fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
5126fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
5127fcf3ce44SJohn Forte 				break;
5128fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
5129fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
5130fcf3ce44SJohn Forte 				break;
5131fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5132fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
5133fcf3ce44SJohn Forte 				break;
5134fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
5135fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
5136fcf3ce44SJohn Forte 				break;
5137fcf3ce44SJohn Forte 			default:
5138fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
5139fcf3ce44SJohn Forte 				break;
5140fcf3ce44SJohn Forte 		}
5141fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
5142fcf3ce44SJohn Forte 			goto out;
5143fcf3ce44SJohn Forte 		}
5144fcf3ce44SJohn Forte 
5145fcf3ce44SJohn Forte 		/* call setProviderData */
5146fcf3ce44SJohn Forte 		ret = setProviderData(fd, providerList->provider[i].name, nvl,
51478fe96085Stim szeto 		    providerType, NULL);
5148fcf3ce44SJohn Forte 		switch (ret) {
5149fcf3ce44SJohn Forte 			case STMF_PS_SUCCESS:
5150fcf3ce44SJohn Forte 				ret = STMF_STATUS_SUCCESS;
5151fcf3ce44SJohn Forte 				break;
5152fcf3ce44SJohn Forte 			case STMF_PS_ERROR_NOT_FOUND:
5153fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
5154fcf3ce44SJohn Forte 				break;
5155fcf3ce44SJohn Forte 			case STMF_PS_ERROR_BUSY:
5156fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
5157fcf3ce44SJohn Forte 				break;
5158fcf3ce44SJohn Forte 			case STMF_PS_ERROR_SERVICE_NOT_FOUND:
5159fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_NOT_FOUND;
5160fcf3ce44SJohn Forte 				break;
5161fcf3ce44SJohn Forte 			case STMF_PS_ERROR_VERSION_MISMATCH:
5162fcf3ce44SJohn Forte 				ret = STMF_ERROR_SERVICE_DATA_VERSION;
5163fcf3ce44SJohn Forte 				break;
5164fcf3ce44SJohn Forte 			default:
5165fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
5166fcf3ce44SJohn Forte 				break;
5167fcf3ce44SJohn Forte 		}
5168fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS) {
5169fcf3ce44SJohn Forte 			goto out;
5170fcf3ce44SJohn Forte 		}
5171fcf3ce44SJohn Forte 
5172fcf3ce44SJohn Forte 		nvlist_free(nvl);
5173fcf3ce44SJohn Forte 		nvl = NULL;
5174fcf3ce44SJohn Forte 	}
5175fcf3ce44SJohn Forte out:
5176fcf3ce44SJohn Forte 	if (groupList != NULL) {
5177fcf3ce44SJohn Forte 		free(groupList);
5178fcf3ce44SJohn Forte 	}
5179fcf3ce44SJohn Forte 	if (guidList != NULL) {
5180fcf3ce44SJohn Forte 		free(guidList);
5181fcf3ce44SJohn Forte 	}
5182fcf3ce44SJohn Forte 	if (viewEntryList != NULL) {
5183fcf3ce44SJohn Forte 		free(viewEntryList);
5184fcf3ce44SJohn Forte 	}
5185fcf3ce44SJohn Forte 	if (nvl != NULL) {
5186fcf3ce44SJohn Forte 		nvlist_free(nvl);
5187fcf3ce44SJohn Forte 	}
5188fcf3ce44SJohn Forte 	return (ret);
5189fcf3ce44SJohn Forte }
5190fcf3ce44SJohn Forte 
5191fcf3ce44SJohn Forte /*
519245039663SJohn Forte  * stmfGetAluaState
519345039663SJohn Forte  *
519445039663SJohn Forte  * Purpose - Get the alua state
519545039663SJohn Forte  *
519645039663SJohn Forte  */
519745039663SJohn Forte int
519845039663SJohn Forte stmfGetAluaState(boolean_t *enabled, uint32_t *node)
519945039663SJohn Forte {
520045039663SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
520145039663SJohn Forte 	int fd;
520245039663SJohn Forte 	stmf_iocdata_t stmfIoctl = {0};
520345039663SJohn Forte 	stmf_alua_state_desc_t alua_state = {0};
520445039663SJohn Forte 	int ioctlRet;
520545039663SJohn Forte 
520645039663SJohn Forte 	if (enabled == NULL || node == NULL) {
520745039663SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
520845039663SJohn Forte 	}
520945039663SJohn Forte 
521045039663SJohn Forte 	/*
521145039663SJohn Forte 	 * Open control node for stmf
521245039663SJohn Forte 	 */
521345039663SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
521445039663SJohn Forte 		return (ret);
521545039663SJohn Forte 
521645039663SJohn Forte 	/*
521745039663SJohn Forte 	 * Issue ioctl to get the stmf state
521845039663SJohn Forte 	 */
521945039663SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
522045039663SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (alua_state);
522145039663SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&alua_state;
522245039663SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_GET_ALUA_STATE, &stmfIoctl);
522345039663SJohn Forte 
522445039663SJohn Forte 	(void) close(fd);
522545039663SJohn Forte 
522645039663SJohn Forte 	if (ioctlRet != 0) {
522745039663SJohn Forte 		switch (errno) {
522845039663SJohn Forte 			case EBUSY:
522945039663SJohn Forte 				ret = STMF_ERROR_BUSY;
523045039663SJohn Forte 				break;
523145039663SJohn Forte 			case EPERM:
523245039663SJohn Forte 			case EACCES:
523345039663SJohn Forte 				ret = STMF_ERROR_PERM;
523445039663SJohn Forte 				break;
523545039663SJohn Forte 			default:
523645039663SJohn Forte 				syslog(LOG_DEBUG,
523745039663SJohn Forte 				    "getStmfState:ioctl errno(%d)", errno);
523845039663SJohn Forte 				ret = STMF_STATUS_ERROR;
523945039663SJohn Forte 				break;
524045039663SJohn Forte 		}
524145039663SJohn Forte 	} else {
524245039663SJohn Forte 		if (alua_state.alua_state == 1) {
524345039663SJohn Forte 			*enabled = B_TRUE;
524445039663SJohn Forte 		} else {
524545039663SJohn Forte 			*enabled = B_FALSE;
524645039663SJohn Forte 		}
524745039663SJohn Forte 		*node = alua_state.alua_node;
524845039663SJohn Forte 	}
524945039663SJohn Forte 
525045039663SJohn Forte 	return (ret);
525145039663SJohn Forte }
525245039663SJohn Forte 
525345039663SJohn Forte /*
525445039663SJohn Forte  * stmfSetAluaState
525545039663SJohn Forte  *
525645039663SJohn Forte  * Purpose - set the alua state to enabled/disabled
525745039663SJohn Forte  *
525845039663SJohn Forte  */
525945039663SJohn Forte int
526045039663SJohn Forte stmfSetAluaState(boolean_t enabled, uint32_t node)
526145039663SJohn Forte {
526245039663SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
526345039663SJohn Forte 	int fd;
526445039663SJohn Forte 	stmf_iocdata_t stmfIoctl = {0};
526545039663SJohn Forte 	stmf_alua_state_desc_t alua_state = {0};
526645039663SJohn Forte 	int ioctlRet;
526745039663SJohn Forte 
526845039663SJohn Forte 	if ((enabled != B_TRUE && enabled != B_FALSE) || (node > 1)) {
526945039663SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
527045039663SJohn Forte 	}
527145039663SJohn Forte 
527245039663SJohn Forte 	if (enabled) {
527345039663SJohn Forte 		alua_state.alua_state = 1;
527445039663SJohn Forte 	}
527545039663SJohn Forte 
527645039663SJohn Forte 	alua_state.alua_node = node;
527745039663SJohn Forte 
527845039663SJohn Forte 	/*
527945039663SJohn Forte 	 * Open control node for stmf
528045039663SJohn Forte 	 */
528145039663SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
528245039663SJohn Forte 		return (ret);
528345039663SJohn Forte 
528445039663SJohn Forte 	/*
528545039663SJohn Forte 	 * Issue ioctl to get the stmf state
528645039663SJohn Forte 	 */
528745039663SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
528845039663SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (alua_state);
528945039663SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&alua_state;
529045039663SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_SET_ALUA_STATE, &stmfIoctl);
529145039663SJohn Forte 
529245039663SJohn Forte 	(void) close(fd);
529345039663SJohn Forte 
529445039663SJohn Forte 	if (ioctlRet != 0) {
529545039663SJohn Forte 		switch (errno) {
529645039663SJohn Forte 			case EBUSY:
529745039663SJohn Forte 				ret = STMF_ERROR_BUSY;
529845039663SJohn Forte 				break;
529945039663SJohn Forte 			case EPERM:
530045039663SJohn Forte 			case EACCES:
530145039663SJohn Forte 				ret = STMF_ERROR_PERM;
530245039663SJohn Forte 				break;
530345039663SJohn Forte 			default:
530445039663SJohn Forte 				syslog(LOG_DEBUG,
530545039663SJohn Forte 				    "getStmfState:ioctl errno(%d)", errno);
530645039663SJohn Forte 				ret = STMF_STATUS_ERROR;
530745039663SJohn Forte 				break;
530845039663SJohn Forte 		}
530945039663SJohn Forte 	}
5310cd36db67SJohn Forte 	if (!enabled && ret == STMF_STATUS_SUCCESS) {
531145039663SJohn Forte 		deleteNonActiveLus();
531245039663SJohn Forte 	}
531345039663SJohn Forte 
531445039663SJohn Forte 	return (ret);
531545039663SJohn Forte }
531645039663SJohn Forte 
531745039663SJohn Forte static void
531845039663SJohn Forte deleteNonActiveLus()
531945039663SJohn Forte {
532045039663SJohn Forte 	int stmfRet;
532145039663SJohn Forte 	int i;
532245039663SJohn Forte 	stmfGuidList *luList;
532345039663SJohn Forte 	luResource hdl = NULL;
532445039663SJohn Forte 	char propVal[10];
532545039663SJohn Forte 	size_t propValSize = sizeof (propVal);
532645039663SJohn Forte 
532745039663SJohn Forte 	stmfRet = stmfGetLogicalUnitList(&luList);
532845039663SJohn Forte 	if (stmfRet != STMF_STATUS_SUCCESS) {
532945039663SJohn Forte 		return;
533045039663SJohn Forte 	}
533145039663SJohn Forte 
533245039663SJohn Forte 	for (i = 0; i < luList->cnt; i++) {
533345039663SJohn Forte 		stmfRet = stmfGetLuResource(&luList->guid[i], &hdl);
533445039663SJohn Forte 		if (stmfRet != STMF_STATUS_SUCCESS) {
533545039663SJohn Forte 			goto err;
533645039663SJohn Forte 		}
533745039663SJohn Forte 		stmfRet = stmfGetLuProp(hdl, STMF_LU_PROP_ACCESS_STATE, propVal,
533845039663SJohn Forte 		    &propValSize);
533945039663SJohn Forte 		if (stmfRet != STMF_STATUS_SUCCESS) {
534045039663SJohn Forte 			goto err;
534145039663SJohn Forte 		}
534245039663SJohn Forte 		if (propVal[0] == '0') {
534345039663SJohn Forte 			(void) stmfFreeLuResource(hdl);
534445039663SJohn Forte 			hdl = NULL;
534545039663SJohn Forte 			continue;
534645039663SJohn Forte 		}
534745039663SJohn Forte 		(void) stmfDeleteLu(&luList->guid[i]);
534845039663SJohn Forte 		(void) stmfFreeLuResource(hdl);
534945039663SJohn Forte 		hdl = NULL;
535045039663SJohn Forte 	}
535145039663SJohn Forte 
535245039663SJohn Forte err:
535345039663SJohn Forte 	stmfFreeMemory(luList);
535445039663SJohn Forte 	(void) stmfFreeLuResource(hdl);
535545039663SJohn Forte }
535645039663SJohn Forte 
535745039663SJohn Forte /*
5358fcf3ce44SJohn Forte  * stmfLoadConfig
5359fcf3ce44SJohn Forte  *
5360fcf3ce44SJohn Forte  * Purpose - load the configuration data from smf into stmf
5361fcf3ce44SJohn Forte  *
5362fcf3ce44SJohn Forte  */
5363fcf3ce44SJohn Forte int
5364fcf3ce44SJohn Forte stmfLoadConfig(void)
5365fcf3ce44SJohn Forte {
53668fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
5367fcf3ce44SJohn Forte 	int fd;
5368fcf3ce44SJohn Forte 	stmf_state_desc_t stmfStateSet;
5369fcf3ce44SJohn Forte 	stmfState state;
5370fcf3ce44SJohn Forte 
53718fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
53728fe96085Stim szeto 		stmfStateSet.state = STMF_STATE_OFFLINE;
5373640428aeSSue Gleeson 
53748fe96085Stim szeto 		if ((ret = openStmf(OPEN_EXCL_STMF, &fd))
53758fe96085Stim szeto 		    != STMF_STATUS_SUCCESS) {
53768fe96085Stim szeto 			return (ret);
53778fe96085Stim szeto 		}
5378640428aeSSue Gleeson 		/*
5379640428aeSSue Gleeson 		 * Configuration not stored persistently; nothing to
5380640428aeSSue Gleeson 		 * initialize so do not set to STMF_CONFIG_INIT.
5381640428aeSSue Gleeson 		 */
53828fe96085Stim szeto 		stmfStateSet.config_state = STMF_CONFIG_INIT_DONE;
53838fe96085Stim szeto 		goto done;
53848fe96085Stim szeto 	}
5385fcf3ce44SJohn Forte 
5386fcf3ce44SJohn Forte 	/* Check to ensure service exists */
5387fcf3ce44SJohn Forte 	if (psCheckService() != STMF_STATUS_SUCCESS) {
5388fcf3ce44SJohn Forte 		return (STMF_ERROR_SERVICE_NOT_FOUND);
5389fcf3ce44SJohn Forte 	}
5390fcf3ce44SJohn Forte 
5391fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
5392fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
5393fcf3ce44SJohn Forte 		if (state.operationalState != STMF_SERVICE_STATE_OFFLINE) {
5394fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_ONLINE);
5395fcf3ce44SJohn Forte 		}
5396fcf3ce44SJohn Forte 	} else {
5397fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
5398fcf3ce44SJohn Forte 	}
5399fcf3ce44SJohn Forte 
5400fcf3ce44SJohn Forte 
5401fcf3ce44SJohn Forte 	stmfStateSet.state = STMF_STATE_OFFLINE;
5402fcf3ce44SJohn Forte 	stmfStateSet.config_state = STMF_CONFIG_INIT;
5403fcf3ce44SJohn Forte 
5404fcf3ce44SJohn Forte 	/*
5405fcf3ce44SJohn Forte 	 * Open control node for stmf
5406fcf3ce44SJohn Forte 	 */
5407fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5408fcf3ce44SJohn Forte 		return (ret);
5409fcf3ce44SJohn Forte 
5410fcf3ce44SJohn Forte 	ret = setStmfState(fd, &stmfStateSet, STMF_SERVICE_TYPE);
5411fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5412fcf3ce44SJohn Forte 		goto done;
5413fcf3ce44SJohn Forte 	}
5414fcf3ce44SJohn Forte 
5415fcf3ce44SJohn Forte 	/* Load the persistent configuration data */
5416fcf3ce44SJohn Forte 	ret = loadStore(fd);
5417fcf3ce44SJohn Forte 	if (ret != 0) {
5418fcf3ce44SJohn Forte 		goto done;
5419fcf3ce44SJohn Forte 	}
5420fcf3ce44SJohn Forte 
5421fcf3ce44SJohn Forte 	stmfStateSet.state = STMF_STATE_OFFLINE;
5422fcf3ce44SJohn Forte 	stmfStateSet.config_state = STMF_CONFIG_INIT_DONE;
5423fcf3ce44SJohn Forte 
5424fcf3ce44SJohn Forte done:
5425fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
5426fcf3ce44SJohn Forte 		ret = setStmfState(fd, &stmfStateSet, STMF_SERVICE_TYPE);
5427fcf3ce44SJohn Forte 	}
5428fcf3ce44SJohn Forte 	(void) close(fd);
5429fcf3ce44SJohn Forte 	return (ret);
5430fcf3ce44SJohn Forte }
5431fcf3ce44SJohn Forte 
54328fe96085Stim szeto 
5433fcf3ce44SJohn Forte /*
5434fcf3ce44SJohn Forte  * getStmfState
5435fcf3ce44SJohn Forte  *
5436fcf3ce44SJohn Forte  * stmfState - pointer to stmf_state_desc_t structure. Will contain the state
5437fcf3ce44SJohn Forte  *             information of the stmf service on success.
5438fcf3ce44SJohn Forte  */
5439fcf3ce44SJohn Forte static int
5440fcf3ce44SJohn Forte getStmfState(stmf_state_desc_t *stmfState)
5441fcf3ce44SJohn Forte {
5442fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5443fcf3ce44SJohn Forte 	int fd;
5444fcf3ce44SJohn Forte 	int ioctlRet;
5445fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
5446fcf3ce44SJohn Forte 
5447fcf3ce44SJohn Forte 	/*
5448fcf3ce44SJohn Forte 	 * Open control node for stmf
5449fcf3ce44SJohn Forte 	 */
5450fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5451fcf3ce44SJohn Forte 		return (ret);
5452fcf3ce44SJohn Forte 
5453fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
5454fcf3ce44SJohn Forte 	/*
5455fcf3ce44SJohn Forte 	 * Issue ioctl to get the stmf state
5456fcf3ce44SJohn Forte 	 */
5457fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
5458fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_state_desc_t);
5459fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)stmfState;
5460fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf_size = sizeof (stmf_state_desc_t);
5461fcf3ce44SJohn Forte 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)stmfState;
5462fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_GET_STMF_STATE, &stmfIoctl);
5463fcf3ce44SJohn Forte 
5464fcf3ce44SJohn Forte 	(void) close(fd);
5465fcf3ce44SJohn Forte 
5466fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
5467fcf3ce44SJohn Forte 		switch (errno) {
5468fcf3ce44SJohn Forte 			case EBUSY:
5469fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
5470fcf3ce44SJohn Forte 				break;
5471fcf3ce44SJohn Forte 			case EPERM:
5472fcf3ce44SJohn Forte 			case EACCES:
5473fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
5474fcf3ce44SJohn Forte 				break;
5475fcf3ce44SJohn Forte 			default:
5476fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
5477fcf3ce44SJohn Forte 				    "getStmfState:ioctl errno(%d)", errno);
5478fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
5479fcf3ce44SJohn Forte 				break;
5480fcf3ce44SJohn Forte 		}
5481fcf3ce44SJohn Forte 	}
5482fcf3ce44SJohn Forte 	return (ret);
5483fcf3ce44SJohn Forte }
5484fcf3ce44SJohn Forte 
5485fcf3ce44SJohn Forte 
5486fcf3ce44SJohn Forte /*
5487fcf3ce44SJohn Forte  * setStmfState
5488fcf3ce44SJohn Forte  *
5489fcf3ce44SJohn Forte  * stmfState - pointer to caller set state structure
5490fcf3ce44SJohn Forte  * objectType - one of:
5491fcf3ce44SJohn Forte  *		LOGICAL_UNIT_TYPE
5492fcf3ce44SJohn Forte  *		TARGET_TYPE
5493fcf3ce44SJohn Forte  *		STMF_SERVICE_TYPE
5494fcf3ce44SJohn Forte  */
5495fcf3ce44SJohn Forte static int
5496fcf3ce44SJohn Forte setStmfState(int fd, stmf_state_desc_t *stmfState, int objectType)
5497fcf3ce44SJohn Forte {
5498fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5499fcf3ce44SJohn Forte 	int ioctlRet;
5500fcf3ce44SJohn Forte 	int cmd;
5501fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
5502fcf3ce44SJohn Forte 
5503fcf3ce44SJohn Forte 	switch (objectType) {
5504fcf3ce44SJohn Forte 		case LOGICAL_UNIT_TYPE:
5505fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_LU_STATE;
5506fcf3ce44SJohn Forte 			break;
5507fcf3ce44SJohn Forte 		case TARGET_TYPE:
5508fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_TARGET_PORT_STATE;
5509fcf3ce44SJohn Forte 			break;
5510fcf3ce44SJohn Forte 		case STMF_SERVICE_TYPE:
5511fcf3ce44SJohn Forte 			cmd = STMF_IOCTL_SET_STMF_STATE;
5512fcf3ce44SJohn Forte 			break;
5513fcf3ce44SJohn Forte 		default:
5514fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
5515fcf3ce44SJohn Forte 			goto done;
5516fcf3ce44SJohn Forte 	}
5517fcf3ce44SJohn Forte 
5518fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
5519fcf3ce44SJohn Forte 	/*
5520fcf3ce44SJohn Forte 	 * Issue ioctl to set the stmf state
5521fcf3ce44SJohn Forte 	 */
5522fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
5523fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_state_desc_t);
5524fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)stmfState;
5525fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, cmd, &stmfIoctl);
5526fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
5527fcf3ce44SJohn Forte 		switch (errno) {
5528fcf3ce44SJohn Forte 			case EBUSY:
5529fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
5530fcf3ce44SJohn Forte 				break;
55318fe96085Stim szeto 			case EPERM:
5532fcf3ce44SJohn Forte 			case EACCES:
5533fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
5534fcf3ce44SJohn Forte 				break;
5535fcf3ce44SJohn Forte 			case ENOENT:
5536fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
5537fcf3ce44SJohn Forte 				break;
5538fcf3ce44SJohn Forte 			default:
5539fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
5540fcf3ce44SJohn Forte 				    "setStmfState:ioctl errno(%d)", errno);
5541fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
5542fcf3ce44SJohn Forte 				break;
5543fcf3ce44SJohn Forte 		}
5544fcf3ce44SJohn Forte 	}
5545fcf3ce44SJohn Forte done:
5546fcf3ce44SJohn Forte 	return (ret);
5547fcf3ce44SJohn Forte }
55484b31676fSsrivijitha dugganapalli int
55494b31676fSsrivijitha dugganapalli stmfSetStmfProp(uint8_t propType, char *propVal)
55504b31676fSsrivijitha dugganapalli {
55514b31676fSsrivijitha dugganapalli 	int ret = STMF_STATUS_SUCCESS;
55524b31676fSsrivijitha dugganapalli 	switch (propType) {
55534b31676fSsrivijitha dugganapalli 		case STMF_DEFAULT_LU_STATE:
55544b31676fSsrivijitha dugganapalli 			break;
55554b31676fSsrivijitha dugganapalli 		case STMF_DEFAULT_TARGET_PORT_STATE:
55564b31676fSsrivijitha dugganapalli 			break;
55574b31676fSsrivijitha dugganapalli 		default:
55584b31676fSsrivijitha dugganapalli 			return (STMF_ERROR_INVALID_ARG);
55594b31676fSsrivijitha dugganapalli 	}
55604b31676fSsrivijitha dugganapalli 	ret = psSetStmfProp(propType, propVal);
55614b31676fSsrivijitha dugganapalli 	switch (ret) {
55624b31676fSsrivijitha dugganapalli 		case STMF_PS_SUCCESS:
55634b31676fSsrivijitha dugganapalli 			ret = STMF_STATUS_SUCCESS;
55644b31676fSsrivijitha dugganapalli 			break;
55654b31676fSsrivijitha dugganapalli 		case STMF_PS_ERROR_BUSY:
55664b31676fSsrivijitha dugganapalli 			ret = STMF_ERROR_BUSY;
55674b31676fSsrivijitha dugganapalli 			break;
55684b31676fSsrivijitha dugganapalli 		default:
55694b31676fSsrivijitha dugganapalli 			syslog(LOG_DEBUG,
55704b31676fSsrivijitha dugganapalli 			    "stmfSetStmfProp:psSetStmfProp:error(%d)",
55714b31676fSsrivijitha dugganapalli 			    ret);
55724b31676fSsrivijitha dugganapalli 			ret = STMF_STATUS_ERROR;
55734b31676fSsrivijitha dugganapalli 			break;
55744b31676fSsrivijitha dugganapalli 	}
55754b31676fSsrivijitha dugganapalli 	return (ret);
55764b31676fSsrivijitha dugganapalli }
55774b31676fSsrivijitha dugganapalli 
55784b31676fSsrivijitha dugganapalli 
55794b31676fSsrivijitha dugganapalli int
55804b31676fSsrivijitha dugganapalli stmfGetStmfProp(uint8_t propType, char *propVal, size_t *propLen)
55814b31676fSsrivijitha dugganapalli {
55824b31676fSsrivijitha dugganapalli 	int ret = STMF_STATUS_SUCCESS;
55834b31676fSsrivijitha dugganapalli 	char prop[MAXNAMELEN] = {0};
55844b31676fSsrivijitha dugganapalli 	size_t reqLen;
55854b31676fSsrivijitha dugganapalli 
55864b31676fSsrivijitha dugganapalli 	if (propVal == NULL || propLen == NULL) {
55874b31676fSsrivijitha dugganapalli 		return (STMF_ERROR_INVALID_ARG);
55884b31676fSsrivijitha dugganapalli 	}
55894b31676fSsrivijitha dugganapalli 	switch (propType) {
55904b31676fSsrivijitha dugganapalli 		case STMF_DEFAULT_LU_STATE:
55914b31676fSsrivijitha dugganapalli 			break;
55924b31676fSsrivijitha dugganapalli 		case STMF_DEFAULT_TARGET_PORT_STATE:
55934b31676fSsrivijitha dugganapalli 			break;
55944b31676fSsrivijitha dugganapalli 		default:
55954b31676fSsrivijitha dugganapalli 			return (STMF_ERROR_INVALID_ARG);
55964b31676fSsrivijitha dugganapalli 	}
55974b31676fSsrivijitha dugganapalli 	ret = psGetStmfProp(propType, prop);
55984b31676fSsrivijitha dugganapalli 	if ((reqLen = strlcpy(propVal, prop, *propLen)) >= *propLen) {
55994b31676fSsrivijitha dugganapalli 		*propLen = reqLen + 1;
56004b31676fSsrivijitha dugganapalli 		return (STMF_ERROR_INVALID_ARG);
56014b31676fSsrivijitha dugganapalli 	}
56024b31676fSsrivijitha dugganapalli 
56034b31676fSsrivijitha dugganapalli 	switch (ret) {
56044b31676fSsrivijitha dugganapalli 		case STMF_PS_SUCCESS:
56054b31676fSsrivijitha dugganapalli 			ret = STMF_STATUS_SUCCESS;
56064b31676fSsrivijitha dugganapalli 			break;
56074b31676fSsrivijitha dugganapalli 		case STMF_PS_ERROR_BUSY:
56084b31676fSsrivijitha dugganapalli 			ret = STMF_ERROR_BUSY;
56094b31676fSsrivijitha dugganapalli 			break;
56104b31676fSsrivijitha dugganapalli 		case STMF_PS_ERROR_NOT_FOUND:
56114b31676fSsrivijitha dugganapalli 			ret = STMF_ERROR_NOT_FOUND;
56124b31676fSsrivijitha dugganapalli 			break;
56134b31676fSsrivijitha dugganapalli 		default:
56144b31676fSsrivijitha dugganapalli 			syslog(LOG_DEBUG,
56154b31676fSsrivijitha dugganapalli 			    "stmfGetStmfProp:psGetStmfProp:error(%d)",
56164b31676fSsrivijitha dugganapalli 			    ret);
56174b31676fSsrivijitha dugganapalli 			ret = STMF_STATUS_ERROR;
56184b31676fSsrivijitha dugganapalli 			break;
56194b31676fSsrivijitha dugganapalli 	}
56204b31676fSsrivijitha dugganapalli 	return (ret);
56214b31676fSsrivijitha dugganapalli }
56224b31676fSsrivijitha dugganapalli 
56234b31676fSsrivijitha dugganapalli static int
56244b31676fSsrivijitha dugganapalli setStmfProp(stmf_set_props_t *stmf_set_props)
56254b31676fSsrivijitha dugganapalli {
56264b31676fSsrivijitha dugganapalli 	char propVal[MAXNAMELEN] = {0};
56274b31676fSsrivijitha dugganapalli 	int ret;
56284b31676fSsrivijitha dugganapalli 	if ((ret = psGetStmfProp(STMF_DEFAULT_LU_STATE, propVal)) ==
56294b31676fSsrivijitha dugganapalli 	    STMF_PS_SUCCESS) {
56304b31676fSsrivijitha dugganapalli 		if (strncmp(propVal, "offline", strlen(propVal)) == 0) {
56314b31676fSsrivijitha dugganapalli 			stmf_set_props->default_lu_state_value =
56324b31676fSsrivijitha dugganapalli 			    STMF_STATE_OFFLINE;
56334b31676fSsrivijitha dugganapalli 		} else {
56344b31676fSsrivijitha dugganapalli 			stmf_set_props->default_lu_state_value =
56354b31676fSsrivijitha dugganapalli 			    STMF_STATE_ONLINE;
56364b31676fSsrivijitha dugganapalli 		}
56374b31676fSsrivijitha dugganapalli 	} else {
56384b31676fSsrivijitha dugganapalli 		syslog(LOG_DEBUG,
56394b31676fSsrivijitha dugganapalli 		    "DefaultLuState:psSetStmfProp:error(%d)", ret);
56404b31676fSsrivijitha dugganapalli 		goto done;
56414b31676fSsrivijitha dugganapalli 	}
56424b31676fSsrivijitha dugganapalli 
56434b31676fSsrivijitha dugganapalli 	if ((ret = psGetStmfProp(STMF_DEFAULT_TARGET_PORT_STATE, propVal)) ==
56444b31676fSsrivijitha dugganapalli 	    STMF_PS_SUCCESS) {
56454b31676fSsrivijitha dugganapalli 		if (strncmp(propVal, "offline", strlen(propVal)) == 0) {
56464b31676fSsrivijitha dugganapalli 			stmf_set_props->default_target_state_value =
56474b31676fSsrivijitha dugganapalli 			    STMF_STATE_OFFLINE;
56484b31676fSsrivijitha dugganapalli 		} else {
56494b31676fSsrivijitha dugganapalli 			stmf_set_props->default_target_state_value =
56504b31676fSsrivijitha dugganapalli 			    STMF_STATE_ONLINE;
56514b31676fSsrivijitha dugganapalli 		}
56524b31676fSsrivijitha dugganapalli 	} else {
56534b31676fSsrivijitha dugganapalli 		syslog(LOG_DEBUG,
56544b31676fSsrivijitha dugganapalli 		    "DefaultTargetPortState:psSetStmfProp:error(%d)", ret);
56554b31676fSsrivijitha dugganapalli 		goto done;
56564b31676fSsrivijitha dugganapalli 	}
56574b31676fSsrivijitha dugganapalli done:
56584b31676fSsrivijitha dugganapalli 	switch (ret) {
56594b31676fSsrivijitha dugganapalli 		case STMF_PS_SUCCESS:
56604b31676fSsrivijitha dugganapalli 			ret = STMF_STATUS_SUCCESS;
56614b31676fSsrivijitha dugganapalli 			break;
56624b31676fSsrivijitha dugganapalli 		case STMF_PS_ERROR_NOT_FOUND:
56634b31676fSsrivijitha dugganapalli 			ret = STMF_ERROR_NOT_FOUND;
56644b31676fSsrivijitha dugganapalli 			break;
56654b31676fSsrivijitha dugganapalli 		case STMF_PS_ERROR_BUSY:
56664b31676fSsrivijitha dugganapalli 			ret = STMF_ERROR_BUSY;
56674b31676fSsrivijitha dugganapalli 			break;
56684b31676fSsrivijitha dugganapalli 		default:
56694b31676fSsrivijitha dugganapalli 			ret = STMF_STATUS_ERROR;
56704b31676fSsrivijitha dugganapalli 			break;
56714b31676fSsrivijitha dugganapalli 	}
56724b31676fSsrivijitha dugganapalli 	return (ret);
56734b31676fSsrivijitha dugganapalli }
56744b31676fSsrivijitha dugganapalli 
56754b31676fSsrivijitha dugganapalli static int
56764b31676fSsrivijitha dugganapalli loadStmfProp(int fd)
56774b31676fSsrivijitha dugganapalli {
56784b31676fSsrivijitha dugganapalli 	int ret = STMF_STATUS_SUCCESS;
56794b31676fSsrivijitha dugganapalli 	int ioctlRet;
56804b31676fSsrivijitha dugganapalli 	stmf_iocdata_t stmfIoctl = {0};
56814b31676fSsrivijitha dugganapalli 	stmf_set_props_t *stmf_set_props = NULL;
56824b31676fSsrivijitha dugganapalli 
56834b31676fSsrivijitha dugganapalli 	stmf_set_props = (stmf_set_props_t *)
56844b31676fSsrivijitha dugganapalli 	    calloc(1, (sizeof (stmf_set_props_t)));
56854b31676fSsrivijitha dugganapalli 	if (stmf_set_props == NULL) {
56864b31676fSsrivijitha dugganapalli 		ret = STMF_ERROR_NOMEM;
56874b31676fSsrivijitha dugganapalli 		goto done;
56884b31676fSsrivijitha dugganapalli 	}
56894b31676fSsrivijitha dugganapalli 
56904b31676fSsrivijitha dugganapalli 	/* Loading the default property values from smf */
56914b31676fSsrivijitha dugganapalli 
56924b31676fSsrivijitha dugganapalli 	if ((ret = setStmfProp(stmf_set_props)) != STMF_STATUS_SUCCESS)
56934b31676fSsrivijitha dugganapalli 		goto done;
56944b31676fSsrivijitha dugganapalli 
56954b31676fSsrivijitha dugganapalli 	stmfIoctl.stmf_version = STMF_VERSION_1;
56964b31676fSsrivijitha dugganapalli 	stmfIoctl.stmf_ibuf_size = sizeof (stmf_set_props_t);
56974b31676fSsrivijitha dugganapalli 	stmfIoctl.stmf_ibuf =
56984b31676fSsrivijitha dugganapalli 	    (uint64_t)(unsigned long)stmf_set_props;
56994b31676fSsrivijitha dugganapalli 
57004b31676fSsrivijitha dugganapalli 	ioctlRet = ioctl(fd, STMF_IOCTL_SET_STMF_PROPS,
57014b31676fSsrivijitha dugganapalli 	    &stmfIoctl);
57024b31676fSsrivijitha dugganapalli 
57034b31676fSsrivijitha dugganapalli 	if (ioctlRet != 0) {
57044b31676fSsrivijitha dugganapalli 		switch (errno) {
57054b31676fSsrivijitha dugganapalli 			case EBUSY:
57064b31676fSsrivijitha dugganapalli 				ret = STMF_ERROR_BUSY;
57074b31676fSsrivijitha dugganapalli 				break;
57084b31676fSsrivijitha dugganapalli 			case EPERM:
57094b31676fSsrivijitha dugganapalli 			case EACCES:
57104b31676fSsrivijitha dugganapalli 				ret = STMF_ERROR_PERM;
57114b31676fSsrivijitha dugganapalli 				break;
57124b31676fSsrivijitha dugganapalli 			case ENOENT:
57134b31676fSsrivijitha dugganapalli 				ret = STMF_ERROR_NOT_FOUND;
57144b31676fSsrivijitha dugganapalli 				break;
57154b31676fSsrivijitha dugganapalli 			default:
57164b31676fSsrivijitha dugganapalli 				syslog(LOG_DEBUG,
57174b31676fSsrivijitha dugganapalli 				    "setDefaultStmfState:"
57184b31676fSsrivijitha dugganapalli 				    "ioctl errno(%d)", errno);
57194b31676fSsrivijitha dugganapalli 				ret = STMF_STATUS_ERROR;
57204b31676fSsrivijitha dugganapalli 				break;
57214b31676fSsrivijitha dugganapalli 		}
57224b31676fSsrivijitha dugganapalli 	}
57234b31676fSsrivijitha dugganapalli done:
57244b31676fSsrivijitha dugganapalli 	if (stmf_set_props != NULL) {
57254b31676fSsrivijitha dugganapalli 		free(stmf_set_props);
57264b31676fSsrivijitha dugganapalli 	}
57274b31676fSsrivijitha dugganapalli 	return (ret);
57284b31676fSsrivijitha dugganapalli }
57294b31676fSsrivijitha dugganapalli 
57304b31676fSsrivijitha dugganapalli int
57314b31676fSsrivijitha dugganapalli stmfLoadStmfProps(void)
57324b31676fSsrivijitha dugganapalli {
57334b31676fSsrivijitha dugganapalli 	int ret = STMF_STATUS_SUCCESS;
57344b31676fSsrivijitha dugganapalli 	int fd;
57354b31676fSsrivijitha dugganapalli 	/* open control node for stmf */
57364b31676fSsrivijitha dugganapalli 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd))
57374b31676fSsrivijitha dugganapalli 	    != STMF_STATUS_SUCCESS) {
57384b31676fSsrivijitha dugganapalli 		goto done;
57394b31676fSsrivijitha dugganapalli 	}
57404b31676fSsrivijitha dugganapalli 	ret = loadStmfProp(fd);
57414b31676fSsrivijitha dugganapalli 
57424b31676fSsrivijitha dugganapalli 	(void) close(fd);
57434b31676fSsrivijitha dugganapalli done:
57444b31676fSsrivijitha dugganapalli 	if (ret != STMF_STATUS_SUCCESS) {
57454b31676fSsrivijitha dugganapalli 		syslog(LOG_DEBUG,
57464b31676fSsrivijitha dugganapalli 		    "stmfLoadStmfProps:Failed");
57474b31676fSsrivijitha dugganapalli 	}
57484b31676fSsrivijitha dugganapalli 	return (ret);
57494b31676fSsrivijitha dugganapalli }
5750fcf3ce44SJohn Forte 
5751fcf3ce44SJohn Forte /*
5752fcf3ce44SJohn Forte  * stmfOnline
5753fcf3ce44SJohn Forte  *
5754fcf3ce44SJohn Forte  * Purpose: Online stmf service
5755fcf3ce44SJohn Forte  *
5756fcf3ce44SJohn Forte  */
5757fcf3ce44SJohn Forte int
5758fcf3ce44SJohn Forte stmfOnline(void)
5759fcf3ce44SJohn Forte {
5760fcf3ce44SJohn Forte 	int ret;
5761fcf3ce44SJohn Forte 	int fd;
5762fcf3ce44SJohn Forte 	stmfState state;
5763fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
5764fcf3ce44SJohn Forte 
5765fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
5766fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
5767fcf3ce44SJohn Forte 		if (state.operationalState == STMF_SERVICE_STATE_ONLINE) {
5768fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_ONLINE);
5769fcf3ce44SJohn Forte 		}
5770fcf3ce44SJohn Forte 	} else {
5771fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
5772fcf3ce44SJohn Forte 	}
5773fcf3ce44SJohn Forte 	iState.state = STMF_STATE_ONLINE;
5774fcf3ce44SJohn Forte 	iState.config_state = STMF_CONFIG_NONE;
5775fcf3ce44SJohn Forte 	/*
5776fcf3ce44SJohn Forte 	 * Open control node for stmf
5777fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5778fcf3ce44SJohn Forte 	 */
5779fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5780fcf3ce44SJohn Forte 		return (ret);
5781fcf3ce44SJohn Forte 	ret = setStmfState(fd, &iState, STMF_SERVICE_TYPE);
5782fcf3ce44SJohn Forte 	(void) close(fd);
5783fcf3ce44SJohn Forte 	return (ret);
5784fcf3ce44SJohn Forte }
5785fcf3ce44SJohn Forte 
5786fcf3ce44SJohn Forte /*
5787fcf3ce44SJohn Forte  * stmfOffline
5788fcf3ce44SJohn Forte  *
5789fcf3ce44SJohn Forte  * Purpose: Offline stmf service
5790fcf3ce44SJohn Forte  *
5791fcf3ce44SJohn Forte  */
5792fcf3ce44SJohn Forte int
5793fcf3ce44SJohn Forte stmfOffline(void)
5794fcf3ce44SJohn Forte {
5795fcf3ce44SJohn Forte 	int ret;
5796fcf3ce44SJohn Forte 	int fd;
5797fcf3ce44SJohn Forte 	stmfState state;
5798fcf3ce44SJohn Forte 	stmf_state_desc_t iState;
5799fcf3ce44SJohn Forte 
5800fcf3ce44SJohn Forte 	ret = stmfGetState(&state);
5801fcf3ce44SJohn Forte 	if (ret == STMF_STATUS_SUCCESS) {
5802fcf3ce44SJohn Forte 		if (state.operationalState == STMF_SERVICE_STATE_OFFLINE) {
5803fcf3ce44SJohn Forte 			return (STMF_ERROR_SERVICE_OFFLINE);
5804fcf3ce44SJohn Forte 		}
5805fcf3ce44SJohn Forte 	} else {
5806fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
5807fcf3ce44SJohn Forte 	}
5808fcf3ce44SJohn Forte 	iState.state = STMF_STATE_OFFLINE;
5809fcf3ce44SJohn Forte 	iState.config_state = STMF_CONFIG_NONE;
5810fcf3ce44SJohn Forte 
5811fcf3ce44SJohn Forte 	/*
5812fcf3ce44SJohn Forte 	 * Open control node for stmf
5813fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5814fcf3ce44SJohn Forte 	 */
5815fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5816fcf3ce44SJohn Forte 		return (ret);
5817fcf3ce44SJohn Forte 	ret = setStmfState(fd, &iState, STMF_SERVICE_TYPE);
5818fcf3ce44SJohn Forte 	(void) close(fd);
5819fcf3ce44SJohn Forte 	return (ret);
5820fcf3ce44SJohn Forte }
5821fcf3ce44SJohn Forte 
5822fcf3ce44SJohn Forte 
5823fcf3ce44SJohn Forte /*
5824fcf3ce44SJohn Forte  * stmfOfflineTarget
5825fcf3ce44SJohn Forte  *
5826fcf3ce44SJohn Forte  * Purpose: Change state of target to offline
5827fcf3ce44SJohn Forte  *
5828fcf3ce44SJohn Forte  * devid - devid of the target to offline
5829fcf3ce44SJohn Forte  */
5830fcf3ce44SJohn Forte int
5831fcf3ce44SJohn Forte stmfOfflineTarget(stmfDevid *devid)
5832fcf3ce44SJohn Forte {
5833fcf3ce44SJohn Forte 	stmf_state_desc_t targetState;
5834fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5835fcf3ce44SJohn Forte 	int fd;
5836fcf3ce44SJohn Forte 
5837fcf3ce44SJohn Forte 	if (devid == NULL) {
5838fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5839fcf3ce44SJohn Forte 	}
5840fcf3ce44SJohn Forte 	bzero(&targetState, sizeof (targetState));
5841fcf3ce44SJohn Forte 
5842fcf3ce44SJohn Forte 	targetState.state = STMF_STATE_OFFLINE;
5843fcf3ce44SJohn Forte 	targetState.ident[IDENT_LENGTH_BYTE] = devid->identLength;
5844fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetState.ident[IDENT_LENGTH_BYTE + 1],
5845fcf3ce44SJohn Forte 	    devid->identLength);
5846fcf3ce44SJohn Forte 	/*
5847fcf3ce44SJohn Forte 	 * Open control node for stmf
5848fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5849fcf3ce44SJohn Forte 	 */
5850fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5851fcf3ce44SJohn Forte 		return (ret);
5852fcf3ce44SJohn Forte 	ret = setStmfState(fd, &targetState, TARGET_TYPE);
5853fcf3ce44SJohn Forte 	(void) close(fd);
5854fcf3ce44SJohn Forte 	return (ret);
5855fcf3ce44SJohn Forte }
5856fcf3ce44SJohn Forte 
5857fcf3ce44SJohn Forte /*
5858fcf3ce44SJohn Forte  * stmfOfflineLogicalUnit
5859fcf3ce44SJohn Forte  *
5860fcf3ce44SJohn Forte  * Purpose: Change state of logical unit to offline
5861fcf3ce44SJohn Forte  *
5862fcf3ce44SJohn Forte  * lu - guid of the logical unit to offline
5863fcf3ce44SJohn Forte  */
5864fcf3ce44SJohn Forte int
5865fcf3ce44SJohn Forte stmfOfflineLogicalUnit(stmfGuid *lu)
5866fcf3ce44SJohn Forte {
5867fcf3ce44SJohn Forte 	stmf_state_desc_t luState;
5868fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5869fcf3ce44SJohn Forte 	int fd;
5870fcf3ce44SJohn Forte 
5871fcf3ce44SJohn Forte 	if (lu == NULL) {
5872fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5873fcf3ce44SJohn Forte 	}
5874fcf3ce44SJohn Forte 
5875fcf3ce44SJohn Forte 	bzero(&luState, sizeof (luState));
5876fcf3ce44SJohn Forte 
5877fcf3ce44SJohn Forte 	luState.state = STMF_STATE_OFFLINE;
5878fcf3ce44SJohn Forte 	bcopy(lu, &luState.ident, sizeof (stmfGuid));
5879fcf3ce44SJohn Forte 	/*
5880fcf3ce44SJohn Forte 	 * Open control node for stmf
5881fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5882fcf3ce44SJohn Forte 	 */
5883fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5884fcf3ce44SJohn Forte 		return (ret);
5885fcf3ce44SJohn Forte 	ret = setStmfState(fd, &luState, LOGICAL_UNIT_TYPE);
5886fcf3ce44SJohn Forte 	(void) close(fd);
5887fcf3ce44SJohn Forte 	return (ret);
5888fcf3ce44SJohn Forte }
5889fcf3ce44SJohn Forte 
5890fcf3ce44SJohn Forte /*
5891fcf3ce44SJohn Forte  * stmfOnlineTarget
5892fcf3ce44SJohn Forte  *
5893fcf3ce44SJohn Forte  * Purpose: Change state of target to online
5894fcf3ce44SJohn Forte  *
5895fcf3ce44SJohn Forte  * devid - devid of the target to online
5896fcf3ce44SJohn Forte  */
5897fcf3ce44SJohn Forte int
5898fcf3ce44SJohn Forte stmfOnlineTarget(stmfDevid *devid)
5899fcf3ce44SJohn Forte {
5900fcf3ce44SJohn Forte 	stmf_state_desc_t targetState;
5901fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5902fcf3ce44SJohn Forte 	int fd;
5903fcf3ce44SJohn Forte 
5904fcf3ce44SJohn Forte 	if (devid == NULL) {
5905fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5906fcf3ce44SJohn Forte 	}
5907fcf3ce44SJohn Forte 	bzero(&targetState, sizeof (targetState));
5908fcf3ce44SJohn Forte 
5909fcf3ce44SJohn Forte 	targetState.state = STMF_STATE_ONLINE;
5910fcf3ce44SJohn Forte 	targetState.ident[IDENT_LENGTH_BYTE] = devid->identLength;
5911fcf3ce44SJohn Forte 	bcopy(&(devid->ident), &targetState.ident[IDENT_LENGTH_BYTE + 1],
5912fcf3ce44SJohn Forte 	    devid->identLength);
5913fcf3ce44SJohn Forte 	/*
5914fcf3ce44SJohn Forte 	 * Open control node for stmf
5915fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5916fcf3ce44SJohn Forte 	 */
5917fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5918fcf3ce44SJohn Forte 		return (ret);
5919fcf3ce44SJohn Forte 	ret = setStmfState(fd, &targetState, TARGET_TYPE);
5920fcf3ce44SJohn Forte 	(void) close(fd);
5921fcf3ce44SJohn Forte 	return (ret);
5922fcf3ce44SJohn Forte }
5923fcf3ce44SJohn Forte 
5924fcf3ce44SJohn Forte /*
5925fcf3ce44SJohn Forte  * stmfOnlineLogicalUnit
5926fcf3ce44SJohn Forte  *
5927fcf3ce44SJohn Forte  * Purpose: Change state of logical unit to online
5928fcf3ce44SJohn Forte  *
5929fcf3ce44SJohn Forte  * lu - guid of the logical unit to online
5930fcf3ce44SJohn Forte  */
5931fcf3ce44SJohn Forte int
5932fcf3ce44SJohn Forte stmfOnlineLogicalUnit(stmfGuid *lu)
5933fcf3ce44SJohn Forte {
5934fcf3ce44SJohn Forte 	stmf_state_desc_t luState;
5935fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
5936fcf3ce44SJohn Forte 	int fd;
5937fcf3ce44SJohn Forte 
5938fcf3ce44SJohn Forte 	if (lu == NULL) {
5939fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5940fcf3ce44SJohn Forte 	}
5941fcf3ce44SJohn Forte 
5942fcf3ce44SJohn Forte 	bzero(&luState, sizeof (luState));
5943fcf3ce44SJohn Forte 
5944fcf3ce44SJohn Forte 	luState.state = STMF_STATE_ONLINE;
5945fcf3ce44SJohn Forte 	bcopy(lu, &luState.ident, sizeof (stmfGuid));
5946fcf3ce44SJohn Forte 	/*
5947fcf3ce44SJohn Forte 	 * Open control node for stmf
5948fcf3ce44SJohn Forte 	 * to make call to setStmfState()
5949fcf3ce44SJohn Forte 	 */
5950fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_EXCL_STMF, &fd)) != STMF_STATUS_SUCCESS)
5951fcf3ce44SJohn Forte 		return (ret);
5952fcf3ce44SJohn Forte 	ret = setStmfState(fd, &luState, LOGICAL_UNIT_TYPE);
5953fcf3ce44SJohn Forte 	(void) close(fd);
5954fcf3ce44SJohn Forte 	return (ret);
5955fcf3ce44SJohn Forte }
5956fcf3ce44SJohn Forte 
5957fcf3ce44SJohn Forte /*
5958fcf3ce44SJohn Forte  * stmfRemoveFromHostGroup
5959fcf3ce44SJohn Forte  *
5960fcf3ce44SJohn Forte  * Purpose: Removes an initiator from an initiator group
5961fcf3ce44SJohn Forte  *
5962fcf3ce44SJohn Forte  * hostGroupName - name of an initiator group
5963fcf3ce44SJohn Forte  * hostName - name of host group member to remove
5964fcf3ce44SJohn Forte  */
5965fcf3ce44SJohn Forte int
5966fcf3ce44SJohn Forte stmfRemoveFromHostGroup(stmfGroupName *hostGroupName, stmfDevid *hostName)
5967fcf3ce44SJohn Forte {
5968fcf3ce44SJohn Forte 	int ret;
5969fcf3ce44SJohn Forte 	int fd;
5970fcf3ce44SJohn Forte 
5971fcf3ce44SJohn Forte 	if (hostGroupName == NULL ||
5972fcf3ce44SJohn Forte 	    (strnlen((char *)hostGroupName, sizeof (stmfGroupName))
5973fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || hostName == NULL) {
5974fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
5975fcf3ce44SJohn Forte 	}
5976fcf3ce44SJohn Forte 
5977fcf3ce44SJohn Forte 	/* call init */
5978fcf3ce44SJohn Forte 	ret = initializeConfig();
5979fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
5980fcf3ce44SJohn Forte 		return (ret);
5981fcf3ce44SJohn Forte 	}
5982fcf3ce44SJohn Forte 
5983fcf3ce44SJohn Forte 	/*
5984fcf3ce44SJohn Forte 	 * Open control node for stmf
5985fcf3ce44SJohn Forte 	 */
5986fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
5987fcf3ce44SJohn Forte 		return (ret);
5988fcf3ce44SJohn Forte 
5989fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_REMOVE_HG_ENTRY,
5990fcf3ce44SJohn Forte 	    hostGroupName, hostName)) != STMF_STATUS_SUCCESS) {
5991fcf3ce44SJohn Forte 		goto done;
5992fcf3ce44SJohn Forte 	}
5993fcf3ce44SJohn Forte 
59948fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
59958fe96085Stim szeto 		goto done;
59968fe96085Stim szeto 	}
59978fe96085Stim szeto 
5998fcf3ce44SJohn Forte 	ret = psRemoveHostGroupMember((char *)hostGroupName,
5999fcf3ce44SJohn Forte 	    (char *)hostName->ident);
6000fcf3ce44SJohn Forte 	switch (ret) {
6001fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
6002fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
6003fcf3ce44SJohn Forte 			break;
6004fcf3ce44SJohn Forte 		case STMF_PS_ERROR_MEMBER_NOT_FOUND:
6005fcf3ce44SJohn Forte 			ret = STMF_ERROR_MEMBER_NOT_FOUND;
6006fcf3ce44SJohn Forte 			break;
6007fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
6008fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
6009fcf3ce44SJohn Forte 			break;
6010fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
6011fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
6012fcf3ce44SJohn Forte 			break;
6013fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
6014fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
6015fcf3ce44SJohn Forte 			break;
6016fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
6017fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
6018fcf3ce44SJohn Forte 			break;
6019fcf3ce44SJohn Forte 		default:
6020fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
6021fcf3ce44SJohn Forte 			    "stmfRemoveFromHostGroup"
6022fcf3ce44SJohn Forte 			    "psRemoveHostGroupMember:error(%d)", ret);
6023fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
6024fcf3ce44SJohn Forte 			break;
6025fcf3ce44SJohn Forte 	}
6026fcf3ce44SJohn Forte 
6027fcf3ce44SJohn Forte done:
6028fcf3ce44SJohn Forte 	(void) close(fd);
6029fcf3ce44SJohn Forte 	return (ret);
6030fcf3ce44SJohn Forte }
6031fcf3ce44SJohn Forte 
6032fcf3ce44SJohn Forte /*
6033fcf3ce44SJohn Forte  * stmfRemoveFromTargetGroup
6034fcf3ce44SJohn Forte  *
6035fcf3ce44SJohn Forte  * Purpose: Removes a local port from a local port group
6036fcf3ce44SJohn Forte  *
6037fcf3ce44SJohn Forte  * targetGroupName - name of a target group
6038fcf3ce44SJohn Forte  * targetName - name of target to remove
6039fcf3ce44SJohn Forte  */
6040fcf3ce44SJohn Forte int
6041fcf3ce44SJohn Forte stmfRemoveFromTargetGroup(stmfGroupName *targetGroupName, stmfDevid *targetName)
6042fcf3ce44SJohn Forte {
6043fcf3ce44SJohn Forte 	int ret;
6044fcf3ce44SJohn Forte 	int fd;
6045fcf3ce44SJohn Forte 
6046fcf3ce44SJohn Forte 	if (targetGroupName == NULL ||
6047fcf3ce44SJohn Forte 	    (strnlen((char *)targetGroupName, sizeof (stmfGroupName))
6048fcf3ce44SJohn Forte 	    == sizeof (stmfGroupName)) || targetName == NULL) {
6049fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
6050fcf3ce44SJohn Forte 	}
6051fcf3ce44SJohn Forte 
6052fcf3ce44SJohn Forte 	/* call init */
6053fcf3ce44SJohn Forte 	ret = initializeConfig();
6054fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
6055fcf3ce44SJohn Forte 		return (ret);
6056fcf3ce44SJohn Forte 	}
6057fcf3ce44SJohn Forte 
6058fcf3ce44SJohn Forte 	/*
6059fcf3ce44SJohn Forte 	 * Open control node for stmf
6060fcf3ce44SJohn Forte 	 */
6061fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
6062fcf3ce44SJohn Forte 		return (ret);
6063fcf3ce44SJohn Forte 
6064fcf3ce44SJohn Forte 	if ((ret = groupMemberIoctl(fd, STMF_IOCTL_REMOVE_TG_ENTRY,
6065fcf3ce44SJohn Forte 	    targetGroupName, targetName)) != STMF_STATUS_SUCCESS) {
6066fcf3ce44SJohn Forte 		goto done;
6067fcf3ce44SJohn Forte 	}
6068fcf3ce44SJohn Forte 
60698fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
60708fe96085Stim szeto 		goto done;
60718fe96085Stim szeto 	}
60728fe96085Stim szeto 
6073fcf3ce44SJohn Forte 	ret = psRemoveTargetGroupMember((char *)targetGroupName,
6074fcf3ce44SJohn Forte 	    (char *)targetName->ident);
6075fcf3ce44SJohn Forte 	switch (ret) {
6076fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
6077fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
6078fcf3ce44SJohn Forte 			break;
6079fcf3ce44SJohn Forte 		case STMF_PS_ERROR_MEMBER_NOT_FOUND:
6080fcf3ce44SJohn Forte 			ret = STMF_ERROR_MEMBER_NOT_FOUND;
6081fcf3ce44SJohn Forte 			break;
6082fcf3ce44SJohn Forte 		case STMF_PS_ERROR_GROUP_NOT_FOUND:
6083fcf3ce44SJohn Forte 			ret = STMF_ERROR_GROUP_NOT_FOUND;
6084fcf3ce44SJohn Forte 			break;
6085fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
6086fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
6087fcf3ce44SJohn Forte 			break;
6088fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
6089fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
6090fcf3ce44SJohn Forte 			break;
6091fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
6092fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
6093fcf3ce44SJohn Forte 			break;
6094fcf3ce44SJohn Forte 		default:
6095fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
6096fcf3ce44SJohn Forte 			    "stmfRemoveFromTargetGroup"
6097fcf3ce44SJohn Forte 			    "psRemoveTargetGroupMember:error(%d)", ret);
6098fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
6099fcf3ce44SJohn Forte 			break;
6100fcf3ce44SJohn Forte 	}
6101fcf3ce44SJohn Forte 
6102fcf3ce44SJohn Forte done:
6103fcf3ce44SJohn Forte 	(void) close(fd);
6104fcf3ce44SJohn Forte 	return (ret);
6105fcf3ce44SJohn Forte }
6106fcf3ce44SJohn Forte 
6107fcf3ce44SJohn Forte /*
6108fcf3ce44SJohn Forte  * stmfRemoveViewEntry
6109fcf3ce44SJohn Forte  *
6110fcf3ce44SJohn Forte  * Purpose: Removes a view entry from a logical unit
6111fcf3ce44SJohn Forte  *
6112fcf3ce44SJohn Forte  * lu - guid of lu for which view entry is being removed
6113fcf3ce44SJohn Forte  * viewEntryIndex - index of view entry to remove
6114fcf3ce44SJohn Forte  *
6115fcf3ce44SJohn Forte  */
6116fcf3ce44SJohn Forte int
6117fcf3ce44SJohn Forte stmfRemoveViewEntry(stmfGuid *lu, uint32_t viewEntryIndex)
6118fcf3ce44SJohn Forte {
6119fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
6120fcf3ce44SJohn Forte 	int fd;
6121fcf3ce44SJohn Forte 	int ioctlRet;
6122fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
6123fcf3ce44SJohn Forte 	stmf_view_op_entry_t ioctlViewEntry;
6124fcf3ce44SJohn Forte 
6125fcf3ce44SJohn Forte 	if (lu == NULL) {
6126fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
6127fcf3ce44SJohn Forte 	}
6128fcf3ce44SJohn Forte 
6129fcf3ce44SJohn Forte 	/* call init */
6130fcf3ce44SJohn Forte 	ret = initializeConfig();
6131fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
6132fcf3ce44SJohn Forte 		return (ret);
6133fcf3ce44SJohn Forte 	}
6134fcf3ce44SJohn Forte 
6135fcf3ce44SJohn Forte 	/*
6136fcf3ce44SJohn Forte 	 * Open control node for stmf
6137fcf3ce44SJohn Forte 	 */
6138fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
6139fcf3ce44SJohn Forte 		return (ret);
6140fcf3ce44SJohn Forte 
6141fcf3ce44SJohn Forte 	bzero(&ioctlViewEntry, sizeof (ioctlViewEntry));
6142fcf3ce44SJohn Forte 	ioctlViewEntry.ve_ndx_valid = B_TRUE;
6143fcf3ce44SJohn Forte 	ioctlViewEntry.ve_ndx = viewEntryIndex;
6144fcf3ce44SJohn Forte 	bcopy(lu, &ioctlViewEntry.ve_guid, sizeof (stmfGuid));
6145fcf3ce44SJohn Forte 
6146fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
6147fcf3ce44SJohn Forte 	/*
6148fcf3ce44SJohn Forte 	 * Issue ioctl to add to the view entry
6149fcf3ce44SJohn Forte 	 */
6150fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
6151fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = sizeof (ioctlViewEntry);
6152fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ioctlViewEntry;
6153fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_REMOVE_VIEW_ENTRY, &stmfIoctl);
6154fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
6155fcf3ce44SJohn Forte 		switch (errno) {
6156fcf3ce44SJohn Forte 			case EBUSY:
6157fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
6158fcf3ce44SJohn Forte 				break;
61598fe96085Stim szeto 			case EPERM:
61608fe96085Stim szeto 				ret = STMF_ERROR_PERM;
61618fe96085Stim szeto 				break;
6162fcf3ce44SJohn Forte 			case EACCES:
6163fcf3ce44SJohn Forte 				switch (stmfIoctl.stmf_error) {
6164fcf3ce44SJohn Forte 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
6165fcf3ce44SJohn Forte 						ret = STMF_ERROR_CONFIG_NONE;
6166fcf3ce44SJohn Forte 						break;
6167fcf3ce44SJohn Forte 					default:
6168fcf3ce44SJohn Forte 						ret = STMF_ERROR_PERM;
6169fcf3ce44SJohn Forte 						break;
6170fcf3ce44SJohn Forte 				}
6171fcf3ce44SJohn Forte 				break;
6172fcf3ce44SJohn Forte 			case ENODEV:
6173fcf3ce44SJohn Forte 			case ENOENT:
6174fcf3ce44SJohn Forte 				ret = STMF_ERROR_NOT_FOUND;
6175fcf3ce44SJohn Forte 				break;
6176fcf3ce44SJohn Forte 			default:
6177fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
6178fcf3ce44SJohn Forte 				    "stmfRemoveViewEntry:ioctl errno(%d)",
6179fcf3ce44SJohn Forte 				    errno);
6180fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
6181fcf3ce44SJohn Forte 				break;
6182fcf3ce44SJohn Forte 		}
6183fcf3ce44SJohn Forte 		goto done;
6184fcf3ce44SJohn Forte 	}
6185fcf3ce44SJohn Forte 
61868fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
61878fe96085Stim szeto 		goto done;
61888fe96085Stim szeto 	}
61898fe96085Stim szeto 
6190fcf3ce44SJohn Forte 	ret = psRemoveViewEntry(lu, viewEntryIndex);
6191fcf3ce44SJohn Forte 	switch (ret) {
6192fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
6193fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
6194fcf3ce44SJohn Forte 			break;
6195fcf3ce44SJohn Forte 		case STMF_PS_ERROR_NOT_FOUND:
6196fcf3ce44SJohn Forte 			ret = STMF_ERROR_NOT_FOUND;
6197fcf3ce44SJohn Forte 			break;
6198fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
6199fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
6200fcf3ce44SJohn Forte 			break;
6201fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
6202fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
6203fcf3ce44SJohn Forte 			break;
6204fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
6205fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
6206fcf3ce44SJohn Forte 			break;
6207fcf3ce44SJohn Forte 		default:
6208fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
6209fcf3ce44SJohn Forte 			    "stmfRemoveViewEntry" "psRemoveViewEntry:error(%d)",
6210fcf3ce44SJohn Forte 			    ret);
6211fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
6212fcf3ce44SJohn Forte 			break;
6213fcf3ce44SJohn Forte 	}
6214fcf3ce44SJohn Forte 
6215fcf3ce44SJohn Forte done:
6216fcf3ce44SJohn Forte 	(void) close(fd);
6217fcf3ce44SJohn Forte 	return (ret);
6218fcf3ce44SJohn Forte }
6219fcf3ce44SJohn Forte 
6220fcf3ce44SJohn Forte /*
6221fcf3ce44SJohn Forte  * stmfSetProviderData
6222fcf3ce44SJohn Forte  *
6223fcf3ce44SJohn Forte  * Purpose: set the provider data
6224fcf3ce44SJohn Forte  *
6225fcf3ce44SJohn Forte  * providerName - unique name of provider
6226fcf3ce44SJohn Forte  * nvl - nvlist to set
6227fcf3ce44SJohn Forte  * providerType - type of provider for which to set data
6228fcf3ce44SJohn Forte  *		STMF_LU_PROVIDER_TYPE
6229fcf3ce44SJohn Forte  *		STMF_PORT_PROVIDER_TYPE
6230fcf3ce44SJohn Forte  */
6231fcf3ce44SJohn Forte int
6232fcf3ce44SJohn Forte stmfSetProviderData(char *providerName, nvlist_t *nvl, int providerType)
6233fcf3ce44SJohn Forte {
6234fcf3ce44SJohn Forte 	return (stmfSetProviderDataProt(providerName, nvl, providerType,
6235fcf3ce44SJohn Forte 	    NULL));
6236fcf3ce44SJohn Forte }
6237fcf3ce44SJohn Forte 
6238fcf3ce44SJohn Forte /*
6239fcf3ce44SJohn Forte  * stmfSetProviderDataProt
6240fcf3ce44SJohn Forte  *
6241fcf3ce44SJohn Forte  * Purpose: set the provider data
6242fcf3ce44SJohn Forte  *
6243fcf3ce44SJohn Forte  * providerName - unique name of provider
6244fcf3ce44SJohn Forte  * nvl - nvlist to set
6245fcf3ce44SJohn Forte  * providerType - type of provider for which to set data
6246fcf3ce44SJohn Forte  *		STMF_LU_PROVIDER_TYPE
6247fcf3ce44SJohn Forte  *		STMF_PORT_PROVIDER_TYPE
6248fcf3ce44SJohn Forte  * setToken - Stale data token returned in the stmfGetProviderDataProt()
6249fcf3ce44SJohn Forte  *	      call or NULL.
6250fcf3ce44SJohn Forte  */
6251fcf3ce44SJohn Forte int
6252fcf3ce44SJohn Forte stmfSetProviderDataProt(char *providerName, nvlist_t *nvl, int providerType,
6253fcf3ce44SJohn Forte     uint64_t *setToken)
6254fcf3ce44SJohn Forte {
6255fcf3ce44SJohn Forte 	int ret;
6256fcf3ce44SJohn Forte 	int fd;
6257fcf3ce44SJohn Forte 
6258fcf3ce44SJohn Forte 	if (providerName == NULL || nvl == NULL) {
6259fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
6260fcf3ce44SJohn Forte 	}
6261fcf3ce44SJohn Forte 
6262fcf3ce44SJohn Forte 	if (providerType != STMF_LU_PROVIDER_TYPE &&
6263fcf3ce44SJohn Forte 	    providerType != STMF_PORT_PROVIDER_TYPE) {
6264fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
6265fcf3ce44SJohn Forte 	}
6266fcf3ce44SJohn Forte 
6267fcf3ce44SJohn Forte 	/* call init */
6268fcf3ce44SJohn Forte 	ret = initializeConfig();
6269fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
6270fcf3ce44SJohn Forte 		return (ret);
6271fcf3ce44SJohn Forte 	}
6272fcf3ce44SJohn Forte 
6273fcf3ce44SJohn Forte 	/*
6274fcf3ce44SJohn Forte 	 * Open control node for stmf
6275fcf3ce44SJohn Forte 	 */
6276fcf3ce44SJohn Forte 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
6277fcf3ce44SJohn Forte 		return (ret);
6278fcf3ce44SJohn Forte 
62798fe96085Stim szeto 	ret = setProviderData(fd, providerName, nvl, providerType, setToken);
6280fcf3ce44SJohn Forte 
6281fcf3ce44SJohn Forte 	(void) close(fd);
6282fcf3ce44SJohn Forte 
6283fcf3ce44SJohn Forte 	if (ret != STMF_STATUS_SUCCESS) {
6284fcf3ce44SJohn Forte 		goto done;
6285fcf3ce44SJohn Forte 	}
6286fcf3ce44SJohn Forte 
62878fe96085Stim szeto 	if (iGetPersistMethod() == STMF_PERSIST_NONE) {
62888fe96085Stim szeto 		goto done;
62898fe96085Stim szeto 	}
62908fe96085Stim szeto 
6291fcf3ce44SJohn Forte 	/* setting driver provider data successful. Now persist it */
62928fe96085Stim szeto 	ret = psSetProviderData(providerName, nvl, providerType, NULL);
6293fcf3ce44SJohn Forte 	switch (ret) {
6294fcf3ce44SJohn Forte 		case STMF_PS_SUCCESS:
6295fcf3ce44SJohn Forte 			ret = STMF_STATUS_SUCCESS;
6296fcf3ce44SJohn Forte 			break;
6297fcf3ce44SJohn Forte 		case STMF_PS_ERROR_EXISTS:
6298fcf3ce44SJohn Forte 			ret = STMF_ERROR_EXISTS;
6299fcf3ce44SJohn Forte 			break;
6300fcf3ce44SJohn Forte 		case STMF_PS_ERROR_BUSY:
6301fcf3ce44SJohn Forte 			ret = STMF_ERROR_BUSY;
6302fcf3ce44SJohn Forte 			break;
6303fcf3ce44SJohn Forte 		case STMF_PS_ERROR_SERVICE_NOT_FOUND:
6304fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_NOT_FOUND;
6305fcf3ce44SJohn Forte 			break;
6306fcf3ce44SJohn Forte 		case STMF_PS_ERROR_VERSION_MISMATCH:
6307fcf3ce44SJohn Forte 			ret = STMF_ERROR_SERVICE_DATA_VERSION;
6308fcf3ce44SJohn Forte 			break;
6309fcf3ce44SJohn Forte 		case STMF_PS_ERROR_PROV_DATA_STALE:
6310fcf3ce44SJohn Forte 			ret = STMF_ERROR_PROV_DATA_STALE;
6311fcf3ce44SJohn Forte 			break;
6312fcf3ce44SJohn Forte 		default:
6313fcf3ce44SJohn Forte 			syslog(LOG_DEBUG,
6314fcf3ce44SJohn Forte 			    "stmfSetProviderData"
6315fcf3ce44SJohn Forte 			    "psSetProviderData:error(%d)", ret);
6316fcf3ce44SJohn Forte 			ret = STMF_STATUS_ERROR;
6317fcf3ce44SJohn Forte 			break;
6318fcf3ce44SJohn Forte 	}
6319fcf3ce44SJohn Forte 
6320fcf3ce44SJohn Forte done:
6321fcf3ce44SJohn Forte 	return (ret);
6322fcf3ce44SJohn Forte }
6323fcf3ce44SJohn Forte 
6324fcf3ce44SJohn Forte /*
63258fe96085Stim szeto  * getProviderData
63268fe96085Stim szeto  *
63278fe96085Stim szeto  * Purpose: set the provider data from stmf
63288fe96085Stim szeto  *
63298fe96085Stim szeto  * providerName - unique name of provider
63308fe96085Stim szeto  * nvl - nvlist to load/retrieve
63318fe96085Stim szeto  * providerType - logical unit or port provider
63328fe96085Stim szeto  * setToken - returned stale data token
63338fe96085Stim szeto  */
63348fe96085Stim szeto int
63358fe96085Stim szeto getProviderData(char *providerName, nvlist_t **nvl, int providerType,
63368fe96085Stim szeto     uint64_t *setToken)
63378fe96085Stim szeto {
63388fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
63398fe96085Stim szeto 	int fd;
63408fe96085Stim szeto 	int ioctlRet;
63418fe96085Stim szeto 	size_t nvlistSize = ALLOC_PP_DATA_SIZE;
63428fe96085Stim szeto 	int retryCnt = 0;
63438fe96085Stim szeto 	int retryCntMax = MAX_PROVIDER_RETRY;
63448fe96085Stim szeto 	stmf_ppioctl_data_t ppi = {0}, *ppi_out = NULL;
63458fe96085Stim szeto 	boolean_t retry = B_TRUE;
63468fe96085Stim szeto 	stmf_iocdata_t stmfIoctl;
63478fe96085Stim szeto 
63488fe96085Stim szeto 	if (providerName == NULL) {
63498fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
63508fe96085Stim szeto 	}
63518fe96085Stim szeto 
63528fe96085Stim szeto 	/*
63538fe96085Stim szeto 	 * Open control node for stmf
63548fe96085Stim szeto 	 */
63558fe96085Stim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
63568fe96085Stim szeto 		return (ret);
63578fe96085Stim szeto 
63588fe96085Stim szeto 	/* set provider name and provider type */
63598fe96085Stim szeto 	if (strlcpy(ppi.ppi_name, providerName,
63608fe96085Stim szeto 	    sizeof (ppi.ppi_name)) >=
63618fe96085Stim szeto 	    sizeof (ppi.ppi_name)) {
63628fe96085Stim szeto 		ret = STMF_ERROR_INVALID_ARG;
63638fe96085Stim szeto 		goto done;
63648fe96085Stim szeto 	}
63658fe96085Stim szeto 	switch (providerType) {
63668fe96085Stim szeto 		case STMF_LU_PROVIDER_TYPE:
63678fe96085Stim szeto 			ppi.ppi_lu_provider = 1;
63688fe96085Stim szeto 			break;
63698fe96085Stim szeto 		case STMF_PORT_PROVIDER_TYPE:
63708fe96085Stim szeto 			ppi.ppi_port_provider = 1;
63718fe96085Stim szeto 			break;
63728fe96085Stim szeto 		default:
63738fe96085Stim szeto 			ret = STMF_ERROR_INVALID_ARG;
63748fe96085Stim szeto 			goto done;
63758fe96085Stim szeto 	}
63768fe96085Stim szeto 
63778fe96085Stim szeto 	do {
63788fe96085Stim szeto 		/* allocate memory for ioctl */
63798fe96085Stim szeto 		ppi_out = (stmf_ppioctl_data_t *)calloc(1, nvlistSize +
63808fe96085Stim szeto 		    sizeof (stmf_ppioctl_data_t));
63818fe96085Stim szeto 		if (ppi_out == NULL) {
63828fe96085Stim szeto 			ret = STMF_ERROR_NOMEM;
63838fe96085Stim szeto 			goto done;
63848fe96085Stim szeto 
63858fe96085Stim szeto 		}
63868fe96085Stim szeto 
63878fe96085Stim szeto 		/* set the size of the ioctl data to allocated buffer */
63888fe96085Stim szeto 		ppi.ppi_data_size = nvlistSize;
63898fe96085Stim szeto 
63908fe96085Stim szeto 		bzero(&stmfIoctl, sizeof (stmfIoctl));
63918fe96085Stim szeto 
63928fe96085Stim szeto 		stmfIoctl.stmf_version = STMF_VERSION_1;
63938fe96085Stim szeto 		stmfIoctl.stmf_ibuf_size = sizeof (stmf_ppioctl_data_t);
63948fe96085Stim szeto 		stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ppi;
63958fe96085Stim szeto 		stmfIoctl.stmf_obuf_size = sizeof (stmf_ppioctl_data_t) +
63968fe96085Stim szeto 		    nvlistSize;
63978fe96085Stim szeto 		stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)ppi_out;
63988fe96085Stim szeto 		ioctlRet = ioctl(fd, STMF_IOCTL_GET_PP_DATA, &stmfIoctl);
63998fe96085Stim szeto 		if (ioctlRet != 0) {
64008fe96085Stim szeto 			switch (errno) {
64018fe96085Stim szeto 				case EBUSY:
64028fe96085Stim szeto 					ret = STMF_ERROR_BUSY;
64038fe96085Stim szeto 					break;
64048fe96085Stim szeto 				case EPERM:
64058fe96085Stim szeto 				case EACCES:
64068fe96085Stim szeto 					ret = STMF_ERROR_PERM;
64078fe96085Stim szeto 					break;
64088fe96085Stim szeto 				case EINVAL:
64098fe96085Stim szeto 					if (stmfIoctl.stmf_error ==
64108fe96085Stim szeto 					    STMF_IOCERR_INSUFFICIENT_BUF) {
64118fe96085Stim szeto 						nvlistSize =
64128fe96085Stim szeto 						    ppi_out->ppi_data_size;
64138fe96085Stim szeto 						free(ppi_out);
64148fe96085Stim szeto 						ppi_out = NULL;
64158fe96085Stim szeto 						if (retryCnt++ > retryCntMax) {
64168fe96085Stim szeto 							retry = B_FALSE;
64178fe96085Stim szeto 							ret = STMF_ERROR_BUSY;
64188fe96085Stim szeto 						} else {
64198fe96085Stim szeto 							ret =
64208fe96085Stim szeto 							    STMF_STATUS_SUCCESS;
64218fe96085Stim szeto 						}
64228fe96085Stim szeto 					} else {
64238fe96085Stim szeto 						syslog(LOG_DEBUG,
64248fe96085Stim szeto 						    "getProviderData:ioctl"
64258fe96085Stim szeto 						    "unable to retrieve "
64268fe96085Stim szeto 						    "nvlist");
64278fe96085Stim szeto 						ret = STMF_STATUS_ERROR;
64288fe96085Stim szeto 					}
64298fe96085Stim szeto 					break;
64308fe96085Stim szeto 				case ENOENT:
64318fe96085Stim szeto 					ret = STMF_ERROR_NOT_FOUND;
64328fe96085Stim szeto 					break;
64338fe96085Stim szeto 				default:
64348fe96085Stim szeto 					syslog(LOG_DEBUG,
64358fe96085Stim szeto 					    "getProviderData:ioctl errno(%d)",
64368fe96085Stim szeto 					    errno);
64378fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
64388fe96085Stim szeto 					break;
64398fe96085Stim szeto 			}
64408fe96085Stim szeto 			if (ret != STMF_STATUS_SUCCESS)
64418fe96085Stim szeto 				goto done;
64428fe96085Stim szeto 		}
64438fe96085Stim szeto 	} while (retry && stmfIoctl.stmf_error == STMF_IOCERR_INSUFFICIENT_BUF);
64448fe96085Stim szeto 
64458fe96085Stim szeto 	if ((ret = nvlist_unpack((char *)ppi_out->ppi_data,
64468fe96085Stim szeto 	    ppi_out->ppi_data_size, nvl, 0)) != 0) {
64478fe96085Stim szeto 		ret = STMF_STATUS_ERROR;
64488fe96085Stim szeto 		goto done;
64498fe96085Stim szeto 	}
64508fe96085Stim szeto 
64518fe96085Stim szeto 	/* caller has asked for new token */
64528fe96085Stim szeto 	if (setToken) {
64538fe96085Stim szeto 		*setToken = ppi_out->ppi_token;
64548fe96085Stim szeto 	}
64558fe96085Stim szeto done:
64568fe96085Stim szeto 	free(ppi_out);
64578fe96085Stim szeto 	(void) close(fd);
64588fe96085Stim szeto 	return (ret);
64598fe96085Stim szeto }
64608fe96085Stim szeto 
64618fe96085Stim szeto /*
6462fcf3ce44SJohn Forte  * setProviderData
6463fcf3ce44SJohn Forte  *
64648fe96085Stim szeto  * Purpose: set the provider data in stmf
6465fcf3ce44SJohn Forte  *
6466fcf3ce44SJohn Forte  * providerName - unique name of provider
6467fcf3ce44SJohn Forte  * nvl - nvlist to set
6468fcf3ce44SJohn Forte  * providerType - logical unit or port provider
64698fe96085Stim szeto  * setToken - stale data token to check if not NULL
6470fcf3ce44SJohn Forte  */
6471fcf3ce44SJohn Forte static int
64728fe96085Stim szeto setProviderData(int fd, char *providerName, nvlist_t *nvl, int providerType,
64738fe96085Stim szeto     uint64_t *setToken)
6474fcf3ce44SJohn Forte {
6475fcf3ce44SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
6476fcf3ce44SJohn Forte 	int ioctlRet;
6477fcf3ce44SJohn Forte 	size_t nvlistEncodedSize;
6478fcf3ce44SJohn Forte 	stmf_ppioctl_data_t *ppi = NULL;
64798fe96085Stim szeto 	uint64_t outToken;
6480fcf3ce44SJohn Forte 	char *allocatedNvBuffer;
6481fcf3ce44SJohn Forte 	stmf_iocdata_t stmfIoctl;
6482fcf3ce44SJohn Forte 
6483fcf3ce44SJohn Forte 	if (providerName == NULL) {
6484fcf3ce44SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
6485fcf3ce44SJohn Forte 	}
6486fcf3ce44SJohn Forte 
6487fcf3ce44SJohn Forte 	/* get size of encoded nvlist */
6488fcf3ce44SJohn Forte 	if (nvlist_size(nvl, &nvlistEncodedSize, NV_ENCODE_XDR) != 0) {
6489fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
6490fcf3ce44SJohn Forte 	}
6491fcf3ce44SJohn Forte 
6492fcf3ce44SJohn Forte 	/* allocate memory for ioctl */
6493fcf3ce44SJohn Forte 	ppi = (stmf_ppioctl_data_t *)calloc(1, nvlistEncodedSize +
6494fcf3ce44SJohn Forte 	    sizeof (stmf_ppioctl_data_t));
6495fcf3ce44SJohn Forte 	if (ppi == NULL) {
6496fcf3ce44SJohn Forte 		return (STMF_ERROR_NOMEM);
6497fcf3ce44SJohn Forte 	}
6498fcf3ce44SJohn Forte 
64998fe96085Stim szeto 	if (setToken) {
65008fe96085Stim szeto 		ppi->ppi_token_valid = 1;
65018fe96085Stim szeto 		ppi->ppi_token = *setToken;
65028fe96085Stim szeto 	}
65038fe96085Stim szeto 
6504fcf3ce44SJohn Forte 	allocatedNvBuffer = (char *)&ppi->ppi_data;
6505fcf3ce44SJohn Forte 	if (nvlist_pack(nvl, &allocatedNvBuffer, &nvlistEncodedSize,
6506fcf3ce44SJohn Forte 	    NV_ENCODE_XDR, 0) != 0) {
6507fcf3ce44SJohn Forte 		return (STMF_STATUS_ERROR);
6508fcf3ce44SJohn Forte 	}
6509fcf3ce44SJohn Forte 
6510fcf3ce44SJohn Forte 	/* set provider name and provider type */
6511fcf3ce44SJohn Forte 	(void) strncpy(ppi->ppi_name, providerName, sizeof (ppi->ppi_name));
6512fcf3ce44SJohn Forte 	switch (providerType) {
6513fcf3ce44SJohn Forte 		case STMF_LU_PROVIDER_TYPE:
6514fcf3ce44SJohn Forte 			ppi->ppi_lu_provider = 1;
6515fcf3ce44SJohn Forte 			break;
6516fcf3ce44SJohn Forte 		case STMF_PORT_PROVIDER_TYPE:
6517fcf3ce44SJohn Forte 			ppi->ppi_port_provider = 1;
6518fcf3ce44SJohn Forte 			break;
6519fcf3ce44SJohn Forte 		default:
6520fcf3ce44SJohn Forte 			return (STMF_ERROR_INVALID_ARG);
6521fcf3ce44SJohn Forte 	}
6522fcf3ce44SJohn Forte 
6523fcf3ce44SJohn Forte 	/* set the size of the ioctl data to packed data size */
6524fcf3ce44SJohn Forte 	ppi->ppi_data_size = nvlistEncodedSize;
6525fcf3ce44SJohn Forte 
6526fcf3ce44SJohn Forte 	bzero(&stmfIoctl, sizeof (stmfIoctl));
6527fcf3ce44SJohn Forte 
6528fcf3ce44SJohn Forte 	stmfIoctl.stmf_version = STMF_VERSION_1;
6529fcf3ce44SJohn Forte 	/*
6530fcf3ce44SJohn Forte 	 * Subtracting 8 from the size as that is the size of the last member
6531fcf3ce44SJohn Forte 	 * of the structure where the packed data resides
6532fcf3ce44SJohn Forte 	 */
6533fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf_size = nvlistEncodedSize +
6534fcf3ce44SJohn Forte 	    sizeof (stmf_ppioctl_data_t) - 8;
6535fcf3ce44SJohn Forte 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)ppi;
65368fe96085Stim szeto 	stmfIoctl.stmf_obuf_size = sizeof (uint64_t);
65378fe96085Stim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&outToken;
6538fcf3ce44SJohn Forte 	ioctlRet = ioctl(fd, STMF_IOCTL_LOAD_PP_DATA, &stmfIoctl);
6539fcf3ce44SJohn Forte 	if (ioctlRet != 0) {
6540fcf3ce44SJohn Forte 		switch (errno) {
6541fcf3ce44SJohn Forte 			case EBUSY:
6542fcf3ce44SJohn Forte 				ret = STMF_ERROR_BUSY;
6543fcf3ce44SJohn Forte 				break;
65448fe96085Stim szeto 			case EPERM:
6545fcf3ce44SJohn Forte 			case EACCES:
6546fcf3ce44SJohn Forte 				ret = STMF_ERROR_PERM;
6547fcf3ce44SJohn Forte 				break;
65488fe96085Stim szeto 			case EINVAL:
65498fe96085Stim szeto 				if (stmfIoctl.stmf_error ==
65508fe96085Stim szeto 				    STMF_IOCERR_PPD_UPDATED) {
65518fe96085Stim szeto 					ret = STMF_ERROR_PROV_DATA_STALE;
65528fe96085Stim szeto 				} else {
65538fe96085Stim szeto 					ret = STMF_STATUS_ERROR;
65548fe96085Stim szeto 				}
65558fe96085Stim szeto 				break;
6556fcf3ce44SJohn Forte 			default:
6557fcf3ce44SJohn Forte 				syslog(LOG_DEBUG,
6558fcf3ce44SJohn Forte 				    "setProviderData:ioctl errno(%d)", errno);
6559fcf3ce44SJohn Forte 				ret = STMF_STATUS_ERROR;
6560fcf3ce44SJohn Forte 				break;
6561fcf3ce44SJohn Forte 		}
6562fcf3ce44SJohn Forte 		if (ret != STMF_STATUS_SUCCESS)
6563fcf3ce44SJohn Forte 			goto done;
6564fcf3ce44SJohn Forte 	}
6565fcf3ce44SJohn Forte 
65668fe96085Stim szeto 	/* caller has asked for new token */
65678fe96085Stim szeto 	if (setToken) {
65688fe96085Stim szeto 		*setToken = outToken;
65698fe96085Stim szeto 	}
6570fcf3ce44SJohn Forte done:
6571fcf3ce44SJohn Forte 	free(ppi);
6572fcf3ce44SJohn Forte 	return (ret);
6573fcf3ce44SJohn Forte }
65748fe96085Stim szeto 
65758fe96085Stim szeto /*
65768fe96085Stim szeto  * set the persistence method in the library only or library and service
65778fe96085Stim szeto  */
65788fe96085Stim szeto int
65798fe96085Stim szeto stmfSetPersistMethod(uint8_t persistType, boolean_t serviceSet)
65808fe96085Stim szeto {
65818fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
65828fe96085Stim szeto 	int oldPersist;
65838fe96085Stim szeto 
65848fe96085Stim szeto 	(void) pthread_mutex_lock(&persistenceTypeLock);
65858fe96085Stim szeto 	oldPersist = iPersistType;
65868fe96085Stim szeto 	if (persistType == STMF_PERSIST_NONE ||
65878fe96085Stim szeto 	    persistType == STMF_PERSIST_SMF) {
65888fe96085Stim szeto 		iLibSetPersist = B_TRUE;
65898fe96085Stim szeto 		iPersistType = persistType;
65908fe96085Stim szeto 	} else {
65918fe96085Stim szeto 		(void) pthread_mutex_unlock(&persistenceTypeLock);
65928fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
65938fe96085Stim szeto 	}
65948fe96085Stim szeto 	/* Is this for this library open or in SMF */
65958fe96085Stim szeto 	if (serviceSet == B_TRUE) {
65968fe96085Stim szeto 		ret = psSetServicePersist(persistType);
65978fe96085Stim szeto 		if (ret != STMF_PS_SUCCESS) {
65988fe96085Stim szeto 			ret = STMF_ERROR_PERSIST_TYPE;
65998fe96085Stim szeto 			/* Set to old value */
66008fe96085Stim szeto 			iPersistType = oldPersist;
66018fe96085Stim szeto 		}
66028fe96085Stim szeto 	}
66038fe96085Stim szeto 	(void) pthread_mutex_unlock(&persistenceTypeLock);
66048fe96085Stim szeto 
66058fe96085Stim szeto 	return (ret);
66068fe96085Stim szeto }
66078fe96085Stim szeto 
66088fe96085Stim szeto /*
66098fe96085Stim szeto  * Only returns internal state for persist. If unset, goes to ps. If that
66108fe96085Stim szeto  * fails, returns default setting
66118fe96085Stim szeto  */
66128fe96085Stim szeto static uint8_t
66138fe96085Stim szeto iGetPersistMethod()
66148fe96085Stim szeto {
66158fe96085Stim szeto 
66168fe96085Stim szeto 	uint8_t persistType = 0;
66178fe96085Stim szeto 
66188fe96085Stim szeto 	(void) pthread_mutex_lock(&persistenceTypeLock);
66198fe96085Stim szeto 	if (iLibSetPersist) {
66208fe96085Stim szeto 		persistType = iPersistType;
66218fe96085Stim szeto 	} else {
66228fe96085Stim szeto 		int ret;
66238fe96085Stim szeto 		ret = psGetServicePersist(&persistType);
66248fe96085Stim szeto 		if (ret != STMF_PS_SUCCESS) {
66258fe96085Stim szeto 			/* set to default */
66268fe96085Stim szeto 			persistType = STMF_DEFAULT_PERSIST;
66278fe96085Stim szeto 		}
66288fe96085Stim szeto 	}
66298fe96085Stim szeto 	(void) pthread_mutex_unlock(&persistenceTypeLock);
66308fe96085Stim szeto 	return (persistType);
66318fe96085Stim szeto }
66328fe96085Stim szeto 
66338fe96085Stim szeto /*
66348fe96085Stim szeto  * Returns either library state or persistent config state depending on
66358fe96085Stim szeto  * serviceState
66368fe96085Stim szeto  */
66378fe96085Stim szeto int
66388fe96085Stim szeto stmfGetPersistMethod(uint8_t *persistType, boolean_t serviceState)
66398fe96085Stim szeto {
66408fe96085Stim szeto 	int ret = STMF_STATUS_SUCCESS;
66418fe96085Stim szeto 
66428fe96085Stim szeto 	if (persistType == NULL) {
66438fe96085Stim szeto 		return (STMF_ERROR_INVALID_ARG);
66448fe96085Stim szeto 	}
66458fe96085Stim szeto 	if (serviceState) {
66468fe96085Stim szeto 		ret = psGetServicePersist(persistType);
66478fe96085Stim szeto 		if (ret != STMF_PS_SUCCESS) {
66488fe96085Stim szeto 			ret = STMF_ERROR_PERSIST_TYPE;
66498fe96085Stim szeto 		}
66508fe96085Stim szeto 	} else {
66518fe96085Stim szeto 		(void) pthread_mutex_lock(&persistenceTypeLock);
66528fe96085Stim szeto 		if (iLibSetPersist) {
66538fe96085Stim szeto 			*persistType = iPersistType;
66548fe96085Stim szeto 		} else {
66558fe96085Stim szeto 			*persistType = STMF_DEFAULT_PERSIST;
66568fe96085Stim szeto 		}
66578fe96085Stim szeto 		(void) pthread_mutex_unlock(&persistenceTypeLock);
66588fe96085Stim szeto 	}
66598fe96085Stim szeto 
66608fe96085Stim szeto 	return (ret);
66618fe96085Stim szeto }
666263ed874aStim szeto 
666363ed874aStim szeto /*
666445039663SJohn Forte  * stmfPostProxyMsg
666545039663SJohn Forte  *
666645039663SJohn Forte  * Purpose: Post a message to the proxy port provider
666745039663SJohn Forte  *
666845039663SJohn Forte  * buf - buffer containing message to post
666945039663SJohn Forte  * buflen - buffer length
667045039663SJohn Forte  */
667145039663SJohn Forte int
667245039663SJohn Forte stmfPostProxyMsg(int hdl, void *buf, uint32_t buflen)
667345039663SJohn Forte {
667445039663SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
667545039663SJohn Forte 	int ioctlRet;
667645039663SJohn Forte 	pppt_iocdata_t ppptIoctl = {0};
667745039663SJohn Forte 
667845039663SJohn Forte 	if (buf == NULL) {
667945039663SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
668045039663SJohn Forte 	}
668145039663SJohn Forte 
668245039663SJohn Forte 	/*
668345039663SJohn Forte 	 * Issue ioctl to post the message
668445039663SJohn Forte 	 */
668545039663SJohn Forte 	ppptIoctl.pppt_version = PPPT_VERSION_1;
668645039663SJohn Forte 	ppptIoctl.pppt_buf_size = buflen;
668745039663SJohn Forte 	ppptIoctl.pppt_buf = (uint64_t)(unsigned long)buf;
668845039663SJohn Forte 	ioctlRet = ioctl(hdl, PPPT_MESSAGE, &ppptIoctl);
668945039663SJohn Forte 	if (ioctlRet != 0) {
669045039663SJohn Forte 		switch (errno) {
669145039663SJohn Forte 			case EPERM:
669245039663SJohn Forte 			case EACCES:
669345039663SJohn Forte 				ret = STMF_ERROR_PERM;
669445039663SJohn Forte 				break;
669545039663SJohn Forte 			default:
669645039663SJohn Forte 				ret = STMF_ERROR_POST_MSG_FAILED;
669745039663SJohn Forte 				break;
669845039663SJohn Forte 		}
669945039663SJohn Forte 	}
670045039663SJohn Forte 
670145039663SJohn Forte 	return (ret);
670245039663SJohn Forte }
670345039663SJohn Forte 
670445039663SJohn Forte /*
670545039663SJohn Forte  * stmfInitProxyDoor
670645039663SJohn Forte  *
670745039663SJohn Forte  * Purpose: Install door in proxy
670845039663SJohn Forte  *
670945039663SJohn Forte  * hdl - pointer to returned handle
671045039663SJohn Forte  * fd - door from door_create()
671145039663SJohn Forte  */
671245039663SJohn Forte int
671345039663SJohn Forte stmfInitProxyDoor(int *hdl, int door)
671445039663SJohn Forte {
671545039663SJohn Forte 	int ret = STMF_STATUS_SUCCESS;
671645039663SJohn Forte 	int ioctlRet;
671745039663SJohn Forte 	int fd;
671845039663SJohn Forte 	pppt_iocdata_t ppptIoctl = {0};
671945039663SJohn Forte 
672045039663SJohn Forte 	if (hdl == NULL) {
672145039663SJohn Forte 		return (STMF_ERROR_INVALID_ARG);
672245039663SJohn Forte 	}
672345039663SJohn Forte 
672445039663SJohn Forte 	/*
672545039663SJohn Forte 	 * Open control node for pppt
672645039663SJohn Forte 	 */
672745039663SJohn Forte 	if ((ret = openPppt(OPEN_PPPT, &fd)) != STMF_STATUS_SUCCESS) {
672845039663SJohn Forte 		return (ret);
672945039663SJohn Forte 	}
673045039663SJohn Forte 
673145039663SJohn Forte 	/*
673245039663SJohn Forte 	 * Issue ioctl to install the door
673345039663SJohn Forte 	 */
673445039663SJohn Forte 	ppptIoctl.pppt_version = PPPT_VERSION_1;
673545039663SJohn Forte 	ppptIoctl.pppt_door_fd = (uint32_t)door;
673645039663SJohn Forte 	ioctlRet = ioctl(fd, PPPT_INSTALL_DOOR, &ppptIoctl);
673745039663SJohn Forte 	if (ioctlRet != 0) {
673845039663SJohn Forte 		switch (errno) {
673945039663SJohn Forte 			case EPERM:
674045039663SJohn Forte 			case EACCES:
674145039663SJohn Forte 				ret = STMF_ERROR_PERM;
674245039663SJohn Forte 				break;
674345039663SJohn Forte 			case EINVAL:
674445039663SJohn Forte 				ret = STMF_ERROR_INVALID_ARG;
674545039663SJohn Forte 				break;
674645039663SJohn Forte 			case EBUSY:
674745039663SJohn Forte 				ret = STMF_ERROR_DOOR_INSTALLED;
674845039663SJohn Forte 				break;
674945039663SJohn Forte 			default:
675045039663SJohn Forte 				ret = STMF_STATUS_ERROR;
675145039663SJohn Forte 				break;
675245039663SJohn Forte 		}
675345039663SJohn Forte 	}
675445039663SJohn Forte 
675545039663SJohn Forte 	/* return driver fd to caller */
675645039663SJohn Forte 	*hdl = fd;
675745039663SJohn Forte 	return (ret);
675845039663SJohn Forte }
675945039663SJohn Forte 
676045039663SJohn Forte void
676145039663SJohn Forte stmfDestroyProxyDoor(int hdl)
676245039663SJohn Forte {
676345039663SJohn Forte 	(void) close(hdl);
676445039663SJohn Forte }
676545039663SJohn Forte 
676645039663SJohn Forte /*
676763ed874aStim szeto  * validateLunNumIoctl
676863ed874aStim szeto  *
676963ed874aStim szeto  * Purpose: Issues ioctl to check and get available lun# in view entry
677063ed874aStim szeto  *
677163ed874aStim szeto  * viewEntry - view entry to use
677263ed874aStim szeto  */
677363ed874aStim szeto static int
677463ed874aStim szeto validateLunNumIoctl(int fd, stmfViewEntry *viewEntry)
677563ed874aStim szeto {
677663ed874aStim szeto 	int ret = STMF_STATUS_SUCCESS;
677763ed874aStim szeto 	int ioctlRet;
677863ed874aStim szeto 	stmf_iocdata_t stmfIoctl;
677963ed874aStim szeto 	stmf_view_op_entry_t ioctlViewEntry;
678063ed874aStim szeto 
678163ed874aStim szeto 	bzero(&ioctlViewEntry, sizeof (ioctlViewEntry));
678263ed874aStim szeto 	/*
678363ed874aStim szeto 	 * don't set ve_ndx or ve_ndx_valid as ve_ndx_valid should be
678463ed874aStim szeto 	 * false on input
678563ed874aStim szeto 	 */
678663ed874aStim szeto 	ioctlViewEntry.ve_lu_number_valid = viewEntry->luNbrValid;
678763ed874aStim szeto 	ioctlViewEntry.ve_all_hosts = viewEntry->allHosts;
678863ed874aStim szeto 	ioctlViewEntry.ve_all_targets = viewEntry->allTargets;
678963ed874aStim szeto 
679063ed874aStim szeto 	if (viewEntry->allHosts == B_FALSE) {
679163ed874aStim szeto 		bcopy(viewEntry->hostGroup, &ioctlViewEntry.ve_host_group.name,
679263ed874aStim szeto 		    sizeof (stmfGroupName));
679363ed874aStim szeto 		ioctlViewEntry.ve_host_group.name_size =
679463ed874aStim szeto 		    strlen((char *)viewEntry->hostGroup);
679563ed874aStim szeto 	}
679663ed874aStim szeto 	if (viewEntry->allTargets == B_FALSE) {
679763ed874aStim szeto 		bcopy(viewEntry->targetGroup,
679863ed874aStim szeto 		    &ioctlViewEntry.ve_target_group.name,
679963ed874aStim szeto 		    sizeof (stmfGroupName));
680063ed874aStim szeto 		ioctlViewEntry.ve_target_group.name_size =
680163ed874aStim szeto 		    strlen((char *)viewEntry->targetGroup);
680263ed874aStim szeto 	}
680363ed874aStim szeto 	/* Validating the lun number */
680463ed874aStim szeto 	if (viewEntry->luNbrValid) {
680563ed874aStim szeto 		bcopy(viewEntry->luNbr, &ioctlViewEntry.ve_lu_nbr,
680663ed874aStim szeto 		    sizeof (ioctlViewEntry.ve_lu_nbr));
680763ed874aStim szeto 	}
680863ed874aStim szeto 
680963ed874aStim szeto 	bzero(&stmfIoctl, sizeof (stmfIoctl));
681063ed874aStim szeto 	/*
681163ed874aStim szeto 	 * Issue ioctl to validate lun# in the view entry
681263ed874aStim szeto 	 */
681363ed874aStim szeto 	stmfIoctl.stmf_version = STMF_VERSION_1;
681463ed874aStim szeto 	stmfIoctl.stmf_ibuf_size = sizeof (ioctlViewEntry);
681563ed874aStim szeto 	stmfIoctl.stmf_ibuf = (uint64_t)(unsigned long)&ioctlViewEntry;
681663ed874aStim szeto 	stmfIoctl.stmf_obuf_size = sizeof (ioctlViewEntry);
681763ed874aStim szeto 	stmfIoctl.stmf_obuf = (uint64_t)(unsigned long)&ioctlViewEntry;
681863ed874aStim szeto 	ioctlRet = ioctl(fd, STMF_IOCTL_VALIDATE_VIEW, &stmfIoctl);
681963ed874aStim szeto 
682063ed874aStim szeto 	/* save available lun number */
682163ed874aStim szeto 	if (!viewEntry->luNbrValid) {
682263ed874aStim szeto 		bcopy(ioctlViewEntry.ve_lu_nbr, viewEntry->luNbr,
682363ed874aStim szeto 		    sizeof (ioctlViewEntry.ve_lu_nbr));
682463ed874aStim szeto 	}
682563ed874aStim szeto 	if (ioctlRet != 0) {
682663ed874aStim szeto 		switch (errno) {
682763ed874aStim szeto 			case EBUSY:
682863ed874aStim szeto 				ret = STMF_ERROR_BUSY;
682963ed874aStim szeto 				break;
683063ed874aStim szeto 			case EPERM:
683163ed874aStim szeto 				ret = STMF_ERROR_PERM;
683263ed874aStim szeto 				break;
683363ed874aStim szeto 			case EACCES:
683463ed874aStim szeto 				switch (stmfIoctl.stmf_error) {
683563ed874aStim szeto 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
683663ed874aStim szeto 						ret = STMF_ERROR_CONFIG_NONE;
683763ed874aStim szeto 						break;
683863ed874aStim szeto 					default:
683963ed874aStim szeto 						ret = STMF_ERROR_PERM;
684063ed874aStim szeto 						break;
684163ed874aStim szeto 				}
684263ed874aStim szeto 				break;
684363ed874aStim szeto 			default:
684463ed874aStim szeto 				switch (stmfIoctl.stmf_error) {
684563ed874aStim szeto 					case STMF_IOCERR_LU_NUMBER_IN_USE:
684663ed874aStim szeto 						ret = STMF_ERROR_LUN_IN_USE;
684763ed874aStim szeto 						break;
684863ed874aStim szeto 					case STMF_IOCERR_VIEW_ENTRY_CONFLICT:
684963ed874aStim szeto 						ret = STMF_ERROR_VE_CONFLICT;
685063ed874aStim szeto 						break;
685163ed874aStim szeto 					case STMF_IOCERR_UPDATE_NEED_CFG_INIT:
685263ed874aStim szeto 						ret = STMF_ERROR_CONFIG_NONE;
685363ed874aStim szeto 						break;
685463ed874aStim szeto 					case STMF_IOCERR_INVALID_HG:
685563ed874aStim szeto 						ret = STMF_ERROR_INVALID_HG;
685663ed874aStim szeto 						break;
685763ed874aStim szeto 					case STMF_IOCERR_INVALID_TG:
685863ed874aStim szeto 						ret = STMF_ERROR_INVALID_TG;
685963ed874aStim szeto 						break;
686063ed874aStim szeto 					default:
686163ed874aStim szeto 						syslog(LOG_DEBUG,
686263ed874aStim szeto 						    "addViewEntryIoctl"
686363ed874aStim szeto 						    ":error(%d)",
686463ed874aStim szeto 						    stmfIoctl.stmf_error);
686563ed874aStim szeto 						ret = STMF_STATUS_ERROR;
686663ed874aStim szeto 						break;
686763ed874aStim szeto 				}
686863ed874aStim szeto 				break;
686963ed874aStim szeto 		}
687063ed874aStim szeto 	}
687163ed874aStim szeto 	return (ret);
687263ed874aStim szeto }
687363ed874aStim szeto 
687463ed874aStim szeto /*
687563ed874aStim szeto  * stmfValidateView
687663ed874aStim szeto  *
687763ed874aStim szeto  * Purpose: Validate or get lun # base on TG, HG of view entry
687863ed874aStim szeto  *
687963ed874aStim szeto  * viewEntry - view entry structure to use
688063ed874aStim szeto  */
688163ed874aStim szeto int
688263ed874aStim szeto stmfValidateView(stmfViewEntry *viewEntry)
688363ed874aStim szeto {
688463ed874aStim szeto 	int ret;
688563ed874aStim szeto 	int fd;
688663ed874aStim szeto 	stmfViewEntry iViewEntry;
688763ed874aStim szeto 
688863ed874aStim szeto 	if (viewEntry == NULL) {
688963ed874aStim szeto 		return (STMF_ERROR_INVALID_ARG);
689063ed874aStim szeto 	}
689163ed874aStim szeto 
689263ed874aStim szeto 	/* initialize and set internal view entry */
689363ed874aStim szeto 	bzero(&iViewEntry, sizeof (iViewEntry));
689463ed874aStim szeto 
689563ed874aStim szeto 	if (!viewEntry->allHosts) {
689663ed874aStim szeto 		bcopy(viewEntry->hostGroup, iViewEntry.hostGroup,
689763ed874aStim szeto 		    sizeof (iViewEntry.hostGroup));
689863ed874aStim szeto 	} else {
689963ed874aStim szeto 		iViewEntry.allHosts = B_TRUE;
690063ed874aStim szeto 	}
690163ed874aStim szeto 
690263ed874aStim szeto 	if (!viewEntry->allTargets) {
690363ed874aStim szeto 		bcopy(viewEntry->targetGroup, iViewEntry.targetGroup,
690463ed874aStim szeto 		    sizeof (iViewEntry.targetGroup));
690563ed874aStim szeto 	} else {
690663ed874aStim szeto 		iViewEntry.allTargets = B_TRUE;
690763ed874aStim szeto 	}
690863ed874aStim szeto 
690963ed874aStim szeto 	if (viewEntry->luNbrValid) {
691063ed874aStim szeto 		iViewEntry.luNbrValid = B_TRUE;
691163ed874aStim szeto 		bcopy(viewEntry->luNbr, iViewEntry.luNbr,
691263ed874aStim szeto 		    sizeof (iViewEntry.luNbr));
691363ed874aStim szeto 	}
691463ed874aStim szeto 
691563ed874aStim szeto 	/*
691663ed874aStim szeto 	 * set users return view entry index valid flag to false
691763ed874aStim szeto 	 * in case of failure
691863ed874aStim szeto 	 */
691963ed874aStim szeto 	viewEntry->veIndexValid = B_FALSE;
692063ed874aStim szeto 
692163ed874aStim szeto 	/* Check to ensure service exists */
692263ed874aStim szeto 	if (psCheckService() != STMF_STATUS_SUCCESS) {
692363ed874aStim szeto 		return (STMF_ERROR_SERVICE_NOT_FOUND);
692463ed874aStim szeto 	}
692563ed874aStim szeto 
692663ed874aStim szeto 	/* call init */
692763ed874aStim szeto 	ret = initializeConfig();
692863ed874aStim szeto 	if (ret != STMF_STATUS_SUCCESS) {
692963ed874aStim szeto 		return (ret);
693063ed874aStim szeto 	}
693163ed874aStim szeto 
693263ed874aStim szeto 	/*
693363ed874aStim szeto 	 * Open control node for stmf
693463ed874aStim szeto 	 */
693563ed874aStim szeto 	if ((ret = openStmf(OPEN_STMF, &fd)) != STMF_STATUS_SUCCESS)
693663ed874aStim szeto 		return (ret);
693763ed874aStim szeto 
693863ed874aStim szeto 	/*
693963ed874aStim szeto 	 * Validate lun# in the view entry from the driver
694063ed874aStim szeto 	 */
694163ed874aStim szeto 	ret = validateLunNumIoctl(fd, &iViewEntry);
694263ed874aStim szeto 	(void) close(fd);
694363ed874aStim szeto 
694463ed874aStim szeto 	/* save available lun number */
694563ed874aStim szeto 	if (!viewEntry->luNbrValid) {
694663ed874aStim szeto 		bcopy(iViewEntry.luNbr, viewEntry->luNbr,
694763ed874aStim szeto 		    sizeof (iViewEntry.luNbr));
694863ed874aStim szeto 	}
694963ed874aStim szeto 
695063ed874aStim szeto 	return (ret);
695163ed874aStim szeto }
6952