xref: /titanic_53/usr/src/cmd/stmsboot/stmsboot_util.c (revision cfcc3aa4ec17a7ad614853f3100e206ebead7b68)
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 #define	SLASH_DISK_AT	"/ssd@"
615a4c37c9Sqh201292 #define	DISK_AT_G	"ssd@g"
625a4c37c9Sqh201292 #else	/* sparc */
635a4c37c9Sqh201292 #define	DISK_NODE_NAME	"disk"
645a4c37c9Sqh201292 #define	DISK_DRV_NAME	"sd"
655a4c37c9Sqh201292 #define	SLASH_DISK_AT	"/disk@"
665a4c37c9Sqh201292 #define	DISK_AT_G	"disk@g"
675a4c37c9Sqh201292 #endif
685a4c37c9Sqh201292 
697c478bd9Sstevel@tonic-gate #define	SLASH_FP_AT	"/fp@"
707c478bd9Sstevel@tonic-gate #define	SLASH_SCSI_VHCI	"/scsi_vhci"
717c478bd9Sstevel@tonic-gate #define	DEV_DSK		"/dev/dsk/"
727c478bd9Sstevel@tonic-gate #define	DEV_RDSK	"/dev/rdsk/"
737c478bd9Sstevel@tonic-gate #define	SYS_FILENAME_LEN	256
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Save directory is the directory in which system files are saved.
777c478bd9Sstevel@tonic-gate  * Save directory must be under the root filesystem, as this program is
787c478bd9Sstevel@tonic-gate  * typically run before any other filesystems are mounted.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate #define	SAVE_DIR	"/etc/mpxio"
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /* fcp driver publishes this property */
837c478bd9Sstevel@tonic-gate #define	NODE_WWN_PROP	"node-wwn"
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate typedef enum {
867c478bd9Sstevel@tonic-gate 	CLIENT_TYPE_UNKNOWN,
877c478bd9Sstevel@tonic-gate 	CLIENT_TYPE_PHCI,
887c478bd9Sstevel@tonic-gate 	CLIENT_TYPE_VHCI
897c478bd9Sstevel@tonic-gate } client_type_t;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate struct devlink_cbarg {
927c478bd9Sstevel@tonic-gate 	char *devlink;
937c478bd9Sstevel@tonic-gate 	size_t len;
947c478bd9Sstevel@tonic-gate };
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static di_node_t devinfo_root = DI_NODE_NIL;
977c478bd9Sstevel@tonic-gate static di_devlink_handle_t devlink_hdl = NULL;
987c478bd9Sstevel@tonic-gate static int vhci_fd = -1;
997c478bd9Sstevel@tonic-gate static int patch_vfstab, cap_m_option, debug;
1007c478bd9Sstevel@tonic-gate static int list_option, list_guid_mappings, list_controllernum = -1;
1017c478bd9Sstevel@tonic-gate static char *mapdev = "";
1025a4c37c9Sqh201292 static char *map_vhciname = "";
1037c478bd9Sstevel@tonic-gate static char *stmsboot = "stmsboot";
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static int make_temp(char *, char *, char *, size_t);
1067c478bd9Sstevel@tonic-gate static void commit_change(char *, char *, char *, int);
1077c478bd9Sstevel@tonic-gate static int map_devname(char *, char *, size_t, int);
1087c478bd9Sstevel@tonic-gate static int update_vfstab(char *, char *);
1097c478bd9Sstevel@tonic-gate static int list_mappings(int, int);
1107c478bd9Sstevel@tonic-gate static int canopen(char *);
1117c478bd9Sstevel@tonic-gate static void logerr(char *, ...);
1127c478bd9Sstevel@tonic-gate static void logdmsg(char *, ...);
1137c478bd9Sstevel@tonic-gate static void *s_malloc(const size_t);
1147c478bd9Sstevel@tonic-gate static char *s_strdup(const char *);
1157c478bd9Sstevel@tonic-gate static void s_strlcpy(char *, const char *, size_t);
116*cfcc3aa4Sqh201292 static int map_openable_vhciname(char *, char *, size_t);
117264d6c47Seota /*
118264d6c47Seota  * Using an exit function not marked __NORETURN causes a warning with gcc.
119264d6c47Seota  * To suppress the warning, use __NORETURN attribute.
120264d6c47Seota  */
121264d6c47Seota static void clean_exit(int)__NORETURN;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Print usage and exit.
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate static void
1277c478bd9Sstevel@tonic-gate usage(char *argv0)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	char *progname;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	progname = strrchr(argv0, '/');
1327c478bd9Sstevel@tonic-gate 	if (progname != NULL)
1337c478bd9Sstevel@tonic-gate 		progname++;
1347c478bd9Sstevel@tonic-gate 	else
1357c478bd9Sstevel@tonic-gate 		progname = argv0;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/*
1387c478bd9Sstevel@tonic-gate 	 * -u	update /etc/vfstab
1397c478bd9Sstevel@tonic-gate 	 * -m devname
1407c478bd9Sstevel@tonic-gate 	 *	if devname is phci based name and not open-able, map it to
1417c478bd9Sstevel@tonic-gate 	 *	vhci based /devices name.
1427c478bd9Sstevel@tonic-gate 	 *	if devname is vhci based name and not open-able, map it to
1437c478bd9Sstevel@tonic-gate 	 *	phci based /devices name.
1447c478bd9Sstevel@tonic-gate 	 * -M devname
1457c478bd9Sstevel@tonic-gate 	 *	same as -m except that /dev link is printed instead of
1467c478bd9Sstevel@tonic-gate 	 *	/devices name.
1477c478bd9Sstevel@tonic-gate 	 * -l controller
1487c478bd9Sstevel@tonic-gate 	 *	list non-STMS to STMS device name mappings for the specific
1497c478bd9Sstevel@tonic-gate 	 *	controller
1507c478bd9Sstevel@tonic-gate 	 * -L	list non-STMS to STMS device name mappings for all controllers
1515a4c37c9Sqh201292 	 * -p devname
1525a4c37c9Sqh201292 	 *	if devname is vhci based name and open-able, get the first
1535a4c37c9Sqh201292 	 *	onlined phci based name without /devices prefix.
1545a4c37c9Sqh201292 	 *	Used in stmsboot to update the phci based bootpath.
1557c478bd9Sstevel@tonic-gate 	 */
1567c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("usage: %s -u | -m devname | "
1575a4c37c9Sqh201292 	    "-M devname | -l controller | -L | -p devname\n"), progname);
1587c478bd9Sstevel@tonic-gate 	exit(2);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * Parse command line arguments.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate static void
1657c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	char opt;
1687c478bd9Sstevel@tonic-gate 	int n = 0;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (argc == 1) {
1717c478bd9Sstevel@tonic-gate 		usage(argv[0]);
1727c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1755a4c37c9Sqh201292 	while ((opt = getopt(argc, argv, "udm:M:Ll:gp:")) != EOF) {
1767c478bd9Sstevel@tonic-gate 		switch (opt) {
1777c478bd9Sstevel@tonic-gate 		case 'u':
1787c478bd9Sstevel@tonic-gate 			patch_vfstab = 1;
1797c478bd9Sstevel@tonic-gate 			n++;
1807c478bd9Sstevel@tonic-gate 			break;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		case 'd':
1837c478bd9Sstevel@tonic-gate 			debug = 1;
1847c478bd9Sstevel@tonic-gate 			break;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		case 'm':
1877c478bd9Sstevel@tonic-gate 			mapdev = s_strdup(optarg);
1887c478bd9Sstevel@tonic-gate 			n++;
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		case 'M':
1927c478bd9Sstevel@tonic-gate 			mapdev = s_strdup(optarg);
1937c478bd9Sstevel@tonic-gate 			cap_m_option = 1;
1947c478bd9Sstevel@tonic-gate 			n++;
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		case 'L':
1987c478bd9Sstevel@tonic-gate 			list_option = 1;
1997c478bd9Sstevel@tonic-gate 			n++;
2007c478bd9Sstevel@tonic-gate 			break;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		case 'l':
2037c478bd9Sstevel@tonic-gate 			list_option = 1;
2047c478bd9Sstevel@tonic-gate 			list_controllernum = (int)atol(optarg);
2057c478bd9Sstevel@tonic-gate 			if (list_controllernum < 0) {
2067c478bd9Sstevel@tonic-gate 				logerr(gettext("controller number %d is "
2077c478bd9Sstevel@tonic-gate 				    "invalid\n"), list_controllernum);
2087c478bd9Sstevel@tonic-gate 				clean_exit(1);
2097c478bd9Sstevel@tonic-gate 			}
2107c478bd9Sstevel@tonic-gate 			n++;
2117c478bd9Sstevel@tonic-gate 			break;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		case 'g':
2147c478bd9Sstevel@tonic-gate 			/*
2157c478bd9Sstevel@tonic-gate 			 * private option to display non-STMS device name
2167c478bd9Sstevel@tonic-gate 			 * to GUID mappings.
2177c478bd9Sstevel@tonic-gate 			 */
2187c478bd9Sstevel@tonic-gate 			list_guid_mappings = 1;
2197c478bd9Sstevel@tonic-gate 			n++;
2207c478bd9Sstevel@tonic-gate 			break;
2215a4c37c9Sqh201292 		case 'p':
2225a4c37c9Sqh201292 			/*
2235a4c37c9Sqh201292 			 * map openable vhci based name to phci base name
2245a4c37c9Sqh201292 			 */
2255a4c37c9Sqh201292 			map_vhciname = s_strdup(optarg);
2265a4c37c9Sqh201292 			n++;
2275a4c37c9Sqh201292 			break;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		default:
2307c478bd9Sstevel@tonic-gate 			usage(argv[0]);
2317c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (n != 1)
2367c478bd9Sstevel@tonic-gate 		usage(argv[0]);
2377c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
24080ab886dSwesolows int
2417c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	char save_vfstab[SYS_FILENAME_LEN], tmp_vfstab[SYS_FILENAME_LEN];
2447c478bd9Sstevel@tonic-gate 	int vfstab_updated;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2477c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if (getuid() != 0) {
2507c478bd9Sstevel@tonic-gate 		logerr(gettext("must be super-user to run this program\n"));
2517c478bd9Sstevel@tonic-gate 		clean_exit(1);
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
2557c478bd9Sstevel@tonic-gate 	(void) umask(022);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/*
2587c478bd9Sstevel@tonic-gate 	 * NOTE: The mpxio boot-up script executes this program with the
2597c478bd9Sstevel@tonic-gate 	 * mapping (-m) option before the /usr is even mounted and when the
2607c478bd9Sstevel@tonic-gate 	 * root filesystem is still mounted read-only.
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 	if (*mapdev != '\0') {
2637c478bd9Sstevel@tonic-gate 		char newname[MAXPATHLEN];
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		if (map_devname(mapdev, newname, sizeof (newname),
2667c478bd9Sstevel@tonic-gate 		    cap_m_option) == 0) {
2677c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", newname);
2687c478bd9Sstevel@tonic-gate 			clean_exit(0);
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 		clean_exit(1);
2717c478bd9Sstevel@tonic-gate 	}
2725a4c37c9Sqh201292 	if (*map_vhciname != '\0') {
2735a4c37c9Sqh201292 		char newname[MAXPATHLEN];
2745a4c37c9Sqh201292 
2755a4c37c9Sqh201292 		if (map_openable_vhciname(map_vhciname, newname,
2765a4c37c9Sqh201292 		    sizeof (newname)) == 0) {
2775a4c37c9Sqh201292 			(void) printf("%s\n", newname);
2785a4c37c9Sqh201292 			clean_exit(0);
2795a4c37c9Sqh201292 		}
2805a4c37c9Sqh201292 		clean_exit(1);
2815a4c37c9Sqh201292 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (list_option || list_guid_mappings) {
2847c478bd9Sstevel@tonic-gate 		if (list_mappings(list_controllernum, list_guid_mappings) == 0)
2857c478bd9Sstevel@tonic-gate 			clean_exit(0);
2867c478bd9Sstevel@tonic-gate 		clean_exit(1);
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/* create a directory where a copy of the system files are saved */
2907c478bd9Sstevel@tonic-gate 	if (patch_vfstab) {
2917c478bd9Sstevel@tonic-gate 		if (mkdirp(SAVE_DIR, 0755) != 0 && errno != EEXIST) {
2927c478bd9Sstevel@tonic-gate 			logerr(gettext("mkdirp: failed to create %1$s: %2$s\n"),
2937c478bd9Sstevel@tonic-gate 			    SAVE_DIR, strerror(errno));
2947c478bd9Sstevel@tonic-gate 			clean_exit(1);
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		if (make_temp(VFSTAB, save_vfstab, tmp_vfstab,
2987c478bd9Sstevel@tonic-gate 		    SYS_FILENAME_LEN) != 0)
2997c478bd9Sstevel@tonic-gate 			clean_exit(1);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		/* build new vfstab without modifying the existing one */
3027c478bd9Sstevel@tonic-gate 		if ((vfstab_updated = update_vfstab(VFSTAB, tmp_vfstab))
3037c478bd9Sstevel@tonic-gate 		    == -1) {
3047c478bd9Sstevel@tonic-gate 			logerr(gettext("failed to update %s\n"), VFSTAB);
3057c478bd9Sstevel@tonic-gate 			clean_exit(1);
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		commit_change(VFSTAB, save_vfstab, tmp_vfstab, vfstab_updated);
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	clean_exit(0);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * Make saved and temporary filenames in SAVE_DIR.
3167c478bd9Sstevel@tonic-gate  *
3177c478bd9Sstevel@tonic-gate  * ex: if the filename is /etc/vfstab then the save_filename and tmp_filename
3187c478bd9Sstevel@tonic-gate  * would be SAVE_DIR/vfstab and SAVE_DIR/vfstab.tmp respectively.
3197c478bd9Sstevel@tonic-gate  *
3207c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
3217c478bd9Sstevel@tonic-gate  */
3227c478bd9Sstevel@tonic-gate static int
3237c478bd9Sstevel@tonic-gate make_temp(char *filename, char *save_filename, char *tmp_filename, size_t len)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	char *ptr;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if ((ptr = strrchr(filename, '/')) == NULL) {
3287c478bd9Sstevel@tonic-gate 		logdmsg("invalid file %s\n", filename);
3297c478bd9Sstevel@tonic-gate 		return (-1);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	(void) snprintf(save_filename, len, "%s%s", SAVE_DIR, ptr);
3327c478bd9Sstevel@tonic-gate 	(void) snprintf(tmp_filename, len, "%s%s.tmp", SAVE_DIR, ptr);
3337c478bd9Sstevel@tonic-gate 	logdmsg("make_temp: %s: save = %s, temp = %s\n", filename,
3347c478bd9Sstevel@tonic-gate 	    save_filename, tmp_filename);
3357c478bd9Sstevel@tonic-gate 	return (0);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate /*
3397c478bd9Sstevel@tonic-gate  * Commit the changes made to the system file
3407c478bd9Sstevel@tonic-gate  */
3417c478bd9Sstevel@tonic-gate static void
3427c478bd9Sstevel@tonic-gate commit_change(char *filename, char *save_filename, char *tmp_filename,
3437c478bd9Sstevel@tonic-gate     int updated)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	int x;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (updated) {
3487c478bd9Sstevel@tonic-gate 		/* save the original */
3497c478bd9Sstevel@tonic-gate 		if ((x = rename(filename, save_filename)) != 0) {
3507c478bd9Sstevel@tonic-gate 			logerr(gettext("rename %1$s to %2$s failed: %3$s\n"),
3517c478bd9Sstevel@tonic-gate 			    filename, save_filename, strerror(errno));
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		/* now rename the new file to the actual file */
3557c478bd9Sstevel@tonic-gate 		if (rename(tmp_filename, filename) != 0) {
3567c478bd9Sstevel@tonic-gate 			logerr(gettext("rename %1$s to %2$s failed: %3$s\n"),
3577c478bd9Sstevel@tonic-gate 			    tmp_filename, filename, strerror(errno));
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 			/* restore the original */
3607c478bd9Sstevel@tonic-gate 			if (x == 0 && rename(save_filename, filename) != 0) {
3617c478bd9Sstevel@tonic-gate 				logerr(
3627c478bd9Sstevel@tonic-gate 				    gettext("rename %1$s to %2$s failed: %3$s\n"
3637c478bd9Sstevel@tonic-gate 				    "%4$s is a copy of the original %5$s file"
3647c478bd9Sstevel@tonic-gate 				    "\n"),
3657c478bd9Sstevel@tonic-gate 				    save_filename, filename, strerror(errno),
3667c478bd9Sstevel@tonic-gate 				    save_filename, filename);
3677c478bd9Sstevel@tonic-gate 			}
3687c478bd9Sstevel@tonic-gate 		} else
3697c478bd9Sstevel@tonic-gate 			(void) printf(gettext("%1$s: %2$s has been updated.\n"),
3707c478bd9Sstevel@tonic-gate 			    stmsboot, filename);
3717c478bd9Sstevel@tonic-gate 	} else {
3727c478bd9Sstevel@tonic-gate 		/* remove the temp file */
3737c478bd9Sstevel@tonic-gate 		(void) unlink(tmp_filename);
3747c478bd9Sstevel@tonic-gate 		(void) printf(gettext("%1$s: %2$s was not modified as no "
3757c478bd9Sstevel@tonic-gate 		    "changes were needed.\n"), stmsboot, filename);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate  * Get the GUID of the device.
3817c478bd9Sstevel@tonic-gate  *
3827c478bd9Sstevel@tonic-gate  * physpath	/devices name without the /devices prefix and minor name
3837c478bd9Sstevel@tonic-gate  *		component.
3847c478bd9Sstevel@tonic-gate  * guid		caller supplied buffer where the GUID will be placed on return
3857c478bd9Sstevel@tonic-gate  * guid_len	length of the caller supplied guid buffer.
3867c478bd9Sstevel@tonic-gate  * no_dealy_flag if set open the device with O_NDELAY
3877c478bd9Sstevel@tonic-gate  * node		di_node corresponding to physpath if already available,
3887c478bd9Sstevel@tonic-gate  *		otherwise pass DI_NODE_NIL.
3897c478bd9Sstevel@tonic-gate  *
3907c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
3917c478bd9Sstevel@tonic-gate  */
3927c478bd9Sstevel@tonic-gate static int
3937c478bd9Sstevel@tonic-gate get_guid(char *physpath, char *guid, int guid_len, int no_delay_flag,
3947c478bd9Sstevel@tonic-gate 	di_node_t node)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	int		fd;
3977c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid;
3987c478bd9Sstevel@tonic-gate 	int		rv	= -1;
3997c478bd9Sstevel@tonic-gate 	char		*i_guid	= NULL;
4007c478bd9Sstevel@tonic-gate 	char		physpath_raw[MAXPATHLEN];
4017c478bd9Sstevel@tonic-gate 	uchar_t		*wwnp;
4027c478bd9Sstevel@tonic-gate 	int		i, n, snapshot_taken = 0;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	logdmsg("get_guid: physpath = %s\n", physpath);
4057c478bd9Sstevel@tonic-gate 
4065a4c37c9Sqh201292 #ifdef sparc
4077c478bd9Sstevel@tonic-gate 	(void) snprintf(physpath_raw, MAXPATHLEN,
4087c478bd9Sstevel@tonic-gate 	    "/devices%s:a,raw", physpath);
4095a4c37c9Sqh201292 #else
4105a4c37c9Sqh201292 	(void) snprintf(physpath_raw, MAXPATHLEN,
4115a4c37c9Sqh201292 	    "/devices%s:c,raw", physpath);
4125a4c37c9Sqh201292 #endif
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	*guid = '\0';
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if (no_delay_flag)
4177c478bd9Sstevel@tonic-gate 		no_delay_flag = O_NDELAY;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	/*
4207c478bd9Sstevel@tonic-gate 	 * Open the raw device
4217c478bd9Sstevel@tonic-gate 	 * Without the O_DELAY flag, the open will fail on standby paths of
4227c478bd9Sstevel@tonic-gate 	 * T3 if its mp_support mode is "mpxio".
4237c478bd9Sstevel@tonic-gate 	 */
4247c478bd9Sstevel@tonic-gate 	if ((fd = open(physpath_raw, O_RDONLY | no_delay_flag)) == -1) {
4257c478bd9Sstevel@tonic-gate 		logdmsg("get_guid: failed to open %s: %s\n", physpath_raw,
4267c478bd9Sstevel@tonic-gate 		    strerror(errno));
4277c478bd9Sstevel@tonic-gate 		return (-1);
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	if (devid_get(fd, &devid) == 0) {
4317c478bd9Sstevel@tonic-gate 		i_guid = devid_to_guid(devid);
4327c478bd9Sstevel@tonic-gate 		devid_free(devid);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		if (i_guid != NULL) {
4357c478bd9Sstevel@tonic-gate 			s_strlcpy(guid, i_guid, guid_len);
4367c478bd9Sstevel@tonic-gate 			devid_free_guid(i_guid);
4377c478bd9Sstevel@tonic-gate 			rv = 0;
4387c478bd9Sstevel@tonic-gate 			goto out;
4397c478bd9Sstevel@tonic-gate 		} else
4407c478bd9Sstevel@tonic-gate 			logdmsg("get_guid: devid_to_guid() failed\n");
4417c478bd9Sstevel@tonic-gate 	} else
4427c478bd9Sstevel@tonic-gate 		logdmsg("get_guid: devid_get() failed: %s\n", strerror(errno));
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/* fallback to node name as the guid as this is what fcp driver does */
4457c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
4467c478bd9Sstevel@tonic-gate 		if ((node = di_init(physpath, DINFOCPYALL | DINFOFORCE))
4477c478bd9Sstevel@tonic-gate 		    == DI_NODE_NIL) {
4487c478bd9Sstevel@tonic-gate 			logdmsg("get_guid: di_init on %s failed: %s\n",
4497c478bd9Sstevel@tonic-gate 			    physpath, strerror(errno));
4507c478bd9Sstevel@tonic-gate 			goto out;
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 		snapshot_taken = 1;
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if ((n = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, NODE_WWN_PROP,
4567c478bd9Sstevel@tonic-gate 	    &wwnp)) == -1) {
4577c478bd9Sstevel@tonic-gate 		logdmsg("get_guid: di_prop_lookup_bytes() failed to lookup "
4587c478bd9Sstevel@tonic-gate 		    "%s: %s\n", NODE_WWN_PROP, strerror(errno));
4597c478bd9Sstevel@tonic-gate 		goto out;
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (guid_len >= ((n * 2) + 1)) {
4637c478bd9Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
4647c478bd9Sstevel@tonic-gate 			(void) sprintf(guid + (i * 2), "%02x", (uint_t)(*wwnp));
4657c478bd9Sstevel@tonic-gate 			wwnp++;
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 		rv = 0;
4687c478bd9Sstevel@tonic-gate 	} else
4697c478bd9Sstevel@tonic-gate 		logerr(gettext("insufficient buffer size: need %1$d "
4707c478bd9Sstevel@tonic-gate 		    "bytes, passed %2$d bytes\n"), (n * 2) + 1, guid_len);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate out:
4737c478bd9Sstevel@tonic-gate 	if (snapshot_taken)
4747c478bd9Sstevel@tonic-gate 		di_fini(node);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	(void) close(fd);
4777c478bd9Sstevel@tonic-gate 	logdmsg("get_guid: GUID = %s\n", guid);
4787c478bd9Sstevel@tonic-gate 	return (rv);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate /*
4827c478bd9Sstevel@tonic-gate  * Given client_name return whether it is a phci or vhci based name.
4837c478bd9Sstevel@tonic-gate  * client_name is /devices name of a client without the /devices prefix.
4847c478bd9Sstevel@tonic-gate  *
4857c478bd9Sstevel@tonic-gate  * client_name			Return value
4865a4c37c9Sqh201292  * on sparc:
4877c478bd9Sstevel@tonic-gate  * .../fp@xxx/ssd@yyy		CLIENT_TYPE_PHCI
4887c478bd9Sstevel@tonic-gate  * .../scsi_vhci/ssd@yyy	CLIENT_TYPE_VHCI
4897c478bd9Sstevel@tonic-gate  * other			CLIENT_TYPE_UNKNOWN
4905a4c37c9Sqh201292  * on x86:
4915a4c37c9Sqh201292  * .../fp@xxx/disk@yyy		CLIENT_TYPE_PHCI
4925a4c37c9Sqh201292  * .../scsi_vhci/disk@yyy	CLIENT_TYPE_VHCI
4935a4c37c9Sqh201292  * other			CLIENT_TYPE_UNKNOWN
4947c478bd9Sstevel@tonic-gate  */
4957c478bd9Sstevel@tonic-gate static client_type_t
4967c478bd9Sstevel@tonic-gate client_name_type(char *client_name)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	client_type_t client_type = CLIENT_TYPE_UNKNOWN;
4997c478bd9Sstevel@tonic-gate 	char *p1, *p2;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (*client_name != '/')
5027c478bd9Sstevel@tonic-gate 		return (CLIENT_TYPE_UNKNOWN);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	if ((p1 = strrchr(client_name, '/')) == NULL ||
5055a4c37c9Sqh201292 	    strncmp(p1, SLASH_DISK_AT, sizeof (SLASH_DISK_AT) - 1) != 0)
5067c478bd9Sstevel@tonic-gate 		return (CLIENT_TYPE_UNKNOWN);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	*p1 = '\0';
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if ((p2 = strrchr(client_name, '/')) != NULL) {
5117c478bd9Sstevel@tonic-gate 		if (strncmp(p2, SLASH_FP_AT, sizeof (SLASH_FP_AT) - 1) == 0)
5127c478bd9Sstevel@tonic-gate 			client_type = CLIENT_TYPE_PHCI;
5137c478bd9Sstevel@tonic-gate 		else if (strncmp(p2, SLASH_SCSI_VHCI,
5147c478bd9Sstevel@tonic-gate 		    sizeof (SLASH_SCSI_VHCI) - 1) == 0)
5157c478bd9Sstevel@tonic-gate 			client_type = CLIENT_TYPE_VHCI;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	*p1 = '/';
5197c478bd9Sstevel@tonic-gate 	return (client_type);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate /*
5237c478bd9Sstevel@tonic-gate  * Map phci based client name to vhci based client name.
5247c478bd9Sstevel@tonic-gate  *
5257c478bd9Sstevel@tonic-gate  * phci_name
5267c478bd9Sstevel@tonic-gate  *	phci based client /devices name without the /devices prefix and
5277c478bd9Sstevel@tonic-gate  *	minor name component.
5285a4c37c9Sqh201292  *	ex:
5295a4c37c9Sqh201292  *	for sparc: /pci@8,600000/SUNW,qlc@4/fp@0,0/ssd@w2100002037cd9f72,0
5305a4c37c9Sqh201292  *	for x86: /pci@8,600000/SUNW,qlc@4/fp@0,0/disk@w2100002037cd9f72,0
5317c478bd9Sstevel@tonic-gate  *
5327c478bd9Sstevel@tonic-gate  * vhci_name
5337c478bd9Sstevel@tonic-gate  *	Caller supplied buffer where vhci /devices name will be placed on
5347c478bd9Sstevel@tonic-gate  *	return (without the /devices prefix and minor name component).
5355a4c37c9Sqh201292  *	ex:
5365a4c37c9Sqh201292  *	for sparc: /scsi_vhci/ssd@g2000002037cd9f72
5375a4c37c9Sqh201292  *	for x86: /scsi_vhci/disk@g2000002037cd9f72
5387c478bd9Sstevel@tonic-gate  *
5397c478bd9Sstevel@tonic-gate  * vhci_name_len
5407c478bd9Sstevel@tonic-gate  *	Length of the caller supplied vhci_name buffer.
5417c478bd9Sstevel@tonic-gate  *
5427c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
5437c478bd9Sstevel@tonic-gate  */
5447c478bd9Sstevel@tonic-gate static int
5457c478bd9Sstevel@tonic-gate phci_to_vhci(char *phci_name, char *vhci_name, size_t vhci_name_len)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	sv_iocdata_t ioc;
5487c478bd9Sstevel@tonic-gate 	char *slash;
5497c478bd9Sstevel@tonic-gate 	char vhci_name_buf[MAXPATHLEN];
5507c478bd9Sstevel@tonic-gate 	char phci_name_buf[MAXPATHLEN];
5517c478bd9Sstevel@tonic-gate 	char addr_buf[MAXNAMELEN];
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	logdmsg("phci_to_vhci: client = %s\n", phci_name);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	s_strlcpy(phci_name_buf, phci_name, MAXPATHLEN);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if (client_name_type(phci_name_buf) != CLIENT_TYPE_PHCI ||
5587c478bd9Sstevel@tonic-gate 	    (slash = strrchr(phci_name_buf, '/')) == NULL ||
5595a4c37c9Sqh201292 	    strncmp(slash, SLASH_DISK_AT, sizeof (SLASH_DISK_AT) - 1) != 0) {
5607c478bd9Sstevel@tonic-gate 		logdmsg("phci_to_vhci: %s is not of CLIENT_TYPE_PHCI\n",
5617c478bd9Sstevel@tonic-gate 		    phci_name);
5627c478bd9Sstevel@tonic-gate 		return (-1);
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	if (vhci_fd < 0) {
5667c478bd9Sstevel@tonic-gate 		if ((vhci_fd = open(VHCI_CTL_NODE, O_RDWR)) < 0)
5677c478bd9Sstevel@tonic-gate 			return (-1);
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	*slash = '\0';
5715a4c37c9Sqh201292 	s_strlcpy(addr_buf, slash + sizeof (SLASH_DISK_AT) - 1, MAXNAMELEN);
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	bzero(&ioc, sizeof (sv_iocdata_t));
5747c478bd9Sstevel@tonic-gate 	ioc.client = vhci_name_buf;
5757c478bd9Sstevel@tonic-gate 	ioc.phci = phci_name_buf;
5767c478bd9Sstevel@tonic-gate 	ioc.addr = addr_buf;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (ioctl(vhci_fd, SCSI_VHCI_GET_CLIENT_NAME, &ioc) != 0) {
5797c478bd9Sstevel@tonic-gate 		logdmsg("SCSI_VHCI_GET_CLIENT_NAME on %s "
5807c478bd9Sstevel@tonic-gate 		    "failed: %s\n", phci_name, strerror(errno));
5817c478bd9Sstevel@tonic-gate 		return (-1);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	s_strlcpy(vhci_name, vhci_name_buf, vhci_name_len);
5857c478bd9Sstevel@tonic-gate 	logdmsg("phci_to_vhci: %s maps to %s\n", phci_name, vhci_name);
5867c478bd9Sstevel@tonic-gate 	return (0);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate /*
5907c478bd9Sstevel@tonic-gate  * Map vhci based client name to phci based client name.
5917c478bd9Sstevel@tonic-gate  * If the client has multiple paths, only one of the paths with which client
5927c478bd9Sstevel@tonic-gate  * can be accessed is returned. This function does not use SCSI_VHCI ioctls
5937c478bd9Sstevel@tonic-gate  * as it is called on mpxio disabled paths.
5947c478bd9Sstevel@tonic-gate  *
5957c478bd9Sstevel@tonic-gate  * vhci_name
5967c478bd9Sstevel@tonic-gate  *	vhci based client /devices name without the /devices prefix and
5977c478bd9Sstevel@tonic-gate  *	minor name component.
5985a4c37c9Sqh201292  *	ex:
5995a4c37c9Sqh201292  *	sparc: /scsi_vhci/ssd@g2000002037cd9f72
6005a4c37c9Sqh201292  *	x86: /scsi_vhci/disk@g2000002037cd9f72
6017c478bd9Sstevel@tonic-gate  *
6027c478bd9Sstevel@tonic-gate  * phci_name
6037c478bd9Sstevel@tonic-gate  *	Caller supplied buffer where phci /devices name will be placed on
6047c478bd9Sstevel@tonic-gate  *	return (without the /devices prefix and minor name component).
6055a4c37c9Sqh201292  *	ex:
6065a4c37c9Sqh201292  *	sparc: /pci@8,600000/SUNW,qlc@4/fp@0,0/ssd@w2100002037cd9f72,0
6075a4c37c9Sqh201292  *	x86: /pci@8,600000/SUNW,qlc@4/fp@0,0/disk@w2100002037cd9f72,0
6087c478bd9Sstevel@tonic-gate  *
6097c478bd9Sstevel@tonic-gate  * phci_name_len
6107c478bd9Sstevel@tonic-gate  *	Length of the caller supplied phci_name buffer.
6117c478bd9Sstevel@tonic-gate  *
6127c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
6137c478bd9Sstevel@tonic-gate  */
6147c478bd9Sstevel@tonic-gate static int
6157c478bd9Sstevel@tonic-gate vhci_to_phci(char *vhci_name, char *phci_name, size_t phci_name_len)
6167c478bd9Sstevel@tonic-gate {
6177c478bd9Sstevel@tonic-gate 	di_node_t node, parent;
6187c478bd9Sstevel@tonic-gate 	char *vhci_guid, *devfspath;
6197c478bd9Sstevel@tonic-gate 	char phci_guid[MAXPATHLEN];
6207c478bd9Sstevel@tonic-gate 	char *parent_name, *node_name;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	logdmsg("vhci_to_phci: client = %s\n", vhci_name);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	if (client_name_type(vhci_name) != CLIENT_TYPE_VHCI) {
6257c478bd9Sstevel@tonic-gate 		logdmsg("vhci_to_phci: %s is not of CLIENT_TYPE_VHCI\n",
6267c478bd9Sstevel@tonic-gate 		    vhci_name);
6277c478bd9Sstevel@tonic-gate 		return (-1);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	if ((vhci_guid = strrchr(vhci_name, '@')) == NULL ||
6317c478bd9Sstevel@tonic-gate 	    *(++vhci_guid) != 'g') {
6327c478bd9Sstevel@tonic-gate 		logerr(gettext("couldn't get guid from %s\n"), vhci_name);
6337c478bd9Sstevel@tonic-gate 		return (-1);
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/* point to guid */
6377c478bd9Sstevel@tonic-gate 	++vhci_guid;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	/*
6407c478bd9Sstevel@tonic-gate 	 * Get devinfo snapshot and walk all ssd nodes whose parent is fp.
6417c478bd9Sstevel@tonic-gate 	 * For each node get the guid and match it with vhci_guid.
6427c478bd9Sstevel@tonic-gate 	 */
6437c478bd9Sstevel@tonic-gate 	if (devinfo_root == DI_NODE_NIL) {
6447c478bd9Sstevel@tonic-gate 		logdmsg("vhci_to_phci: taking devinfo snapshot\n");
6457c478bd9Sstevel@tonic-gate 		if ((devinfo_root = di_init("/", DINFOCPYALL | DINFOFORCE))
6467c478bd9Sstevel@tonic-gate 		    == DI_NODE_NIL) {
6477c478bd9Sstevel@tonic-gate 			logerr(gettext("di_init failed: %s\n"),
6487c478bd9Sstevel@tonic-gate 			    strerror(errno));
6497c478bd9Sstevel@tonic-gate 			return (-1);
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 		logdmsg("vhci_to_phci: done taking devinfo snapshot\n");
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6545a4c37c9Sqh201292 	for (node = di_drv_first_node(DISK_DRV_NAME, devinfo_root);
6557c478bd9Sstevel@tonic-gate 	    node != DI_NODE_NIL; node = di_drv_next_node(node)) {
6567c478bd9Sstevel@tonic-gate 		if ((node_name = di_node_name(node)) == NULL ||
6575a4c37c9Sqh201292 		    strcmp(node_name, DISK_NODE_NAME) != 0 ||
6587c478bd9Sstevel@tonic-gate 		    (parent = di_parent_node(node)) == DI_NODE_NIL ||
6597c478bd9Sstevel@tonic-gate 		    (parent_name = di_node_name(parent)) == NULL ||
6607c478bd9Sstevel@tonic-gate 		    strcmp(parent_name, "fp") != 0 ||
6617c478bd9Sstevel@tonic-gate 		    (devfspath = di_devfs_path(node)) == NULL)
6627c478bd9Sstevel@tonic-gate 			continue;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 		/*
6657c478bd9Sstevel@tonic-gate 		 * Don't set no_delay_flag to have get_guid() fail on
6667c478bd9Sstevel@tonic-gate 		 * standby paths of T3. So we'll find the preferred paths.
6677c478bd9Sstevel@tonic-gate 		 */
6687c478bd9Sstevel@tonic-gate 		if (get_guid(devfspath, phci_guid,
6697c478bd9Sstevel@tonic-gate 		    sizeof (phci_guid), 0, node) == 0 &&
6707c478bd9Sstevel@tonic-gate 		    strcmp(phci_guid, vhci_guid) == 0) {
6717c478bd9Sstevel@tonic-gate 			s_strlcpy(phci_name, devfspath, phci_name_len);
6727c478bd9Sstevel@tonic-gate 			di_devfs_path_free(devfspath);
6737c478bd9Sstevel@tonic-gate 			logdmsg("vhci_to_phci: %s maps to %s\n", vhci_name,
6747c478bd9Sstevel@tonic-gate 			    phci_name);
6757c478bd9Sstevel@tonic-gate 			return (0);
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		di_devfs_path_free(devfspath);
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	logdmsg("vhci_to_phci: couldn't get phci name for %s\n", vhci_name);
6827c478bd9Sstevel@tonic-gate 	return (-1);
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate /*
6865a4c37c9Sqh201292  * Map vhci based client name to phci based client name.
6875a4c37c9Sqh201292  * If the client has multiple paths, only one of the paths with which client
6885a4c37c9Sqh201292  * can be accessed is returned.
6895a4c37c9Sqh201292  * This function uses SCSI_VHCI ioctls to get the phci paths
6905a4c37c9Sqh201292  *
6915a4c37c9Sqh201292  * vhci_name
6925a4c37c9Sqh201292  *	vhci based client /devices name without the /devices prefix and
6935a4c37c9Sqh201292  *	minor name component.
6945a4c37c9Sqh201292  *	ex:
6955a4c37c9Sqh201292  *	sparc: /scsi_vhci/ssd@g2000002037cd9f72
6965a4c37c9Sqh201292  *	x86: /scsi_vhci/disk@g2000002037cd9f72
6975a4c37c9Sqh201292  *
6985a4c37c9Sqh201292  * phci_name
6995a4c37c9Sqh201292  *	Caller supplied buffer where phci /devices name will be placed on
7005a4c37c9Sqh201292  *	return (without the /devices prefix and minor name component).
7015a4c37c9Sqh201292  *	ex:
7025a4c37c9Sqh201292  *	sparc: /pci@8,600000/SUNW,qlc@4/fp@0,0/ssd@w2100002037cd9f72,0
7035a4c37c9Sqh201292  *	x86: /pci@8,600000/SUNW,qlc@4/fp@0,0/disk@w2100002037cd9f72,0
7045a4c37c9Sqh201292  *
7055a4c37c9Sqh201292  * phci_name_len
7065a4c37c9Sqh201292  *	Length of the caller supplied phci_name buffer.
7075a4c37c9Sqh201292  *
7085a4c37c9Sqh201292  * Returns 0 on success, -1 on failure.
7095a4c37c9Sqh201292  */
7105a4c37c9Sqh201292 
7115a4c37c9Sqh201292 static int
7125a4c37c9Sqh201292 vhci_to_phci_by_ioctl(char *vhci_name, char *phci_name, size_t phci_name_len)
7135a4c37c9Sqh201292 {
7145a4c37c9Sqh201292 	sv_iocdata_t	ioc;
7155a4c37c9Sqh201292 	uint_t npaths;
7165a4c37c9Sqh201292 	char *node_name, *at;
7175a4c37c9Sqh201292 	char vhci_name_buf[MAXPATHLEN];
7185a4c37c9Sqh201292 	int  ret;
7195a4c37c9Sqh201292 	sv_path_info_t *pi;
7205a4c37c9Sqh201292 
7215a4c37c9Sqh201292 	logdmsg("vhci_to_phci_by_ioctl: client = %s\n", vhci_name);
7225a4c37c9Sqh201292 
7235a4c37c9Sqh201292 	if (vhci_fd < 0) {
7245a4c37c9Sqh201292 		if ((vhci_fd = open(VHCI_CTL_NODE, O_RDWR)) < 0)
7255a4c37c9Sqh201292 			return (-1);
7265a4c37c9Sqh201292 	}
7275a4c37c9Sqh201292 
7285a4c37c9Sqh201292 	(void) strlcpy(vhci_name_buf, vhci_name, MAXPATHLEN);
7295a4c37c9Sqh201292 
7305a4c37c9Sqh201292 	/* first get the number paths */
7315a4c37c9Sqh201292 	bzero(&ioc, sizeof (sv_iocdata_t));
7325a4c37c9Sqh201292 	ioc.client = vhci_name_buf;
7335a4c37c9Sqh201292 	ioc.ret_elem = &npaths;
7345a4c37c9Sqh201292 	if ((ret = ioctl(vhci_fd, SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO,
7355a4c37c9Sqh201292 	    &ioc)) != 0 || npaths == 0) {
7365a4c37c9Sqh201292 		logdmsg("SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO on %s "
7375a4c37c9Sqh201292 		    "failed: %s\n", vhci_name,
7385a4c37c9Sqh201292 		    ret?strerror(errno):"got 0 paths");
7395a4c37c9Sqh201292 		return (-1);
7405a4c37c9Sqh201292 	}
7415a4c37c9Sqh201292 
7425a4c37c9Sqh201292 	/* now allocate memory for the path information and get all paths */
7435a4c37c9Sqh201292 	bzero(&ioc, sizeof (sv_iocdata_t));
7445a4c37c9Sqh201292 	ioc.client = vhci_name_buf;
7455a4c37c9Sqh201292 	ioc.buf_elem = npaths;
7465a4c37c9Sqh201292 	ioc.ret_elem = &npaths;
7475a4c37c9Sqh201292 	if ((ioc.ret_buf = (sv_path_info_t *)calloc(npaths,
7485a4c37c9Sqh201292 	    sizeof (sv_path_info_t))) == NULL)
7495a4c37c9Sqh201292 		return (-1);
7505a4c37c9Sqh201292 	if ((ret = ioctl(vhci_fd, SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO,
7515a4c37c9Sqh201292 	    &ioc)) != 0 || npaths == 0) {
7525a4c37c9Sqh201292 		logdmsg("SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO on %s "
7535a4c37c9Sqh201292 		    "failed: %s\n", vhci_name,
7545a4c37c9Sqh201292 		    ret?strerror(errno):"got 0 paths");
7555a4c37c9Sqh201292 		goto out;
7565a4c37c9Sqh201292 	}
7575a4c37c9Sqh201292 
7585a4c37c9Sqh201292 	if (ioc.buf_elem < npaths)
7595a4c37c9Sqh201292 		npaths = ioc.buf_elem;
7605a4c37c9Sqh201292 	if ((node_name = strrchr(vhci_name_buf, '/')) == NULL ||
7615a4c37c9Sqh201292 	    (at = strchr(node_name, '@')) == NULL)
7625a4c37c9Sqh201292 		goto out;
7635a4c37c9Sqh201292 
7645a4c37c9Sqh201292 	node_name++;
7655a4c37c9Sqh201292 	*at = '\0';
7665a4c37c9Sqh201292 
7675a4c37c9Sqh201292 	/*
7685a4c37c9Sqh201292 	 * return the first online paths as non-online paths may
7695a4c37c9Sqh201292 	 * not be accessible in the target environment.
7705a4c37c9Sqh201292 	 */
7715a4c37c9Sqh201292 	pi = (sv_path_info_t *)ioc.ret_buf;
7725a4c37c9Sqh201292 	while (npaths--) {
7735a4c37c9Sqh201292 		if (MDI_PATHINFO_STATE_ONLINE == pi->ret_state) {
7745a4c37c9Sqh201292 			(void) snprintf(phci_name, phci_name_len, "%s/%s@%s",
7755a4c37c9Sqh201292 			    pi->device.ret_phci, node_name,
7765a4c37c9Sqh201292 			    pi->ret_addr);
7775a4c37c9Sqh201292 			logdmsg("vhci_to_phci_by_ioctl: %s maps to %s\n",
7785a4c37c9Sqh201292 			    vhci_name, phci_name);
7795a4c37c9Sqh201292 			free(ioc.ret_buf);
7805a4c37c9Sqh201292 			return (0);
7815a4c37c9Sqh201292 		}
7825a4c37c9Sqh201292 		pi++;
7835a4c37c9Sqh201292 	}
7845a4c37c9Sqh201292 
7855a4c37c9Sqh201292 out:
7865a4c37c9Sqh201292 	logdmsg("vhci_to_phci_by_ioctl: couldn't get phci name for %s\n",
7875a4c37c9Sqh201292 	    vhci_name);
7885a4c37c9Sqh201292 	free(ioc.ret_buf);
7895a4c37c9Sqh201292 	return (-1);
7905a4c37c9Sqh201292 
7915a4c37c9Sqh201292 }
7925a4c37c9Sqh201292 
7935a4c37c9Sqh201292 /*
7947c478bd9Sstevel@tonic-gate  * Map physname from phci name space to vhci name space or vice-versa
7957c478bd9Sstevel@tonic-gate  *
7967c478bd9Sstevel@tonic-gate  * physname
7977c478bd9Sstevel@tonic-gate  *	phci or vhci based client /devices name without the /devices prefix and
7987c478bd9Sstevel@tonic-gate  *	minor name component.
7997c478bd9Sstevel@tonic-gate  *
8007c478bd9Sstevel@tonic-gate  * new_physname
8017c478bd9Sstevel@tonic-gate  *	Caller supplied buffer where the mapped physical name is stored on
8027c478bd9Sstevel@tonic-gate  *	return (without the /devices prefix and minor name component).
8037c478bd9Sstevel@tonic-gate  *
8047c478bd9Sstevel@tonic-gate  * len
8057c478bd9Sstevel@tonic-gate  *	Length of the caller supplied new_physname buffer.
8067c478bd9Sstevel@tonic-gate  *
8077c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
8087c478bd9Sstevel@tonic-gate  */
8097c478bd9Sstevel@tonic-gate static int
8107c478bd9Sstevel@tonic-gate map_physname(char *physname, char *new_physname, size_t len)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	int type;
8137c478bd9Sstevel@tonic-gate 	int rv;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	if ((type = client_name_type(physname)) == CLIENT_TYPE_VHCI)
8167c478bd9Sstevel@tonic-gate 		rv = vhci_to_phci(physname, new_physname, len);
8177c478bd9Sstevel@tonic-gate 	else if (type == CLIENT_TYPE_PHCI)
8187c478bd9Sstevel@tonic-gate 		rv = phci_to_vhci(physname, new_physname, len);
8197c478bd9Sstevel@tonic-gate 	else
8207c478bd9Sstevel@tonic-gate 		rv = -1;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	return (rv);
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate /*
8267c478bd9Sstevel@tonic-gate  * Given a phci or vhci devname which is either a /dev link or /devices name
8277c478bd9Sstevel@tonic-gate  * get the corresponding physical node path (without the /devices prefix)
8287c478bd9Sstevel@tonic-gate  * and minor name.
8297c478bd9Sstevel@tonic-gate  *
8307c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
8317c478bd9Sstevel@tonic-gate  */
8327c478bd9Sstevel@tonic-gate static int
8337c478bd9Sstevel@tonic-gate get_physname_minor(char *devname, char *physname, int physname_len,
8347c478bd9Sstevel@tonic-gate     char *minorname, int minorname_len)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	int linksize;
8377c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
8387c478bd9Sstevel@tonic-gate 	char *p, *m;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	if (strncmp(devname, DEV_DSK, sizeof (DEV_DSK) - 1) == 0 ||
8417c478bd9Sstevel@tonic-gate 	    strncmp(devname, DEV_RDSK, sizeof (DEV_RDSK) - 1) == 0) {
8427c478bd9Sstevel@tonic-gate 		if ((linksize = readlink(devname, buf, MAXPATHLEN))
8437c478bd9Sstevel@tonic-gate 		    > 0 && linksize <= (MAXPATHLEN - 1)) {
8447c478bd9Sstevel@tonic-gate 			buf[linksize] = '\0';
8457c478bd9Sstevel@tonic-gate 		} else
8467c478bd9Sstevel@tonic-gate 			return (-1);
8477c478bd9Sstevel@tonic-gate 	} else
8487c478bd9Sstevel@tonic-gate 		s_strlcpy(buf, devname, MAXPATHLEN);
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if ((p = strstr(buf, SLASH_DEVICES)) == NULL)
8517c478bd9Sstevel@tonic-gate 		return (-1);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	/* point to '/' after /devices */
8547c478bd9Sstevel@tonic-gate 	p += sizeof (SLASH_DEVICES) - 2;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	if ((m = strrchr(p, ':')) == NULL) {
8577c478bd9Sstevel@tonic-gate 		logdmsg("get_physname_minor: no minor name component in %s\n",
8587c478bd9Sstevel@tonic-gate 		    buf);
8597c478bd9Sstevel@tonic-gate 		return (-1);
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	*m = '\0';
8637c478bd9Sstevel@tonic-gate 	m++;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	if (client_name_type(p) == CLIENT_TYPE_UNKNOWN)
8667c478bd9Sstevel@tonic-gate 		return (-1);
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	s_strlcpy(physname, p, physname_len);
8697c478bd9Sstevel@tonic-gate 	s_strlcpy(minorname, m, minorname_len);
8707c478bd9Sstevel@tonic-gate 	logdmsg("get_physname_minor: %s: physname = %s, minor = %s\n",
8717c478bd9Sstevel@tonic-gate 	    devname, physname, minorname);
8727c478bd9Sstevel@tonic-gate 	return (0);
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate static int
8767c478bd9Sstevel@tonic-gate devlink_callback(di_devlink_t devlink, void *argptr)
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate 	const char *link;
8797c478bd9Sstevel@tonic-gate 	struct devlink_cbarg *argp = argptr;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	if ((link = di_devlink_path(devlink)) != NULL) {
8827c478bd9Sstevel@tonic-gate 		s_strlcpy(argp->devlink, link, argp->len);
8837c478bd9Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate  * Lookup the /dev link corresponding to physname and minorname.
8917c478bd9Sstevel@tonic-gate  *
8927c478bd9Sstevel@tonic-gate  * physname	client /devices path without the /devices prefix and minor
8937c478bd9Sstevel@tonic-gate  *		name component.
8947c478bd9Sstevel@tonic-gate  * minorname	client minor name.
8957c478bd9Sstevel@tonic-gate  * devlink	caller supplied buffer where the /dev link is placed on return.
8967c478bd9Sstevel@tonic-gate  * len		caller supplied devlink buffer length
8977c478bd9Sstevel@tonic-gate  *
8987c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
8997c478bd9Sstevel@tonic-gate  */
9007c478bd9Sstevel@tonic-gate static int
9017c478bd9Sstevel@tonic-gate lookup_devlink(char *physname, char *minorname, char *devlink, size_t len)
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
9047c478bd9Sstevel@tonic-gate 	struct devlink_cbarg arg;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if (devlink_hdl == NULL) {
9077c478bd9Sstevel@tonic-gate 		logdmsg("lookup_devlink: taking devlink snapshot\n");
9087c478bd9Sstevel@tonic-gate 		if ((devlink_hdl = di_devlink_init(NULL, 0)) == NULL) {
9097c478bd9Sstevel@tonic-gate 			logerr(gettext("di_devlink_init failed: %s\n"),
9107c478bd9Sstevel@tonic-gate 			    strerror(errno));
9117c478bd9Sstevel@tonic-gate 			clean_exit(1);
9127c478bd9Sstevel@tonic-gate 		}
9137c478bd9Sstevel@tonic-gate 	}
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	*devlink = '\0';
9167c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, MAXPATHLEN, "%s:%s", physname, minorname);
9177c478bd9Sstevel@tonic-gate 	arg.devlink = devlink;
9187c478bd9Sstevel@tonic-gate 	arg.len = len;
9197c478bd9Sstevel@tonic-gate 	if (di_devlink_walk(devlink_hdl, NULL, buf, DI_PRIMARY_LINK, &arg,
9207c478bd9Sstevel@tonic-gate 	    devlink_callback) != 0) {
9217c478bd9Sstevel@tonic-gate 		logdmsg("lookup_devlink: di_devlink_walk on %s failed: %s\n",
9227c478bd9Sstevel@tonic-gate 		    buf, strerror(errno));
9237c478bd9Sstevel@tonic-gate 		return (-1);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	if (*devlink == '\0') {
9277c478bd9Sstevel@tonic-gate 		logdmsg("lookup_devlink: failed to lookup devlink for %s\n",
9287c478bd9Sstevel@tonic-gate 		    buf);
9297c478bd9Sstevel@tonic-gate 		return (-1);
9307c478bd9Sstevel@tonic-gate 	}
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	logdmsg("lookup_devlink: /dev link for %s:%s = %s\n", physname,
9337c478bd9Sstevel@tonic-gate 	    minorname, devlink);
9347c478bd9Sstevel@tonic-gate 	return (0);
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /*
9387c478bd9Sstevel@tonic-gate  * open infile for reading and return its file pointer in *fp_in.
9397c478bd9Sstevel@tonic-gate  * open outfile for writing and return its file pointer in *fp_out.
9407c478bd9Sstevel@tonic-gate  *
9417c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
9427c478bd9Sstevel@tonic-gate  */
9437c478bd9Sstevel@tonic-gate static int
9447c478bd9Sstevel@tonic-gate open_in_out_files(char *infile, char *outfile, FILE **fp_in, FILE **fp_out)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate 	FILE *fin = NULL;
9477c478bd9Sstevel@tonic-gate 	FILE *fout = NULL;
9487c478bd9Sstevel@tonic-gate 	struct stat sbuf;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	if ((fin = fopen(infile, "r")) == NULL) {
9517c478bd9Sstevel@tonic-gate 		logerr(gettext("failed to fopen %1$s: %2$s\n"),
9527c478bd9Sstevel@tonic-gate 		    infile, strerror(errno));
9537c478bd9Sstevel@tonic-gate 		goto out;
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	if (fstat(fileno(fin), &sbuf) != 0) {
9577c478bd9Sstevel@tonic-gate 		logerr(gettext("fstat failed on %1$s: %2$s\n"),
9587c478bd9Sstevel@tonic-gate 		    infile, strerror(errno));
9597c478bd9Sstevel@tonic-gate 		goto out;
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	if ((fout = fopen(outfile, "w")) == NULL) {
9637c478bd9Sstevel@tonic-gate 		logerr(gettext("failed to fopen %1$s: %2$s\n"),
9647c478bd9Sstevel@tonic-gate 		    outfile, strerror(errno));
9657c478bd9Sstevel@tonic-gate 		goto out;
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (fchmod(fileno(fout), (sbuf.st_mode & 0777)) != 0) {
9697c478bd9Sstevel@tonic-gate 		logerr(gettext("failed to fchmod %1$s to 0%2$o: %3$s\n"),
9707c478bd9Sstevel@tonic-gate 		    outfile, sbuf.st_mode & 0777, strerror(errno));
9717c478bd9Sstevel@tonic-gate 		goto out;
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	if (fchown(fileno(fout), sbuf.st_uid, sbuf.st_gid) != 0) {
9757c478bd9Sstevel@tonic-gate 		logerr(gettext("failed to fchown %1$s to uid %2$d and "
9767c478bd9Sstevel@tonic-gate 		    "gid %3$d: %4$s\n"),
9777c478bd9Sstevel@tonic-gate 		    outfile, sbuf.st_uid, sbuf.st_gid, strerror(errno));
9787c478bd9Sstevel@tonic-gate 		goto out;
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	*fp_in = fin;
9827c478bd9Sstevel@tonic-gate 	*fp_out = fout;
9837c478bd9Sstevel@tonic-gate 	return (0);
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate out:
9867c478bd9Sstevel@tonic-gate 	if (fin != NULL)
9877c478bd9Sstevel@tonic-gate 		(void) fclose(fin);
9887c478bd9Sstevel@tonic-gate 	if (fout != NULL)
9897c478bd9Sstevel@tonic-gate 		(void) fclose(fout);
9907c478bd9Sstevel@tonic-gate 	return (-1);
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate /*
9947c478bd9Sstevel@tonic-gate  * If the devname is a phci based name and not open-able, map it to vhci
9957c478bd9Sstevel@tonic-gate  * based name. If the devname is a vhci based name and not open-able, map it
9967c478bd9Sstevel@tonic-gate  * to phci based name.
9977c478bd9Sstevel@tonic-gate  *
9987c478bd9Sstevel@tonic-gate  * devname	either a /dev link or /devices name to client device
9997c478bd9Sstevel@tonic-gate  * new_devname	caller supplied buffer where the mapped device name is
10007c478bd9Sstevel@tonic-gate  *		placed on return.
10017c478bd9Sstevel@tonic-gate  * len		caller supplied new_devname buffer length
10027c478bd9Sstevel@tonic-gate  * devlink_flag	pass 1 if requesting the /dev link to the mapped device.
10037c478bd9Sstevel@tonic-gate  *		pass 0 if requesting the /devices name of the mapped device.
10047c478bd9Sstevel@tonic-gate  *
10057c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
10067c478bd9Sstevel@tonic-gate  */
10077c478bd9Sstevel@tonic-gate static int
10087c478bd9Sstevel@tonic-gate map_devname(char *devname, char *new_devname, size_t len, int devlink_flag)
10097c478bd9Sstevel@tonic-gate {
10107c478bd9Sstevel@tonic-gate 	char physname[MAXPATHLEN];
10117c478bd9Sstevel@tonic-gate 	char minor[MAXNAMELEN];
10125a4c37c9Sqh201292 	char new_physname[MAXPATHLEN];
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	if (get_physname_minor(devname, physname, sizeof (physname),
10157c478bd9Sstevel@tonic-gate 	    minor, sizeof (minor)) == 0 &&
10167c478bd9Sstevel@tonic-gate 	    canopen(devname) == 0 &&
10177c478bd9Sstevel@tonic-gate 	    map_physname(physname, new_physname, sizeof (new_physname)) == 0) {
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 		if (devlink_flag) {
10207c478bd9Sstevel@tonic-gate 			if (lookup_devlink(new_physname, minor, new_devname,
10217c478bd9Sstevel@tonic-gate 			    len) == 0)
10227c478bd9Sstevel@tonic-gate 				return (0);
10237c478bd9Sstevel@tonic-gate 		} else {
10247c478bd9Sstevel@tonic-gate 			(void) snprintf(new_devname, len, "/devices%s:%s",
10257c478bd9Sstevel@tonic-gate 			    new_physname, minor);
10267c478bd9Sstevel@tonic-gate 			return (0);
10277c478bd9Sstevel@tonic-gate 		}
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	return (-1);
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate /*
10345a4c37c9Sqh201292  * If the devname is a vhci based name and open-able, map it to phci
10355a4c37c9Sqh201292  * based name.
10365a4c37c9Sqh201292  *
10375a4c37c9Sqh201292  * devname	either a /dev link or /devices name to client device
10385a4c37c9Sqh201292  * new_devname	caller supplied buffer where the mapped device name without
10395a4c37c9Sqh201292  *		/devices prefix is placed on return.
10405a4c37c9Sqh201292  * len		caller supplied new_devname buffer length
10415a4c37c9Sqh201292  */
10425a4c37c9Sqh201292 static int
10435a4c37c9Sqh201292 map_openable_vhciname(char *devname, char *new_devname, size_t len)
10445a4c37c9Sqh201292 {
10455a4c37c9Sqh201292 	char physname[MAXPATHLEN];
10465a4c37c9Sqh201292 	char minor[MAXNAMELEN];
10475a4c37c9Sqh201292 	char new_physname[MAXPATHLEN];
10485a4c37c9Sqh201292 
10495a4c37c9Sqh201292 	if (get_physname_minor(devname, physname, sizeof (physname),
10505a4c37c9Sqh201292 	    minor, sizeof (minor)) == 0 &&
10515a4c37c9Sqh201292 	    canopen(devname) == 1 &&
10525a4c37c9Sqh201292 	    client_name_type(physname) == CLIENT_TYPE_VHCI &&
10535a4c37c9Sqh201292 	    vhci_to_phci_by_ioctl(physname, new_physname,
10545a4c37c9Sqh201292 		sizeof (new_physname)) == 0) {
10555a4c37c9Sqh201292 		(void) snprintf(new_devname, len, "%s:%s",
10565a4c37c9Sqh201292 		    new_physname, minor);
10575a4c37c9Sqh201292 		return (0);
10585a4c37c9Sqh201292 	}
10595a4c37c9Sqh201292 
10605a4c37c9Sqh201292 	return (-1);
10615a4c37c9Sqh201292 }
10625a4c37c9Sqh201292 /*
10637c478bd9Sstevel@tonic-gate  * Make a new /etc/vfstab:
10647c478bd9Sstevel@tonic-gate  * Read vfstab_in, convert the device name entries to appropriate vhci or phci
10657c478bd9Sstevel@tonic-gate  * based names, and write to vfstab_out. Only device names whose physical
10667c478bd9Sstevel@tonic-gate  * paths are either phci or vhci based names and not open-able are considered
10677c478bd9Sstevel@tonic-gate  * for conversion. Open-able device name entries are not converted as it
10687c478bd9Sstevel@tonic-gate  * means that the device is already accessible; hence no need to convert.
10697c478bd9Sstevel@tonic-gate  *
10707c478bd9Sstevel@tonic-gate  * Returns:
10717c478bd9Sstevel@tonic-gate  * 	0	successful but vfstab_out contents are the same as vfstab_in
10727c478bd9Sstevel@tonic-gate  *	1	successful and vfstab_out changed from vfstab_in
10737c478bd9Sstevel@tonic-gate  *	-1	failed
10747c478bd9Sstevel@tonic-gate  */
10757c478bd9Sstevel@tonic-gate static int
10767c478bd9Sstevel@tonic-gate update_vfstab(char *vfstab_in, char *vfstab_out)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	FILE *fp_in, *fp_out;
10797c478bd9Sstevel@tonic-gate 	char *buf, *tmpbuf;
10807c478bd9Sstevel@tonic-gate 	char *vfs_cache[2];
10817c478bd9Sstevel@tonic-gate 	int idx = 0, count = 0;
10827c478bd9Sstevel@tonic-gate 	int rv = -1;
10837c478bd9Sstevel@tonic-gate 	int vfstab_updated = 0;
10847c478bd9Sstevel@tonic-gate 	int i;
10857c478bd9Sstevel@tonic-gate 	char cdev[MAXPATHLEN];
10867c478bd9Sstevel@tonic-gate 	char bdev[MAXPATHLEN];
10877c478bd9Sstevel@tonic-gate 	char mntpt[MAXPATHLEN];
10887c478bd9Sstevel@tonic-gate 	char fstype[512];
10897c478bd9Sstevel@tonic-gate 	char fsckpass[512];
10907c478bd9Sstevel@tonic-gate 	char mntboot[512];
10917c478bd9Sstevel@tonic-gate 	char mntopt[MAX_MNTOPT_STR];
10927c478bd9Sstevel@tonic-gate 	char phys_bdev[MAXPATHLEN], phys_cdev[MAXPATHLEN];
10937c478bd9Sstevel@tonic-gate 	char bdev_minor[MAXNAMELEN], cdev_minor[MAXNAMELEN];
10947c478bd9Sstevel@tonic-gate 	char new_physname[MAXPATHLEN];
10957c478bd9Sstevel@tonic-gate 	char new_bdevlink[MAXPATHLEN], new_cdevlink[MAXPATHLEN];
10967c478bd9Sstevel@tonic-gate 	char fmt[80];
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	if (open_in_out_files(vfstab_in, vfstab_out, &fp_in, &fp_out) != 0)
10997c478bd9Sstevel@tonic-gate 		return (-1);
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	/*
11027c478bd9Sstevel@tonic-gate 	 * Read one line at time from vfstab_in. If no conversion is needed
11037c478bd9Sstevel@tonic-gate 	 * for the line simply write the line to vfstab_out. If conversion is
11047c478bd9Sstevel@tonic-gate 	 * needed, first write the existing line as a comment to vfstab_out
11057c478bd9Sstevel@tonic-gate 	 * and then write the converted line.
11067c478bd9Sstevel@tonic-gate 	 *
11077c478bd9Sstevel@tonic-gate 	 * To avoid commented entries piling up in vfstab in case if the
11087c478bd9Sstevel@tonic-gate 	 * user runs stmsboot multiple times to switch on and off from mpxio,
11097c478bd9Sstevel@tonic-gate 	 * add the commented line only if not already there. To do this
11107c478bd9Sstevel@tonic-gate 	 * cache the last two vfstab lines processed and add the commented
11117c478bd9Sstevel@tonic-gate 	 * entry only if it is not found in the cache. We only need to cache
11127c478bd9Sstevel@tonic-gate 	 * the last two lines because a device can have at most two names -
11137c478bd9Sstevel@tonic-gate 	 * one mpxio and one non-mpxio name. Therefore for any device name
11147c478bd9Sstevel@tonic-gate 	 * entry we at most add two comments - one with mpxio name and one
11157c478bd9Sstevel@tonic-gate 	 * with non-mpxio name - no matter how many times stmsboot is run.
11167c478bd9Sstevel@tonic-gate 	 */
11177c478bd9Sstevel@tonic-gate 	buf = (char *)s_malloc(VFS_LINE_MAX);
11187c478bd9Sstevel@tonic-gate 	tmpbuf = (char *)s_malloc(VFS_LINE_MAX);
11197c478bd9Sstevel@tonic-gate 	vfs_cache[0] = (char *)s_malloc(VFS_LINE_MAX);
11207c478bd9Sstevel@tonic-gate 	vfs_cache[1] = (char *)s_malloc(VFS_LINE_MAX);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	(void) snprintf(fmt, sizeof (fmt),
11237c478bd9Sstevel@tonic-gate 	    "%%%ds %%%ds %%%ds %%%ds %%%ds %%%ds %%%ds", sizeof (bdev) - 1,
11247c478bd9Sstevel@tonic-gate 	    sizeof (cdev) - 1, sizeof (mntpt) - 1, sizeof (fstype) - 1,
11257c478bd9Sstevel@tonic-gate 	    sizeof (fsckpass) - 1, sizeof (mntboot) - 1, sizeof (mntopt) - 1);
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	while (fgets(buf, VFS_LINE_MAX, fp_in) != NULL) {
11287c478bd9Sstevel@tonic-gate 		if (strlen(buf) == (VFS_LINE_MAX - 1) &&
11297c478bd9Sstevel@tonic-gate 		    buf[VFS_LINE_MAX-2] != '\n') {
11307c478bd9Sstevel@tonic-gate 			logerr(gettext("%1$s line size too long, "
11317c478bd9Sstevel@tonic-gate 			    "exceeded %2$d: \"%3$s\"\n"),
11327c478bd9Sstevel@tonic-gate 			    VFSTAB, VFS_LINE_MAX - 2, buf);
11337c478bd9Sstevel@tonic-gate 			goto out;
11347c478bd9Sstevel@tonic-gate 		}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 		/* LINTED - format specifier */
11377c478bd9Sstevel@tonic-gate 		if ((sscanf(buf, fmt, bdev, cdev, mntpt,
11387c478bd9Sstevel@tonic-gate 		    fstype, fsckpass, mntboot, mntopt) != 7) ||
11397c478bd9Sstevel@tonic-gate 		    (bdev[0] == '#') ||
11407c478bd9Sstevel@tonic-gate 		    (get_physname_minor(bdev, phys_bdev, sizeof (phys_bdev),
11417c478bd9Sstevel@tonic-gate 		    bdev_minor, sizeof (bdev_minor)) != 0) ||
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 		    (strcmp(fstype, "swap") != 0 &&
11447c478bd9Sstevel@tonic-gate 		    ((get_physname_minor(cdev, phys_cdev, sizeof (phys_cdev),
11457c478bd9Sstevel@tonic-gate 		    cdev_minor, sizeof (cdev_minor)) != 0) ||
11467c478bd9Sstevel@tonic-gate 		    (strcmp(phys_bdev, phys_cdev) != 0))) ||
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 		    canopen(bdev) ||
11497c478bd9Sstevel@tonic-gate 		    (map_physname(phys_bdev, new_physname,
11507c478bd9Sstevel@tonic-gate 		    sizeof (new_physname)) != 0) ||
11517c478bd9Sstevel@tonic-gate 		    (lookup_devlink(new_physname, bdev_minor, new_bdevlink,
11527c478bd9Sstevel@tonic-gate 		    sizeof (new_bdevlink)) != 0) ||
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 		    (strcmp(fstype, "swap") != 0 &&
11557c478bd9Sstevel@tonic-gate 		    (lookup_devlink(new_physname, cdev_minor, new_cdevlink,
11567c478bd9Sstevel@tonic-gate 		    sizeof (new_cdevlink)) != 0))) {
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 			/* cache the last two entries */
11597c478bd9Sstevel@tonic-gate 			(void) strlcpy(vfs_cache[idx], buf, VFS_LINE_MAX);
11607c478bd9Sstevel@tonic-gate 			idx = (idx == 0) ? 1 : 0;
11617c478bd9Sstevel@tonic-gate 			if (count < 2)
11627c478bd9Sstevel@tonic-gate 				count++;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 			if (fputs(buf, fp_out) == EOF) {
11657c478bd9Sstevel@tonic-gate 				logerr(gettext("fputs \"%1$s\" to %2$s "
11667c478bd9Sstevel@tonic-gate 				    "failed: %3$s\n"),
11677c478bd9Sstevel@tonic-gate 				    buf, vfstab_out, strerror(errno));
11687c478bd9Sstevel@tonic-gate 				goto out;
11697c478bd9Sstevel@tonic-gate 			}
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 		} else {
11727c478bd9Sstevel@tonic-gate 			/*
11737c478bd9Sstevel@tonic-gate 			 * comment the entry in vfstab only if it is not
11747c478bd9Sstevel@tonic-gate 			 * already in the cache.
11757c478bd9Sstevel@tonic-gate 			 */
11767c478bd9Sstevel@tonic-gate 			if (client_name_type(phys_bdev) == CLIENT_TYPE_VHCI)
11777c478bd9Sstevel@tonic-gate 				(void) snprintf(tmpbuf, VFS_LINE_MAX,
11787c478bd9Sstevel@tonic-gate 				    "# mpxio: %s", buf);
11797c478bd9Sstevel@tonic-gate 			else
11807c478bd9Sstevel@tonic-gate 				(void) snprintf(tmpbuf, VFS_LINE_MAX,
11817c478bd9Sstevel@tonic-gate 				    "# non-mpxio: %s", buf);
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 			for (i = 0; i < count; i++) {
11847c478bd9Sstevel@tonic-gate 				if (strcmp(vfs_cache[i], tmpbuf) == 0)
11857c478bd9Sstevel@tonic-gate 					break;
11867c478bd9Sstevel@tonic-gate 			}
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 			if (i == count) {
11897c478bd9Sstevel@tonic-gate 				if (fputs(tmpbuf, fp_out) == EOF) {
11907c478bd9Sstevel@tonic-gate 					logerr(gettext("fputs \"%1$s\" to %2$s "
11917c478bd9Sstevel@tonic-gate 					    "failed: %3$s\n"), tmpbuf,
11927c478bd9Sstevel@tonic-gate 					    vfstab_out, strerror(errno));
11937c478bd9Sstevel@tonic-gate 					goto out;
11947c478bd9Sstevel@tonic-gate 				}
11957c478bd9Sstevel@tonic-gate 			}
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 			count = 0;
11987c478bd9Sstevel@tonic-gate 			idx = 0;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 			if (fprintf(fp_out, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
12017c478bd9Sstevel@tonic-gate 			    new_bdevlink,
12027c478bd9Sstevel@tonic-gate 			    (strcmp(fstype, "swap") != 0) ? new_cdevlink : cdev,
12037c478bd9Sstevel@tonic-gate 			    mntpt, fstype, fsckpass, mntboot, mntopt) < 0) {
12047c478bd9Sstevel@tonic-gate 				logerr(gettext("fprintf failed to write to "
12057c478bd9Sstevel@tonic-gate 				    "%1$s: %2$s\n"),
12067c478bd9Sstevel@tonic-gate 				    vfstab_out, strerror(errno));
12077c478bd9Sstevel@tonic-gate 				goto out;
12087c478bd9Sstevel@tonic-gate 			}
12097c478bd9Sstevel@tonic-gate 			vfstab_updated = 1;
12107c478bd9Sstevel@tonic-gate 		}
12117c478bd9Sstevel@tonic-gate 	}
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	rv = vfstab_updated;
12147c478bd9Sstevel@tonic-gate out:
12157c478bd9Sstevel@tonic-gate 	(void) fclose(fp_in);
12167c478bd9Sstevel@tonic-gate 	(void) fclose(fp_out);
12177c478bd9Sstevel@tonic-gate 	free(buf);
12187c478bd9Sstevel@tonic-gate 	free(tmpbuf);
12197c478bd9Sstevel@tonic-gate 	free(vfs_cache[0]);
12207c478bd9Sstevel@tonic-gate 	free(vfs_cache[1]);
12217c478bd9Sstevel@tonic-gate 	return (rv);
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate /*
12257c478bd9Sstevel@tonic-gate  * if guidmap is 0, list non-STMS to STMS device name mappings for the
12267c478bd9Sstevel@tonic-gate  * specified controller.
12277c478bd9Sstevel@tonic-gate  * if guidmap is 1, list non-STMS to GUID mappings for the specified controller.
12287c478bd9Sstevel@tonic-gate  * If controller is -1 list mappings for all controllers.
12297c478bd9Sstevel@tonic-gate  *
12307c478bd9Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
12317c478bd9Sstevel@tonic-gate  */
12327c478bd9Sstevel@tonic-gate static int
12337c478bd9Sstevel@tonic-gate list_mappings(int controller, int guidmap)
12347c478bd9Sstevel@tonic-gate {
12357c478bd9Sstevel@tonic-gate 	int cnum, len, mapped;
12367c478bd9Sstevel@tonic-gate 	int header = 1;
12377c478bd9Sstevel@tonic-gate 	char *p1, *p2;
12387c478bd9Sstevel@tonic-gate 	DIR *dirp;
12397c478bd9Sstevel@tonic-gate 	struct dirent *direntry;
12407c478bd9Sstevel@tonic-gate 	char devname[MAXPATHLEN];
12417c478bd9Sstevel@tonic-gate 	char physname[MAXPATHLEN];
12427c478bd9Sstevel@tonic-gate 	char new_devname[MAXPATHLEN];
12437c478bd9Sstevel@tonic-gate 	char new_physname[MAXPATHLEN];
12447c478bd9Sstevel@tonic-gate 	char guid[MAXPATHLEN];
12457c478bd9Sstevel@tonic-gate 	char minor[MAXNAMELEN];
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	if ((dirp = opendir("/dev/rdsk")) == NULL)
12487c478bd9Sstevel@tonic-gate 		return (-1);
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	while ((direntry = readdir(dirp)) != NULL) {
12517c478bd9Sstevel@tonic-gate 		if (strcmp(direntry->d_name, ".") == 0 ||
12527c478bd9Sstevel@tonic-gate 		    strcmp(direntry->d_name, "..") == 0 ||
12537c478bd9Sstevel@tonic-gate 		    (len = strlen(direntry->d_name)) < 2 ||
12547c478bd9Sstevel@tonic-gate 		    strcmp(direntry->d_name + len - 2, "s0") != 0 ||
12557c478bd9Sstevel@tonic-gate 		    sscanf(direntry->d_name, "c%dt", &cnum) != 1 ||
12567c478bd9Sstevel@tonic-gate 		    (controller != -1 && controller != cnum))
12577c478bd9Sstevel@tonic-gate 			continue;
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 		(void) snprintf(devname, MAXPATHLEN, "/dev/rdsk/%s",
12607c478bd9Sstevel@tonic-gate 		    direntry->d_name);
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 		if (get_physname_minor(devname, physname, sizeof (physname),
12637c478bd9Sstevel@tonic-gate 		    minor, sizeof (minor)) != 0 ||
12647c478bd9Sstevel@tonic-gate 		    client_name_type(physname) != CLIENT_TYPE_PHCI)
12657c478bd9Sstevel@tonic-gate 			continue;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 		/*
12687c478bd9Sstevel@tonic-gate 		 * First try phci_to_vhci() mapping. It will work if the
12697c478bd9Sstevel@tonic-gate 		 * device is under MPxIO control. If the device is not under
12707c478bd9Sstevel@tonic-gate 		 * MPxIO, phci_to_vhci() will fail in which case try to lookup
12717c478bd9Sstevel@tonic-gate 		 * if an old mapping exists using guid lookup.
12727c478bd9Sstevel@tonic-gate 		 */
12737c478bd9Sstevel@tonic-gate 		mapped = 1;
12747c478bd9Sstevel@tonic-gate 		if (phci_to_vhci(physname, new_physname,
12757c478bd9Sstevel@tonic-gate 		    sizeof (new_physname)) != 0) {
12767c478bd9Sstevel@tonic-gate 			if (get_guid(physname, guid, sizeof (guid), 1,
12777c478bd9Sstevel@tonic-gate 			    DI_NODE_NIL) == 0)
12787c478bd9Sstevel@tonic-gate 				(void) snprintf(new_physname, MAXPATHLEN,
12795a4c37c9Sqh201292 				    "/scsi_vhci/%s%s", DISK_AT_G, guid);
12807c478bd9Sstevel@tonic-gate 			else
12817c478bd9Sstevel@tonic-gate 				mapped = 0;
12827c478bd9Sstevel@tonic-gate 		}
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 		if (mapped == 0)
12857c478bd9Sstevel@tonic-gate 			continue;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 		/* strip the slice number part */
12887c478bd9Sstevel@tonic-gate 		devname[strlen(devname) - 2] = '\0';
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 		if (guidmap == 0) {
12917c478bd9Sstevel@tonic-gate 			if (lookup_devlink(new_physname, minor,
12927c478bd9Sstevel@tonic-gate 			    new_devname, sizeof (new_devname)) != 0)
12937c478bd9Sstevel@tonic-gate 				continue;
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 			/* strip the slice number part */
12967c478bd9Sstevel@tonic-gate 			new_devname[strlen(new_devname) - 2] = '\0';
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 			if (header) {
12997c478bd9Sstevel@tonic-gate 				(void) printf(
13007c478bd9Sstevel@tonic-gate 				    gettext("non-STMS device name\t\t\t"
13017c478bd9Sstevel@tonic-gate 				    "STMS device name\n"
13027c478bd9Sstevel@tonic-gate 				    "------------------------------------------"
13037c478bd9Sstevel@tonic-gate 				    "------------------------\n"));
13047c478bd9Sstevel@tonic-gate 				header = 0;
13057c478bd9Sstevel@tonic-gate 			}
13067c478bd9Sstevel@tonic-gate 			(void) printf("%s\t\t%s\n", devname, new_devname);
13077c478bd9Sstevel@tonic-gate 		} else {
13087c478bd9Sstevel@tonic-gate 			/* extract guid part */
13095a4c37c9Sqh201292 			if ((p1 = strstr(new_physname, DISK_AT_G)) == NULL) {
13107c478bd9Sstevel@tonic-gate 				logdmsg("invalid vhci: %s\n", new_physname);
13117c478bd9Sstevel@tonic-gate 				continue;
13127c478bd9Sstevel@tonic-gate 			}
13135a4c37c9Sqh201292 			p1 += sizeof (DISK_AT_G) - 1;
13147c478bd9Sstevel@tonic-gate 			if ((p2 = strrchr(p1, ':')) != NULL)
13157c478bd9Sstevel@tonic-gate 				*p2 = '\0';
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 			if (header) {
13187c478bd9Sstevel@tonic-gate 				(void) printf(
13197c478bd9Sstevel@tonic-gate 				    gettext("non-STMS device name\t\t\tGUID\n"
13207c478bd9Sstevel@tonic-gate 				    "------------------------------------------"
13217c478bd9Sstevel@tonic-gate 				    "------------------------\n"));
13227c478bd9Sstevel@tonic-gate 				header = 0;
13237c478bd9Sstevel@tonic-gate 			}
13247c478bd9Sstevel@tonic-gate 			(void) printf("%s\t\t%s\n", devname, p1);
13257c478bd9Sstevel@tonic-gate 		}
13267c478bd9Sstevel@tonic-gate 	}
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
13297c478bd9Sstevel@tonic-gate 	return (0);
13307c478bd9Sstevel@tonic-gate }
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate /*
13337c478bd9Sstevel@tonic-gate  * Check if the file can be opened.
13347c478bd9Sstevel@tonic-gate  *
13357c478bd9Sstevel@tonic-gate  * Return 1 if the file can be opened, 0 otherwise.
13367c478bd9Sstevel@tonic-gate  */
13377c478bd9Sstevel@tonic-gate static int
13387c478bd9Sstevel@tonic-gate canopen(char *filename)
13397c478bd9Sstevel@tonic-gate {
13407c478bd9Sstevel@tonic-gate 	int fd;
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	if ((fd = open(filename, O_RDONLY)) == -1)
13437c478bd9Sstevel@tonic-gate 		return (0);
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	(void) close(fd);
13467c478bd9Sstevel@tonic-gate 	return (1);
13477c478bd9Sstevel@tonic-gate }
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate static void
13507c478bd9Sstevel@tonic-gate logerr(char *msg, ...)
13517c478bd9Sstevel@tonic-gate {
13527c478bd9Sstevel@tonic-gate 	va_list ap;
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", stmsboot);
13557c478bd9Sstevel@tonic-gate 	va_start(ap, msg);
13567c478bd9Sstevel@tonic-gate 	/* LINTED - format specifier */
13577c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, msg, ap);
13587c478bd9Sstevel@tonic-gate 	va_end(ap);
13597c478bd9Sstevel@tonic-gate }
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate /* log debug message */
13627c478bd9Sstevel@tonic-gate static void
13637c478bd9Sstevel@tonic-gate logdmsg(char *msg, ...)
13647c478bd9Sstevel@tonic-gate {
13657c478bd9Sstevel@tonic-gate 	va_list ap;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	if (debug) {
13687c478bd9Sstevel@tonic-gate 		va_start(ap, msg);
13697c478bd9Sstevel@tonic-gate 		/* LINTED - format specifier */
13707c478bd9Sstevel@tonic-gate 		(void) vprintf(msg, ap);
13717c478bd9Sstevel@tonic-gate 		va_end(ap);
13727c478bd9Sstevel@tonic-gate 	}
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate static void *
13767c478bd9Sstevel@tonic-gate s_malloc(const size_t size)
13777c478bd9Sstevel@tonic-gate {
13787c478bd9Sstevel@tonic-gate 	void *rp;
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	if ((rp = malloc(size)) == NULL) {
13817c478bd9Sstevel@tonic-gate 		logerr(gettext("malloc failed to allocate %d bytes\n"), size);
13827c478bd9Sstevel@tonic-gate 		clean_exit(1);
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 	return (rp);
13857c478bd9Sstevel@tonic-gate }
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate static char *
13887c478bd9Sstevel@tonic-gate s_strdup(const char *ptr)
13897c478bd9Sstevel@tonic-gate {
13907c478bd9Sstevel@tonic-gate 	void *rp;
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	if ((rp = strdup(ptr)) == NULL) {
13937c478bd9Sstevel@tonic-gate 		logerr(gettext("strdup failed to dup %s\n"), ptr);
13947c478bd9Sstevel@tonic-gate 		clean_exit(1);
13957c478bd9Sstevel@tonic-gate 	}
13967c478bd9Sstevel@tonic-gate 	return (rp);
13977c478bd9Sstevel@tonic-gate }
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate static void
14007c478bd9Sstevel@tonic-gate s_strlcpy(char *dst, const char *src, size_t dstsize)
14017c478bd9Sstevel@tonic-gate {
14027c478bd9Sstevel@tonic-gate 	int n;
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	if ((n = strlcpy(dst, src, dstsize)) >= dstsize) {
14057c478bd9Sstevel@tonic-gate 		logerr(gettext("strlcpy: destination buffer size is %1$d "
14067c478bd9Sstevel@tonic-gate 		    "bytes, need to at least %2$d bytes\n"), dstsize, n + 1);
14077c478bd9Sstevel@tonic-gate 		clean_exit(1);
14087c478bd9Sstevel@tonic-gate 	}
14097c478bd9Sstevel@tonic-gate }
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate static void
14127c478bd9Sstevel@tonic-gate clean_exit(int status)
14137c478bd9Sstevel@tonic-gate {
14147c478bd9Sstevel@tonic-gate 	if (devinfo_root != DI_NODE_NIL)
14157c478bd9Sstevel@tonic-gate 		di_fini(devinfo_root);
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	if (devlink_hdl != NULL)
14187c478bd9Sstevel@tonic-gate 		(void) di_devlink_fini(&devlink_hdl);
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	if (vhci_fd != -1)
14217c478bd9Sstevel@tonic-gate 		(void) close(vhci_fd);
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	exit(status);
14247c478bd9Sstevel@tonic-gate }
1425