xref: /titanic_53/usr/src/cmd/stmsboot/stmsboot_util.c (revision 815dd9170b31c7359f1b3fbe11df4da223766647)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
580ab886dSwesolows  * Common Development and Distribution License (the "License").
680ab886dSwesolows  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2180ab886dSwesolows 
227c478bd9Sstevel@tonic-gate /*
235a4c37c9Sqh201292  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <stdarg.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <stropts.h>
387c478bd9Sstevel@tonic-gate #include <strings.h>
397c478bd9Sstevel@tonic-gate #include <dirent.h>
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <sys/scsi/adapters/scsi_vhci.h>
427c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
437c478bd9Sstevel@tonic-gate #include <libgen.h>
447c478bd9Sstevel@tonic-gate #include <dlfcn.h>
457c478bd9Sstevel@tonic-gate #include <link.h>
467c478bd9Sstevel@tonic-gate #include <locale.h>
477c478bd9Sstevel@tonic-gate #include <libintl.h>
487c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
497c478bd9Sstevel@tonic-gate #include <sys/vfstab.h>
507c478bd9Sstevel@tonic-gate #include <sys/mount.h>
517c478bd9Sstevel@tonic-gate #include <devid.h>
527c478bd9Sstevel@tonic-gate #include <sys/libdevid.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	VHCI_CTL_NODE	"/devices/scsi_vhci:devctl"
557c478bd9Sstevel@tonic-gate #define	SLASH_DEVICES	"/devices/"
565a4c37c9Sqh201292 
575a4c37c9Sqh201292 #ifdef	sparc
585a4c37c9Sqh201292 #define	DISK_NODE_NAME	"ssd"
595a4c37c9Sqh201292 #define	DISK_DRV_NAME	"ssd"
605a4c37c9Sqh201292 #else	/* sparc */
615a4c37c9Sqh201292 #define	DISK_NODE_NAME	"disk"
625a4c37c9Sqh201292 #define	DISK_DRV_NAME	"sd"
635a4c37c9Sqh201292 #endif
645a4c37c9Sqh201292 
6560fffc19Sjw149990 #define	DISK_AT_G	"disk@g"
667c478bd9Sstevel@tonic-gate #define	SLASH_FP_AT	"/fp@"
677c478bd9Sstevel@tonic-gate #define	SLASH_SCSI_VHCI	"/scsi_vhci"
687c478bd9Sstevel@tonic-gate #define	DEV_DSK		"/dev/dsk/"
697c478bd9Sstevel@tonic-gate #define	DEV_RDSK	"/dev/rdsk/"
707c478bd9Sstevel@tonic-gate #define	SYS_FILENAME_LEN	256
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Save directory is the directory in which system files are saved.
747c478bd9Sstevel@tonic-gate  * Save directory must be under the root filesystem, as this program is
757c478bd9Sstevel@tonic-gate  * typically run before any other filesystems are mounted.
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate #define	SAVE_DIR	"/etc/mpxio"
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /* fcp driver publishes this property */
807c478bd9Sstevel@tonic-gate #define	NODE_WWN_PROP	"node-wwn"
817c478bd9Sstevel@tonic-gate 
8260fffc19Sjw149990 /*
8360fffc19Sjw149990  * For SAS, we look for "sas-$drivername", eg sas-mpt, but
8460fffc19Sjw149990  * we strncat the driver name later once we've parsed the
8560fffc19Sjw149990  * args passed in from the shell.
8660fffc19Sjw149990  */
8760fffc19Sjw149990 #define	SASPROP	 "sas-"
8860fffc19Sjw149990 
8960fffc19Sjw149990 
907c478bd9Sstevel@tonic-gate typedef enum {
917c478bd9Sstevel@tonic-gate 	CLIENT_TYPE_UNKNOWN,
927c478bd9Sstevel@tonic-gate 	CLIENT_TYPE_PHCI,
937c478bd9Sstevel@tonic-gate 	CLIENT_TYPE_VHCI
947c478bd9Sstevel@tonic-gate } client_type_t;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate struct devlink_cbarg {
977c478bd9Sstevel@tonic-gate 	char *devlink;
987c478bd9Sstevel@tonic-gate 	size_t len;
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static di_node_t devinfo_root = DI_NODE_NIL;
1027c478bd9Sstevel@tonic-gate static di_devlink_handle_t devlink_hdl = NULL;
1037c478bd9Sstevel@tonic-gate static int vhci_fd = -1;
1047c478bd9Sstevel@tonic-gate static int patch_vfstab, cap_m_option, debug;
1057c478bd9Sstevel@tonic-gate static int list_option, list_guid_mappings, list_controllernum = -1;
1067c478bd9Sstevel@tonic-gate static char *mapdev = "";
1075a4c37c9Sqh201292 static char *map_vhciname = "";
1087c478bd9Sstevel@tonic-gate static char *stmsboot = "stmsboot";
1097c478bd9Sstevel@tonic-gate 
11060fffc19Sjw149990 char *drvname = (char *)NULL; /* "fp" or "mpt" or ... */
11160fffc19Sjw149990 /* "node-wwn" if drvname=fp, or "sas-$drivername" otherwise */
11260fffc19Sjw149990 char *drvprop = (char *)NULL;
11360fffc19Sjw149990 static int parent = 0; /* for "-n" usage */
11460fffc19Sjw149990 
1157c478bd9Sstevel@tonic-gate static int make_temp(char *, char *, char *, size_t);
1167c478bd9Sstevel@tonic-gate static void commit_change(char *, char *, char *, int);
1177c478bd9Sstevel@tonic-gate static int map_devname(char *, char *, size_t, int);
1187c478bd9Sstevel@tonic-gate static int update_vfstab(char *, char *);
1197c478bd9Sstevel@tonic-gate static int list_mappings(int, int);
1207c478bd9Sstevel@tonic-gate static int canopen(char *);
12160fffc19Sjw149990 static client_type_t client_by_props(char *path);
12260fffc19Sjw149990 static void list_nodes(char *drivername);
12360fffc19Sjw149990 
1247c478bd9Sstevel@tonic-gate static void logerr(char *, ...);
1257c478bd9Sstevel@tonic-gate static void logdmsg(char *, ...);
1267c478bd9Sstevel@tonic-gate static void *s_malloc(const size_t);
1277c478bd9Sstevel@tonic-gate static char *s_strdup(const char *);
1287c478bd9Sstevel@tonic-gate static void s_strlcpy(char *, const char *, size_t);
129cfcc3aa4Sqh201292 static int map_openable_vhciname(char *, char *, size_t);
130264d6c47Seota /*
131264d6c47Seota  * Using an exit function not marked __NORETURN causes a warning with gcc.
132264d6c47Seota  * To suppress the warning, use __NORETURN attribute.
133264d6c47Seota  */
134264d6c47Seota static void clean_exit(int)__NORETURN;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Print usage and exit.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate static void
1407c478bd9Sstevel@tonic-gate usage(char *argv0)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	char *progname;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	progname = strrchr(argv0, '/');
1457c478bd9Sstevel@tonic-gate 	if (progname != NULL)
1467c478bd9Sstevel@tonic-gate 		progname++;
1477c478bd9Sstevel@tonic-gate 	else
1487c478bd9Sstevel@tonic-gate 		progname = argv0;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/*
1517c478bd9Sstevel@tonic-gate 	 * -u	update /etc/vfstab
1527c478bd9Sstevel@tonic-gate 	 * -m devname
1537c478bd9Sstevel@tonic-gate 	 *	if devname is phci based name and not open-able, map it to
1547c478bd9Sstevel@tonic-gate 	 *	vhci based /devices name.
1557c478bd9Sstevel@tonic-gate 	 *	if devname is vhci based name and not open-able, map it to
1567c478bd9Sstevel@tonic-gate 	 *	phci based /devices name.
1577c478bd9Sstevel@tonic-gate 	 * -M devname
1587c478bd9Sstevel@tonic-gate 	 *	same as -m except that /dev link is printed instead of
1597c478bd9Sstevel@tonic-gate 	 *	/devices name.
1607c478bd9Sstevel@tonic-gate 	 * -l controller
1617c478bd9Sstevel@tonic-gate 	 *	list non-STMS to STMS device name mappings for the specific
1627c478bd9Sstevel@tonic-gate 	 *	controller
1637c478bd9Sstevel@tonic-gate 	 * -L	list non-STMS to STMS device name mappings for all controllers
1645a4c37c9Sqh201292 	 * -p devname
1655a4c37c9Sqh201292 	 *	if devname is vhci based name and open-able, get the first
1665a4c37c9Sqh201292 	 *	onlined phci based name without /devices prefix.
1675a4c37c9Sqh201292 	 *	Used in stmsboot to update the phci based bootpath.
16860fffc19Sjw149990 	 * -D drvname
16960fffc19Sjw149990 	 *	if supplied, indicates that we're going to operate on
17060fffc19Sjw149990 	 *	devices attached to this driver
17160fffc19Sjw149990 	 * -n
17260fffc19Sjw149990 	 *	if supplied, returns name of the node containing "fp" or
17360fffc19Sjw149990 	 *	"sas-$driver", appends "sd@" or "ssd@" or "disk@". Can only
17460fffc19Sjw149990 	 *	be used if -D drv is specified as well
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("usage: %s -u | -m devname | "
17760fffc19Sjw149990 	    "-M devname | -l controller | -L | \n"
17860fffc19Sjw149990 	    "\t\t-p devname | -D { fp | mpt } | -n\n"), progname);
1797c478bd9Sstevel@tonic-gate 	exit(2);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * Parse command line arguments.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate static void
1867c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	char opt;
1897c478bd9Sstevel@tonic-gate 	int n = 0;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (argc == 1) {
1927c478bd9Sstevel@tonic-gate 		usage(argv[0]);
1937c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
19660fffc19Sjw149990 	while ((opt = getopt(argc, argv, "udm:M:Ll:gp:D:n")) != EOF) {
1977c478bd9Sstevel@tonic-gate 		switch (opt) {
1987c478bd9Sstevel@tonic-gate 		case 'u':
1997c478bd9Sstevel@tonic-gate 			patch_vfstab = 1;
2007c478bd9Sstevel@tonic-gate 			n++;
2017c478bd9Sstevel@tonic-gate 			break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		case 'd':
2047c478bd9Sstevel@tonic-gate 			debug = 1;
2057c478bd9Sstevel@tonic-gate 			break;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		case 'm':
2087c478bd9Sstevel@tonic-gate 			mapdev = s_strdup(optarg);
2097c478bd9Sstevel@tonic-gate 			n++;
2107c478bd9Sstevel@tonic-gate 			break;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		case 'M':
2137c478bd9Sstevel@tonic-gate 			mapdev = s_strdup(optarg);
2147c478bd9Sstevel@tonic-gate 			cap_m_option = 1;
2157c478bd9Sstevel@tonic-gate 			n++;
2167c478bd9Sstevel@tonic-gate 			break;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		case 'L':
2197c478bd9Sstevel@tonic-gate 			list_option = 1;
2207c478bd9Sstevel@tonic-gate 			n++;
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		case 'l':
2247c478bd9Sstevel@tonic-gate 			list_option = 1;
2257c478bd9Sstevel@tonic-gate 			list_controllernum = (int)atol(optarg);
2267c478bd9Sstevel@tonic-gate 			if (list_controllernum < 0) {
2277c478bd9Sstevel@tonic-gate 				logerr(gettext("controller number %d is "
2287c478bd9Sstevel@tonic-gate 				    "invalid\n"), list_controllernum);
2297c478bd9Sstevel@tonic-gate 				clean_exit(1);
2307c478bd9Sstevel@tonic-gate 			}
2317c478bd9Sstevel@tonic-gate 			n++;
2327c478bd9Sstevel@tonic-gate 			break;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 		case 'g':
2357c478bd9Sstevel@tonic-gate 			/*
2367c478bd9Sstevel@tonic-gate 			 * private option to display non-STMS device name
2377c478bd9Sstevel@tonic-gate 			 * to GUID mappings.
2387c478bd9Sstevel@tonic-gate 			 */
2397c478bd9Sstevel@tonic-gate 			list_guid_mappings = 1;
2407c478bd9Sstevel@tonic-gate 			break;
24160fffc19Sjw149990 
2425a4c37c9Sqh201292 		case 'p':
2435a4c37c9Sqh201292 			/*
2445a4c37c9Sqh201292 			 * map openable vhci based name to phci base name
2455a4c37c9Sqh201292 			 */
2465a4c37c9Sqh201292 			map_vhciname = s_strdup(optarg);
2475a4c37c9Sqh201292 			n++;
2485a4c37c9Sqh201292 			break;
2497c478bd9Sstevel@tonic-gate 
25060fffc19Sjw149990 		case 'D':
25160fffc19Sjw149990 			/*
25260fffc19Sjw149990 			 * Grab the driver name we need to look for. Each
25360fffc19Sjw149990 			 * time we add support for a new SAS or FC driver
25460fffc19Sjw149990 			 * to this utility, make sure that its driver name
25560fffc19Sjw149990 			 * is checked here.
25660fffc19Sjw149990 			 */
25760fffc19Sjw149990 			drvname = s_malloc(sizeof (optarg) + 1);
25860fffc19Sjw149990 			drvname = s_strdup(optarg);
25960fffc19Sjw149990 			if (strcmp(drvname, "fp") == 0) {
26060fffc19Sjw149990 				drvprop = s_malloc(sizeof (NODE_WWN_PROP));
26160fffc19Sjw149990 				(void) snprintf(drvprop, sizeof (NODE_WWN_PROP),
26260fffc19Sjw149990 				    NODE_WWN_PROP);
26360fffc19Sjw149990 			} else if (strcmp(drvname, "mpt") == 0) {
26460fffc19Sjw149990 				drvprop = s_malloc(sizeof (SASPROP) +
26560fffc19Sjw149990 				    sizeof (drvname) + 1);
26660fffc19Sjw149990 				(void) snprintf(drvprop, sizeof (SASPROP) +
26760fffc19Sjw149990 				    sizeof (drvname), "%s%s",
26860fffc19Sjw149990 				    SASPROP, drvname);
26960fffc19Sjw149990 			} else {
27060fffc19Sjw149990 				logerr(gettext("Driver %s is not supported\n"),
27160fffc19Sjw149990 				    drvname);
27260fffc19Sjw149990 				clean_exit(1);
27360fffc19Sjw149990 			}
27460fffc19Sjw149990 
27560fffc19Sjw149990 			break;
27660fffc19Sjw149990 
27760fffc19Sjw149990 		case 'n':
27860fffc19Sjw149990 			++parent;
27960fffc19Sjw149990 			n++;
28060fffc19Sjw149990 			break;
28160fffc19Sjw149990 
2827c478bd9Sstevel@tonic-gate 		default:
2837c478bd9Sstevel@tonic-gate 			usage(argv[0]);
2847c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
28860fffc19Sjw149990 	if (n != 1) {
2897c478bd9Sstevel@tonic-gate 		usage(argv[0]);
2907c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
2917c478bd9Sstevel@tonic-gate 	}
29260fffc19Sjw149990 }
2937c478bd9Sstevel@tonic-gate 
29480ab886dSwesolows int
2957c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	char save_vfstab[SYS_FILENAME_LEN], tmp_vfstab[SYS_FILENAME_LEN];
2987c478bd9Sstevel@tonic-gate 	int vfstab_updated;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3017c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (getuid() != 0) {
3047c478bd9Sstevel@tonic-gate 		logerr(gettext("must be super-user to run this program\n"));
3057c478bd9Sstevel@tonic-gate 		clean_exit(1);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
3097c478bd9Sstevel@tonic-gate 	(void) umask(022);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/*
3127c478bd9Sstevel@tonic-gate 	 * NOTE: The mpxio boot-up script executes this program with the
3137c478bd9Sstevel@tonic-gate 	 * mapping (-m) option before the /usr is even mounted and when the
3147c478bd9Sstevel@tonic-gate 	 * root filesystem is still mounted read-only.
3157c478bd9Sstevel@tonic-gate 	 */
3167c478bd9Sstevel@tonic-gate 	if (*mapdev != '\0') {
3177c478bd9Sstevel@tonic-gate 		char newname[MAXPATHLEN];
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		if (map_devname(mapdev, newname, sizeof (newname),
3207c478bd9Sstevel@tonic-gate 		    cap_m_option) == 0) {
3217c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", newname);
3227c478bd9Sstevel@tonic-gate 			clean_exit(0);
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 		clean_exit(1);
3257c478bd9Sstevel@tonic-gate 	}
3265a4c37c9Sqh201292 	if (*map_vhciname != '\0') {
3275a4c37c9Sqh201292 		char newname[MAXPATHLEN];
3285a4c37c9Sqh201292 
3295a4c37c9Sqh201292 		if (map_openable_vhciname(map_vhciname, newname,
3305a4c37c9Sqh201292 		    sizeof (newname)) == 0) {
3315a4c37c9Sqh201292 			(void) printf("%s\n", newname);
3325a4c37c9Sqh201292 			clean_exit(0);
3335a4c37c9Sqh201292 		}
3345a4c37c9Sqh201292 		clean_exit(1);
3355a4c37c9Sqh201292 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (list_option || list_guid_mappings) {
3387c478bd9Sstevel@tonic-gate 		if (list_mappings(list_controllernum, list_guid_mappings) == 0)
3397c478bd9Sstevel@tonic-gate 			clean_exit(0);
3407c478bd9Sstevel@tonic-gate 		clean_exit(1);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
34360fffc19Sjw149990 	if (parent > 0) {
34460fffc19Sjw149990 		if (strcmp(drvname, "") == 0) {
34560fffc19Sjw149990 			usage(argv[0]);
34660fffc19Sjw149990 			clean_exit(1);
34760fffc19Sjw149990 		} else {
34860fffc19Sjw149990 			list_nodes(drvname);
34960fffc19Sjw149990 			clean_exit(0);
35060fffc19Sjw149990 		}
35160fffc19Sjw149990 	}
35260fffc19Sjw149990 
3537c478bd9Sstevel@tonic-gate 	/* create a directory where a copy of the system files are saved */
3547c478bd9Sstevel@tonic-gate 	if (patch_vfstab) {
3557c478bd9Sstevel@tonic-gate 		if (mkdirp(SAVE_DIR, 0755) != 0 && errno != EEXIST) {
3567c478bd9Sstevel@tonic-gate 			logerr(gettext("mkdirp: failed to create %1$s: %2$s\n"),
3577c478bd9Sstevel@tonic-gate 			    SAVE_DIR, strerror(errno));
3587c478bd9Sstevel@tonic-gate 			clean_exit(1);
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		if (make_temp(VFSTAB, save_vfstab, tmp_vfstab,
3627c478bd9Sstevel@tonic-gate 		    SYS_FILENAME_LEN) != 0)
3637c478bd9Sstevel@tonic-gate 			clean_exit(1);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		/* build new vfstab without modifying the existing one */
3667c478bd9Sstevel@tonic-gate 		if ((vfstab_updated = update_vfstab(VFSTAB, tmp_vfstab))
3677c478bd9Sstevel@tonic-gate 		    == -1) {
3687c478bd9Sstevel@tonic-gate 			logerr(gettext("failed to update %s\n"), VFSTAB);
3697c478bd9Sstevel@tonic-gate 			clean_exit(1);
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		commit_change(VFSTAB, save_vfstab, tmp_vfstab, vfstab_updated);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	clean_exit(0);
376*815dd917Sjmcp 	/*NOTREACHED*/
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate  * Make saved and temporary filenames in SAVE_DIR.
3817c478bd9Sstevel@tonic-gate  *
3827c478bd9Sstevel@tonic-gate  * ex: if the filename is /etc/vfstab then the save_filename and tmp_filename
3837c478bd9Sstevel@tonic-gate  * would be SAVE_DIR/vfstab and SAVE_DIR/vfstab.tmp respectively.
3847c478bd9Sstevel@tonic-gate  *
3857c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate static int
3887c478bd9Sstevel@tonic-gate make_temp(char *filename, char *save_filename, char *tmp_filename, size_t len)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	char *ptr;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if ((ptr = strrchr(filename, '/')) == NULL) {
3937c478bd9Sstevel@tonic-gate 		logdmsg("invalid file %s\n", filename);
3947c478bd9Sstevel@tonic-gate 		return (-1);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 	(void) snprintf(save_filename, len, "%s%s", SAVE_DIR, ptr);
3977c478bd9Sstevel@tonic-gate 	(void) snprintf(tmp_filename, len, "%s%s.tmp", SAVE_DIR, ptr);
3987c478bd9Sstevel@tonic-gate 	logdmsg("make_temp: %s: save = %s, temp = %s\n", filename,
3997c478bd9Sstevel@tonic-gate 	    save_filename, tmp_filename);
4007c478bd9Sstevel@tonic-gate 	return (0);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate  * Commit the changes made to the system file
4057c478bd9Sstevel@tonic-gate  */
4067c478bd9Sstevel@tonic-gate static void
4077c478bd9Sstevel@tonic-gate commit_change(char *filename, char *save_filename, char *tmp_filename,
4087c478bd9Sstevel@tonic-gate     int updated)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	int x;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if (updated) {
4137c478bd9Sstevel@tonic-gate 		/* save the original */
4147c478bd9Sstevel@tonic-gate 		if ((x = rename(filename, save_filename)) != 0) {
4157c478bd9Sstevel@tonic-gate 			logerr(gettext("rename %1$s to %2$s failed: %3$s\n"),
4167c478bd9Sstevel@tonic-gate 			    filename, save_filename, strerror(errno));
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		/* now rename the new file to the actual file */
4207c478bd9Sstevel@tonic-gate 		if (rename(tmp_filename, filename) != 0) {
4217c478bd9Sstevel@tonic-gate 			logerr(gettext("rename %1$s to %2$s failed: %3$s\n"),
4227c478bd9Sstevel@tonic-gate 			    tmp_filename, filename, strerror(errno));
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 			/* restore the original */
4257c478bd9Sstevel@tonic-gate 			if (x == 0 && rename(save_filename, filename) != 0) {
4267c478bd9Sstevel@tonic-gate 				logerr(
4277c478bd9Sstevel@tonic-gate 				    gettext("rename %1$s to %2$s failed: %3$s\n"
4287c478bd9Sstevel@tonic-gate 				    "%4$s is a copy of the original %5$s file"
4297c478bd9Sstevel@tonic-gate 				    "\n"),
4307c478bd9Sstevel@tonic-gate 				    save_filename, filename, strerror(errno),
4317c478bd9Sstevel@tonic-gate 				    save_filename, filename);
4327c478bd9Sstevel@tonic-gate 			}
4337c478bd9Sstevel@tonic-gate 		} else
4347c478bd9Sstevel@tonic-gate 			(void) printf(gettext("%1$s: %2$s has been updated.\n"),
4357c478bd9Sstevel@tonic-gate 			    stmsboot, filename);
4367c478bd9Sstevel@tonic-gate 	} else {
4377c478bd9Sstevel@tonic-gate 		/* remove the temp file */
4387c478bd9Sstevel@tonic-gate 		(void) unlink(tmp_filename);
4397c478bd9Sstevel@tonic-gate 		(void) printf(gettext("%1$s: %2$s was not modified as no "
4407c478bd9Sstevel@tonic-gate 		    "changes were needed.\n"), stmsboot, filename);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate /*
4457c478bd9Sstevel@tonic-gate  * Get the GUID of the device.
4467c478bd9Sstevel@tonic-gate  *
4477c478bd9Sstevel@tonic-gate  * physpath	/devices name without the /devices prefix and minor name
4487c478bd9Sstevel@tonic-gate  *		component.
4497c478bd9Sstevel@tonic-gate  * guid		caller supplied buffer where the GUID will be placed on return
4507c478bd9Sstevel@tonic-gate  * guid_len	length of the caller supplied guid buffer.
45160fffc19Sjw149990  * no_delay_flag if set open the device with O_NDELAY
4527c478bd9Sstevel@tonic-gate  * node		di_node corresponding to physpath if already available,
4537c478bd9Sstevel@tonic-gate  *		otherwise pass DI_NODE_NIL.
4547c478bd9Sstevel@tonic-gate  *
4557c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate static int
4587c478bd9Sstevel@tonic-gate get_guid(char *physpath, char *guid, int guid_len, int no_delay_flag,
4597c478bd9Sstevel@tonic-gate 	di_node_t node)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	int		fd;
4627c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid;
4637c478bd9Sstevel@tonic-gate 	int		rv	= -1;
4647c478bd9Sstevel@tonic-gate 	char		*i_guid	= NULL;
4657c478bd9Sstevel@tonic-gate 	char		physpath_raw[MAXPATHLEN];
4667c478bd9Sstevel@tonic-gate 	uchar_t		*wwnp;
4677c478bd9Sstevel@tonic-gate 	int		i, n, snapshot_taken = 0;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	logdmsg("get_guid: physpath = %s\n", physpath);
4707c478bd9Sstevel@tonic-gate 
4715a4c37c9Sqh201292 #ifdef sparc
4727c478bd9Sstevel@tonic-gate 	(void) snprintf(physpath_raw, MAXPATHLEN,
4737c478bd9Sstevel@tonic-gate 	    "/devices%s:a,raw", physpath);
4745a4c37c9Sqh201292 #else
4755a4c37c9Sqh201292 	(void) snprintf(physpath_raw, MAXPATHLEN,
4765a4c37c9Sqh201292 	    "/devices%s:c,raw", physpath);
4775a4c37c9Sqh201292 #endif
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	*guid = '\0';
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (no_delay_flag)
4827c478bd9Sstevel@tonic-gate 		no_delay_flag = O_NDELAY;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/*
4857c478bd9Sstevel@tonic-gate 	 * Open the raw device
4867c478bd9Sstevel@tonic-gate 	 * Without the O_DELAY flag, the open will fail on standby paths of
4877c478bd9Sstevel@tonic-gate 	 * T3 if its mp_support mode is "mpxio".
4887c478bd9Sstevel@tonic-gate 	 */
4897c478bd9Sstevel@tonic-gate 	if ((fd = open(physpath_raw, O_RDONLY | no_delay_flag)) == -1) {
4907c478bd9Sstevel@tonic-gate 		logdmsg("get_guid: failed to open %s: %s\n", physpath_raw,
4917c478bd9Sstevel@tonic-gate 		    strerror(errno));
4927c478bd9Sstevel@tonic-gate 		return (-1);
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	if (devid_get(fd, &devid) == 0) {
4967c478bd9Sstevel@tonic-gate 		i_guid = devid_to_guid(devid);
4977c478bd9Sstevel@tonic-gate 		devid_free(devid);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		if (i_guid != NULL) {
5007c478bd9Sstevel@tonic-gate 			s_strlcpy(guid, i_guid, guid_len);
5017c478bd9Sstevel@tonic-gate 			devid_free_guid(i_guid);
5027c478bd9Sstevel@tonic-gate 			rv = 0;
5037c478bd9Sstevel@tonic-gate 			goto out;
50460fffc19Sjw149990 		} else {
5057c478bd9Sstevel@tonic-gate 			logdmsg("get_guid: devid_to_guid() failed\n");
50660fffc19Sjw149990 			logdmsg("Unable to get a GUID for device "
50760fffc19Sjw149990 			    "%s\n", physpath_raw);
50860fffc19Sjw149990 		}
50960fffc19Sjw149990 
5107c478bd9Sstevel@tonic-gate 	} else
5117c478bd9Sstevel@tonic-gate 		logdmsg("get_guid: devid_get() failed: %s\n", strerror(errno));
5127c478bd9Sstevel@tonic-gate 
51360fffc19Sjw149990 	/*
51460fffc19Sjw149990 	 * Unless we're looking at an fp-attached device, we now
51560fffc19Sjw149990 	 * fallback to node name as the guid as this is what the
51660fffc19Sjw149990 	 * fcp driver does. A sas-attached device will have the
51760fffc19Sjw149990 	 * client-guid property set.
51860fffc19Sjw149990 	 */
5197c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
5207c478bd9Sstevel@tonic-gate 		if ((node = di_init(physpath, DINFOCPYALL | DINFOFORCE))
5217c478bd9Sstevel@tonic-gate 		    == DI_NODE_NIL) {
5227c478bd9Sstevel@tonic-gate 			logdmsg("get_guid: di_init on %s failed: %s\n",
5237c478bd9Sstevel@tonic-gate 			    physpath, strerror(errno));
5247c478bd9Sstevel@tonic-gate 			goto out;
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 		snapshot_taken = 1;
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
52960fffc19Sjw149990 	/* non-fp fallout */
53060fffc19Sjw149990 	if (strstr(physpath, "fp") == (char *)NULL) {
53160fffc19Sjw149990 		if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
53260fffc19Sjw149990 		    "client-guid", &guid) < 0) {
53360fffc19Sjw149990 			logdmsg("get_guid: non-fp-attached device, "
53460fffc19Sjw149990 			    "bailing out\n");
53560fffc19Sjw149990 			goto out;
53660fffc19Sjw149990 		}
53760fffc19Sjw149990 	}
53860fffc19Sjw149990 
5397c478bd9Sstevel@tonic-gate 	if ((n = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, NODE_WWN_PROP,
5407c478bd9Sstevel@tonic-gate 	    &wwnp)) == -1) {
5417c478bd9Sstevel@tonic-gate 		logdmsg("get_guid: di_prop_lookup_bytes() failed to lookup "
5427c478bd9Sstevel@tonic-gate 		    "%s: %s\n", NODE_WWN_PROP, strerror(errno));
5437c478bd9Sstevel@tonic-gate 		goto out;
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	if (guid_len >= ((n * 2) + 1)) {
5477c478bd9Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
5487c478bd9Sstevel@tonic-gate 			(void) sprintf(guid + (i * 2), "%02x", (uint_t)(*wwnp));
5497c478bd9Sstevel@tonic-gate 			wwnp++;
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 		rv = 0;
5527c478bd9Sstevel@tonic-gate 	} else
5537c478bd9Sstevel@tonic-gate 		logerr(gettext("insufficient buffer size: need %1$d "
5547c478bd9Sstevel@tonic-gate 		    "bytes, passed %2$d bytes\n"), (n * 2) + 1, guid_len);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate out:
5577c478bd9Sstevel@tonic-gate 	if (snapshot_taken)
5587c478bd9Sstevel@tonic-gate 		di_fini(node);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	(void) close(fd);
5617c478bd9Sstevel@tonic-gate 	logdmsg("get_guid: GUID = %s\n", guid);
5627c478bd9Sstevel@tonic-gate 	return (rv);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * Given client_name return whether it is a phci or vhci based name.
5677c478bd9Sstevel@tonic-gate  * client_name is /devices name of a client without the /devices prefix.
5687c478bd9Sstevel@tonic-gate  *
5697c478bd9Sstevel@tonic-gate  * client_name				Return value
5705a4c37c9Sqh201292  * on sparc:
57160fffc19Sjw149990  * .../fp@xxx/ssd@yyy			CLIENT_TYPE_PHCI (fc)
57260fffc19Sjw149990  * .../LSILogic,sas@xxx/sd@yyy		CLIENT_TYPE_PHCI (sas)
57360fffc19Sjw149990  * .../scsi_vhci/ssd@yyy		CLIENT_TYPE_VHCI (fc)
57460fffc19Sjw149990  * .../scsi_vhci/disk@yyy		CLIENT_TYPE_VHCI (sas)
5757c478bd9Sstevel@tonic-gate  * other				CLIENT_TYPE_UNKNOWN
5765a4c37c9Sqh201292  * on x86:
57760fffc19Sjw149990  * .../fp@xxx/disk@yyy			CLIENT_TYPE_PHCI (fc)
57860fffc19Sjw149990  * .../pci1000,????@xxx/sd@yyy		CLIENT_TYPE_PHCI (sas)
5795a4c37c9Sqh201292  * .../scsi_vhci/disk@yyy		CLIENT_TYPE_VHCI
5805a4c37c9Sqh201292  * other				CLIENT_TYPE_UNKNOWN
5817c478bd9Sstevel@tonic-gate  */
5827c478bd9Sstevel@tonic-gate static client_type_t
5837c478bd9Sstevel@tonic-gate client_name_type(char *client_name)
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate 	client_type_t client_type = CLIENT_TYPE_UNKNOWN;
58660fffc19Sjw149990 	char *p1;
58760fffc19Sjw149990 	char *client_path;
58860fffc19Sjw149990 
58960fffc19Sjw149990 	client_path = s_strdup(client_name);
59060fffc19Sjw149990 	logdmsg("client_name_type: client is %s\n", client_path);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (*client_name != '/')
5937c478bd9Sstevel@tonic-gate 		return (CLIENT_TYPE_UNKNOWN);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if ((p1 = strrchr(client_name, '/')) == NULL ||
59660fffc19Sjw149990 	    ((strncmp(p1, "/ssd@", sizeof ("/ssd@") - 1) != 0) &&
59760fffc19Sjw149990 	    (strncmp(p1, "/sd@", sizeof ("/sd@") - 1) != 0) &&
59860fffc19Sjw149990 	    (strncmp(p1, "/disk@", sizeof ("/disk@") - 1) != 0))) {
59960fffc19Sjw149990 		logdmsg("client_name_type: p1 = %s\n", p1);
6007c478bd9Sstevel@tonic-gate 		return (CLIENT_TYPE_UNKNOWN);
60160fffc19Sjw149990 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	*p1 = '\0';
6047c478bd9Sstevel@tonic-gate 
60560fffc19Sjw149990 	/*
60660fffc19Sjw149990 	 * Courtesy of the if (..) block above, we know that any
60760fffc19Sjw149990 	 * device path we have now is either PHCI or VHCI
60860fffc19Sjw149990 	 */
60960fffc19Sjw149990 	client_type = client_by_props(client_path);
61060fffc19Sjw149990 
61160fffc19Sjw149990 	logdmsg("client_name_type: client_type = %d\n", client_type);
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	*p1 = '/';
6147c478bd9Sstevel@tonic-gate 	return (client_type);
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate /*
61860fffc19Sjw149990  * client_by_props() is called to determine what the client type
61960fffc19Sjw149990  * is, based on properties in the device tree:
62060fffc19Sjw149990  *
62160fffc19Sjw149990  * drivername	property	type
62260fffc19Sjw149990  * -------------------------------------
62360fffc19Sjw149990  *  fp		node-wwn	CLIENT_TYPE_PHCI
62460fffc19Sjw149990  *  mpt		sas-mpt		CLIENT_TYPE_PHCI
62560fffc19Sjw149990  *  mpt		client-guid	CLIENT_TYPE_PHCI (corner case)
62660fffc19Sjw149990  *
62760fffc19Sjw149990  * Normally, the "client-guid" property only shows up for a node
62860fffc19Sjw149990  * if we've enumerated that node under scsi_vhci. During testing
62960fffc19Sjw149990  * of this function, one particular corner case was found which
63060fffc19Sjw149990  * requires an exception handler.
63160fffc19Sjw149990  */
63260fffc19Sjw149990 
63360fffc19Sjw149990 static client_type_t
63460fffc19Sjw149990 client_by_props(char *path) {
63560fffc19Sjw149990 
63660fffc19Sjw149990 	di_node_t clientnode = DI_NODE_NIL;
63760fffc19Sjw149990 	di_node_t parentnode = DI_NODE_NIL;
63860fffc19Sjw149990 	unsigned int rval = CLIENT_TYPE_UNKNOWN;
63960fffc19Sjw149990 	uchar_t *byteprop[32];
64060fffc19Sjw149990 	char *charprop = NULL;
64160fffc19Sjw149990 	char *physpath;
64260fffc19Sjw149990 	char *parentpath;
64360fffc19Sjw149990 
64460fffc19Sjw149990 	physpath = s_malloc(MAXPATHLEN);
64560fffc19Sjw149990 	bzero(physpath, MAXPATHLEN);
64660fffc19Sjw149990 
64760fffc19Sjw149990 	physpath = s_strdup(path);
64860fffc19Sjw149990 
64960fffc19Sjw149990 	logdmsg("client_by_props: physpath = (%s)\n", physpath);
65060fffc19Sjw149990 
65160fffc19Sjw149990 	/* easy short-circuits */
65260fffc19Sjw149990 	if (strstr(physpath, "scsi_vhci") != (char *)NULL) {
65360fffc19Sjw149990 		logdmsg("client_by_props: found "
65460fffc19Sjw149990 		    "'scsi_vhci' on path (%s)\n", physpath);
65560fffc19Sjw149990 		rval = CLIENT_TYPE_VHCI;
65660fffc19Sjw149990 		goto out;
65760fffc19Sjw149990 	} else if ((strstr(physpath, "ide") != (char *)NULL) ||
65860fffc19Sjw149990 	    (strstr(physpath, "storage") != (char *)NULL)) {
65960fffc19Sjw149990 		logdmsg("client_by_props: ignoring this device\n");
66060fffc19Sjw149990 		goto out;
66160fffc19Sjw149990 	}
66260fffc19Sjw149990 
66360fffc19Sjw149990 	parentpath = s_malloc(MAXPATHLEN);
66460fffc19Sjw149990 	bzero(parentpath, MAXPATHLEN);
66560fffc19Sjw149990 
66660fffc19Sjw149990 	(void) strncpy(parentpath, physpath, strlen(physpath) -
66760fffc19Sjw149990 	    strlen(strrchr(physpath, '/')));
66860fffc19Sjw149990 
66960fffc19Sjw149990 	if ((parentnode = di_init(parentpath, DINFOCPYALL |
67060fffc19Sjw149990 	    DINFOFORCE)) == DI_NODE_NIL) {
67160fffc19Sjw149990 		logdmsg("client_by_props: unable to di_init(%s)\n",
67260fffc19Sjw149990 		    parentpath);
67360fffc19Sjw149990 		goto out;
67460fffc19Sjw149990 	}
67560fffc19Sjw149990 
67660fffc19Sjw149990 	if (strstr(physpath, "fp") != (char *)NULL) {
67760fffc19Sjw149990 		if (drvprop == (char *)NULL) {
67860fffc19Sjw149990 			drvprop = s_malloc(strlen(NODE_WWN_PROP) + 1);
67960fffc19Sjw149990 		}
68060fffc19Sjw149990 		logdmsg("NODE_WWN_PROP\n");
68160fffc19Sjw149990 		(void) snprintf(drvprop, strlen(NODE_WWN_PROP) + 1,
68260fffc19Sjw149990 		    NODE_WWN_PROP);
68360fffc19Sjw149990 	} else {
68460fffc19Sjw149990 		if (drvname == (char *)NULL) {
68560fffc19Sjw149990 			drvname = di_driver_name(parentnode);
68660fffc19Sjw149990 			logdmsg("client_by_props: drvname = %s\n", drvname);
68760fffc19Sjw149990 		}
68860fffc19Sjw149990 
68960fffc19Sjw149990 		if (drvprop == (char *)NULL) {
69060fffc19Sjw149990 			drvprop = s_malloc(sizeof (SASPROP) +
69160fffc19Sjw149990 			    sizeof (drvname) + 1);
69260fffc19Sjw149990 		}
69360fffc19Sjw149990 		(void) snprintf(drvprop, sizeof (SASPROP) +
69460fffc19Sjw149990 		    sizeof (drvname), "%s%s", SASPROP, drvname);
69560fffc19Sjw149990 
69660fffc19Sjw149990 		logdmsg("parentpath: %s\nphyspath: %s\n"
69760fffc19Sjw149990 		    "length %d, strrchr: %d\n",
69860fffc19Sjw149990 		    parentpath, physpath, strlen(physpath),
69960fffc19Sjw149990 		    strlen(strrchr(physpath, '/')));
70060fffc19Sjw149990 	}
70160fffc19Sjw149990 
70260fffc19Sjw149990 	logdmsg("client_by_props: searching for property '%s'\n", drvprop);
70360fffc19Sjw149990 
70460fffc19Sjw149990 	if ((clientnode = di_init(physpath, DINFOCPYALL | DINFOFORCE)) ==
70560fffc19Sjw149990 	    DI_NODE_NIL) {
70660fffc19Sjw149990 		logdmsg("client_by_props: unable to di_init(%s)\n",
70760fffc19Sjw149990 		    physpath);
70860fffc19Sjw149990 
70960fffc19Sjw149990 		/*
71060fffc19Sjw149990 		 * On x86/x64 systems, we won't be able to di_init() the
71160fffc19Sjw149990 		 * node we want in the device tree, however the parent
71260fffc19Sjw149990 		 * node will still have 'mpxio-disable' set, so we can
71360fffc19Sjw149990 		 * check for that property and make our decision on type
71460fffc19Sjw149990 		 */
71560fffc19Sjw149990 
71660fffc19Sjw149990 		if (di_prop_lookup_strings(DDI_DEV_T_ANY, parentnode,
71760fffc19Sjw149990 		    "mpxio-disable", &charprop) > -1) {
71860fffc19Sjw149990 			rval = CLIENT_TYPE_PHCI;
71960fffc19Sjw149990 			di_fini(parentnode);
72060fffc19Sjw149990 			logdmsg("client_by_props: device %s is PHCI\n",
72160fffc19Sjw149990 			    physpath);
72260fffc19Sjw149990 		}
72360fffc19Sjw149990 		goto out;
72460fffc19Sjw149990 	}
72560fffc19Sjw149990 
72660fffc19Sjw149990 	if (di_prop_lookup_bytes(DDI_DEV_T_ANY,
72760fffc19Sjw149990 	    clientnode, drvprop, byteprop) > -1) {
72860fffc19Sjw149990 		logdmsg("client_by_props: found prop %s on "
72960fffc19Sjw149990 		    "path %s\n", drvprop, physpath);
73060fffc19Sjw149990 		rval = CLIENT_TYPE_PHCI;
73160fffc19Sjw149990 	} else if (di_prop_lookup_strings(DDI_DEV_T_ANY,
73260fffc19Sjw149990 	    clientnode, "client-guid", &charprop) > -1) {
73360fffc19Sjw149990 			/*
73460fffc19Sjw149990 			 * A corner case was seen during testing where
73560fffc19Sjw149990 			 * scsi_vhci was loaded, but not all applicable
73660fffc19Sjw149990 			 * devices were enumerated under it. That left
73760fffc19Sjw149990 			 * the phci mapping along with the "client-guid"
73860fffc19Sjw149990 			 * property.
73960fffc19Sjw149990 			 */
74060fffc19Sjw149990 			logdmsg("client_by_props: weird... \n");
74160fffc19Sjw149990 			rval = CLIENT_TYPE_PHCI;
74260fffc19Sjw149990 	} else {
74360fffc19Sjw149990 		logdmsg("client_by_props: unable to find "
74460fffc19Sjw149990 		    "property 'client-guid', 'mpxio-disable' "
74560fffc19Sjw149990 		    "or '%s' anywhere on path (%s)\n",
74660fffc19Sjw149990 		    drvprop, physpath);
74760fffc19Sjw149990 		logdmsg("client_by_props: this node is unknown\n");
74860fffc19Sjw149990 	}
74960fffc19Sjw149990 
75060fffc19Sjw149990 	di_fini(parentnode);
75160fffc19Sjw149990 	di_fini(clientnode);
75260fffc19Sjw149990 out:
75360fffc19Sjw149990 	free(physpath);
75460fffc19Sjw149990 	return (rval);
75560fffc19Sjw149990 }
75660fffc19Sjw149990 
75760fffc19Sjw149990 
75860fffc19Sjw149990 /*
7597c478bd9Sstevel@tonic-gate  * Map phci based client name to vhci based client name.
7607c478bd9Sstevel@tonic-gate  *
7617c478bd9Sstevel@tonic-gate  * phci_name
7627c478bd9Sstevel@tonic-gate  *	phci based client /devices name without the /devices prefix and
7637c478bd9Sstevel@tonic-gate  *	minor name component.
7645a4c37c9Sqh201292  *	ex:
76560fffc19Sjw149990  *
76660fffc19Sjw149990  *	(FC)
7675a4c37c9Sqh201292  *	for sparc: /pci@8,600000/SUNW,qlc@4/fp@0,0/ssd@w2100002037cd9f72,0
7685a4c37c9Sqh201292  *	for x86: /pci@8,600000/SUNW,qlc@4/fp@0,0/disk@w2100002037cd9f72,0
7697c478bd9Sstevel@tonic-gate  *
77060fffc19Sjw149990  *	(SAS)
77160fffc19Sjw149990  *	for sparc: /pci@0,2/LSILogic,sas@1/disk@6,0
77260fffc19Sjw149990  *	for x86: /pci1000,3060@3/sd@0,0
77360fffc19Sjw149990  *
7747c478bd9Sstevel@tonic-gate  * vhci_name
7757c478bd9Sstevel@tonic-gate  *	Caller supplied buffer where vhci /devices name will be placed on
7767c478bd9Sstevel@tonic-gate  *	return (without the /devices prefix and minor name component).
7775a4c37c9Sqh201292  *	ex:
77860fffc19Sjw149990  *
77960fffc19Sjw149990  *	(FC)
7805a4c37c9Sqh201292  *	for sparc: /scsi_vhci/ssd@g2000002037cd9f72
7815a4c37c9Sqh201292  *	for x86: /scsi_vhci/disk@g2000002037cd9f72
7827c478bd9Sstevel@tonic-gate  *
78360fffc19Sjw149990  *	(SAS)
78460fffc19Sjw149990  *	both: /scsi_vhci/disk@g600a0b8000254d3e00000284453ed8ac
78560fffc19Sjw149990  *
7867c478bd9Sstevel@tonic-gate  * vhci_name_len
7877c478bd9Sstevel@tonic-gate  *	Length of the caller supplied vhci_name buffer.
7887c478bd9Sstevel@tonic-gate  *
7897c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
7907c478bd9Sstevel@tonic-gate  */
7917c478bd9Sstevel@tonic-gate static int
7927c478bd9Sstevel@tonic-gate phci_to_vhci(char *phci_name, char *vhci_name, size_t vhci_name_len)
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate 	sv_iocdata_t ioc;
79560fffc19Sjw149990 	char *slash, *at;
7967c478bd9Sstevel@tonic-gate 	char vhci_name_buf[MAXPATHLEN];
7977c478bd9Sstevel@tonic-gate 	char phci_name_buf[MAXPATHLEN];
7987c478bd9Sstevel@tonic-gate 	char addr_buf[MAXNAMELEN];
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	logdmsg("phci_to_vhci: client = %s\n", phci_name);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	s_strlcpy(phci_name_buf, phci_name, MAXPATHLEN);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	if (client_name_type(phci_name_buf) != CLIENT_TYPE_PHCI ||
8057c478bd9Sstevel@tonic-gate 	    (slash = strrchr(phci_name_buf, '/')) == NULL ||
80660fffc19Sjw149990 	    ((strncmp(slash, "/ssd@", sizeof ("/ssd@") - 1) != 0) &&
80760fffc19Sjw149990 	    (strncmp(slash, "/sd@", sizeof ("/sd@") - 1) != 0) &&
80860fffc19Sjw149990 	    (strncmp(slash, "/disk@", sizeof ("/disk@") - 1) != 0))) {
8097c478bd9Sstevel@tonic-gate 		logdmsg("phci_to_vhci: %s is not of CLIENT_TYPE_PHCI\n",
8107c478bd9Sstevel@tonic-gate 		    phci_name);
8117c478bd9Sstevel@tonic-gate 		return (-1);
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	if (vhci_fd < 0) {
8157c478bd9Sstevel@tonic-gate 		if ((vhci_fd = open(VHCI_CTL_NODE, O_RDWR)) < 0)
8167c478bd9Sstevel@tonic-gate 			return (-1);
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	*slash = '\0';
82060fffc19Sjw149990 
82160fffc19Sjw149990 	at = strchr(slash + 1, '@');
82260fffc19Sjw149990 	s_strlcpy(addr_buf, at + 1, MAXNAMELEN);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	bzero(&ioc, sizeof (sv_iocdata_t));
8257c478bd9Sstevel@tonic-gate 	ioc.client = vhci_name_buf;
8267c478bd9Sstevel@tonic-gate 	ioc.phci = phci_name_buf;
8277c478bd9Sstevel@tonic-gate 	ioc.addr = addr_buf;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	if (ioctl(vhci_fd, SCSI_VHCI_GET_CLIENT_NAME, &ioc) != 0) {
8307c478bd9Sstevel@tonic-gate 		logdmsg("SCSI_VHCI_GET_CLIENT_NAME on %s "
8317c478bd9Sstevel@tonic-gate 		    "failed: %s\n", phci_name, strerror(errno));
8327c478bd9Sstevel@tonic-gate 		return (-1);
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	s_strlcpy(vhci_name, vhci_name_buf, vhci_name_len);
8367c478bd9Sstevel@tonic-gate 	logdmsg("phci_to_vhci: %s maps to %s\n", phci_name, vhci_name);
8377c478bd9Sstevel@tonic-gate 	return (0);
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate /*
8417c478bd9Sstevel@tonic-gate  * Map vhci based client name to phci based client name.
8427c478bd9Sstevel@tonic-gate  * If the client has multiple paths, only one of the paths with which client
8437c478bd9Sstevel@tonic-gate  * can be accessed is returned. This function does not use SCSI_VHCI ioctls
8447c478bd9Sstevel@tonic-gate  * as it is called on mpxio disabled paths.
8457c478bd9Sstevel@tonic-gate  *
8467c478bd9Sstevel@tonic-gate  * vhci_name
8477c478bd9Sstevel@tonic-gate  *	vhci based client /devices name without the /devices prefix and
8487c478bd9Sstevel@tonic-gate  *	minor name component.
8495a4c37c9Sqh201292  *	ex:
8505a4c37c9Sqh201292  *	sparc: /scsi_vhci/ssd@g2000002037cd9f72
8515a4c37c9Sqh201292  *	x86: /scsi_vhci/disk@g2000002037cd9f72
8527c478bd9Sstevel@tonic-gate  *
8537c478bd9Sstevel@tonic-gate  * phci_name
8547c478bd9Sstevel@tonic-gate  *	Caller supplied buffer where phci /devices name will be placed on
8557c478bd9Sstevel@tonic-gate  *	return (without the /devices prefix and minor name component).
8565a4c37c9Sqh201292  *	ex:
8575a4c37c9Sqh201292  *	sparc: /pci@8,600000/SUNW,qlc@4/fp@0,0/ssd@w2100002037cd9f72,0
8585a4c37c9Sqh201292  *	x86: /pci@8,600000/SUNW,qlc@4/fp@0,0/disk@w2100002037cd9f72,0
8597c478bd9Sstevel@tonic-gate  *
8607c478bd9Sstevel@tonic-gate  * phci_name_len
8617c478bd9Sstevel@tonic-gate  *	Length of the caller supplied phci_name buffer.
8627c478bd9Sstevel@tonic-gate  *
8637c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
8647c478bd9Sstevel@tonic-gate  */
8657c478bd9Sstevel@tonic-gate static int
8667c478bd9Sstevel@tonic-gate vhci_to_phci(char *vhci_name, char *phci_name, size_t phci_name_len)
8677c478bd9Sstevel@tonic-gate {
86860fffc19Sjw149990 	di_node_t node = DI_NODE_NIL;
8697c478bd9Sstevel@tonic-gate 	char *vhci_guid, *devfspath;
8707c478bd9Sstevel@tonic-gate 	char phci_guid[MAXPATHLEN];
87160fffc19Sjw149990 	char *node_name;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	logdmsg("vhci_to_phci: client = %s\n", vhci_name);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	if (client_name_type(vhci_name) != CLIENT_TYPE_VHCI) {
8767c478bd9Sstevel@tonic-gate 		logdmsg("vhci_to_phci: %s is not of CLIENT_TYPE_VHCI\n",
8777c478bd9Sstevel@tonic-gate 		    vhci_name);
8787c478bd9Sstevel@tonic-gate 		return (-1);
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 
88160fffc19Sjw149990 
8827c478bd9Sstevel@tonic-gate 	if ((vhci_guid = strrchr(vhci_name, '@')) == NULL ||
8837c478bd9Sstevel@tonic-gate 	    *(++vhci_guid) != 'g') {
8847c478bd9Sstevel@tonic-gate 		logerr(gettext("couldn't get guid from %s\n"), vhci_name);
8857c478bd9Sstevel@tonic-gate 		return (-1);
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	/* point to guid */
8897c478bd9Sstevel@tonic-gate 	++vhci_guid;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	/*
8927c478bd9Sstevel@tonic-gate 	 * Get devinfo snapshot and walk all ssd nodes whose parent is fp.
8937c478bd9Sstevel@tonic-gate 	 * For each node get the guid and match it with vhci_guid.
8947c478bd9Sstevel@tonic-gate 	 */
8957c478bd9Sstevel@tonic-gate 	if (devinfo_root == DI_NODE_NIL) {
8967c478bd9Sstevel@tonic-gate 		logdmsg("vhci_to_phci: taking devinfo snapshot\n");
8977c478bd9Sstevel@tonic-gate 		if ((devinfo_root = di_init("/", DINFOCPYALL | DINFOFORCE))
8987c478bd9Sstevel@tonic-gate 		    == DI_NODE_NIL) {
8997c478bd9Sstevel@tonic-gate 			logerr(gettext("di_init failed: %s\n"),
9007c478bd9Sstevel@tonic-gate 			    strerror(errno));
9017c478bd9Sstevel@tonic-gate 			return (-1);
9027c478bd9Sstevel@tonic-gate 		}
9037c478bd9Sstevel@tonic-gate 		logdmsg("vhci_to_phci: done taking devinfo snapshot\n");
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
90660fffc19Sjw149990 
90760fffc19Sjw149990 	/*
90860fffc19Sjw149990 	 * When we finally get a unified "sd" driver for all
90960fffc19Sjw149990 	 * architectures that Solaris runs on, we can remove this
91060fffc19Sjw149990 	 * first loop around for "ssd"
91160fffc19Sjw149990 	 */
91260fffc19Sjw149990 	for (node = di_drv_first_node("ssd", devinfo_root);
9137c478bd9Sstevel@tonic-gate 	    node != DI_NODE_NIL; node = di_drv_next_node(node)) {
91460fffc19Sjw149990 
91560fffc19Sjw149990 		if ((node_name = di_node_name(node)) == NULL)
91660fffc19Sjw149990 			continue;
91760fffc19Sjw149990 
91860fffc19Sjw149990 		if ((strcmp(node_name, "disk") != 0) &&
91960fffc19Sjw149990 		    (strcmp(node_name, "sd") != 0) &&
92060fffc19Sjw149990 		    (strcmp(node_name, "ssd") != 0))
92160fffc19Sjw149990 			continue;
92260fffc19Sjw149990 
92360fffc19Sjw149990 		if (di_parent_node(node) == DI_NODE_NIL)
92460fffc19Sjw149990 			continue;
92560fffc19Sjw149990 
92660fffc19Sjw149990 		if ((devfspath = di_devfs_path(node)) == NULL)
9277c478bd9Sstevel@tonic-gate 			continue;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 		/*
9307c478bd9Sstevel@tonic-gate 		 * Don't set no_delay_flag to have get_guid() fail on
9317c478bd9Sstevel@tonic-gate 		 * standby paths of T3. So we'll find the preferred paths.
9327c478bd9Sstevel@tonic-gate 		 */
9337c478bd9Sstevel@tonic-gate 		if (get_guid(devfspath, phci_guid,
93460fffc19Sjw149990 		    sizeof (phci_guid), 0, node) != 0)
93560fffc19Sjw149990 			continue;
93660fffc19Sjw149990 
93760fffc19Sjw149990 		if (strcmp(phci_guid, vhci_guid) == 0) {
93860fffc19Sjw149990 			s_strlcpy(phci_name, devfspath, phci_name_len);
93960fffc19Sjw149990 			di_devfs_path_free(devfspath);
94060fffc19Sjw149990 			logdmsg("vhci_to_phci: %s maps to %s\n", vhci_name,
94160fffc19Sjw149990 			    phci_name);
94260fffc19Sjw149990 			return (0);
94360fffc19Sjw149990 		}
94460fffc19Sjw149990 
94560fffc19Sjw149990 		di_devfs_path_free(devfspath);
94660fffc19Sjw149990 	}
94760fffc19Sjw149990 
94860fffc19Sjw149990 	for (node = di_drv_first_node("sd", devinfo_root);
94960fffc19Sjw149990 	    node != DI_NODE_NIL; node = di_drv_next_node(node)) {
95060fffc19Sjw149990 
95160fffc19Sjw149990 		if ((node_name = di_node_name(node)) == NULL)
95260fffc19Sjw149990 			continue;
95360fffc19Sjw149990 
95460fffc19Sjw149990 		if ((strcmp(node_name, "disk") != 0) &&
95560fffc19Sjw149990 		    (strcmp(node_name, "sd") != 0) &&
95660fffc19Sjw149990 		    (strcmp(node_name, "ssd") != 0))
95760fffc19Sjw149990 			continue;
95860fffc19Sjw149990 
95960fffc19Sjw149990 		if (di_parent_node(node) == DI_NODE_NIL)
96060fffc19Sjw149990 			continue;
96160fffc19Sjw149990 
96260fffc19Sjw149990 		if ((devfspath = di_devfs_path(node)) == NULL)
96360fffc19Sjw149990 			continue;
96460fffc19Sjw149990 
96560fffc19Sjw149990 		/*
96660fffc19Sjw149990 		 * Don't set no_delay_flag to have get_guid() fail on
96760fffc19Sjw149990 		 * standby paths of T3. So we'll find the preferred paths.
96860fffc19Sjw149990 		 */
96960fffc19Sjw149990 		if (get_guid(devfspath, phci_guid,
97060fffc19Sjw149990 		    sizeof (phci_guid), 0, node) != 0)
97160fffc19Sjw149990 			continue;
97260fffc19Sjw149990 
97360fffc19Sjw149990 		if (strcmp(phci_guid, vhci_guid) == 0) {
9747c478bd9Sstevel@tonic-gate 			s_strlcpy(phci_name, devfspath, phci_name_len);
9757c478bd9Sstevel@tonic-gate 			di_devfs_path_free(devfspath);
9767c478bd9Sstevel@tonic-gate 			logdmsg("vhci_to_phci: %s maps to %s\n", vhci_name,
9777c478bd9Sstevel@tonic-gate 			    phci_name);
9787c478bd9Sstevel@tonic-gate 			return (0);
9797c478bd9Sstevel@tonic-gate 		}
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		di_devfs_path_free(devfspath);
9827c478bd9Sstevel@tonic-gate 	}
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	logdmsg("vhci_to_phci: couldn't get phci name for %s\n", vhci_name);
9857c478bd9Sstevel@tonic-gate 	return (-1);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate /*
9895a4c37c9Sqh201292  * Map vhci based client name to phci based client name.
9905a4c37c9Sqh201292  * If the client has multiple paths, only one of the paths with which client
9915a4c37c9Sqh201292  * can be accessed is returned.
9925a4c37c9Sqh201292  * This function uses SCSI_VHCI ioctls to get the phci paths
9935a4c37c9Sqh201292  *
9945a4c37c9Sqh201292  * vhci_name
9955a4c37c9Sqh201292  *	vhci based client /devices name without the /devices prefix and
9965a4c37c9Sqh201292  *	minor name component.
9975a4c37c9Sqh201292  *	ex:
9985a4c37c9Sqh201292  *	sparc: /scsi_vhci/ssd@g2000002037cd9f72
9995a4c37c9Sqh201292  *	x86: /scsi_vhci/disk@g2000002037cd9f72
10005a4c37c9Sqh201292  *
10015a4c37c9Sqh201292  * phci_name
10025a4c37c9Sqh201292  *	Caller supplied buffer where phci /devices name will be placed on
10035a4c37c9Sqh201292  *	return (without the /devices prefix and minor name component).
10045a4c37c9Sqh201292  *	ex:
10055a4c37c9Sqh201292  *	sparc: /pci@8,600000/SUNW,qlc@4/fp@0,0/ssd@w2100002037cd9f72,0
10065a4c37c9Sqh201292  *	x86: /pci@8,600000/SUNW,qlc@4/fp@0,0/disk@w2100002037cd9f72,0
10075a4c37c9Sqh201292  *
10085a4c37c9Sqh201292  * phci_name_len
10095a4c37c9Sqh201292  *	Length of the caller supplied phci_name buffer.
10105a4c37c9Sqh201292  *
10115a4c37c9Sqh201292  * Returns 0 on success, -1 on failure.
10125a4c37c9Sqh201292  */
10135a4c37c9Sqh201292 
10145a4c37c9Sqh201292 static int
10155a4c37c9Sqh201292 vhci_to_phci_by_ioctl(char *vhci_name, char *phci_name, size_t phci_name_len)
10165a4c37c9Sqh201292 {
10175a4c37c9Sqh201292 	sv_iocdata_t	ioc;
10185a4c37c9Sqh201292 	uint_t npaths;
10195a4c37c9Sqh201292 	char *node_name, *at;
10205a4c37c9Sqh201292 	char vhci_name_buf[MAXPATHLEN];
10215a4c37c9Sqh201292 	int  ret;
10225a4c37c9Sqh201292 	sv_path_info_t *pi;
10235a4c37c9Sqh201292 
10245a4c37c9Sqh201292 	logdmsg("vhci_to_phci_by_ioctl: client = %s\n", vhci_name);
10255a4c37c9Sqh201292 
10265a4c37c9Sqh201292 	if (vhci_fd < 0) {
10275a4c37c9Sqh201292 		if ((vhci_fd = open(VHCI_CTL_NODE, O_RDWR)) < 0)
10285a4c37c9Sqh201292 			return (-1);
10295a4c37c9Sqh201292 	}
10305a4c37c9Sqh201292 
10315a4c37c9Sqh201292 	(void) strlcpy(vhci_name_buf, vhci_name, MAXPATHLEN);
10325a4c37c9Sqh201292 
10335a4c37c9Sqh201292 	/* first get the number paths */
10345a4c37c9Sqh201292 	bzero(&ioc, sizeof (sv_iocdata_t));
10355a4c37c9Sqh201292 	ioc.client = vhci_name_buf;
10365a4c37c9Sqh201292 	ioc.ret_elem = &npaths;
10375a4c37c9Sqh201292 	if ((ret = ioctl(vhci_fd, SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO,
10385a4c37c9Sqh201292 	    &ioc)) != 0 || npaths == 0) {
10395a4c37c9Sqh201292 		logdmsg("SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO on %s "
10405a4c37c9Sqh201292 		    "failed: %s\n", vhci_name,
10415a4c37c9Sqh201292 		    ret?strerror(errno):"got 0 paths");
10425a4c37c9Sqh201292 		return (-1);
10435a4c37c9Sqh201292 	}
10445a4c37c9Sqh201292 
10455a4c37c9Sqh201292 	/* now allocate memory for the path information and get all paths */
10465a4c37c9Sqh201292 	bzero(&ioc, sizeof (sv_iocdata_t));
10475a4c37c9Sqh201292 	ioc.client = vhci_name_buf;
10485a4c37c9Sqh201292 	ioc.buf_elem = npaths;
10495a4c37c9Sqh201292 	ioc.ret_elem = &npaths;
10505a4c37c9Sqh201292 	if ((ioc.ret_buf = (sv_path_info_t *)calloc(npaths,
10515a4c37c9Sqh201292 	    sizeof (sv_path_info_t))) == NULL)
10525a4c37c9Sqh201292 		return (-1);
10535a4c37c9Sqh201292 	if ((ret = ioctl(vhci_fd, SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO,
10545a4c37c9Sqh201292 	    &ioc)) != 0 || npaths == 0) {
10555a4c37c9Sqh201292 		logdmsg("SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO on %s "
10565a4c37c9Sqh201292 		    "failed: %s\n", vhci_name,
10575a4c37c9Sqh201292 		    ret?strerror(errno):"got 0 paths");
10585a4c37c9Sqh201292 		goto out;
10595a4c37c9Sqh201292 	}
10605a4c37c9Sqh201292 
10615a4c37c9Sqh201292 	if (ioc.buf_elem < npaths)
10625a4c37c9Sqh201292 		npaths = ioc.buf_elem;
10635a4c37c9Sqh201292 	if ((node_name = strrchr(vhci_name_buf, '/')) == NULL ||
10645a4c37c9Sqh201292 	    (at = strchr(node_name, '@')) == NULL)
10655a4c37c9Sqh201292 		goto out;
10665a4c37c9Sqh201292 
10675a4c37c9Sqh201292 	node_name++;
10685a4c37c9Sqh201292 	*at = '\0';
10695a4c37c9Sqh201292 
107060fffc19Sjw149990 	logdmsg("vhci_to_phci_by_ioctl: node_name is %s\n", node_name);
107160fffc19Sjw149990 #ifndef sparc
107260fffc19Sjw149990 	/*
107360fffc19Sjw149990 	 * We need to use a libdevinfo call to get this info
107460fffc19Sjw149990 	 * in an architecturally-neutral fashion. Phase-II for sure!
107560fffc19Sjw149990 	 */
107660fffc19Sjw149990 	node_name = "sd";
107760fffc19Sjw149990 #endif
107860fffc19Sjw149990 
10795a4c37c9Sqh201292 	/*
10805a4c37c9Sqh201292 	 * return the first online paths as non-online paths may
10815a4c37c9Sqh201292 	 * not be accessible in the target environment.
10825a4c37c9Sqh201292 	 */
10835a4c37c9Sqh201292 	pi = (sv_path_info_t *)ioc.ret_buf;
10845a4c37c9Sqh201292 	while (npaths--) {
10855a4c37c9Sqh201292 		if (MDI_PATHINFO_STATE_ONLINE == pi->ret_state) {
10865a4c37c9Sqh201292 			(void) snprintf(phci_name, phci_name_len, "%s/%s@%s",
10875a4c37c9Sqh201292 			    pi->device.ret_phci, node_name,
10885a4c37c9Sqh201292 			    pi->ret_addr);
10895a4c37c9Sqh201292 			logdmsg("vhci_to_phci_by_ioctl: %s maps to %s\n",
10905a4c37c9Sqh201292 			    vhci_name, phci_name);
10915a4c37c9Sqh201292 			free(ioc.ret_buf);
10925a4c37c9Sqh201292 			return (0);
10935a4c37c9Sqh201292 		}
10945a4c37c9Sqh201292 		pi++;
10955a4c37c9Sqh201292 	}
10965a4c37c9Sqh201292 
10975a4c37c9Sqh201292 out:
10985a4c37c9Sqh201292 	logdmsg("vhci_to_phci_by_ioctl: couldn't get phci name for %s\n",
10995a4c37c9Sqh201292 	    vhci_name);
11005a4c37c9Sqh201292 	free(ioc.ret_buf);
11015a4c37c9Sqh201292 	return (-1);
11025a4c37c9Sqh201292 
11035a4c37c9Sqh201292 }
11045a4c37c9Sqh201292 
11055a4c37c9Sqh201292 /*
11067c478bd9Sstevel@tonic-gate  * Map physname from phci name space to vhci name space or vice-versa
11077c478bd9Sstevel@tonic-gate  *
11087c478bd9Sstevel@tonic-gate  * physname
11097c478bd9Sstevel@tonic-gate  *	phci or vhci based client /devices name without the /devices prefix and
11107c478bd9Sstevel@tonic-gate  *	minor name component.
11117c478bd9Sstevel@tonic-gate  *
11127c478bd9Sstevel@tonic-gate  * new_physname
11137c478bd9Sstevel@tonic-gate  *	Caller supplied buffer where the mapped physical name is stored on
11147c478bd9Sstevel@tonic-gate  *	return (without the /devices prefix and minor name component).
11157c478bd9Sstevel@tonic-gate  *
11167c478bd9Sstevel@tonic-gate  * len
11177c478bd9Sstevel@tonic-gate  *	Length of the caller supplied new_physname buffer.
11187c478bd9Sstevel@tonic-gate  *
11197c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
11207c478bd9Sstevel@tonic-gate  */
11217c478bd9Sstevel@tonic-gate static int
11227c478bd9Sstevel@tonic-gate map_physname(char *physname, char *new_physname, size_t len)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate 	int type;
11257c478bd9Sstevel@tonic-gate 	int rv;
11267c478bd9Sstevel@tonic-gate 
112760fffc19Sjw149990 	type = client_name_type(physname);
112860fffc19Sjw149990 	logdmsg("map_physname: type (%d) physname = %s\n",
112960fffc19Sjw149990 	    type, physname);
113060fffc19Sjw149990 
113160fffc19Sjw149990 	if (type == CLIENT_TYPE_VHCI)
11327c478bd9Sstevel@tonic-gate 		rv = vhci_to_phci(physname, new_physname, len);
11337c478bd9Sstevel@tonic-gate 	else if (type == CLIENT_TYPE_PHCI)
11347c478bd9Sstevel@tonic-gate 		rv = phci_to_vhci(physname, new_physname, len);
11357c478bd9Sstevel@tonic-gate 	else
11367c478bd9Sstevel@tonic-gate 		rv = -1;
11377c478bd9Sstevel@tonic-gate 
113860fffc19Sjw149990 	logdmsg("map_physname: returning %d\n", rv);
11397c478bd9Sstevel@tonic-gate 	return (rv);
11407c478bd9Sstevel@tonic-gate }
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate /*
11437c478bd9Sstevel@tonic-gate  * Given a phci or vhci devname which is either a /dev link or /devices name
11447c478bd9Sstevel@tonic-gate  * get the corresponding physical node path (without the /devices prefix)
11457c478bd9Sstevel@tonic-gate  * and minor name.
11467c478bd9Sstevel@tonic-gate  *
11477c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
11487c478bd9Sstevel@tonic-gate  */
11497c478bd9Sstevel@tonic-gate static int
11507c478bd9Sstevel@tonic-gate get_physname_minor(char *devname, char *physname, int physname_len,
11517c478bd9Sstevel@tonic-gate     char *minorname, int minorname_len)
11527c478bd9Sstevel@tonic-gate {
11537c478bd9Sstevel@tonic-gate 	int linksize;
11547c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
11557c478bd9Sstevel@tonic-gate 	char *p, *m;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	if (strncmp(devname, DEV_DSK, sizeof (DEV_DSK) - 1) == 0 ||
11587c478bd9Sstevel@tonic-gate 	    strncmp(devname, DEV_RDSK, sizeof (DEV_RDSK) - 1) == 0) {
11597c478bd9Sstevel@tonic-gate 		if ((linksize = readlink(devname, buf, MAXPATHLEN))
11607c478bd9Sstevel@tonic-gate 		    > 0 && linksize <= (MAXPATHLEN - 1)) {
11617c478bd9Sstevel@tonic-gate 			buf[linksize] = '\0';
11627c478bd9Sstevel@tonic-gate 		} else
11637c478bd9Sstevel@tonic-gate 			return (-1);
11647c478bd9Sstevel@tonic-gate 	} else
11657c478bd9Sstevel@tonic-gate 		s_strlcpy(buf, devname, MAXPATHLEN);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	if ((p = strstr(buf, SLASH_DEVICES)) == NULL)
11687c478bd9Sstevel@tonic-gate 		return (-1);
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	/* point to '/' after /devices */
11717c478bd9Sstevel@tonic-gate 	p += sizeof (SLASH_DEVICES) - 2;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	if ((m = strrchr(p, ':')) == NULL) {
11747c478bd9Sstevel@tonic-gate 		logdmsg("get_physname_minor: no minor name component in %s\n",
11757c478bd9Sstevel@tonic-gate 		    buf);
11767c478bd9Sstevel@tonic-gate 		return (-1);
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	*m = '\0';
11807c478bd9Sstevel@tonic-gate 	m++;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	if (client_name_type(p) == CLIENT_TYPE_UNKNOWN)
11837c478bd9Sstevel@tonic-gate 		return (-1);
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	s_strlcpy(physname, p, physname_len);
11867c478bd9Sstevel@tonic-gate 	s_strlcpy(minorname, m, minorname_len);
11877c478bd9Sstevel@tonic-gate 	logdmsg("get_physname_minor: %s: physname = %s, minor = %s\n",
11887c478bd9Sstevel@tonic-gate 	    devname, physname, minorname);
11897c478bd9Sstevel@tonic-gate 	return (0);
11907c478bd9Sstevel@tonic-gate }
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate static int
11937c478bd9Sstevel@tonic-gate devlink_callback(di_devlink_t devlink, void *argptr)
11947c478bd9Sstevel@tonic-gate {
11957c478bd9Sstevel@tonic-gate 	const char *link;
11967c478bd9Sstevel@tonic-gate 	struct devlink_cbarg *argp = argptr;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	if ((link = di_devlink_path(devlink)) != NULL) {
11997c478bd9Sstevel@tonic-gate 		s_strlcpy(argp->devlink, link, argp->len);
12007c478bd9Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate /*
12077c478bd9Sstevel@tonic-gate  * Lookup the /dev link corresponding to physname and minorname.
12087c478bd9Sstevel@tonic-gate  *
12097c478bd9Sstevel@tonic-gate  * physname	client /devices path without the /devices prefix and minor
12107c478bd9Sstevel@tonic-gate  *		name component.
12117c478bd9Sstevel@tonic-gate  * minorname	client minor name.
12127c478bd9Sstevel@tonic-gate  * devlink	caller supplied buffer where the /dev link is placed on return.
12137c478bd9Sstevel@tonic-gate  * len		caller supplied devlink buffer length
12147c478bd9Sstevel@tonic-gate  *
12157c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
12167c478bd9Sstevel@tonic-gate  */
12177c478bd9Sstevel@tonic-gate static int
12187c478bd9Sstevel@tonic-gate lookup_devlink(char *physname, char *minorname, char *devlink, size_t len)
12197c478bd9Sstevel@tonic-gate {
12207c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
12217c478bd9Sstevel@tonic-gate 	struct devlink_cbarg arg;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	if (devlink_hdl == NULL) {
12247c478bd9Sstevel@tonic-gate 		logdmsg("lookup_devlink: taking devlink snapshot\n");
12257c478bd9Sstevel@tonic-gate 		if ((devlink_hdl = di_devlink_init(NULL, 0)) == NULL) {
12267c478bd9Sstevel@tonic-gate 			logerr(gettext("di_devlink_init failed: %s\n"),
12277c478bd9Sstevel@tonic-gate 			    strerror(errno));
12287c478bd9Sstevel@tonic-gate 			clean_exit(1);
12297c478bd9Sstevel@tonic-gate 		}
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	*devlink = '\0';
12337c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, MAXPATHLEN, "%s:%s", physname, minorname);
12347c478bd9Sstevel@tonic-gate 	arg.devlink = devlink;
12357c478bd9Sstevel@tonic-gate 	arg.len = len;
12367c478bd9Sstevel@tonic-gate 	if (di_devlink_walk(devlink_hdl, NULL, buf, DI_PRIMARY_LINK, &arg,
12377c478bd9Sstevel@tonic-gate 	    devlink_callback) != 0) {
12387c478bd9Sstevel@tonic-gate 		logdmsg("lookup_devlink: di_devlink_walk on %s failed: %s\n",
12397c478bd9Sstevel@tonic-gate 		    buf, strerror(errno));
12407c478bd9Sstevel@tonic-gate 		return (-1);
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	if (*devlink == '\0') {
12447c478bd9Sstevel@tonic-gate 		logdmsg("lookup_devlink: failed to lookup devlink for %s\n",
12457c478bd9Sstevel@tonic-gate 		    buf);
12467c478bd9Sstevel@tonic-gate 		return (-1);
12477c478bd9Sstevel@tonic-gate 	}
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	logdmsg("lookup_devlink: /dev link for %s:%s = %s\n", physname,
12507c478bd9Sstevel@tonic-gate 	    minorname, devlink);
12517c478bd9Sstevel@tonic-gate 	return (0);
12527c478bd9Sstevel@tonic-gate }
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate /*
12557c478bd9Sstevel@tonic-gate  * open infile for reading and return its file pointer in *fp_in.
12567c478bd9Sstevel@tonic-gate  * open outfile for writing and return its file pointer in *fp_out.
12577c478bd9Sstevel@tonic-gate  *
12587c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
12597c478bd9Sstevel@tonic-gate  */
12607c478bd9Sstevel@tonic-gate static int
12617c478bd9Sstevel@tonic-gate open_in_out_files(char *infile, char *outfile, FILE **fp_in, FILE **fp_out)
12627c478bd9Sstevel@tonic-gate {
12637c478bd9Sstevel@tonic-gate 	FILE *fin = NULL;
12647c478bd9Sstevel@tonic-gate 	FILE *fout = NULL;
12657c478bd9Sstevel@tonic-gate 	struct stat sbuf;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	if ((fin = fopen(infile, "r")) == NULL) {
12687c478bd9Sstevel@tonic-gate 		logerr(gettext("failed to fopen %1$s: %2$s\n"),
12697c478bd9Sstevel@tonic-gate 		    infile, strerror(errno));
12707c478bd9Sstevel@tonic-gate 		goto out;
12717c478bd9Sstevel@tonic-gate 	}
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 	if (fstat(fileno(fin), &sbuf) != 0) {
12747c478bd9Sstevel@tonic-gate 		logerr(gettext("fstat failed on %1$s: %2$s\n"),
12757c478bd9Sstevel@tonic-gate 		    infile, strerror(errno));
12767c478bd9Sstevel@tonic-gate 		goto out;
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	if ((fout = fopen(outfile, "w")) == NULL) {
12807c478bd9Sstevel@tonic-gate 		logerr(gettext("failed to fopen %1$s: %2$s\n"),
12817c478bd9Sstevel@tonic-gate 		    outfile, strerror(errno));
12827c478bd9Sstevel@tonic-gate 		goto out;
12837c478bd9Sstevel@tonic-gate 	}
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	if (fchmod(fileno(fout), (sbuf.st_mode & 0777)) != 0) {
12867c478bd9Sstevel@tonic-gate 		logerr(gettext("failed to fchmod %1$s to 0%2$o: %3$s\n"),
12877c478bd9Sstevel@tonic-gate 		    outfile, sbuf.st_mode & 0777, strerror(errno));
12887c478bd9Sstevel@tonic-gate 		goto out;
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	if (fchown(fileno(fout), sbuf.st_uid, sbuf.st_gid) != 0) {
12927c478bd9Sstevel@tonic-gate 		logerr(gettext("failed to fchown %1$s to uid %2$d and "
12937c478bd9Sstevel@tonic-gate 		    "gid %3$d: %4$s\n"),
12947c478bd9Sstevel@tonic-gate 		    outfile, sbuf.st_uid, sbuf.st_gid, strerror(errno));
12957c478bd9Sstevel@tonic-gate 		goto out;
12967c478bd9Sstevel@tonic-gate 	}
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	*fp_in = fin;
12997c478bd9Sstevel@tonic-gate 	*fp_out = fout;
13007c478bd9Sstevel@tonic-gate 	return (0);
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate out:
13037c478bd9Sstevel@tonic-gate 	if (fin != NULL)
13047c478bd9Sstevel@tonic-gate 		(void) fclose(fin);
13057c478bd9Sstevel@tonic-gate 	if (fout != NULL)
13067c478bd9Sstevel@tonic-gate 		(void) fclose(fout);
13077c478bd9Sstevel@tonic-gate 	return (-1);
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate /*
13117c478bd9Sstevel@tonic-gate  * If the devname is a phci based name and not open-able, map it to vhci
13127c478bd9Sstevel@tonic-gate  * based name. If the devname is a vhci based name and not open-able, map it
13137c478bd9Sstevel@tonic-gate  * to phci based name.
13147c478bd9Sstevel@tonic-gate  *
13157c478bd9Sstevel@tonic-gate  * devname	either a /dev link or /devices name to client device
13167c478bd9Sstevel@tonic-gate  * new_devname	caller supplied buffer where the mapped device name is
13177c478bd9Sstevel@tonic-gate  *		placed on return.
13187c478bd9Sstevel@tonic-gate  * len		caller supplied new_devname buffer length
13197c478bd9Sstevel@tonic-gate  * devlink_flag	pass 1 if requesting the /dev link to the mapped device.
13207c478bd9Sstevel@tonic-gate  *		pass 0 if requesting the /devices name of the mapped device.
13217c478bd9Sstevel@tonic-gate  *
13227c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
13237c478bd9Sstevel@tonic-gate  */
13247c478bd9Sstevel@tonic-gate static int
13257c478bd9Sstevel@tonic-gate map_devname(char *devname, char *new_devname, size_t len, int devlink_flag)
13267c478bd9Sstevel@tonic-gate {
13277c478bd9Sstevel@tonic-gate 	char physname[MAXPATHLEN];
13287c478bd9Sstevel@tonic-gate 	char minor[MAXNAMELEN];
13295a4c37c9Sqh201292 	char new_physname[MAXPATHLEN];
13307c478bd9Sstevel@tonic-gate 
133160fffc19Sjw149990 	logdmsg("map_devname: checking devname %s\n", devname);
133260fffc19Sjw149990 	if ((get_physname_minor(devname, physname, sizeof (physname),
133360fffc19Sjw149990 	    minor, sizeof (minor)) == 0) &&
133460fffc19Sjw149990 	    (canopen(devname) == 0) &&
133560fffc19Sjw149990 	    (map_physname(physname, new_physname,
133660fffc19Sjw149990 	    sizeof (new_physname)) == 0)) {
133760fffc19Sjw149990 
133860fffc19Sjw149990 		logdmsg("map_devname: now looking up devlink\n");
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 		if (devlink_flag) {
13417c478bd9Sstevel@tonic-gate 			if (lookup_devlink(new_physname, minor, new_devname,
13427c478bd9Sstevel@tonic-gate 			    len) == 0)
13437c478bd9Sstevel@tonic-gate 				return (0);
13447c478bd9Sstevel@tonic-gate 		} else {
13457c478bd9Sstevel@tonic-gate 			(void) snprintf(new_devname, len, "/devices%s:%s",
13467c478bd9Sstevel@tonic-gate 			    new_physname, minor);
13477c478bd9Sstevel@tonic-gate 			return (0);
13487c478bd9Sstevel@tonic-gate 		}
13497c478bd9Sstevel@tonic-gate 	}
13507c478bd9Sstevel@tonic-gate 
135160fffc19Sjw149990 	logdmsg("map_devname: failed to find mapping for %s\n", devname);
13527c478bd9Sstevel@tonic-gate 	return (-1);
13537c478bd9Sstevel@tonic-gate }
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate /*
13565a4c37c9Sqh201292  * If the devname is a vhci based name and open-able, map it to phci
13575a4c37c9Sqh201292  * based name.
13585a4c37c9Sqh201292  *
13595a4c37c9Sqh201292  * devname	either a /dev link or /devices name to client device
13605a4c37c9Sqh201292  * new_devname	caller supplied buffer where the mapped device name without
13615a4c37c9Sqh201292  *		/devices prefix is placed on return.
13625a4c37c9Sqh201292  * len		caller supplied new_devname buffer length
13635a4c37c9Sqh201292  */
13645a4c37c9Sqh201292 static int
13655a4c37c9Sqh201292 map_openable_vhciname(char *devname, char *new_devname, size_t len)
13665a4c37c9Sqh201292 {
13675a4c37c9Sqh201292 	char physname[MAXPATHLEN];
13685a4c37c9Sqh201292 	char minor[MAXNAMELEN];
13695a4c37c9Sqh201292 	char new_physname[MAXPATHLEN];
13705a4c37c9Sqh201292 
13715a4c37c9Sqh201292 	if (get_physname_minor(devname, physname, sizeof (physname),
13725a4c37c9Sqh201292 	    minor, sizeof (minor)) == 0 &&
13735a4c37c9Sqh201292 	    canopen(devname) == 1 &&
13745a4c37c9Sqh201292 	    client_name_type(physname) == CLIENT_TYPE_VHCI &&
13755a4c37c9Sqh201292 	    vhci_to_phci_by_ioctl(physname, new_physname,
13765a4c37c9Sqh201292 		sizeof (new_physname)) == 0) {
13775a4c37c9Sqh201292 		(void) snprintf(new_devname, len, "%s:%s",
13785a4c37c9Sqh201292 		    new_physname, minor);
13795a4c37c9Sqh201292 		return (0);
13805a4c37c9Sqh201292 	}
13815a4c37c9Sqh201292 
13825a4c37c9Sqh201292 	return (-1);
13835a4c37c9Sqh201292 }
13845a4c37c9Sqh201292 /*
13857c478bd9Sstevel@tonic-gate  * Make a new /etc/vfstab:
13867c478bd9Sstevel@tonic-gate  * Read vfstab_in, convert the device name entries to appropriate vhci or phci
13877c478bd9Sstevel@tonic-gate  * based names, and write to vfstab_out. Only device names whose physical
13887c478bd9Sstevel@tonic-gate  * paths are either phci or vhci based names and not open-able are considered
13897c478bd9Sstevel@tonic-gate  * for conversion. Open-able device name entries are not converted as it
13907c478bd9Sstevel@tonic-gate  * means that the device is already accessible; hence no need to convert.
13917c478bd9Sstevel@tonic-gate  *
13927c478bd9Sstevel@tonic-gate  * Returns:
13937c478bd9Sstevel@tonic-gate  * 	0	successful but vfstab_out contents are the same as vfstab_in
13947c478bd9Sstevel@tonic-gate  *	1	successful and vfstab_out changed from vfstab_in
13957c478bd9Sstevel@tonic-gate  *	-1	failed
13967c478bd9Sstevel@tonic-gate  */
13977c478bd9Sstevel@tonic-gate static int
13987c478bd9Sstevel@tonic-gate update_vfstab(char *vfstab_in, char *vfstab_out)
13997c478bd9Sstevel@tonic-gate {
14007c478bd9Sstevel@tonic-gate 	FILE *fp_in, *fp_out;
14017c478bd9Sstevel@tonic-gate 	char *buf, *tmpbuf;
14027c478bd9Sstevel@tonic-gate 	char *vfs_cache[2];
14037c478bd9Sstevel@tonic-gate 	int idx = 0, count = 0;
14047c478bd9Sstevel@tonic-gate 	int rv = -1;
14057c478bd9Sstevel@tonic-gate 	int vfstab_updated = 0;
14067c478bd9Sstevel@tonic-gate 	int i;
14077c478bd9Sstevel@tonic-gate 	char cdev[MAXPATHLEN];
14087c478bd9Sstevel@tonic-gate 	char bdev[MAXPATHLEN];
14097c478bd9Sstevel@tonic-gate 	char mntpt[MAXPATHLEN];
14107c478bd9Sstevel@tonic-gate 	char fstype[512];
14117c478bd9Sstevel@tonic-gate 	char fsckpass[512];
14127c478bd9Sstevel@tonic-gate 	char mntboot[512];
14137c478bd9Sstevel@tonic-gate 	char mntopt[MAX_MNTOPT_STR];
14147c478bd9Sstevel@tonic-gate 	char phys_bdev[MAXPATHLEN], phys_cdev[MAXPATHLEN];
14157c478bd9Sstevel@tonic-gate 	char bdev_minor[MAXNAMELEN], cdev_minor[MAXNAMELEN];
14167c478bd9Sstevel@tonic-gate 	char new_physname[MAXPATHLEN];
14177c478bd9Sstevel@tonic-gate 	char new_bdevlink[MAXPATHLEN], new_cdevlink[MAXPATHLEN];
14187c478bd9Sstevel@tonic-gate 	char fmt[80];
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	if (open_in_out_files(vfstab_in, vfstab_out, &fp_in, &fp_out) != 0)
14217c478bd9Sstevel@tonic-gate 		return (-1);
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	/*
14247c478bd9Sstevel@tonic-gate 	 * Read one line at time from vfstab_in. If no conversion is needed
14257c478bd9Sstevel@tonic-gate 	 * for the line simply write the line to vfstab_out. If conversion is
14267c478bd9Sstevel@tonic-gate 	 * needed, first write the existing line as a comment to vfstab_out
14277c478bd9Sstevel@tonic-gate 	 * and then write the converted line.
14287c478bd9Sstevel@tonic-gate 	 *
14297c478bd9Sstevel@tonic-gate 	 * To avoid commented entries piling up in vfstab in case if the
14307c478bd9Sstevel@tonic-gate 	 * user runs stmsboot multiple times to switch on and off from mpxio,
14317c478bd9Sstevel@tonic-gate 	 * add the commented line only if not already there. To do this
14327c478bd9Sstevel@tonic-gate 	 * cache the last two vfstab lines processed and add the commented
14337c478bd9Sstevel@tonic-gate 	 * entry only if it is not found in the cache. We only need to cache
14347c478bd9Sstevel@tonic-gate 	 * the last two lines because a device can have at most two names -
14357c478bd9Sstevel@tonic-gate 	 * one mpxio and one non-mpxio name. Therefore for any device name
14367c478bd9Sstevel@tonic-gate 	 * entry we at most add two comments - one with mpxio name and one
14377c478bd9Sstevel@tonic-gate 	 * with non-mpxio name - no matter how many times stmsboot is run.
14387c478bd9Sstevel@tonic-gate 	 */
14397c478bd9Sstevel@tonic-gate 	buf = (char *)s_malloc(VFS_LINE_MAX);
14407c478bd9Sstevel@tonic-gate 	tmpbuf = (char *)s_malloc(VFS_LINE_MAX);
14417c478bd9Sstevel@tonic-gate 	vfs_cache[0] = (char *)s_malloc(VFS_LINE_MAX);
14427c478bd9Sstevel@tonic-gate 	vfs_cache[1] = (char *)s_malloc(VFS_LINE_MAX);
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	(void) snprintf(fmt, sizeof (fmt),
14457c478bd9Sstevel@tonic-gate 	    "%%%ds %%%ds %%%ds %%%ds %%%ds %%%ds %%%ds", sizeof (bdev) - 1,
14467c478bd9Sstevel@tonic-gate 	    sizeof (cdev) - 1, sizeof (mntpt) - 1, sizeof (fstype) - 1,
14477c478bd9Sstevel@tonic-gate 	    sizeof (fsckpass) - 1, sizeof (mntboot) - 1, sizeof (mntopt) - 1);
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	while (fgets(buf, VFS_LINE_MAX, fp_in) != NULL) {
14507c478bd9Sstevel@tonic-gate 		if (strlen(buf) == (VFS_LINE_MAX - 1) &&
14517c478bd9Sstevel@tonic-gate 		    buf[VFS_LINE_MAX-2] != '\n') {
14527c478bd9Sstevel@tonic-gate 			logerr(gettext("%1$s line size too long, "
14537c478bd9Sstevel@tonic-gate 			    "exceeded %2$d: \"%3$s\"\n"),
14547c478bd9Sstevel@tonic-gate 			    VFSTAB, VFS_LINE_MAX - 2, buf);
14557c478bd9Sstevel@tonic-gate 			goto out;
14567c478bd9Sstevel@tonic-gate 		}
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 		/* LINTED - format specifier */
14597c478bd9Sstevel@tonic-gate 		if ((sscanf(buf, fmt, bdev, cdev, mntpt,
14607c478bd9Sstevel@tonic-gate 		    fstype, fsckpass, mntboot, mntopt) != 7) ||
14617c478bd9Sstevel@tonic-gate 		    (bdev[0] == '#') ||
14627c478bd9Sstevel@tonic-gate 		    (get_physname_minor(bdev, phys_bdev, sizeof (phys_bdev),
14637c478bd9Sstevel@tonic-gate 		    bdev_minor, sizeof (bdev_minor)) != 0) ||
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 		    (strcmp(fstype, "swap") != 0 &&
14667c478bd9Sstevel@tonic-gate 		    ((get_physname_minor(cdev, phys_cdev, sizeof (phys_cdev),
14677c478bd9Sstevel@tonic-gate 		    cdev_minor, sizeof (cdev_minor)) != 0) ||
14687c478bd9Sstevel@tonic-gate 		    (strcmp(phys_bdev, phys_cdev) != 0))) ||
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 		    canopen(bdev) ||
14717c478bd9Sstevel@tonic-gate 		    (map_physname(phys_bdev, new_physname,
14727c478bd9Sstevel@tonic-gate 		    sizeof (new_physname)) != 0) ||
14737c478bd9Sstevel@tonic-gate 		    (lookup_devlink(new_physname, bdev_minor, new_bdevlink,
14747c478bd9Sstevel@tonic-gate 		    sizeof (new_bdevlink)) != 0) ||
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 		    (strcmp(fstype, "swap") != 0 &&
14777c478bd9Sstevel@tonic-gate 		    (lookup_devlink(new_physname, cdev_minor, new_cdevlink,
14787c478bd9Sstevel@tonic-gate 		    sizeof (new_cdevlink)) != 0))) {
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 			/* cache the last two entries */
14817c478bd9Sstevel@tonic-gate 			(void) strlcpy(vfs_cache[idx], buf, VFS_LINE_MAX);
14827c478bd9Sstevel@tonic-gate 			idx = (idx == 0) ? 1 : 0;
14837c478bd9Sstevel@tonic-gate 			if (count < 2)
14847c478bd9Sstevel@tonic-gate 				count++;
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 			if (fputs(buf, fp_out) == EOF) {
14877c478bd9Sstevel@tonic-gate 				logerr(gettext("fputs \"%1$s\" to %2$s "
14887c478bd9Sstevel@tonic-gate 				    "failed: %3$s\n"),
14897c478bd9Sstevel@tonic-gate 				    buf, vfstab_out, strerror(errno));
14907c478bd9Sstevel@tonic-gate 				goto out;
14917c478bd9Sstevel@tonic-gate 			}
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 		} else {
14947c478bd9Sstevel@tonic-gate 			/*
14957c478bd9Sstevel@tonic-gate 			 * comment the entry in vfstab only if it is not
14967c478bd9Sstevel@tonic-gate 			 * already in the cache.
14977c478bd9Sstevel@tonic-gate 			 */
14987c478bd9Sstevel@tonic-gate 			if (client_name_type(phys_bdev) == CLIENT_TYPE_VHCI)
14997c478bd9Sstevel@tonic-gate 				(void) snprintf(tmpbuf, VFS_LINE_MAX,
15007c478bd9Sstevel@tonic-gate 				    "# mpxio: %s", buf);
15017c478bd9Sstevel@tonic-gate 			else
15027c478bd9Sstevel@tonic-gate 				(void) snprintf(tmpbuf, VFS_LINE_MAX,
15037c478bd9Sstevel@tonic-gate 				    "# non-mpxio: %s", buf);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 			for (i = 0; i < count; i++) {
15067c478bd9Sstevel@tonic-gate 				if (strcmp(vfs_cache[i], tmpbuf) == 0)
15077c478bd9Sstevel@tonic-gate 					break;
15087c478bd9Sstevel@tonic-gate 			}
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 			if (i == count) {
15117c478bd9Sstevel@tonic-gate 				if (fputs(tmpbuf, fp_out) == EOF) {
15127c478bd9Sstevel@tonic-gate 					logerr(gettext("fputs \"%1$s\" to %2$s "
15137c478bd9Sstevel@tonic-gate 					    "failed: %3$s\n"), tmpbuf,
15147c478bd9Sstevel@tonic-gate 					    vfstab_out, strerror(errno));
15157c478bd9Sstevel@tonic-gate 					goto out;
15167c478bd9Sstevel@tonic-gate 				}
15177c478bd9Sstevel@tonic-gate 			}
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 			count = 0;
15207c478bd9Sstevel@tonic-gate 			idx = 0;
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 			if (fprintf(fp_out, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
15237c478bd9Sstevel@tonic-gate 			    new_bdevlink,
15247c478bd9Sstevel@tonic-gate 			    (strcmp(fstype, "swap") != 0) ? new_cdevlink : cdev,
15257c478bd9Sstevel@tonic-gate 			    mntpt, fstype, fsckpass, mntboot, mntopt) < 0) {
15267c478bd9Sstevel@tonic-gate 				logerr(gettext("fprintf failed to write to "
15277c478bd9Sstevel@tonic-gate 				    "%1$s: %2$s\n"),
15287c478bd9Sstevel@tonic-gate 				    vfstab_out, strerror(errno));
15297c478bd9Sstevel@tonic-gate 				goto out;
15307c478bd9Sstevel@tonic-gate 			}
15317c478bd9Sstevel@tonic-gate 			vfstab_updated = 1;
15327c478bd9Sstevel@tonic-gate 		}
15337c478bd9Sstevel@tonic-gate 	}
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	rv = vfstab_updated;
15367c478bd9Sstevel@tonic-gate out:
15377c478bd9Sstevel@tonic-gate 	(void) fclose(fp_in);
15387c478bd9Sstevel@tonic-gate 	(void) fclose(fp_out);
15397c478bd9Sstevel@tonic-gate 	free(buf);
15407c478bd9Sstevel@tonic-gate 	free(tmpbuf);
15417c478bd9Sstevel@tonic-gate 	free(vfs_cache[0]);
15427c478bd9Sstevel@tonic-gate 	free(vfs_cache[1]);
15437c478bd9Sstevel@tonic-gate 	return (rv);
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate /*
15477c478bd9Sstevel@tonic-gate  * if guidmap is 0, list non-STMS to STMS device name mappings for the
15487c478bd9Sstevel@tonic-gate  * specified controller.
15497c478bd9Sstevel@tonic-gate  * if guidmap is 1, list non-STMS to GUID mappings for the specified controller.
15507c478bd9Sstevel@tonic-gate  * If controller is -1 list mappings for all controllers.
15517c478bd9Sstevel@tonic-gate  *
15527c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
15537c478bd9Sstevel@tonic-gate  */
15547c478bd9Sstevel@tonic-gate static int
15557c478bd9Sstevel@tonic-gate list_mappings(int controller, int guidmap)
15567c478bd9Sstevel@tonic-gate {
15577c478bd9Sstevel@tonic-gate 	int cnum, len, mapped;
15587c478bd9Sstevel@tonic-gate 	int header = 1;
15597c478bd9Sstevel@tonic-gate 	char *p1, *p2;
15607c478bd9Sstevel@tonic-gate 	DIR *dirp;
15617c478bd9Sstevel@tonic-gate 	struct dirent *direntry;
15627c478bd9Sstevel@tonic-gate 	char devname[MAXPATHLEN];
15637c478bd9Sstevel@tonic-gate 	char physname[MAXPATHLEN];
15647c478bd9Sstevel@tonic-gate 	char new_devname[MAXPATHLEN];
15657c478bd9Sstevel@tonic-gate 	char new_physname[MAXPATHLEN];
15667c478bd9Sstevel@tonic-gate 	char guid[MAXPATHLEN];
15677c478bd9Sstevel@tonic-gate 	char minor[MAXNAMELEN];
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	if ((dirp = opendir("/dev/rdsk")) == NULL)
15707c478bd9Sstevel@tonic-gate 		return (-1);
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	while ((direntry = readdir(dirp)) != NULL) {
15737c478bd9Sstevel@tonic-gate 		if (strcmp(direntry->d_name, ".") == 0 ||
15747c478bd9Sstevel@tonic-gate 		    strcmp(direntry->d_name, "..") == 0 ||
15757c478bd9Sstevel@tonic-gate 		    (len = strlen(direntry->d_name)) < 2 ||
15767c478bd9Sstevel@tonic-gate 		    strcmp(direntry->d_name + len - 2, "s0") != 0 ||
15777c478bd9Sstevel@tonic-gate 		    sscanf(direntry->d_name, "c%dt", &cnum) != 1 ||
15787c478bd9Sstevel@tonic-gate 		    (controller != -1 && controller != cnum))
15797c478bd9Sstevel@tonic-gate 			continue;
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 		(void) snprintf(devname, MAXPATHLEN, "/dev/rdsk/%s",
15827c478bd9Sstevel@tonic-gate 		    direntry->d_name);
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 		if (get_physname_minor(devname, physname, sizeof (physname),
15857c478bd9Sstevel@tonic-gate 		    minor, sizeof (minor)) != 0 ||
158660fffc19Sjw149990 		    client_name_type(physname) != CLIENT_TYPE_PHCI) {
158760fffc19Sjw149990 			logdmsg("list_mappings: continuing\n");
15887c478bd9Sstevel@tonic-gate 			continue;
158960fffc19Sjw149990 		}
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 		/*
15927c478bd9Sstevel@tonic-gate 		 * First try phci_to_vhci() mapping. It will work if the
15937c478bd9Sstevel@tonic-gate 		 * device is under MPxIO control. If the device is not under
15947c478bd9Sstevel@tonic-gate 		 * MPxIO, phci_to_vhci() will fail in which case try to lookup
15957c478bd9Sstevel@tonic-gate 		 * if an old mapping exists using guid lookup.
15967c478bd9Sstevel@tonic-gate 		 */
15977c478bd9Sstevel@tonic-gate 		mapped = 1;
15987c478bd9Sstevel@tonic-gate 		if (phci_to_vhci(physname, new_physname,
15997c478bd9Sstevel@tonic-gate 		    sizeof (new_physname)) != 0) {
16007c478bd9Sstevel@tonic-gate 			if (get_guid(physname, guid, sizeof (guid), 1,
16017c478bd9Sstevel@tonic-gate 			    DI_NODE_NIL) == 0)
16027c478bd9Sstevel@tonic-gate 				(void) snprintf(new_physname, MAXPATHLEN,
16035a4c37c9Sqh201292 				    "/scsi_vhci/%s%s", DISK_AT_G, guid);
16047c478bd9Sstevel@tonic-gate 			else
16057c478bd9Sstevel@tonic-gate 				mapped = 0;
16067c478bd9Sstevel@tonic-gate 		}
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 		if (mapped == 0)
16097c478bd9Sstevel@tonic-gate 			continue;
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 		/* strip the slice number part */
16127c478bd9Sstevel@tonic-gate 		devname[strlen(devname) - 2] = '\0';
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 		if (guidmap == 0) {
16157c478bd9Sstevel@tonic-gate 			if (lookup_devlink(new_physname, minor,
16167c478bd9Sstevel@tonic-gate 			    new_devname, sizeof (new_devname)) != 0)
16177c478bd9Sstevel@tonic-gate 				continue;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 			/* strip the slice number part */
16207c478bd9Sstevel@tonic-gate 			new_devname[strlen(new_devname) - 2] = '\0';
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 			if (header) {
16237c478bd9Sstevel@tonic-gate 				(void) printf(
16247c478bd9Sstevel@tonic-gate 				    gettext("non-STMS device name\t\t\t"
16257c478bd9Sstevel@tonic-gate 				    "STMS device name\n"
16267c478bd9Sstevel@tonic-gate 				    "------------------------------------------"
16277c478bd9Sstevel@tonic-gate 				    "------------------------\n"));
16287c478bd9Sstevel@tonic-gate 				header = 0;
16297c478bd9Sstevel@tonic-gate 			}
16307c478bd9Sstevel@tonic-gate 			(void) printf("%s\t\t%s\n", devname, new_devname);
16317c478bd9Sstevel@tonic-gate 		} else {
16327c478bd9Sstevel@tonic-gate 			/* extract guid part */
163360fffc19Sjw149990 			/* we should be using a getguid() call instead */
163460fffc19Sjw149990 			if ((p1 = strstr(new_physname, "@"))
163560fffc19Sjw149990 			    == NULL) {
16367c478bd9Sstevel@tonic-gate 				logdmsg("invalid vhci: %s\n", new_physname);
16377c478bd9Sstevel@tonic-gate 				continue;
16387c478bd9Sstevel@tonic-gate 			}
163960fffc19Sjw149990 
164060fffc19Sjw149990 			logdmsg("\tp1 = %s\n", p1);
164160fffc19Sjw149990 
164260fffc19Sjw149990 			p1 += 2; /* "@" + [nwg] */
16437c478bd9Sstevel@tonic-gate 			if ((p2 = strrchr(p1, ':')) != NULL)
16447c478bd9Sstevel@tonic-gate 				*p2 = '\0';
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 			if (header) {
16477c478bd9Sstevel@tonic-gate 				(void) printf(
16487c478bd9Sstevel@tonic-gate 				    gettext("non-STMS device name\t\t\tGUID\n"
16497c478bd9Sstevel@tonic-gate 				    "------------------------------------------"
16507c478bd9Sstevel@tonic-gate 				    "------------------------\n"));
16517c478bd9Sstevel@tonic-gate 				header = 0;
16527c478bd9Sstevel@tonic-gate 			}
16537c478bd9Sstevel@tonic-gate 			(void) printf("%s\t\t%s\n", devname, p1);
16547c478bd9Sstevel@tonic-gate 		}
16557c478bd9Sstevel@tonic-gate 	}
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
16587c478bd9Sstevel@tonic-gate 	return (0);
16597c478bd9Sstevel@tonic-gate }
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate /*
16627c478bd9Sstevel@tonic-gate  * Check if the file can be opened.
16637c478bd9Sstevel@tonic-gate  *
16647c478bd9Sstevel@tonic-gate  * Return 1 if the file can be opened, 0 otherwise.
16657c478bd9Sstevel@tonic-gate  */
16667c478bd9Sstevel@tonic-gate static int
16677c478bd9Sstevel@tonic-gate canopen(char *filename)
16687c478bd9Sstevel@tonic-gate {
16697c478bd9Sstevel@tonic-gate 	int fd;
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	if ((fd = open(filename, O_RDONLY)) == -1)
16727c478bd9Sstevel@tonic-gate 		return (0);
16737c478bd9Sstevel@tonic-gate 
167460fffc19Sjw149990 	logdmsg("canopen: was able to open %s\n", filename);
16757c478bd9Sstevel@tonic-gate 	(void) close(fd);
16767c478bd9Sstevel@tonic-gate 	return (1);
16777c478bd9Sstevel@tonic-gate }
16787c478bd9Sstevel@tonic-gate 
167960fffc19Sjw149990 
168060fffc19Sjw149990 /*
168160fffc19Sjw149990  * This function traverses the device tree looking for nodes
168260fffc19Sjw149990  * which have "drivername" as a property. We return a list of
1683*815dd917Sjmcp  * these nodes, without duplicate entries.
168460fffc19Sjw149990  * Since there can be many different pci/pcie devices that all
168560fffc19Sjw149990  * share the same driver but which have different pci vid/did
168660fffc19Sjw149990  * combinations, we have to be smart about returning only those
168760fffc19Sjw149990  * pci vid/dids which have the "sas-*" property unless the
168860fffc19Sjw149990  * drivername is "fp", in which case we're searching for "node-wwn"
168960fffc19Sjw149990  */
169060fffc19Sjw149990 static void
169160fffc19Sjw149990 list_nodes(char *drivername)
169260fffc19Sjw149990 {
169360fffc19Sjw149990 	di_node_t devroot = DI_NODE_NIL;
169460fffc19Sjw149990 	di_node_t thisnode = DI_NODE_NIL;
1695*815dd917Sjmcp 	char *aliaslist;
1696*815dd917Sjmcp 	char *iitype = NULL; /* the "initiator-interconnect-type" property */
169760fffc19Sjw149990 	int *intprop = NULL;
169860fffc19Sjw149990 	int i = 1; /* fencepost */
1699*815dd917Sjmcp 	int irval = 0;
1700*815dd917Sjmcp 	int crval = 0;
170160fffc19Sjw149990 
170260fffc19Sjw149990 	/*
170360fffc19Sjw149990 	 * Since the "fp" driver enumerates with its own name,
170460fffc19Sjw149990 	 * we can special-case its handling.
170560fffc19Sjw149990 	 */
170660fffc19Sjw149990 	if (strcmp(drvname, "fp") == 0) {
170760fffc19Sjw149990 		(void) fprintf(stdout, "fp\n");
170860fffc19Sjw149990 	} else {
170960fffc19Sjw149990 
171060fffc19Sjw149990 		if ((devroot = di_init("/", DINFOCPYALL | DINFOFORCE))
171160fffc19Sjw149990 		    == DI_NODE_NIL) {
171260fffc19Sjw149990 			logerr(gettext("list_nodes: di_init failed: "
171360fffc19Sjw149990 			"%s\n"), strerror(errno));
171460fffc19Sjw149990 		}
171560fffc19Sjw149990 
171660fffc19Sjw149990 		if ((thisnode = di_drv_first_node(drivername, devroot))
171760fffc19Sjw149990 		    != NULL) {
171860fffc19Sjw149990 			logdmsg("list_nodes: searching for property "
171960fffc19Sjw149990 			    "%s\n", drvprop);
172060fffc19Sjw149990 
172160fffc19Sjw149990 			aliaslist = s_malloc(1024 * sizeof (char));
172260fffc19Sjw149990 			bzero(aliaslist, 1024);
172360fffc19Sjw149990 			while (thisnode != DI_NODE_NIL) {
172460fffc19Sjw149990 				logdmsg("devfs-name %s driver-name %s "
172560fffc19Sjw149990 				    "node-name %s\n",
172660fffc19Sjw149990 				    di_devfs_path(thisnode),
172760fffc19Sjw149990 				    di_driver_name(thisnode),
172860fffc19Sjw149990 				    di_node_name(thisnode));
172960fffc19Sjw149990 
173060fffc19Sjw149990 			/* We check the child node for drvprop */
1731*815dd917Sjmcp 			irval = di_prop_lookup_ints(DDI_DEV_T_ANY,
1732*815dd917Sjmcp 			    di_child_node(thisnode), drvprop, &intprop);
1733*815dd917Sjmcp 			/* and this node for the correct initiator type */
1734*815dd917Sjmcp 			crval = di_prop_lookup_strings(DDI_DEV_T_ANY,
1735*815dd917Sjmcp 			    thisnode, "initiator-interconnect-type", &iitype);
1736*815dd917Sjmcp 
1737*815dd917Sjmcp 			/*
1738*815dd917Sjmcp 			 * examine the return codes from di_prop_lookup*()
1739*815dd917Sjmcp 			 * functions to guard against library errors
1740*815dd917Sjmcp 			 */
1741*815dd917Sjmcp 			if ((irval > -1) || ((crval > -1) &&
1742*815dd917Sjmcp 			    (strncmp(iitype, "SATA", 4) == 0))) {
174360fffc19Sjw149990 
174460fffc19Sjw149990 				if (strstr(aliaslist,
1745*815dd917Sjmcp 				    di_node_name(thisnode)) == (char *)NULL) {
174660fffc19Sjw149990 					char *nname;
174760fffc19Sjw149990 
174860fffc19Sjw149990 					nname = di_node_name(thisnode);
174960fffc19Sjw149990 
175060fffc19Sjw149990 					if (i) {
175160fffc19Sjw149990 					(void) snprintf(aliaslist,
1752*815dd917Sjmcp 					    strlen(nname) + 1, "%s", nname);
175360fffc19Sjw149990 						--i;
175460fffc19Sjw149990 					} else {
175560fffc19Sjw149990 					if (strstr(aliaslist,
175660fffc19Sjw149990 					    di_node_name(thisnode)) ==
175760fffc19Sjw149990 					    (char *)NULL) {
1758*815dd917Sjmcp 						/* add 2 for the n-1 + "|" */
175960fffc19Sjw149990 						(void) snprintf(aliaslist,
1760*815dd917Sjmcp 						    strlen(nname) + 2 +
176160fffc19Sjw149990 						    strlen(aliaslist),
176260fffc19Sjw149990 						    "%s|%s", aliaslist,
176360fffc19Sjw149990 						    nname);
176460fffc19Sjw149990 						}
176560fffc19Sjw149990 					}
176660fffc19Sjw149990 				}
176760fffc19Sjw149990 			} else {
176860fffc19Sjw149990 				logdmsg("unable to lookup property %s "
176960fffc19Sjw149990 				    "for node %s. Error %d: %s\n",
177060fffc19Sjw149990 				    drvprop, di_devfs_path(thisnode),
177160fffc19Sjw149990 				    errno, strerror(errno));
177260fffc19Sjw149990 			}
177360fffc19Sjw149990 			thisnode = di_drv_next_node(thisnode);
177460fffc19Sjw149990 		}
177560fffc19Sjw149990 		(void) fprintf(stdout, "%s\n", aliaslist);
177660fffc19Sjw149990 		}
177760fffc19Sjw149990 
177860fffc19Sjw149990 		di_fini(devroot);
177960fffc19Sjw149990 	}
178060fffc19Sjw149990 }
178160fffc19Sjw149990 
17827c478bd9Sstevel@tonic-gate static void
17837c478bd9Sstevel@tonic-gate logerr(char *msg, ...)
17847c478bd9Sstevel@tonic-gate {
17857c478bd9Sstevel@tonic-gate 	va_list ap;
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", stmsboot);
17887c478bd9Sstevel@tonic-gate 	va_start(ap, msg);
17897c478bd9Sstevel@tonic-gate 	/* LINTED - format specifier */
17907c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, msg, ap);
17917c478bd9Sstevel@tonic-gate 	va_end(ap);
17927c478bd9Sstevel@tonic-gate }
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate /* log debug message */
17957c478bd9Sstevel@tonic-gate static void
17967c478bd9Sstevel@tonic-gate logdmsg(char *msg, ...)
17977c478bd9Sstevel@tonic-gate {
17987c478bd9Sstevel@tonic-gate 	va_list ap;
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	if (debug) {
18017c478bd9Sstevel@tonic-gate 		va_start(ap, msg);
18027c478bd9Sstevel@tonic-gate 		/* LINTED - format specifier */
18037c478bd9Sstevel@tonic-gate 		(void) vprintf(msg, ap);
18047c478bd9Sstevel@tonic-gate 		va_end(ap);
18057c478bd9Sstevel@tonic-gate 	}
18067c478bd9Sstevel@tonic-gate }
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate static void *
18097c478bd9Sstevel@tonic-gate s_malloc(const size_t size)
18107c478bd9Sstevel@tonic-gate {
18117c478bd9Sstevel@tonic-gate 	void *rp;
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	if ((rp = malloc(size)) == NULL) {
18147c478bd9Sstevel@tonic-gate 		logerr(gettext("malloc failed to allocate %d bytes\n"), size);
18157c478bd9Sstevel@tonic-gate 		clean_exit(1);
18167c478bd9Sstevel@tonic-gate 	}
18177c478bd9Sstevel@tonic-gate 	return (rp);
18187c478bd9Sstevel@tonic-gate }
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate static char *
18217c478bd9Sstevel@tonic-gate s_strdup(const char *ptr)
18227c478bd9Sstevel@tonic-gate {
18237c478bd9Sstevel@tonic-gate 	void *rp;
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	if ((rp = strdup(ptr)) == NULL) {
18267c478bd9Sstevel@tonic-gate 		logerr(gettext("strdup failed to dup %s\n"), ptr);
18277c478bd9Sstevel@tonic-gate 		clean_exit(1);
18287c478bd9Sstevel@tonic-gate 	}
18297c478bd9Sstevel@tonic-gate 	return (rp);
18307c478bd9Sstevel@tonic-gate }
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate static void
18337c478bd9Sstevel@tonic-gate s_strlcpy(char *dst, const char *src, size_t dstsize)
18347c478bd9Sstevel@tonic-gate {
18357c478bd9Sstevel@tonic-gate 	int n;
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	if ((n = strlcpy(dst, src, dstsize)) >= dstsize) {
18387c478bd9Sstevel@tonic-gate 		logerr(gettext("strlcpy: destination buffer size is %1$d "
18397c478bd9Sstevel@tonic-gate 		    "bytes, need to at least %2$d bytes\n"), dstsize, n + 1);
18407c478bd9Sstevel@tonic-gate 		clean_exit(1);
18417c478bd9Sstevel@tonic-gate 	}
18427c478bd9Sstevel@tonic-gate }
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate static void
18457c478bd9Sstevel@tonic-gate clean_exit(int status)
18467c478bd9Sstevel@tonic-gate {
18477c478bd9Sstevel@tonic-gate 	if (devinfo_root != DI_NODE_NIL)
18487c478bd9Sstevel@tonic-gate 		di_fini(devinfo_root);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	if (devlink_hdl != NULL)
18517c478bd9Sstevel@tonic-gate 		(void) di_devlink_fini(&devlink_hdl);
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if (vhci_fd != -1)
18547c478bd9Sstevel@tonic-gate 		(void) close(vhci_fd);
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	exit(status);
18577c478bd9Sstevel@tonic-gate }
1858