1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <stdarg.h>
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <sys/stat.h>
34*0Sstevel@tonic-gate #include <fcntl.h>
35*0Sstevel@tonic-gate #include <errno.h>
36*0Sstevel@tonic-gate #include <unistd.h>
37*0Sstevel@tonic-gate #include <stropts.h>
38*0Sstevel@tonic-gate #include <strings.h>
39*0Sstevel@tonic-gate #include <dirent.h>
40*0Sstevel@tonic-gate #include <sys/param.h>
41*0Sstevel@tonic-gate #include <sys/scsi/adapters/scsi_vhci.h>
42*0Sstevel@tonic-gate #include <libdevinfo.h>
43*0Sstevel@tonic-gate #include <libgen.h>
44*0Sstevel@tonic-gate #include <dlfcn.h>
45*0Sstevel@tonic-gate #include <link.h>
46*0Sstevel@tonic-gate #include <locale.h>
47*0Sstevel@tonic-gate #include <libintl.h>
48*0Sstevel@tonic-gate #include <sys/syscall.h>
49*0Sstevel@tonic-gate #include <sys/vfstab.h>
50*0Sstevel@tonic-gate #include <sys/mount.h>
51*0Sstevel@tonic-gate #include <devid.h>
52*0Sstevel@tonic-gate #include <sys/libdevid.h>
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #define	VHCI_CTL_NODE	"/devices/scsi_vhci:devctl"
55*0Sstevel@tonic-gate #define	SLASH_DEVICES	"/devices/"
56*0Sstevel@tonic-gate #define	SLASH_SSD_AT	"/ssd@"
57*0Sstevel@tonic-gate #define	SLASH_FP_AT	"/fp@"
58*0Sstevel@tonic-gate #define	SLASH_SCSI_VHCI	"/scsi_vhci"
59*0Sstevel@tonic-gate #define	DEV_DSK		"/dev/dsk/"
60*0Sstevel@tonic-gate #define	DEV_RDSK	"/dev/rdsk/"
61*0Sstevel@tonic-gate #define	SYS_FILENAME_LEN	256
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /*
64*0Sstevel@tonic-gate  * Save directory is the directory in which system files are saved.
65*0Sstevel@tonic-gate  * Save directory must be under the root filesystem, as this program is
66*0Sstevel@tonic-gate  * typically run before any other filesystems are mounted.
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate #define	SAVE_DIR	"/etc/mpxio"
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /* fcp driver publishes this property */
71*0Sstevel@tonic-gate #define	NODE_WWN_PROP	"node-wwn"
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate typedef enum {
74*0Sstevel@tonic-gate 	CLIENT_TYPE_UNKNOWN,
75*0Sstevel@tonic-gate 	CLIENT_TYPE_PHCI,
76*0Sstevel@tonic-gate 	CLIENT_TYPE_VHCI
77*0Sstevel@tonic-gate } client_type_t;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate struct devlink_cbarg {
80*0Sstevel@tonic-gate 	char *devlink;
81*0Sstevel@tonic-gate 	size_t len;
82*0Sstevel@tonic-gate };
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate static di_node_t devinfo_root = DI_NODE_NIL;
85*0Sstevel@tonic-gate static di_devlink_handle_t devlink_hdl = NULL;
86*0Sstevel@tonic-gate static int vhci_fd = -1;
87*0Sstevel@tonic-gate static int patch_vfstab, cap_m_option, debug;
88*0Sstevel@tonic-gate static int list_option, list_guid_mappings, list_controllernum = -1;
89*0Sstevel@tonic-gate static char *mapdev = "";
90*0Sstevel@tonic-gate static char *stmsboot = "stmsboot";
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate static int make_temp(char *, char *, char *, size_t);
93*0Sstevel@tonic-gate static void commit_change(char *, char *, char *, int);
94*0Sstevel@tonic-gate static int map_devname(char *, char *, size_t, int);
95*0Sstevel@tonic-gate static int update_vfstab(char *, char *);
96*0Sstevel@tonic-gate static int list_mappings(int, int);
97*0Sstevel@tonic-gate static int canopen(char *);
98*0Sstevel@tonic-gate static void logerr(char *, ...);
99*0Sstevel@tonic-gate static void logdmsg(char *, ...);
100*0Sstevel@tonic-gate static void *s_malloc(const size_t);
101*0Sstevel@tonic-gate static char *s_strdup(const char *);
102*0Sstevel@tonic-gate static void s_strlcpy(char *, const char *, size_t);
103*0Sstevel@tonic-gate static void clean_exit(int);
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate /*
106*0Sstevel@tonic-gate  * Print usage and exit.
107*0Sstevel@tonic-gate  */
108*0Sstevel@tonic-gate static void
109*0Sstevel@tonic-gate usage(char *argv0)
110*0Sstevel@tonic-gate {
111*0Sstevel@tonic-gate 	char *progname;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	progname = strrchr(argv0, '/');
114*0Sstevel@tonic-gate 	if (progname != NULL)
115*0Sstevel@tonic-gate 		progname++;
116*0Sstevel@tonic-gate 	else
117*0Sstevel@tonic-gate 		progname = argv0;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	/*
120*0Sstevel@tonic-gate 	 * -u	update /etc/vfstab
121*0Sstevel@tonic-gate 	 * -m devname
122*0Sstevel@tonic-gate 	 *	if devname is phci based name and not open-able, map it to
123*0Sstevel@tonic-gate 	 *	vhci based /devices name.
124*0Sstevel@tonic-gate 	 *	if devname is vhci based name and not open-able, map it to
125*0Sstevel@tonic-gate 	 *	phci based /devices name.
126*0Sstevel@tonic-gate 	 * -M devname
127*0Sstevel@tonic-gate 	 *	same as -m except that /dev link is printed instead of
128*0Sstevel@tonic-gate 	 *	/devices name.
129*0Sstevel@tonic-gate 	 * -l controller
130*0Sstevel@tonic-gate 	 *	list non-STMS to STMS device name mappings for the specific
131*0Sstevel@tonic-gate 	 *	controller
132*0Sstevel@tonic-gate 	 * -L	list non-STMS to STMS device name mappings for all controllers
133*0Sstevel@tonic-gate 	 */
134*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("usage: %s -u | -m devname | "
135*0Sstevel@tonic-gate 	    "-M devname | -l controller | -L\n"), progname);
136*0Sstevel@tonic-gate 	exit(2);
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate  * Parse command line arguments.
141*0Sstevel@tonic-gate  */
142*0Sstevel@tonic-gate static void
143*0Sstevel@tonic-gate parse_args(int argc, char *argv[])
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	char opt;
146*0Sstevel@tonic-gate 	int n = 0;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	if (argc == 1) {
149*0Sstevel@tonic-gate 		usage(argv[0]);
150*0Sstevel@tonic-gate 		/*NOTREACHED*/
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "udm:M:Ll:g")) != EOF) {
154*0Sstevel@tonic-gate 		switch (opt) {
155*0Sstevel@tonic-gate 		case 'u':
156*0Sstevel@tonic-gate 			patch_vfstab = 1;
157*0Sstevel@tonic-gate 			n++;
158*0Sstevel@tonic-gate 			break;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 		case 'd':
161*0Sstevel@tonic-gate 			debug = 1;
162*0Sstevel@tonic-gate 			break;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		case 'm':
165*0Sstevel@tonic-gate 			mapdev = s_strdup(optarg);
166*0Sstevel@tonic-gate 			n++;
167*0Sstevel@tonic-gate 			break;
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 		case 'M':
170*0Sstevel@tonic-gate 			mapdev = s_strdup(optarg);
171*0Sstevel@tonic-gate 			cap_m_option = 1;
172*0Sstevel@tonic-gate 			n++;
173*0Sstevel@tonic-gate 			break;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		case 'L':
176*0Sstevel@tonic-gate 			list_option = 1;
177*0Sstevel@tonic-gate 			n++;
178*0Sstevel@tonic-gate 			break;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 		case 'l':
181*0Sstevel@tonic-gate 			list_option = 1;
182*0Sstevel@tonic-gate 			list_controllernum = (int)atol(optarg);
183*0Sstevel@tonic-gate 			if (list_controllernum < 0) {
184*0Sstevel@tonic-gate 				logerr(gettext("controller number %d is "
185*0Sstevel@tonic-gate 				    "invalid\n"), list_controllernum);
186*0Sstevel@tonic-gate 				clean_exit(1);
187*0Sstevel@tonic-gate 			}
188*0Sstevel@tonic-gate 			n++;
189*0Sstevel@tonic-gate 			break;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 		case 'g':
192*0Sstevel@tonic-gate 			/*
193*0Sstevel@tonic-gate 			 * private option to display non-STMS device name
194*0Sstevel@tonic-gate 			 * to GUID mappings.
195*0Sstevel@tonic-gate 			 */
196*0Sstevel@tonic-gate 			list_guid_mappings = 1;
197*0Sstevel@tonic-gate 			n++;
198*0Sstevel@tonic-gate 			break;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 		default:
201*0Sstevel@tonic-gate 			usage(argv[0]);
202*0Sstevel@tonic-gate 			/*NOTREACHED*/
203*0Sstevel@tonic-gate 		}
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	if (n != 1)
207*0Sstevel@tonic-gate 		usage(argv[0]);
208*0Sstevel@tonic-gate 		/*NOTREACHED*/
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate void
212*0Sstevel@tonic-gate main(int argc, char *argv[])
213*0Sstevel@tonic-gate {
214*0Sstevel@tonic-gate 	char save_vfstab[SYS_FILENAME_LEN], tmp_vfstab[SYS_FILENAME_LEN];
215*0Sstevel@tonic-gate 	int vfstab_updated;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
218*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	if (getuid() != 0) {
221*0Sstevel@tonic-gate 		logerr(gettext("must be super-user to run this program\n"));
222*0Sstevel@tonic-gate 		clean_exit(1);
223*0Sstevel@tonic-gate 	}
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	parse_args(argc, argv);
226*0Sstevel@tonic-gate 	(void) umask(022);
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	/*
229*0Sstevel@tonic-gate 	 * NOTE: The mpxio boot-up script executes this program with the
230*0Sstevel@tonic-gate 	 * mapping (-m) option before the /usr is even mounted and when the
231*0Sstevel@tonic-gate 	 * root filesystem is still mounted read-only.
232*0Sstevel@tonic-gate 	 */
233*0Sstevel@tonic-gate 	if (*mapdev != '\0') {
234*0Sstevel@tonic-gate 		char newname[MAXPATHLEN];
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 		if (map_devname(mapdev, newname, sizeof (newname),
237*0Sstevel@tonic-gate 		    cap_m_option) == 0) {
238*0Sstevel@tonic-gate 			(void) printf("%s\n", newname);
239*0Sstevel@tonic-gate 			clean_exit(0);
240*0Sstevel@tonic-gate 		}
241*0Sstevel@tonic-gate 		clean_exit(1);
242*0Sstevel@tonic-gate 	}
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	if (list_option || list_guid_mappings) {
245*0Sstevel@tonic-gate 		if (list_mappings(list_controllernum, list_guid_mappings) == 0)
246*0Sstevel@tonic-gate 			clean_exit(0);
247*0Sstevel@tonic-gate 		clean_exit(1);
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	/* create a directory where a copy of the system files are saved */
251*0Sstevel@tonic-gate 	if (patch_vfstab) {
252*0Sstevel@tonic-gate 		if (mkdirp(SAVE_DIR, 0755) != 0 && errno != EEXIST) {
253*0Sstevel@tonic-gate 			logerr(gettext("mkdirp: failed to create %1$s: %2$s\n"),
254*0Sstevel@tonic-gate 			    SAVE_DIR, strerror(errno));
255*0Sstevel@tonic-gate 			clean_exit(1);
256*0Sstevel@tonic-gate 		}
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 		if (make_temp(VFSTAB, save_vfstab, tmp_vfstab,
259*0Sstevel@tonic-gate 		    SYS_FILENAME_LEN) != 0)
260*0Sstevel@tonic-gate 			clean_exit(1);
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 		/* build new vfstab without modifying the existing one */
263*0Sstevel@tonic-gate 		if ((vfstab_updated = update_vfstab(VFSTAB, tmp_vfstab))
264*0Sstevel@tonic-gate 		    == -1) {
265*0Sstevel@tonic-gate 			logerr(gettext("failed to update %s\n"), VFSTAB);
266*0Sstevel@tonic-gate 			clean_exit(1);
267*0Sstevel@tonic-gate 		}
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 		commit_change(VFSTAB, save_vfstab, tmp_vfstab, vfstab_updated);
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	clean_exit(0);
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate /*
276*0Sstevel@tonic-gate  * Make saved and temporary filenames in SAVE_DIR.
277*0Sstevel@tonic-gate  *
278*0Sstevel@tonic-gate  * ex: if the filename is /etc/vfstab then the save_filename and tmp_filename
279*0Sstevel@tonic-gate  * would be SAVE_DIR/vfstab and SAVE_DIR/vfstab.tmp respectively.
280*0Sstevel@tonic-gate  *
281*0Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
282*0Sstevel@tonic-gate  */
283*0Sstevel@tonic-gate static int
284*0Sstevel@tonic-gate make_temp(char *filename, char *save_filename, char *tmp_filename, size_t len)
285*0Sstevel@tonic-gate {
286*0Sstevel@tonic-gate 	char *ptr;
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	if ((ptr = strrchr(filename, '/')) == NULL) {
289*0Sstevel@tonic-gate 		logdmsg("invalid file %s\n", filename);
290*0Sstevel@tonic-gate 		return (-1);
291*0Sstevel@tonic-gate 	}
292*0Sstevel@tonic-gate 	(void) snprintf(save_filename, len, "%s%s", SAVE_DIR, ptr);
293*0Sstevel@tonic-gate 	(void) snprintf(tmp_filename, len, "%s%s.tmp", SAVE_DIR, ptr);
294*0Sstevel@tonic-gate 	logdmsg("make_temp: %s: save = %s, temp = %s\n", filename,
295*0Sstevel@tonic-gate 	    save_filename, tmp_filename);
296*0Sstevel@tonic-gate 	return (0);
297*0Sstevel@tonic-gate }
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate /*
300*0Sstevel@tonic-gate  * Commit the changes made to the system file
301*0Sstevel@tonic-gate  */
302*0Sstevel@tonic-gate static void
303*0Sstevel@tonic-gate commit_change(char *filename, char *save_filename, char *tmp_filename,
304*0Sstevel@tonic-gate     int updated)
305*0Sstevel@tonic-gate {
306*0Sstevel@tonic-gate 	int x;
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	if (updated) {
309*0Sstevel@tonic-gate 		/* save the original */
310*0Sstevel@tonic-gate 		if ((x = rename(filename, save_filename)) != 0) {
311*0Sstevel@tonic-gate 			logerr(gettext("rename %1$s to %2$s failed: %3$s\n"),
312*0Sstevel@tonic-gate 			    filename, save_filename, strerror(errno));
313*0Sstevel@tonic-gate 		}
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 		/* now rename the new file to the actual file */
316*0Sstevel@tonic-gate 		if (rename(tmp_filename, filename) != 0) {
317*0Sstevel@tonic-gate 			logerr(gettext("rename %1$s to %2$s failed: %3$s\n"),
318*0Sstevel@tonic-gate 			    tmp_filename, filename, strerror(errno));
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 			/* restore the original */
321*0Sstevel@tonic-gate 			if (x == 0 && rename(save_filename, filename) != 0) {
322*0Sstevel@tonic-gate 				logerr(
323*0Sstevel@tonic-gate 				    gettext("rename %1$s to %2$s failed: %3$s\n"
324*0Sstevel@tonic-gate 				    "%4$s is a copy of the original %5$s file"
325*0Sstevel@tonic-gate 				    "\n"),
326*0Sstevel@tonic-gate 				    save_filename, filename, strerror(errno),
327*0Sstevel@tonic-gate 				    save_filename, filename);
328*0Sstevel@tonic-gate 			}
329*0Sstevel@tonic-gate 		} else
330*0Sstevel@tonic-gate 			(void) printf(gettext("%1$s: %2$s has been updated.\n"),
331*0Sstevel@tonic-gate 			    stmsboot, filename);
332*0Sstevel@tonic-gate 	} else {
333*0Sstevel@tonic-gate 		/* remove the temp file */
334*0Sstevel@tonic-gate 		(void) unlink(tmp_filename);
335*0Sstevel@tonic-gate 		(void) printf(gettext("%1$s: %2$s was not modified as no "
336*0Sstevel@tonic-gate 		    "changes were needed.\n"), stmsboot, filename);
337*0Sstevel@tonic-gate 	}
338*0Sstevel@tonic-gate }
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate /*
341*0Sstevel@tonic-gate  * Get the GUID of the device.
342*0Sstevel@tonic-gate  *
343*0Sstevel@tonic-gate  * physpath	/devices name without the /devices prefix and minor name
344*0Sstevel@tonic-gate  *		component.
345*0Sstevel@tonic-gate  * guid		caller supplied buffer where the GUID will be placed on return
346*0Sstevel@tonic-gate  * guid_len	length of the caller supplied guid buffer.
347*0Sstevel@tonic-gate  * no_dealy_flag if set open the device with O_NDELAY
348*0Sstevel@tonic-gate  * node		di_node corresponding to physpath if already available,
349*0Sstevel@tonic-gate  *		otherwise pass DI_NODE_NIL.
350*0Sstevel@tonic-gate  *
351*0Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
352*0Sstevel@tonic-gate  */
353*0Sstevel@tonic-gate static int
354*0Sstevel@tonic-gate get_guid(char *physpath, char *guid, int guid_len, int no_delay_flag,
355*0Sstevel@tonic-gate 	di_node_t node)
356*0Sstevel@tonic-gate {
357*0Sstevel@tonic-gate 	int		fd;
358*0Sstevel@tonic-gate 	ddi_devid_t	devid;
359*0Sstevel@tonic-gate 	int		rv	= -1;
360*0Sstevel@tonic-gate 	char		*i_guid	= NULL;
361*0Sstevel@tonic-gate 	char		physpath_raw[MAXPATHLEN];
362*0Sstevel@tonic-gate 	uchar_t		*wwnp;
363*0Sstevel@tonic-gate 	int		i, n, snapshot_taken = 0;
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	logdmsg("get_guid: physpath = %s\n", physpath);
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	(void) snprintf(physpath_raw, MAXPATHLEN,
368*0Sstevel@tonic-gate 	    "/devices%s:a,raw", physpath);
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 	*guid = '\0';
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	if (no_delay_flag)
373*0Sstevel@tonic-gate 		no_delay_flag = O_NDELAY;
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	/*
376*0Sstevel@tonic-gate 	 * Open the raw device
377*0Sstevel@tonic-gate 	 * Without the O_DELAY flag, the open will fail on standby paths of
378*0Sstevel@tonic-gate 	 * T3 if its mp_support mode is "mpxio".
379*0Sstevel@tonic-gate 	 */
380*0Sstevel@tonic-gate 	if ((fd = open(physpath_raw, O_RDONLY | no_delay_flag)) == -1) {
381*0Sstevel@tonic-gate 		logdmsg("get_guid: failed to open %s: %s\n", physpath_raw,
382*0Sstevel@tonic-gate 		    strerror(errno));
383*0Sstevel@tonic-gate 		return (-1);
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	if (devid_get(fd, &devid) == 0) {
387*0Sstevel@tonic-gate 		i_guid = devid_to_guid(devid);
388*0Sstevel@tonic-gate 		devid_free(devid);
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 		if (i_guid != NULL) {
391*0Sstevel@tonic-gate 			s_strlcpy(guid, i_guid, guid_len);
392*0Sstevel@tonic-gate 			devid_free_guid(i_guid);
393*0Sstevel@tonic-gate 			rv = 0;
394*0Sstevel@tonic-gate 			goto out;
395*0Sstevel@tonic-gate 		} else
396*0Sstevel@tonic-gate 			logdmsg("get_guid: devid_to_guid() failed\n");
397*0Sstevel@tonic-gate 	} else
398*0Sstevel@tonic-gate 		logdmsg("get_guid: devid_get() failed: %s\n", strerror(errno));
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	/* fallback to node name as the guid as this is what fcp driver does */
401*0Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
402*0Sstevel@tonic-gate 		if ((node = di_init(physpath, DINFOCPYALL | DINFOFORCE))
403*0Sstevel@tonic-gate 		    == DI_NODE_NIL) {
404*0Sstevel@tonic-gate 			logdmsg("get_guid: di_init on %s failed: %s\n",
405*0Sstevel@tonic-gate 			    physpath, strerror(errno));
406*0Sstevel@tonic-gate 			goto out;
407*0Sstevel@tonic-gate 		}
408*0Sstevel@tonic-gate 		snapshot_taken = 1;
409*0Sstevel@tonic-gate 	}
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	if ((n = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, NODE_WWN_PROP,
412*0Sstevel@tonic-gate 	    &wwnp)) == -1) {
413*0Sstevel@tonic-gate 		logdmsg("get_guid: di_prop_lookup_bytes() failed to lookup "
414*0Sstevel@tonic-gate 		    "%s: %s\n", NODE_WWN_PROP, strerror(errno));
415*0Sstevel@tonic-gate 		goto out;
416*0Sstevel@tonic-gate 	}
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 	if (guid_len >= ((n * 2) + 1)) {
419*0Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
420*0Sstevel@tonic-gate 			(void) sprintf(guid + (i * 2), "%02x", (uint_t)(*wwnp));
421*0Sstevel@tonic-gate 			wwnp++;
422*0Sstevel@tonic-gate 		}
423*0Sstevel@tonic-gate 		rv = 0;
424*0Sstevel@tonic-gate 	} else
425*0Sstevel@tonic-gate 		logerr(gettext("insufficient buffer size: need %1$d "
426*0Sstevel@tonic-gate 		    "bytes, passed %2$d bytes\n"), (n * 2) + 1, guid_len);
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate out:
429*0Sstevel@tonic-gate 	if (snapshot_taken)
430*0Sstevel@tonic-gate 		di_fini(node);
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 	(void) close(fd);
433*0Sstevel@tonic-gate 	logdmsg("get_guid: GUID = %s\n", guid);
434*0Sstevel@tonic-gate 	return (rv);
435*0Sstevel@tonic-gate }
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate /*
438*0Sstevel@tonic-gate  * Given client_name return whether it is a phci or vhci based name.
439*0Sstevel@tonic-gate  * client_name is /devices name of a client without the /devices prefix.
440*0Sstevel@tonic-gate  *
441*0Sstevel@tonic-gate  * client_name			Return value
442*0Sstevel@tonic-gate  * .../fp@xxx/ssd@yyy		CLIENT_TYPE_PHCI
443*0Sstevel@tonic-gate  * .../scsi_vhci/ssd@yyy	CLIENT_TYPE_VHCI
444*0Sstevel@tonic-gate  * other			CLIENT_TYPE_UNKNOWN
445*0Sstevel@tonic-gate  */
446*0Sstevel@tonic-gate static client_type_t
447*0Sstevel@tonic-gate client_name_type(char *client_name)
448*0Sstevel@tonic-gate {
449*0Sstevel@tonic-gate 	client_type_t client_type = CLIENT_TYPE_UNKNOWN;
450*0Sstevel@tonic-gate 	char *p1, *p2;
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	if (*client_name != '/')
453*0Sstevel@tonic-gate 		return (CLIENT_TYPE_UNKNOWN);
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 	/* we only care for ssd devices */
456*0Sstevel@tonic-gate 	if ((p1 = strrchr(client_name, '/')) == NULL ||
457*0Sstevel@tonic-gate 	    strncmp(p1, SLASH_SSD_AT, sizeof (SLASH_SSD_AT) - 1) != 0)
458*0Sstevel@tonic-gate 		return (CLIENT_TYPE_UNKNOWN);
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 	*p1 = '\0';
461*0Sstevel@tonic-gate 
462*0Sstevel@tonic-gate 	if ((p2 = strrchr(client_name, '/')) != NULL) {
463*0Sstevel@tonic-gate 		if (strncmp(p2, SLASH_FP_AT, sizeof (SLASH_FP_AT) - 1) == 0)
464*0Sstevel@tonic-gate 			client_type = CLIENT_TYPE_PHCI;
465*0Sstevel@tonic-gate 		else if (strncmp(p2, SLASH_SCSI_VHCI,
466*0Sstevel@tonic-gate 		    sizeof (SLASH_SCSI_VHCI) - 1) == 0)
467*0Sstevel@tonic-gate 			client_type = CLIENT_TYPE_VHCI;
468*0Sstevel@tonic-gate 	}
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 	*p1 = '/';
471*0Sstevel@tonic-gate 	return (client_type);
472*0Sstevel@tonic-gate }
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate /*
475*0Sstevel@tonic-gate  * Map phci based client name to vhci based client name.
476*0Sstevel@tonic-gate  *
477*0Sstevel@tonic-gate  * phci_name
478*0Sstevel@tonic-gate  *	phci based client /devices name without the /devices prefix and
479*0Sstevel@tonic-gate  *	minor name component.
480*0Sstevel@tonic-gate  *	ex: /pci@8,600000/SUNW,qlc@4/fp@0,0/ssd@w2100002037cd9f72,0
481*0Sstevel@tonic-gate  *
482*0Sstevel@tonic-gate  * vhci_name
483*0Sstevel@tonic-gate  *	Caller supplied buffer where vhci /devices name will be placed on
484*0Sstevel@tonic-gate  *	return (without the /devices prefix and minor name component).
485*0Sstevel@tonic-gate  *	ex: /scsi_vhci/ssd@g2000002037cd9f72
486*0Sstevel@tonic-gate  *
487*0Sstevel@tonic-gate  * vhci_name_len
488*0Sstevel@tonic-gate  *	Length of the caller supplied vhci_name buffer.
489*0Sstevel@tonic-gate  *
490*0Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
491*0Sstevel@tonic-gate  */
492*0Sstevel@tonic-gate static int
493*0Sstevel@tonic-gate phci_to_vhci(char *phci_name, char *vhci_name, size_t vhci_name_len)
494*0Sstevel@tonic-gate {
495*0Sstevel@tonic-gate 	sv_iocdata_t ioc;
496*0Sstevel@tonic-gate 	char *slash;
497*0Sstevel@tonic-gate 	char vhci_name_buf[MAXPATHLEN];
498*0Sstevel@tonic-gate 	char phci_name_buf[MAXPATHLEN];
499*0Sstevel@tonic-gate 	char addr_buf[MAXNAMELEN];
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	logdmsg("phci_to_vhci: client = %s\n", phci_name);
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 	s_strlcpy(phci_name_buf, phci_name, MAXPATHLEN);
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	if (client_name_type(phci_name_buf) != CLIENT_TYPE_PHCI ||
506*0Sstevel@tonic-gate 	    (slash = strrchr(phci_name_buf, '/')) == NULL ||
507*0Sstevel@tonic-gate 	    strncmp(slash, SLASH_SSD_AT, sizeof (SLASH_SSD_AT) - 1) != 0) {
508*0Sstevel@tonic-gate 		logdmsg("phci_to_vhci: %s is not of CLIENT_TYPE_PHCI\n",
509*0Sstevel@tonic-gate 		    phci_name);
510*0Sstevel@tonic-gate 		return (-1);
511*0Sstevel@tonic-gate 	}
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	if (vhci_fd < 0) {
514*0Sstevel@tonic-gate 		if ((vhci_fd = open(VHCI_CTL_NODE, O_RDWR)) < 0)
515*0Sstevel@tonic-gate 			return (-1);
516*0Sstevel@tonic-gate 	}
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate 	*slash = '\0';
519*0Sstevel@tonic-gate 	s_strlcpy(addr_buf, slash + sizeof (SLASH_SSD_AT) - 1, MAXNAMELEN);
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 	bzero(&ioc, sizeof (sv_iocdata_t));
522*0Sstevel@tonic-gate 	ioc.client = vhci_name_buf;
523*0Sstevel@tonic-gate 	ioc.phci = phci_name_buf;
524*0Sstevel@tonic-gate 	ioc.addr = addr_buf;
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 	if (ioctl(vhci_fd, SCSI_VHCI_GET_CLIENT_NAME, &ioc) != 0) {
527*0Sstevel@tonic-gate 		logdmsg("SCSI_VHCI_GET_CLIENT_NAME on %s "
528*0Sstevel@tonic-gate 		    "failed: %s\n", phci_name, strerror(errno));
529*0Sstevel@tonic-gate 		return (-1);
530*0Sstevel@tonic-gate 	}
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 	s_strlcpy(vhci_name, vhci_name_buf, vhci_name_len);
533*0Sstevel@tonic-gate 	logdmsg("phci_to_vhci: %s maps to %s\n", phci_name, vhci_name);
534*0Sstevel@tonic-gate 	return (0);
535*0Sstevel@tonic-gate }
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate /*
538*0Sstevel@tonic-gate  * Map vhci based client name to phci based client name.
539*0Sstevel@tonic-gate  * If the client has multiple paths, only one of the paths with which client
540*0Sstevel@tonic-gate  * can be accessed is returned. This function does not use SCSI_VHCI ioctls
541*0Sstevel@tonic-gate  * as it is called on mpxio disabled paths.
542*0Sstevel@tonic-gate  *
543*0Sstevel@tonic-gate  * vhci_name
544*0Sstevel@tonic-gate  *	vhci based client /devices name without the /devices prefix and
545*0Sstevel@tonic-gate  *	minor name component.
546*0Sstevel@tonic-gate  *	ex: /scsi_vhci/ssd@g2000002037cd9f72
547*0Sstevel@tonic-gate  *
548*0Sstevel@tonic-gate  * phci_name
549*0Sstevel@tonic-gate  *	Caller supplied buffer where phci /devices name will be placed on
550*0Sstevel@tonic-gate  *	return (without the /devices prefix and minor name component).
551*0Sstevel@tonic-gate  *	ex: /pci@8,600000/SUNW,qlc@4/fp@0,0/ssd@w2100002037cd9f72,0
552*0Sstevel@tonic-gate  *
553*0Sstevel@tonic-gate  * phci_name_len
554*0Sstevel@tonic-gate  *	Length of the caller supplied phci_name buffer.
555*0Sstevel@tonic-gate  *
556*0Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
557*0Sstevel@tonic-gate  */
558*0Sstevel@tonic-gate static int
559*0Sstevel@tonic-gate vhci_to_phci(char *vhci_name, char *phci_name, size_t phci_name_len)
560*0Sstevel@tonic-gate {
561*0Sstevel@tonic-gate 	di_node_t node, parent;
562*0Sstevel@tonic-gate 	char *vhci_guid, *devfspath;
563*0Sstevel@tonic-gate 	char phci_guid[MAXPATHLEN];
564*0Sstevel@tonic-gate 	char *parent_name, *node_name;
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 	logdmsg("vhci_to_phci: client = %s\n", vhci_name);
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 	if (client_name_type(vhci_name) != CLIENT_TYPE_VHCI) {
569*0Sstevel@tonic-gate 		logdmsg("vhci_to_phci: %s is not of CLIENT_TYPE_VHCI\n",
570*0Sstevel@tonic-gate 		    vhci_name);
571*0Sstevel@tonic-gate 		return (-1);
572*0Sstevel@tonic-gate 	}
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate 	if ((vhci_guid = strrchr(vhci_name, '@')) == NULL ||
575*0Sstevel@tonic-gate 	    *(++vhci_guid) != 'g') {
576*0Sstevel@tonic-gate 		logerr(gettext("couldn't get guid from %s\n"), vhci_name);
577*0Sstevel@tonic-gate 		return (-1);
578*0Sstevel@tonic-gate 	}
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 	/* point to guid */
581*0Sstevel@tonic-gate 	++vhci_guid;
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate 	/*
584*0Sstevel@tonic-gate 	 * Get devinfo snapshot and walk all ssd nodes whose parent is fp.
585*0Sstevel@tonic-gate 	 * For each node get the guid and match it with vhci_guid.
586*0Sstevel@tonic-gate 	 */
587*0Sstevel@tonic-gate 	if (devinfo_root == DI_NODE_NIL) {
588*0Sstevel@tonic-gate 		logdmsg("vhci_to_phci: taking devinfo snapshot\n");
589*0Sstevel@tonic-gate 		if ((devinfo_root = di_init("/", DINFOCPYALL | DINFOFORCE))
590*0Sstevel@tonic-gate 		    == DI_NODE_NIL) {
591*0Sstevel@tonic-gate 			logerr(gettext("di_init failed: %s\n"),
592*0Sstevel@tonic-gate 			    strerror(errno));
593*0Sstevel@tonic-gate 			return (-1);
594*0Sstevel@tonic-gate 		}
595*0Sstevel@tonic-gate 		logdmsg("vhci_to_phci: done taking devinfo snapshot\n");
596*0Sstevel@tonic-gate 	}
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 	for (node = di_drv_first_node("ssd", devinfo_root);
599*0Sstevel@tonic-gate 	    node != DI_NODE_NIL; node = di_drv_next_node(node)) {
600*0Sstevel@tonic-gate 		if ((node_name = di_node_name(node)) == NULL ||
601*0Sstevel@tonic-gate 		    strcmp(node_name, "ssd") != 0 ||
602*0Sstevel@tonic-gate 		    (parent = di_parent_node(node)) == DI_NODE_NIL ||
603*0Sstevel@tonic-gate 		    (parent_name = di_node_name(parent)) == NULL ||
604*0Sstevel@tonic-gate 		    strcmp(parent_name, "fp") != 0 ||
605*0Sstevel@tonic-gate 		    (devfspath = di_devfs_path(node)) == NULL)
606*0Sstevel@tonic-gate 			continue;
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 		/*
609*0Sstevel@tonic-gate 		 * Don't set no_delay_flag to have get_guid() fail on
610*0Sstevel@tonic-gate 		 * standby paths of T3. So we'll find the preferred paths.
611*0Sstevel@tonic-gate 		 */
612*0Sstevel@tonic-gate 		if (get_guid(devfspath, phci_guid,
613*0Sstevel@tonic-gate 		    sizeof (phci_guid), 0, node) == 0 &&
614*0Sstevel@tonic-gate 		    strcmp(phci_guid, vhci_guid) == 0) {
615*0Sstevel@tonic-gate 			s_strlcpy(phci_name, devfspath, phci_name_len);
616*0Sstevel@tonic-gate 			di_devfs_path_free(devfspath);
617*0Sstevel@tonic-gate 			logdmsg("vhci_to_phci: %s maps to %s\n", vhci_name,
618*0Sstevel@tonic-gate 			    phci_name);
619*0Sstevel@tonic-gate 			return (0);
620*0Sstevel@tonic-gate 		}
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 		di_devfs_path_free(devfspath);
623*0Sstevel@tonic-gate 	}
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 	logdmsg("vhci_to_phci: couldn't get phci name for %s\n", vhci_name);
626*0Sstevel@tonic-gate 	return (-1);
627*0Sstevel@tonic-gate }
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate /*
630*0Sstevel@tonic-gate  * Map physname from phci name space to vhci name space or vice-versa
631*0Sstevel@tonic-gate  *
632*0Sstevel@tonic-gate  * physname
633*0Sstevel@tonic-gate  *	phci or vhci based client /devices name without the /devices prefix and
634*0Sstevel@tonic-gate  *	minor name component.
635*0Sstevel@tonic-gate  *
636*0Sstevel@tonic-gate  * new_physname
637*0Sstevel@tonic-gate  *	Caller supplied buffer where the mapped physical name is stored on
638*0Sstevel@tonic-gate  *	return (without the /devices prefix and minor name component).
639*0Sstevel@tonic-gate  *
640*0Sstevel@tonic-gate  * len
641*0Sstevel@tonic-gate  *	Length of the caller supplied new_physname buffer.
642*0Sstevel@tonic-gate  *
643*0Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
644*0Sstevel@tonic-gate  */
645*0Sstevel@tonic-gate static int
646*0Sstevel@tonic-gate map_physname(char *physname, char *new_physname, size_t len)
647*0Sstevel@tonic-gate {
648*0Sstevel@tonic-gate 	int type;
649*0Sstevel@tonic-gate 	int rv;
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate 	if ((type = client_name_type(physname)) == CLIENT_TYPE_VHCI)
652*0Sstevel@tonic-gate 		rv = vhci_to_phci(physname, new_physname, len);
653*0Sstevel@tonic-gate 	else if (type == CLIENT_TYPE_PHCI)
654*0Sstevel@tonic-gate 		rv = phci_to_vhci(physname, new_physname, len);
655*0Sstevel@tonic-gate 	else
656*0Sstevel@tonic-gate 		rv = -1;
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 	return (rv);
659*0Sstevel@tonic-gate }
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate /*
662*0Sstevel@tonic-gate  * Given a phci or vhci devname which is either a /dev link or /devices name
663*0Sstevel@tonic-gate  * get the corresponding physical node path (without the /devices prefix)
664*0Sstevel@tonic-gate  * and minor name.
665*0Sstevel@tonic-gate  *
666*0Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
667*0Sstevel@tonic-gate  */
668*0Sstevel@tonic-gate static int
669*0Sstevel@tonic-gate get_physname_minor(char *devname, char *physname, int physname_len,
670*0Sstevel@tonic-gate     char *minorname, int minorname_len)
671*0Sstevel@tonic-gate {
672*0Sstevel@tonic-gate 	int linksize;
673*0Sstevel@tonic-gate 	char buf[MAXPATHLEN];
674*0Sstevel@tonic-gate 	char *p, *m;
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate 	if (strncmp(devname, DEV_DSK, sizeof (DEV_DSK) - 1) == 0 ||
677*0Sstevel@tonic-gate 	    strncmp(devname, DEV_RDSK, sizeof (DEV_RDSK) - 1) == 0) {
678*0Sstevel@tonic-gate 		if ((linksize = readlink(devname, buf, MAXPATHLEN))
679*0Sstevel@tonic-gate 		    > 0 && linksize <= (MAXPATHLEN - 1)) {
680*0Sstevel@tonic-gate 			buf[linksize] = '\0';
681*0Sstevel@tonic-gate 		} else
682*0Sstevel@tonic-gate 			return (-1);
683*0Sstevel@tonic-gate 	} else
684*0Sstevel@tonic-gate 		s_strlcpy(buf, devname, MAXPATHLEN);
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate 	if ((p = strstr(buf, SLASH_DEVICES)) == NULL)
687*0Sstevel@tonic-gate 		return (-1);
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate 	/* point to '/' after /devices */
690*0Sstevel@tonic-gate 	p += sizeof (SLASH_DEVICES) - 2;
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	if ((m = strrchr(p, ':')) == NULL) {
693*0Sstevel@tonic-gate 		logdmsg("get_physname_minor: no minor name component in %s\n",
694*0Sstevel@tonic-gate 		    buf);
695*0Sstevel@tonic-gate 		return (-1);
696*0Sstevel@tonic-gate 	}
697*0Sstevel@tonic-gate 
698*0Sstevel@tonic-gate 	*m = '\0';
699*0Sstevel@tonic-gate 	m++;
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 	if (client_name_type(p) == CLIENT_TYPE_UNKNOWN)
702*0Sstevel@tonic-gate 		return (-1);
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate 	s_strlcpy(physname, p, physname_len);
705*0Sstevel@tonic-gate 	s_strlcpy(minorname, m, minorname_len);
706*0Sstevel@tonic-gate 	logdmsg("get_physname_minor: %s: physname = %s, minor = %s\n",
707*0Sstevel@tonic-gate 	    devname, physname, minorname);
708*0Sstevel@tonic-gate 	return (0);
709*0Sstevel@tonic-gate }
710*0Sstevel@tonic-gate 
711*0Sstevel@tonic-gate static int
712*0Sstevel@tonic-gate devlink_callback(di_devlink_t devlink, void *argptr)
713*0Sstevel@tonic-gate {
714*0Sstevel@tonic-gate 	const char *link;
715*0Sstevel@tonic-gate 	struct devlink_cbarg *argp = argptr;
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	if ((link = di_devlink_path(devlink)) != NULL) {
718*0Sstevel@tonic-gate 		s_strlcpy(argp->devlink, link, argp->len);
719*0Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
720*0Sstevel@tonic-gate 	}
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
723*0Sstevel@tonic-gate }
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate /*
726*0Sstevel@tonic-gate  * Lookup the /dev link corresponding to physname and minorname.
727*0Sstevel@tonic-gate  *
728*0Sstevel@tonic-gate  * physname	client /devices path without the /devices prefix and minor
729*0Sstevel@tonic-gate  *		name component.
730*0Sstevel@tonic-gate  * minorname	client minor name.
731*0Sstevel@tonic-gate  * devlink	caller supplied buffer where the /dev link is placed on return.
732*0Sstevel@tonic-gate  * len		caller supplied devlink buffer length
733*0Sstevel@tonic-gate  *
734*0Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
735*0Sstevel@tonic-gate  */
736*0Sstevel@tonic-gate static int
737*0Sstevel@tonic-gate lookup_devlink(char *physname, char *minorname, char *devlink, size_t len)
738*0Sstevel@tonic-gate {
739*0Sstevel@tonic-gate 	char buf[MAXPATHLEN];
740*0Sstevel@tonic-gate 	struct devlink_cbarg arg;
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 	if (devlink_hdl == NULL) {
743*0Sstevel@tonic-gate 		logdmsg("lookup_devlink: taking devlink snapshot\n");
744*0Sstevel@tonic-gate 		if ((devlink_hdl = di_devlink_init(NULL, 0)) == NULL) {
745*0Sstevel@tonic-gate 			logerr(gettext("di_devlink_init failed: %s\n"),
746*0Sstevel@tonic-gate 			    strerror(errno));
747*0Sstevel@tonic-gate 			clean_exit(1);
748*0Sstevel@tonic-gate 		}
749*0Sstevel@tonic-gate 	}
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 	*devlink = '\0';
752*0Sstevel@tonic-gate 	(void) snprintf(buf, MAXPATHLEN, "%s:%s", physname, minorname);
753*0Sstevel@tonic-gate 	arg.devlink = devlink;
754*0Sstevel@tonic-gate 	arg.len = len;
755*0Sstevel@tonic-gate 	if (di_devlink_walk(devlink_hdl, NULL, buf, DI_PRIMARY_LINK, &arg,
756*0Sstevel@tonic-gate 	    devlink_callback) != 0) {
757*0Sstevel@tonic-gate 		logdmsg("lookup_devlink: di_devlink_walk on %s failed: %s\n",
758*0Sstevel@tonic-gate 		    buf, strerror(errno));
759*0Sstevel@tonic-gate 		return (-1);
760*0Sstevel@tonic-gate 	}
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 	if (*devlink == '\0') {
763*0Sstevel@tonic-gate 		logdmsg("lookup_devlink: failed to lookup devlink for %s\n",
764*0Sstevel@tonic-gate 		    buf);
765*0Sstevel@tonic-gate 		return (-1);
766*0Sstevel@tonic-gate 	}
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	logdmsg("lookup_devlink: /dev link for %s:%s = %s\n", physname,
769*0Sstevel@tonic-gate 	    minorname, devlink);
770*0Sstevel@tonic-gate 	return (0);
771*0Sstevel@tonic-gate }
772*0Sstevel@tonic-gate 
773*0Sstevel@tonic-gate /*
774*0Sstevel@tonic-gate  * open infile for reading and return its file pointer in *fp_in.
775*0Sstevel@tonic-gate  * open outfile for writing and return its file pointer in *fp_out.
776*0Sstevel@tonic-gate  *
777*0Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
778*0Sstevel@tonic-gate  */
779*0Sstevel@tonic-gate static int
780*0Sstevel@tonic-gate open_in_out_files(char *infile, char *outfile, FILE **fp_in, FILE **fp_out)
781*0Sstevel@tonic-gate {
782*0Sstevel@tonic-gate 	FILE *fin = NULL;
783*0Sstevel@tonic-gate 	FILE *fout = NULL;
784*0Sstevel@tonic-gate 	struct stat sbuf;
785*0Sstevel@tonic-gate 
786*0Sstevel@tonic-gate 	if ((fin = fopen(infile, "r")) == NULL) {
787*0Sstevel@tonic-gate 		logerr(gettext("failed to fopen %1$s: %2$s\n"),
788*0Sstevel@tonic-gate 		    infile, strerror(errno));
789*0Sstevel@tonic-gate 		goto out;
790*0Sstevel@tonic-gate 	}
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 	if (fstat(fileno(fin), &sbuf) != 0) {
793*0Sstevel@tonic-gate 		logerr(gettext("fstat failed on %1$s: %2$s\n"),
794*0Sstevel@tonic-gate 		    infile, strerror(errno));
795*0Sstevel@tonic-gate 		goto out;
796*0Sstevel@tonic-gate 	}
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	if ((fout = fopen(outfile, "w")) == NULL) {
799*0Sstevel@tonic-gate 		logerr(gettext("failed to fopen %1$s: %2$s\n"),
800*0Sstevel@tonic-gate 		    outfile, strerror(errno));
801*0Sstevel@tonic-gate 		goto out;
802*0Sstevel@tonic-gate 	}
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 	if (fchmod(fileno(fout), (sbuf.st_mode & 0777)) != 0) {
805*0Sstevel@tonic-gate 		logerr(gettext("failed to fchmod %1$s to 0%2$o: %3$s\n"),
806*0Sstevel@tonic-gate 		    outfile, sbuf.st_mode & 0777, strerror(errno));
807*0Sstevel@tonic-gate 		goto out;
808*0Sstevel@tonic-gate 	}
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate 	if (fchown(fileno(fout), sbuf.st_uid, sbuf.st_gid) != 0) {
811*0Sstevel@tonic-gate 		logerr(gettext("failed to fchown %1$s to uid %2$d and "
812*0Sstevel@tonic-gate 		    "gid %3$d: %4$s\n"),
813*0Sstevel@tonic-gate 		    outfile, sbuf.st_uid, sbuf.st_gid, strerror(errno));
814*0Sstevel@tonic-gate 		goto out;
815*0Sstevel@tonic-gate 	}
816*0Sstevel@tonic-gate 
817*0Sstevel@tonic-gate 	*fp_in = fin;
818*0Sstevel@tonic-gate 	*fp_out = fout;
819*0Sstevel@tonic-gate 	return (0);
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate out:
822*0Sstevel@tonic-gate 	if (fin != NULL)
823*0Sstevel@tonic-gate 		(void) fclose(fin);
824*0Sstevel@tonic-gate 	if (fout != NULL)
825*0Sstevel@tonic-gate 		(void) fclose(fout);
826*0Sstevel@tonic-gate 	return (-1);
827*0Sstevel@tonic-gate }
828*0Sstevel@tonic-gate 
829*0Sstevel@tonic-gate /*
830*0Sstevel@tonic-gate  * If the devname is a phci based name and not open-able, map it to vhci
831*0Sstevel@tonic-gate  * based name. If the devname is a vhci based name and not open-able, map it
832*0Sstevel@tonic-gate  * to phci based name.
833*0Sstevel@tonic-gate  *
834*0Sstevel@tonic-gate  * devname	either a /dev link or /devices name to client device
835*0Sstevel@tonic-gate  * new_devname	caller supplied buffer where the mapped device name is
836*0Sstevel@tonic-gate  *		placed on return.
837*0Sstevel@tonic-gate  * len		caller supplied new_devname buffer length
838*0Sstevel@tonic-gate  * devlink_flag	pass 1 if requesting the /dev link to the mapped device.
839*0Sstevel@tonic-gate  *		pass 0 if requesting the /devices name of the mapped device.
840*0Sstevel@tonic-gate  *
841*0Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
842*0Sstevel@tonic-gate  */
843*0Sstevel@tonic-gate static int
844*0Sstevel@tonic-gate map_devname(char *devname, char *new_devname, size_t len, int devlink_flag)
845*0Sstevel@tonic-gate {
846*0Sstevel@tonic-gate 	char physname[MAXPATHLEN];
847*0Sstevel@tonic-gate 	char minor[MAXNAMELEN];
848*0Sstevel@tonic-gate 	char new_physname[MAXNAMELEN];
849*0Sstevel@tonic-gate 
850*0Sstevel@tonic-gate 	if (get_physname_minor(devname, physname, sizeof (physname),
851*0Sstevel@tonic-gate 	    minor, sizeof (minor)) == 0 &&
852*0Sstevel@tonic-gate 	    canopen(devname) == 0 &&
853*0Sstevel@tonic-gate 	    map_physname(physname, new_physname, sizeof (new_physname)) == 0) {
854*0Sstevel@tonic-gate 
855*0Sstevel@tonic-gate 		if (devlink_flag) {
856*0Sstevel@tonic-gate 			if (lookup_devlink(new_physname, minor, new_devname,
857*0Sstevel@tonic-gate 			    len) == 0)
858*0Sstevel@tonic-gate 				return (0);
859*0Sstevel@tonic-gate 		} else {
860*0Sstevel@tonic-gate 			(void) snprintf(new_devname, len, "/devices%s:%s",
861*0Sstevel@tonic-gate 			    new_physname, minor);
862*0Sstevel@tonic-gate 			return (0);
863*0Sstevel@tonic-gate 		}
864*0Sstevel@tonic-gate 	}
865*0Sstevel@tonic-gate 
866*0Sstevel@tonic-gate 	return (-1);
867*0Sstevel@tonic-gate }
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate /*
870*0Sstevel@tonic-gate  * Make a new /etc/vfstab:
871*0Sstevel@tonic-gate  * Read vfstab_in, convert the device name entries to appropriate vhci or phci
872*0Sstevel@tonic-gate  * based names, and write to vfstab_out. Only device names whose physical
873*0Sstevel@tonic-gate  * paths are either phci or vhci based names and not open-able are considered
874*0Sstevel@tonic-gate  * for conversion. Open-able device name entries are not converted as it
875*0Sstevel@tonic-gate  * means that the device is already accessible; hence no need to convert.
876*0Sstevel@tonic-gate  *
877*0Sstevel@tonic-gate  * Returns:
878*0Sstevel@tonic-gate  * 	0	successful but vfstab_out contents are the same as vfstab_in
879*0Sstevel@tonic-gate  *	1	successful and vfstab_out changed from vfstab_in
880*0Sstevel@tonic-gate  *	-1	failed
881*0Sstevel@tonic-gate  */
882*0Sstevel@tonic-gate static int
883*0Sstevel@tonic-gate update_vfstab(char *vfstab_in, char *vfstab_out)
884*0Sstevel@tonic-gate {
885*0Sstevel@tonic-gate 	FILE *fp_in, *fp_out;
886*0Sstevel@tonic-gate 	char *buf, *tmpbuf;
887*0Sstevel@tonic-gate 	char *vfs_cache[2];
888*0Sstevel@tonic-gate 	int idx = 0, count = 0;
889*0Sstevel@tonic-gate 	int rv = -1;
890*0Sstevel@tonic-gate 	int vfstab_updated = 0;
891*0Sstevel@tonic-gate 	int i;
892*0Sstevel@tonic-gate 	char cdev[MAXPATHLEN];
893*0Sstevel@tonic-gate 	char bdev[MAXPATHLEN];
894*0Sstevel@tonic-gate 	char mntpt[MAXPATHLEN];
895*0Sstevel@tonic-gate 	char fstype[512];
896*0Sstevel@tonic-gate 	char fsckpass[512];
897*0Sstevel@tonic-gate 	char mntboot[512];
898*0Sstevel@tonic-gate 	char mntopt[MAX_MNTOPT_STR];
899*0Sstevel@tonic-gate 	char phys_bdev[MAXPATHLEN], phys_cdev[MAXPATHLEN];
900*0Sstevel@tonic-gate 	char bdev_minor[MAXNAMELEN], cdev_minor[MAXNAMELEN];
901*0Sstevel@tonic-gate 	char new_physname[MAXPATHLEN];
902*0Sstevel@tonic-gate 	char new_bdevlink[MAXPATHLEN], new_cdevlink[MAXPATHLEN];
903*0Sstevel@tonic-gate 	char fmt[80];
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate 	if (open_in_out_files(vfstab_in, vfstab_out, &fp_in, &fp_out) != 0)
906*0Sstevel@tonic-gate 		return (-1);
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate 	/*
909*0Sstevel@tonic-gate 	 * Read one line at time from vfstab_in. If no conversion is needed
910*0Sstevel@tonic-gate 	 * for the line simply write the line to vfstab_out. If conversion is
911*0Sstevel@tonic-gate 	 * needed, first write the existing line as a comment to vfstab_out
912*0Sstevel@tonic-gate 	 * and then write the converted line.
913*0Sstevel@tonic-gate 	 *
914*0Sstevel@tonic-gate 	 * To avoid commented entries piling up in vfstab in case if the
915*0Sstevel@tonic-gate 	 * user runs stmsboot multiple times to switch on and off from mpxio,
916*0Sstevel@tonic-gate 	 * add the commented line only if not already there. To do this
917*0Sstevel@tonic-gate 	 * cache the last two vfstab lines processed and add the commented
918*0Sstevel@tonic-gate 	 * entry only if it is not found in the cache. We only need to cache
919*0Sstevel@tonic-gate 	 * the last two lines because a device can have at most two names -
920*0Sstevel@tonic-gate 	 * one mpxio and one non-mpxio name. Therefore for any device name
921*0Sstevel@tonic-gate 	 * entry we at most add two comments - one with mpxio name and one
922*0Sstevel@tonic-gate 	 * with non-mpxio name - no matter how many times stmsboot is run.
923*0Sstevel@tonic-gate 	 */
924*0Sstevel@tonic-gate 	buf = (char *)s_malloc(VFS_LINE_MAX);
925*0Sstevel@tonic-gate 	tmpbuf = (char *)s_malloc(VFS_LINE_MAX);
926*0Sstevel@tonic-gate 	vfs_cache[0] = (char *)s_malloc(VFS_LINE_MAX);
927*0Sstevel@tonic-gate 	vfs_cache[1] = (char *)s_malloc(VFS_LINE_MAX);
928*0Sstevel@tonic-gate 
929*0Sstevel@tonic-gate 	(void) snprintf(fmt, sizeof (fmt),
930*0Sstevel@tonic-gate 	    "%%%ds %%%ds %%%ds %%%ds %%%ds %%%ds %%%ds", sizeof (bdev) - 1,
931*0Sstevel@tonic-gate 	    sizeof (cdev) - 1, sizeof (mntpt) - 1, sizeof (fstype) - 1,
932*0Sstevel@tonic-gate 	    sizeof (fsckpass) - 1, sizeof (mntboot) - 1, sizeof (mntopt) - 1);
933*0Sstevel@tonic-gate 
934*0Sstevel@tonic-gate 	while (fgets(buf, VFS_LINE_MAX, fp_in) != NULL) {
935*0Sstevel@tonic-gate 		if (strlen(buf) == (VFS_LINE_MAX - 1) &&
936*0Sstevel@tonic-gate 		    buf[VFS_LINE_MAX-2] != '\n') {
937*0Sstevel@tonic-gate 			logerr(gettext("%1$s line size too long, "
938*0Sstevel@tonic-gate 			    "exceeded %2$d: \"%3$s\"\n"),
939*0Sstevel@tonic-gate 			    VFSTAB, VFS_LINE_MAX - 2, buf);
940*0Sstevel@tonic-gate 			goto out;
941*0Sstevel@tonic-gate 		}
942*0Sstevel@tonic-gate 
943*0Sstevel@tonic-gate 		/* LINTED - format specifier */
944*0Sstevel@tonic-gate 		if ((sscanf(buf, fmt, bdev, cdev, mntpt,
945*0Sstevel@tonic-gate 		    fstype, fsckpass, mntboot, mntopt) != 7) ||
946*0Sstevel@tonic-gate 		    (bdev[0] == '#') ||
947*0Sstevel@tonic-gate 		    (get_physname_minor(bdev, phys_bdev, sizeof (phys_bdev),
948*0Sstevel@tonic-gate 		    bdev_minor, sizeof (bdev_minor)) != 0) ||
949*0Sstevel@tonic-gate 
950*0Sstevel@tonic-gate 		    (strcmp(fstype, "swap") != 0 &&
951*0Sstevel@tonic-gate 		    ((get_physname_minor(cdev, phys_cdev, sizeof (phys_cdev),
952*0Sstevel@tonic-gate 		    cdev_minor, sizeof (cdev_minor)) != 0) ||
953*0Sstevel@tonic-gate 		    (strcmp(phys_bdev, phys_cdev) != 0))) ||
954*0Sstevel@tonic-gate 
955*0Sstevel@tonic-gate 		    canopen(bdev) ||
956*0Sstevel@tonic-gate 		    (map_physname(phys_bdev, new_physname,
957*0Sstevel@tonic-gate 		    sizeof (new_physname)) != 0) ||
958*0Sstevel@tonic-gate 		    (lookup_devlink(new_physname, bdev_minor, new_bdevlink,
959*0Sstevel@tonic-gate 		    sizeof (new_bdevlink)) != 0) ||
960*0Sstevel@tonic-gate 
961*0Sstevel@tonic-gate 		    (strcmp(fstype, "swap") != 0 &&
962*0Sstevel@tonic-gate 		    (lookup_devlink(new_physname, cdev_minor, new_cdevlink,
963*0Sstevel@tonic-gate 		    sizeof (new_cdevlink)) != 0))) {
964*0Sstevel@tonic-gate 
965*0Sstevel@tonic-gate 			/* cache the last two entries */
966*0Sstevel@tonic-gate 			(void) strlcpy(vfs_cache[idx], buf, VFS_LINE_MAX);
967*0Sstevel@tonic-gate 			idx = (idx == 0) ? 1 : 0;
968*0Sstevel@tonic-gate 			if (count < 2)
969*0Sstevel@tonic-gate 				count++;
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 			if (fputs(buf, fp_out) == EOF) {
972*0Sstevel@tonic-gate 				logerr(gettext("fputs \"%1$s\" to %2$s "
973*0Sstevel@tonic-gate 				    "failed: %3$s\n"),
974*0Sstevel@tonic-gate 				    buf, vfstab_out, strerror(errno));
975*0Sstevel@tonic-gate 				goto out;
976*0Sstevel@tonic-gate 			}
977*0Sstevel@tonic-gate 
978*0Sstevel@tonic-gate 		} else {
979*0Sstevel@tonic-gate 			/*
980*0Sstevel@tonic-gate 			 * comment the entry in vfstab only if it is not
981*0Sstevel@tonic-gate 			 * already in the cache.
982*0Sstevel@tonic-gate 			 */
983*0Sstevel@tonic-gate 			if (client_name_type(phys_bdev) == CLIENT_TYPE_VHCI)
984*0Sstevel@tonic-gate 				(void) snprintf(tmpbuf, VFS_LINE_MAX,
985*0Sstevel@tonic-gate 				    "# mpxio: %s", buf);
986*0Sstevel@tonic-gate 			else
987*0Sstevel@tonic-gate 				(void) snprintf(tmpbuf, VFS_LINE_MAX,
988*0Sstevel@tonic-gate 				    "# non-mpxio: %s", buf);
989*0Sstevel@tonic-gate 
990*0Sstevel@tonic-gate 			for (i = 0; i < count; i++) {
991*0Sstevel@tonic-gate 				if (strcmp(vfs_cache[i], tmpbuf) == 0)
992*0Sstevel@tonic-gate 					break;
993*0Sstevel@tonic-gate 			}
994*0Sstevel@tonic-gate 
995*0Sstevel@tonic-gate 			if (i == count) {
996*0Sstevel@tonic-gate 				if (fputs(tmpbuf, fp_out) == EOF) {
997*0Sstevel@tonic-gate 					logerr(gettext("fputs \"%1$s\" to %2$s "
998*0Sstevel@tonic-gate 					    "failed: %3$s\n"), tmpbuf,
999*0Sstevel@tonic-gate 					    vfstab_out, strerror(errno));
1000*0Sstevel@tonic-gate 					goto out;
1001*0Sstevel@tonic-gate 				}
1002*0Sstevel@tonic-gate 			}
1003*0Sstevel@tonic-gate 
1004*0Sstevel@tonic-gate 			count = 0;
1005*0Sstevel@tonic-gate 			idx = 0;
1006*0Sstevel@tonic-gate 
1007*0Sstevel@tonic-gate 			if (fprintf(fp_out, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
1008*0Sstevel@tonic-gate 			    new_bdevlink,
1009*0Sstevel@tonic-gate 			    (strcmp(fstype, "swap") != 0) ? new_cdevlink : cdev,
1010*0Sstevel@tonic-gate 			    mntpt, fstype, fsckpass, mntboot, mntopt) < 0) {
1011*0Sstevel@tonic-gate 				logerr(gettext("fprintf failed to write to "
1012*0Sstevel@tonic-gate 				    "%1$s: %2$s\n"),
1013*0Sstevel@tonic-gate 				    vfstab_out, strerror(errno));
1014*0Sstevel@tonic-gate 				goto out;
1015*0Sstevel@tonic-gate 			}
1016*0Sstevel@tonic-gate 			vfstab_updated = 1;
1017*0Sstevel@tonic-gate 		}
1018*0Sstevel@tonic-gate 	}
1019*0Sstevel@tonic-gate 
1020*0Sstevel@tonic-gate 	rv = vfstab_updated;
1021*0Sstevel@tonic-gate out:
1022*0Sstevel@tonic-gate 	(void) fclose(fp_in);
1023*0Sstevel@tonic-gate 	(void) fclose(fp_out);
1024*0Sstevel@tonic-gate 	free(buf);
1025*0Sstevel@tonic-gate 	free(tmpbuf);
1026*0Sstevel@tonic-gate 	free(vfs_cache[0]);
1027*0Sstevel@tonic-gate 	free(vfs_cache[1]);
1028*0Sstevel@tonic-gate 	return (rv);
1029*0Sstevel@tonic-gate }
1030*0Sstevel@tonic-gate 
1031*0Sstevel@tonic-gate /*
1032*0Sstevel@tonic-gate  * if guidmap is 0, list non-STMS to STMS device name mappings for the
1033*0Sstevel@tonic-gate  * specified controller.
1034*0Sstevel@tonic-gate  * if guidmap is 1, list non-STMS to GUID mappings for the specified controller.
1035*0Sstevel@tonic-gate  * If controller is -1 list mappings for all controllers.
1036*0Sstevel@tonic-gate  *
1037*0Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
1038*0Sstevel@tonic-gate  */
1039*0Sstevel@tonic-gate static int
1040*0Sstevel@tonic-gate list_mappings(int controller, int guidmap)
1041*0Sstevel@tonic-gate {
1042*0Sstevel@tonic-gate 	int cnum, len, mapped;
1043*0Sstevel@tonic-gate 	int header = 1;
1044*0Sstevel@tonic-gate 	char *p1, *p2;
1045*0Sstevel@tonic-gate 	DIR *dirp;
1046*0Sstevel@tonic-gate 	struct dirent *direntry;
1047*0Sstevel@tonic-gate 	char devname[MAXPATHLEN];
1048*0Sstevel@tonic-gate 	char physname[MAXPATHLEN];
1049*0Sstevel@tonic-gate 	char new_devname[MAXPATHLEN];
1050*0Sstevel@tonic-gate 	char new_physname[MAXPATHLEN];
1051*0Sstevel@tonic-gate 	char guid[MAXPATHLEN];
1052*0Sstevel@tonic-gate 	char minor[MAXNAMELEN];
1053*0Sstevel@tonic-gate 
1054*0Sstevel@tonic-gate 	if ((dirp = opendir("/dev/rdsk")) == NULL)
1055*0Sstevel@tonic-gate 		return (-1);
1056*0Sstevel@tonic-gate 
1057*0Sstevel@tonic-gate 	while ((direntry = readdir(dirp)) != NULL) {
1058*0Sstevel@tonic-gate 		if (strcmp(direntry->d_name, ".") == 0 ||
1059*0Sstevel@tonic-gate 		    strcmp(direntry->d_name, "..") == 0 ||
1060*0Sstevel@tonic-gate 		    (len = strlen(direntry->d_name)) < 2 ||
1061*0Sstevel@tonic-gate 		    strcmp(direntry->d_name + len - 2, "s0") != 0 ||
1062*0Sstevel@tonic-gate 		    sscanf(direntry->d_name, "c%dt", &cnum) != 1 ||
1063*0Sstevel@tonic-gate 		    (controller != -1 && controller != cnum))
1064*0Sstevel@tonic-gate 			continue;
1065*0Sstevel@tonic-gate 
1066*0Sstevel@tonic-gate 		(void) snprintf(devname, MAXPATHLEN, "/dev/rdsk/%s",
1067*0Sstevel@tonic-gate 		    direntry->d_name);
1068*0Sstevel@tonic-gate 
1069*0Sstevel@tonic-gate 		if (get_physname_minor(devname, physname, sizeof (physname),
1070*0Sstevel@tonic-gate 		    minor, sizeof (minor)) != 0 ||
1071*0Sstevel@tonic-gate 		    client_name_type(physname) != CLIENT_TYPE_PHCI)
1072*0Sstevel@tonic-gate 			continue;
1073*0Sstevel@tonic-gate 
1074*0Sstevel@tonic-gate 		/*
1075*0Sstevel@tonic-gate 		 * First try phci_to_vhci() mapping. It will work if the
1076*0Sstevel@tonic-gate 		 * device is under MPxIO control. If the device is not under
1077*0Sstevel@tonic-gate 		 * MPxIO, phci_to_vhci() will fail in which case try to lookup
1078*0Sstevel@tonic-gate 		 * if an old mapping exists using guid lookup.
1079*0Sstevel@tonic-gate 		 */
1080*0Sstevel@tonic-gate 		mapped = 1;
1081*0Sstevel@tonic-gate 		if (phci_to_vhci(physname, new_physname,
1082*0Sstevel@tonic-gate 		    sizeof (new_physname)) != 0) {
1083*0Sstevel@tonic-gate 			if (get_guid(physname, guid, sizeof (guid), 1,
1084*0Sstevel@tonic-gate 			    DI_NODE_NIL) == 0)
1085*0Sstevel@tonic-gate 				(void) snprintf(new_physname, MAXPATHLEN,
1086*0Sstevel@tonic-gate 				    "/scsi_vhci/ssd@g%s", guid);
1087*0Sstevel@tonic-gate 			else
1088*0Sstevel@tonic-gate 				mapped = 0;
1089*0Sstevel@tonic-gate 		}
1090*0Sstevel@tonic-gate 
1091*0Sstevel@tonic-gate 		if (mapped == 0)
1092*0Sstevel@tonic-gate 			continue;
1093*0Sstevel@tonic-gate 
1094*0Sstevel@tonic-gate 		/* strip the slice number part */
1095*0Sstevel@tonic-gate 		devname[strlen(devname) - 2] = '\0';
1096*0Sstevel@tonic-gate 
1097*0Sstevel@tonic-gate 		if (guidmap == 0) {
1098*0Sstevel@tonic-gate 			if (lookup_devlink(new_physname, minor,
1099*0Sstevel@tonic-gate 			    new_devname, sizeof (new_devname)) != 0)
1100*0Sstevel@tonic-gate 				continue;
1101*0Sstevel@tonic-gate 
1102*0Sstevel@tonic-gate 			/* strip the slice number part */
1103*0Sstevel@tonic-gate 			new_devname[strlen(new_devname) - 2] = '\0';
1104*0Sstevel@tonic-gate 
1105*0Sstevel@tonic-gate 			if (header) {
1106*0Sstevel@tonic-gate 				(void) printf(
1107*0Sstevel@tonic-gate 				    gettext("non-STMS device name\t\t\t"
1108*0Sstevel@tonic-gate 				    "STMS device name\n"
1109*0Sstevel@tonic-gate 				    "------------------------------------------"
1110*0Sstevel@tonic-gate 				    "------------------------\n"));
1111*0Sstevel@tonic-gate 				header = 0;
1112*0Sstevel@tonic-gate 			}
1113*0Sstevel@tonic-gate 			(void) printf("%s\t\t%s\n", devname, new_devname);
1114*0Sstevel@tonic-gate 		} else {
1115*0Sstevel@tonic-gate 			/* extract guid part */
1116*0Sstevel@tonic-gate 			if ((p1 = strstr(new_physname, "ssd@g")) == NULL) {
1117*0Sstevel@tonic-gate 				logdmsg("invalid vhci: %s\n", new_physname);
1118*0Sstevel@tonic-gate 				continue;
1119*0Sstevel@tonic-gate 			}
1120*0Sstevel@tonic-gate 			p1 += sizeof ("ssd@g") - 1;
1121*0Sstevel@tonic-gate 			if ((p2 = strrchr(p1, ':')) != NULL)
1122*0Sstevel@tonic-gate 				*p2 = '\0';
1123*0Sstevel@tonic-gate 
1124*0Sstevel@tonic-gate 			if (header) {
1125*0Sstevel@tonic-gate 				(void) printf(
1126*0Sstevel@tonic-gate 				    gettext("non-STMS device name\t\t\tGUID\n"
1127*0Sstevel@tonic-gate 				    "------------------------------------------"
1128*0Sstevel@tonic-gate 				    "------------------------\n"));
1129*0Sstevel@tonic-gate 				header = 0;
1130*0Sstevel@tonic-gate 			}
1131*0Sstevel@tonic-gate 			(void) printf("%s\t\t%s\n", devname, p1);
1132*0Sstevel@tonic-gate 		}
1133*0Sstevel@tonic-gate 	}
1134*0Sstevel@tonic-gate 
1135*0Sstevel@tonic-gate 	(void) closedir(dirp);
1136*0Sstevel@tonic-gate 	return (0);
1137*0Sstevel@tonic-gate }
1138*0Sstevel@tonic-gate 
1139*0Sstevel@tonic-gate /*
1140*0Sstevel@tonic-gate  * Check if the file can be opened.
1141*0Sstevel@tonic-gate  *
1142*0Sstevel@tonic-gate  * Return 1 if the file can be opened, 0 otherwise.
1143*0Sstevel@tonic-gate  */
1144*0Sstevel@tonic-gate static int
1145*0Sstevel@tonic-gate canopen(char *filename)
1146*0Sstevel@tonic-gate {
1147*0Sstevel@tonic-gate 	int fd;
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate 	if ((fd = open(filename, O_RDONLY)) == -1)
1150*0Sstevel@tonic-gate 		return (0);
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate 	(void) close(fd);
1153*0Sstevel@tonic-gate 	return (1);
1154*0Sstevel@tonic-gate }
1155*0Sstevel@tonic-gate 
1156*0Sstevel@tonic-gate static void
1157*0Sstevel@tonic-gate logerr(char *msg, ...)
1158*0Sstevel@tonic-gate {
1159*0Sstevel@tonic-gate 	va_list ap;
1160*0Sstevel@tonic-gate 
1161*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", stmsboot);
1162*0Sstevel@tonic-gate 	va_start(ap, msg);
1163*0Sstevel@tonic-gate 	/* LINTED - format specifier */
1164*0Sstevel@tonic-gate 	(void) vfprintf(stderr, msg, ap);
1165*0Sstevel@tonic-gate 	va_end(ap);
1166*0Sstevel@tonic-gate }
1167*0Sstevel@tonic-gate 
1168*0Sstevel@tonic-gate /* log debug message */
1169*0Sstevel@tonic-gate static void
1170*0Sstevel@tonic-gate logdmsg(char *msg, ...)
1171*0Sstevel@tonic-gate {
1172*0Sstevel@tonic-gate 	va_list ap;
1173*0Sstevel@tonic-gate 
1174*0Sstevel@tonic-gate 	if (debug) {
1175*0Sstevel@tonic-gate 		va_start(ap, msg);
1176*0Sstevel@tonic-gate 		/* LINTED - format specifier */
1177*0Sstevel@tonic-gate 		(void) vprintf(msg, ap);
1178*0Sstevel@tonic-gate 		va_end(ap);
1179*0Sstevel@tonic-gate 	}
1180*0Sstevel@tonic-gate }
1181*0Sstevel@tonic-gate 
1182*0Sstevel@tonic-gate static void *
1183*0Sstevel@tonic-gate s_malloc(const size_t size)
1184*0Sstevel@tonic-gate {
1185*0Sstevel@tonic-gate 	void *rp;
1186*0Sstevel@tonic-gate 
1187*0Sstevel@tonic-gate 	if ((rp = malloc(size)) == NULL) {
1188*0Sstevel@tonic-gate 		logerr(gettext("malloc failed to allocate %d bytes\n"), size);
1189*0Sstevel@tonic-gate 		clean_exit(1);
1190*0Sstevel@tonic-gate 	}
1191*0Sstevel@tonic-gate 	return (rp);
1192*0Sstevel@tonic-gate }
1193*0Sstevel@tonic-gate 
1194*0Sstevel@tonic-gate static char *
1195*0Sstevel@tonic-gate s_strdup(const char *ptr)
1196*0Sstevel@tonic-gate {
1197*0Sstevel@tonic-gate 	void *rp;
1198*0Sstevel@tonic-gate 
1199*0Sstevel@tonic-gate 	if ((rp = strdup(ptr)) == NULL) {
1200*0Sstevel@tonic-gate 		logerr(gettext("strdup failed to dup %s\n"), ptr);
1201*0Sstevel@tonic-gate 		clean_exit(1);
1202*0Sstevel@tonic-gate 	}
1203*0Sstevel@tonic-gate 	return (rp);
1204*0Sstevel@tonic-gate }
1205*0Sstevel@tonic-gate 
1206*0Sstevel@tonic-gate static void
1207*0Sstevel@tonic-gate s_strlcpy(char *dst, const char *src, size_t dstsize)
1208*0Sstevel@tonic-gate {
1209*0Sstevel@tonic-gate 	int n;
1210*0Sstevel@tonic-gate 
1211*0Sstevel@tonic-gate 	if ((n = strlcpy(dst, src, dstsize)) >= dstsize) {
1212*0Sstevel@tonic-gate 		logerr(gettext("strlcpy: destination buffer size is %1$d "
1213*0Sstevel@tonic-gate 		    "bytes, need to at least %2$d bytes\n"), dstsize, n + 1);
1214*0Sstevel@tonic-gate 		clean_exit(1);
1215*0Sstevel@tonic-gate 	}
1216*0Sstevel@tonic-gate }
1217*0Sstevel@tonic-gate 
1218*0Sstevel@tonic-gate static void
1219*0Sstevel@tonic-gate clean_exit(int status)
1220*0Sstevel@tonic-gate {
1221*0Sstevel@tonic-gate 	if (devinfo_root != DI_NODE_NIL)
1222*0Sstevel@tonic-gate 		di_fini(devinfo_root);
1223*0Sstevel@tonic-gate 
1224*0Sstevel@tonic-gate 	if (devlink_hdl != NULL)
1225*0Sstevel@tonic-gate 		(void) di_devlink_fini(&devlink_hdl);
1226*0Sstevel@tonic-gate 
1227*0Sstevel@tonic-gate 	if (vhci_fd != -1)
1228*0Sstevel@tonic-gate 		(void) close(vhci_fd);
1229*0Sstevel@tonic-gate 
1230*0Sstevel@tonic-gate 	exit(status);
1231*0Sstevel@tonic-gate }
1232