1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <errno.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <unistd.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/vfstab.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/lofi.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/ramdisk.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/fssnap_if.h>
42*7c478bd9Sstevel@tonic-gate #include "libadm.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * Globals:
46*7c478bd9Sstevel@tonic-gate  *	getfullrawname - returns a fully-qualified raw device name
47*7c478bd9Sstevel@tonic-gate  *	getfullblkname - returns a fully-qualified block device name
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  * These two routines take a device pathname and return corresponding
50*7c478bd9Sstevel@tonic-gate  * the raw or block device name.
51*7c478bd9Sstevel@tonic-gate  *
52*7c478bd9Sstevel@tonic-gate  * First the device name is fully qualified:
53*7c478bd9Sstevel@tonic-gate  * 	If the device name does not start with a '/' or starts with
54*7c478bd9Sstevel@tonic-gate  *	'./' then the current working directory is added to the beginning
55*7c478bd9Sstevel@tonic-gate  *	of the pathname.
56*7c478bd9Sstevel@tonic-gate  *
57*7c478bd9Sstevel@tonic-gate  *	If the device name starts with a '../' then all but the last
58*7c478bd9Sstevel@tonic-gate  *	sub-directory of the current working directory is added to the
59*7c478bd9Sstevel@tonic-gate  *	the beginning of the pathname.
60*7c478bd9Sstevel@tonic-gate  *
61*7c478bd9Sstevel@tonic-gate  * Second if the fully-qualified device name given is the raw/block
62*7c478bd9Sstevel@tonic-gate  * device that is being asked for then the fully-qualified device name is
63*7c478bd9Sstevel@tonic-gate  * returned.
64*7c478bd9Sstevel@tonic-gate  *
65*7c478bd9Sstevel@tonic-gate  * Third if an entry is found in /etc/vfstab which matches the given name
66*7c478bd9Sstevel@tonic-gate  * then the corresponding raw/block device is returned.  This allows
67*7c478bd9Sstevel@tonic-gate  * non-standard names to be converted (.i.e., block device "/dev/joe" can
68*7c478bd9Sstevel@tonic-gate  * be converted to raw device "/dev/fred", via this mechanism).
69*7c478bd9Sstevel@tonic-gate  *
70*7c478bd9Sstevel@tonic-gate  * Last standard names are converted.  Standard names are those
71*7c478bd9Sstevel@tonic-gate  * with a '/dsk/' for block or '/rdsk/' for raw sub-directory components
72*7c478bd9Sstevel@tonic-gate  * in the device name. Or, the filename component has an 'r' for raw or
73*7c478bd9Sstevel@tonic-gate  * no 'r' for block (e.g., rsd0a <=> sd0a).
74*7c478bd9Sstevel@tonic-gate  *
75*7c478bd9Sstevel@tonic-gate  * Caveat:
76*7c478bd9Sstevel@tonic-gate  * It is assumed that the block and raw devices have the
77*7c478bd9Sstevel@tonic-gate  * same device number, and this is used to verify the conversion
78*7c478bd9Sstevel@tonic-gate  * happened corretly.  If this happens not to be true, due to mapping
79*7c478bd9Sstevel@tonic-gate  * of minor numbers or sometheing, then entries can be put in the
80*7c478bd9Sstevel@tonic-gate  * the '/etc/vfstab' file to over-ride this checking.
81*7c478bd9Sstevel@tonic-gate  *
82*7c478bd9Sstevel@tonic-gate  *
83*7c478bd9Sstevel@tonic-gate  * Return Values:
84*7c478bd9Sstevel@tonic-gate  * 	raw/block device name	- (depending on which routine is used)
85*7c478bd9Sstevel@tonic-gate  *	null string		- When the conversion failed
86*7c478bd9Sstevel@tonic-gate  *	null pointer		- malloc problems
87*7c478bd9Sstevel@tonic-gate  *
88*7c478bd9Sstevel@tonic-gate  * It is up to the user of these routines to free the memory, of
89*7c478bd9Sstevel@tonic-gate  * the device name or null string returned by these library routines,
90*7c478bd9Sstevel@tonic-gate  * when appropriate by the application.
91*7c478bd9Sstevel@tonic-gate  */
92*7c478bd9Sstevel@tonic-gate #define	GET_BLK	0
93*7c478bd9Sstevel@tonic-gate #define	GET_RAW	1
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate static int test_if_blk(char *, dev_t);
96*7c478bd9Sstevel@tonic-gate static int test_if_raw(char *, dev_t);
97*7c478bd9Sstevel@tonic-gate static char *getblkcomplete(char *, struct stat64 *);
98*7c478bd9Sstevel@tonic-gate static char *getrawcomplete(char *, struct stat64 *);
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate /*
101*7c478bd9Sstevel@tonic-gate  * getfullname() - Builds a fully qualified pathname.
102*7c478bd9Sstevel@tonic-gate  *		   This handles . and .. as well.
103*7c478bd9Sstevel@tonic-gate  *		   NOTE: This is different from realpath(3C) because
104*7c478bd9Sstevel@tonic-gate  *			 it does not follow links.
105*7c478bd9Sstevel@tonic-gate  */
106*7c478bd9Sstevel@tonic-gate static char *
getfullname(char * path)107*7c478bd9Sstevel@tonic-gate getfullname(char *path)
108*7c478bd9Sstevel@tonic-gate {
109*7c478bd9Sstevel@tonic-gate 	char	cwd[MAXPATHLEN];
110*7c478bd9Sstevel@tonic-gate 	char	*c;
111*7c478bd9Sstevel@tonic-gate 	char	*wa;
112*7c478bd9Sstevel@tonic-gate 	size_t	len;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	if (*path == '/')
115*7c478bd9Sstevel@tonic-gate 		return (strdup(path));
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	if (getcwd(cwd, sizeof (cwd)) == NULL)
118*7c478bd9Sstevel@tonic-gate 		return (strdup(""));
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	/* handle . and .. */
121*7c478bd9Sstevel@tonic-gate 	if (strncmp(path, "./", 2) == 0) {
122*7c478bd9Sstevel@tonic-gate 		/* strip the ./ from the given path */
123*7c478bd9Sstevel@tonic-gate 		path += 2;
124*7c478bd9Sstevel@tonic-gate 	} else if (strncmp(path, "../", 3) == 0) {
125*7c478bd9Sstevel@tonic-gate 		/* strip the last directory component from cwd */
126*7c478bd9Sstevel@tonic-gate 		c = strrchr(cwd, '/');
127*7c478bd9Sstevel@tonic-gate 		*c = '\0';
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 		/* strip the ../ from the given path */
130*7c478bd9Sstevel@tonic-gate 		path += 3;
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	/*
134*7c478bd9Sstevel@tonic-gate 	 * Adding 2 takes care of slash and null terminator.
135*7c478bd9Sstevel@tonic-gate 	 */
136*7c478bd9Sstevel@tonic-gate 	len = strlen(cwd) + strlen(path) + 2;
137*7c478bd9Sstevel@tonic-gate 	if ((wa = malloc(len)) == NULL)
138*7c478bd9Sstevel@tonic-gate 		return (NULL);
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	(void) strcpy(wa, cwd);
141*7c478bd9Sstevel@tonic-gate 	(void) strcat(wa, "/");
142*7c478bd9Sstevel@tonic-gate 	(void) strcat(wa, path);
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	return (wa);
145*7c478bd9Sstevel@tonic-gate }
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate /*
148*7c478bd9Sstevel@tonic-gate  * test the path/fname to see if is blk special
149*7c478bd9Sstevel@tonic-gate  */
150*7c478bd9Sstevel@tonic-gate static int
test_if_blk(char * new_path,dev_t raw_dev)151*7c478bd9Sstevel@tonic-gate test_if_blk(char *new_path, dev_t raw_dev)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	struct stat64	buf;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	/* check if we got a char special file */
156*7c478bd9Sstevel@tonic-gate 	if (stat64(new_path, &buf) != 0)
157*7c478bd9Sstevel@tonic-gate 		return (0);
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	if (!S_ISBLK(buf.st_mode))
160*7c478bd9Sstevel@tonic-gate 		return (0);
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	if (raw_dev != buf.st_rdev)
163*7c478bd9Sstevel@tonic-gate 		return (0);
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	return (1);
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate  * test the path/fname to see if is char special
170*7c478bd9Sstevel@tonic-gate  */
171*7c478bd9Sstevel@tonic-gate static int
test_if_raw(char * new_path,dev_t blk_dev)172*7c478bd9Sstevel@tonic-gate test_if_raw(char *new_path, dev_t blk_dev)
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	struct stat64	buf;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	/* check if we got a char special file */
177*7c478bd9Sstevel@tonic-gate 	if (stat64(new_path, &buf) != 0)
178*7c478bd9Sstevel@tonic-gate 		return (0);
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if (!S_ISCHR(buf.st_mode))
181*7c478bd9Sstevel@tonic-gate 		return (0);
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	if (blk_dev != buf.st_rdev)
184*7c478bd9Sstevel@tonic-gate 		return (0);
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	return (1);
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate /*
190*7c478bd9Sstevel@tonic-gate  * complete getblkrawname() for blk->raw to handle volmgt devices
191*7c478bd9Sstevel@tonic-gate  */
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate static char *
getblkcomplete(char * cp,struct stat64 * dat)194*7c478bd9Sstevel@tonic-gate getblkcomplete(char *cp, struct stat64 *dat)
195*7c478bd9Sstevel@tonic-gate {
196*7c478bd9Sstevel@tonic-gate 	char 		*dp;
197*7c478bd9Sstevel@tonic-gate 	char		*new_path;
198*7c478bd9Sstevel@tonic-gate 	char		c;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	/* ok, so we either have a bad device or a floppy */
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	/* try the rfd# form */
203*7c478bd9Sstevel@tonic-gate 	if ((dp = strstr(cp, "/rfd")) != NULL) {
204*7c478bd9Sstevel@tonic-gate 		if ((new_path = malloc(strlen(cp))) == NULL)
205*7c478bd9Sstevel@tonic-gate 			return (NULL);
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 		c = *++dp;			/* save the 'r' */
208*7c478bd9Sstevel@tonic-gate 		*dp = '\0';			/* replace it with a null */
209*7c478bd9Sstevel@tonic-gate 		(void) strcpy(new_path, cp);	/* save first part of it */
210*7c478bd9Sstevel@tonic-gate 		*dp++ = c;			/* give the 'r' back */
211*7c478bd9Sstevel@tonic-gate 		(void) strcat(new_path, dp);	/* copy, skipping the 'r' */
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 		if (test_if_blk(new_path, dat->st_rdev))
214*7c478bd9Sstevel@tonic-gate 			return (new_path);
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 		free(new_path);
217*7c478bd9Sstevel@tonic-gate 		return (strdup(""));
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	/* try the rdiskette form */
221*7c478bd9Sstevel@tonic-gate 	if ((dp = strstr(cp, "/rdiskette")) != NULL) {
222*7c478bd9Sstevel@tonic-gate 		if ((new_path = malloc(strlen(cp))) == NULL)
223*7c478bd9Sstevel@tonic-gate 			return (NULL);
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 		c = *++dp;			/* save the 'r' */
226*7c478bd9Sstevel@tonic-gate 		*dp = '\0';			/* replace it with a null */
227*7c478bd9Sstevel@tonic-gate 		(void) strcpy(new_path, cp);	/* save first part of it */
228*7c478bd9Sstevel@tonic-gate 		*dp++ = c;			/* give the 'r' back */
229*7c478bd9Sstevel@tonic-gate 		(void) strcat(new_path, dp);	/* copy, skipping the 'r' */
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 		if (test_if_blk(new_path, dat->st_rdev))
232*7c478bd9Sstevel@tonic-gate 			return (new_path);
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 		free(new_path);
235*7c478bd9Sstevel@tonic-gate 		return (strdup(""));
236*7c478bd9Sstevel@tonic-gate 	}
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	/* no match found */
239*7c478bd9Sstevel@tonic-gate 	return (strdup(""));
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate /*
243*7c478bd9Sstevel@tonic-gate  * complete getfullrawname() for raw->blk to handle volmgt devices
244*7c478bd9Sstevel@tonic-gate  */
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate static char *
getrawcomplete(char * cp,struct stat64 * dat)247*7c478bd9Sstevel@tonic-gate getrawcomplete(char *cp, struct stat64 *dat)
248*7c478bd9Sstevel@tonic-gate {
249*7c478bd9Sstevel@tonic-gate 	char 		*dp;
250*7c478bd9Sstevel@tonic-gate 	char		*new_path;
251*7c478bd9Sstevel@tonic-gate 	char		c;
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 	/* ok, so we either have a bad device or a floppy */
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	/* try the fd# form */
256*7c478bd9Sstevel@tonic-gate 	if ((dp = strstr(cp, "/fd")) != NULL) {
257*7c478bd9Sstevel@tonic-gate 		/* malloc path for new_path to hold raw */
258*7c478bd9Sstevel@tonic-gate 		if ((new_path = malloc(strlen(cp)+2)) == NULL)
259*7c478bd9Sstevel@tonic-gate 			return (NULL);
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 		c = *++dp;			/* save the 'f' */
262*7c478bd9Sstevel@tonic-gate 		*dp = '\0';			/* replace it with a null */
263*7c478bd9Sstevel@tonic-gate 		(void) strcpy(new_path, cp);	/* save first part of it */
264*7c478bd9Sstevel@tonic-gate 		*dp = c;			/* put the 'f' back */
265*7c478bd9Sstevel@tonic-gate 		(void) strcat(new_path, "r");	/* insert an 'r' */
266*7c478bd9Sstevel@tonic-gate 		(void) strcat(new_path, dp);	/* copy the rest */
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 		if (test_if_raw(new_path, dat->st_rdev))
269*7c478bd9Sstevel@tonic-gate 			return (new_path);
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 		free(new_path);
272*7c478bd9Sstevel@tonic-gate 	}
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	/* try the diskette form */
275*7c478bd9Sstevel@tonic-gate 	if ((dp = strstr(cp, "/diskette")) != NULL) {
276*7c478bd9Sstevel@tonic-gate 		/* malloc path for new_path to hold raw */
277*7c478bd9Sstevel@tonic-gate 		if ((new_path = malloc(strlen(cp)+2)) == NULL)
278*7c478bd9Sstevel@tonic-gate 			return (NULL);
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 		c = *++dp;			/* save at 'd' */
281*7c478bd9Sstevel@tonic-gate 		*dp = '\0';			/* replace it with a null */
282*7c478bd9Sstevel@tonic-gate 		(void) strcpy(new_path, cp);	/* save first part */
283*7c478bd9Sstevel@tonic-gate 		*dp = c;			/* put the 'd' back */
284*7c478bd9Sstevel@tonic-gate 		(void) strcat(new_path, "r");	/* insert an 'r' */
285*7c478bd9Sstevel@tonic-gate 		(void) strcat(new_path, dp);	/* copy the rest */
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 		if (test_if_raw(new_path, dat->st_rdev))
288*7c478bd9Sstevel@tonic-gate 			return (new_path);
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 		free(new_path);
291*7c478bd9Sstevel@tonic-gate 		return (strdup(""));
292*7c478bd9Sstevel@tonic-gate 	}
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	/* failed to build raw name, return null string */
295*7c478bd9Sstevel@tonic-gate 	return (strdup(""));
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate }
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate static char *
getvfsspecial(char * path,int raw_special)302*7c478bd9Sstevel@tonic-gate getvfsspecial(char *path, int raw_special)
303*7c478bd9Sstevel@tonic-gate {
304*7c478bd9Sstevel@tonic-gate 	FILE		*fp;
305*7c478bd9Sstevel@tonic-gate 	struct vfstab	vp;
306*7c478bd9Sstevel@tonic-gate 	struct vfstab	ref_vp;
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 	if ((fp = fopen("/etc/vfstab", "r")) == NULL)
309*7c478bd9Sstevel@tonic-gate 		return (NULL);
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 	(void) memset(&ref_vp, 0, sizeof (struct vfstab));
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 	if (raw_special)
314*7c478bd9Sstevel@tonic-gate 		ref_vp.vfs_special = path;
315*7c478bd9Sstevel@tonic-gate 	else
316*7c478bd9Sstevel@tonic-gate 		ref_vp.vfs_fsckdev = path;
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	if (getvfsany(fp, &vp, &ref_vp)) {
319*7c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
320*7c478bd9Sstevel@tonic-gate 		return (NULL);
321*7c478bd9Sstevel@tonic-gate 	}
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	if (raw_special)
326*7c478bd9Sstevel@tonic-gate 		return (vp.vfs_fsckdev);
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 	return (vp.vfs_special);
329*7c478bd9Sstevel@tonic-gate }
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate /*
332*7c478bd9Sstevel@tonic-gate  * change the device name to a block device name
333*7c478bd9Sstevel@tonic-gate  */
334*7c478bd9Sstevel@tonic-gate char *
getfullblkname(char * cp)335*7c478bd9Sstevel@tonic-gate getfullblkname(char *cp)
336*7c478bd9Sstevel@tonic-gate {
337*7c478bd9Sstevel@tonic-gate 	struct stat64	buf;
338*7c478bd9Sstevel@tonic-gate 	char		*dp;
339*7c478bd9Sstevel@tonic-gate 	char		*new_path;
340*7c478bd9Sstevel@tonic-gate 	dev_t		raw_dev;
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	if (cp == NULL)
343*7c478bd9Sstevel@tonic-gate 		return (strdup(""));
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 	/*
346*7c478bd9Sstevel@tonic-gate 	 * Create a fully qualified name.
347*7c478bd9Sstevel@tonic-gate 	 */
348*7c478bd9Sstevel@tonic-gate 	if ((cp = getfullname(cp)) == NULL)
349*7c478bd9Sstevel@tonic-gate 		return (NULL);
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 	if (*cp == '\0')
352*7c478bd9Sstevel@tonic-gate 		return (cp);
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	if (stat64(cp, &buf) != 0) {
355*7c478bd9Sstevel@tonic-gate 		free(cp);
356*7c478bd9Sstevel@tonic-gate 		return (strdup(""));
357*7c478bd9Sstevel@tonic-gate 	}
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	if (S_ISBLK(buf.st_mode))
360*7c478bd9Sstevel@tonic-gate 		return (cp);
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	if (!S_ISCHR(buf.st_mode)) {
363*7c478bd9Sstevel@tonic-gate 		free(cp);
364*7c478bd9Sstevel@tonic-gate 		return (strdup(""));
365*7c478bd9Sstevel@tonic-gate 	}
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 	if ((dp = getvfsspecial(cp, GET_BLK)) != NULL) {
368*7c478bd9Sstevel@tonic-gate 		free(cp);
369*7c478bd9Sstevel@tonic-gate 		return (strdup(dp));
370*7c478bd9Sstevel@tonic-gate 	}
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	raw_dev = buf.st_rdev;
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	/*
375*7c478bd9Sstevel@tonic-gate 	 * We have a raw device name, go find the block name.
376*7c478bd9Sstevel@tonic-gate 	 */
377*7c478bd9Sstevel@tonic-gate 	if ((dp = strstr(cp, "/rdsk/")) == NULL &&
378*7c478bd9Sstevel@tonic-gate 	    (dp = strstr(cp, "/" LOFI_CHAR_NAME "/")) == NULL &&
379*7c478bd9Sstevel@tonic-gate 	    (dp = strstr(cp, "/" RD_CHAR_NAME "/")) == NULL &&
380*7c478bd9Sstevel@tonic-gate 	    (dp = strstr(cp, "/" SNAP_CHAR_NAME "/")) == NULL &&
381*7c478bd9Sstevel@tonic-gate 	    (dp = strrchr(cp, '/')) == NULL) {
382*7c478bd9Sstevel@tonic-gate 		/* this is not really possible */
383*7c478bd9Sstevel@tonic-gate 		free(cp);
384*7c478bd9Sstevel@tonic-gate 		return (strdup(""));
385*7c478bd9Sstevel@tonic-gate 	}
386*7c478bd9Sstevel@tonic-gate 	dp++;
387*7c478bd9Sstevel@tonic-gate 	if (*dp != 'r') {
388*7c478bd9Sstevel@tonic-gate 		dp = getblkcomplete(cp, &buf);
389*7c478bd9Sstevel@tonic-gate 		free(cp);
390*7c478bd9Sstevel@tonic-gate 		return (dp);
391*7c478bd9Sstevel@tonic-gate 	}
392*7c478bd9Sstevel@tonic-gate 	if ((new_path = malloc(strlen(cp))) == NULL) {
393*7c478bd9Sstevel@tonic-gate 		free(cp);
394*7c478bd9Sstevel@tonic-gate 		return (NULL);
395*7c478bd9Sstevel@tonic-gate 	}
396*7c478bd9Sstevel@tonic-gate 	(void) strncpy(new_path, cp, dp - cp);
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 	/* fill in the rest of the unraw name */
399*7c478bd9Sstevel@tonic-gate 	(void) strcpy(new_path + (dp - cp), dp + 1);
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate 	if (test_if_blk(new_path, raw_dev)) {
402*7c478bd9Sstevel@tonic-gate 		free(cp);
403*7c478bd9Sstevel@tonic-gate 		/* block name was found, return it here */
404*7c478bd9Sstevel@tonic-gate 		return (new_path);
405*7c478bd9Sstevel@tonic-gate 	}
406*7c478bd9Sstevel@tonic-gate 	free(new_path);
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	dp = getblkcomplete(cp, &buf);
409*7c478bd9Sstevel@tonic-gate 	free(cp);
410*7c478bd9Sstevel@tonic-gate 	return (dp);
411*7c478bd9Sstevel@tonic-gate }
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate /*
414*7c478bd9Sstevel@tonic-gate  * change the device name to a raw devname
415*7c478bd9Sstevel@tonic-gate  */
416*7c478bd9Sstevel@tonic-gate char *
getfullrawname(char * cp)417*7c478bd9Sstevel@tonic-gate getfullrawname(char *cp)
418*7c478bd9Sstevel@tonic-gate {
419*7c478bd9Sstevel@tonic-gate 	struct stat64	buf;
420*7c478bd9Sstevel@tonic-gate 	char		*dp;
421*7c478bd9Sstevel@tonic-gate 	char		*new_path;
422*7c478bd9Sstevel@tonic-gate 	dev_t		blk_dev;
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 	if (cp == NULL)
425*7c478bd9Sstevel@tonic-gate 		return (strdup(""));
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	/*
428*7c478bd9Sstevel@tonic-gate 	 * Create a fully qualified name.
429*7c478bd9Sstevel@tonic-gate 	 */
430*7c478bd9Sstevel@tonic-gate 	if ((cp = getfullname(cp)) == NULL)
431*7c478bd9Sstevel@tonic-gate 		return (NULL);
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	if (*cp == '\0')
434*7c478bd9Sstevel@tonic-gate 		return (cp);
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate 	if (stat64(cp, &buf) != 0) {
437*7c478bd9Sstevel@tonic-gate 		free(cp);
438*7c478bd9Sstevel@tonic-gate 		return (strdup(""));
439*7c478bd9Sstevel@tonic-gate 	}
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	if (S_ISCHR(buf.st_mode))
442*7c478bd9Sstevel@tonic-gate 		return (cp);
443*7c478bd9Sstevel@tonic-gate 
444*7c478bd9Sstevel@tonic-gate 	if (!S_ISBLK(buf.st_mode)) {
445*7c478bd9Sstevel@tonic-gate 		free(cp);
446*7c478bd9Sstevel@tonic-gate 		return (strdup(""));
447*7c478bd9Sstevel@tonic-gate 	}
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 	blk_dev = buf.st_rdev;
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 	if ((dp = getvfsspecial(cp, GET_RAW)) != NULL) {
452*7c478bd9Sstevel@tonic-gate 		free(cp);
453*7c478bd9Sstevel@tonic-gate 		return (strdup(dp));
454*7c478bd9Sstevel@tonic-gate 	}
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 	/*
457*7c478bd9Sstevel@tonic-gate 	 * We have a block device name, go find the raw name.
458*7c478bd9Sstevel@tonic-gate 	 */
459*7c478bd9Sstevel@tonic-gate 	if ((dp = strstr(cp, "/dsk/")) == NULL &&
460*7c478bd9Sstevel@tonic-gate 	    (dp = strstr(cp, "/" LOFI_BLOCK_NAME "/")) == NULL &&
461*7c478bd9Sstevel@tonic-gate 	    (dp = strstr(cp, "/" RD_BLOCK_NAME "/")) == NULL &&
462*7c478bd9Sstevel@tonic-gate 	    (dp = strstr(cp, "/" SNAP_BLOCK_NAME "/")) == NULL &&
463*7c478bd9Sstevel@tonic-gate 	    (dp = strrchr(cp, '/')) == NULL) {
464*7c478bd9Sstevel@tonic-gate 		/* this is not really possible */
465*7c478bd9Sstevel@tonic-gate 		free(cp);
466*7c478bd9Sstevel@tonic-gate 		return (strdup(""));
467*7c478bd9Sstevel@tonic-gate 	}
468*7c478bd9Sstevel@tonic-gate 	dp++;
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 	if ((new_path = malloc(strlen(cp)+2)) == NULL) {
471*7c478bd9Sstevel@tonic-gate 		free(cp);
472*7c478bd9Sstevel@tonic-gate 		return (NULL);
473*7c478bd9Sstevel@tonic-gate 	}
474*7c478bd9Sstevel@tonic-gate 	(void) strncpy(new_path, cp, dp - cp);
475*7c478bd9Sstevel@tonic-gate 	/* fill in the rest of the raw name */
476*7c478bd9Sstevel@tonic-gate 	new_path[dp - cp] = 'r';
477*7c478bd9Sstevel@tonic-gate 	(void) strcpy(new_path + (dp - cp) + 1, dp);
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 	if (test_if_raw(new_path, blk_dev)) {
480*7c478bd9Sstevel@tonic-gate 		free(cp);
481*7c478bd9Sstevel@tonic-gate 		return (new_path);
482*7c478bd9Sstevel@tonic-gate 	}
483*7c478bd9Sstevel@tonic-gate 	free(new_path);
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate 	dp = getrawcomplete(cp, &buf);
486*7c478bd9Sstevel@tonic-gate 	free(cp);
487*7c478bd9Sstevel@tonic-gate 	return (dp);
488*7c478bd9Sstevel@tonic-gate }
489