xref: /onnv-gate/usr/src/lib/lvm/libmeta/common/meta_name.c (revision 1945:74cee1cd404b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5908Spetede  * Common Development and Distribution License (the "License").
6908Spetede  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221623Stw21770  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <meta.h>
290Sstevel@tonic-gate #include <metad.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <string.h>
331623Stw21770 #include <sys/fs/ufs_fsdir.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate  * Just in case we're not in a build environment, make sure that
370Sstevel@tonic-gate  * TEXT_DOMAIN gets set to something.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
400Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
410Sstevel@tonic-gate #endif
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  *	Macros to produce a quoted string containing the value of a
450Sstevel@tonic-gate  *	preprocessor macro. For example, if SIZE is defined to be 256,
460Sstevel@tonic-gate  *	VAL2STR(SIZE) is "256". This is used to construct format
470Sstevel@tonic-gate  *	strings for scanf-family functions below.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate #define	QUOTE(x)	#x
500Sstevel@tonic-gate #define	VAL2STR(x)	QUOTE(x)
510Sstevel@tonic-gate 
520Sstevel@tonic-gate extern	char	*getfullblkname();
530Sstevel@tonic-gate extern	char	*getfullrawname();
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * caches
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate static	mdsetnamelist_t		*setlistp = NULL;
590Sstevel@tonic-gate static	mddrivenamelist_t	*drivelistp = NULL;
600Sstevel@tonic-gate static	mdnamelist_t		*fastnmlp = NULL;
610Sstevel@tonic-gate static	mdhspnamelist_t		*hsplistp = NULL;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /*
641623Stw21770  * Static definitions
651623Stw21770  */
661623Stw21770 static int chksetname(mdsetname_t **spp, char *sname, md_error_t *ep);
671623Stw21770 
681623Stw21770 /*
690Sstevel@tonic-gate  * leak proof name conversion
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate static char *
720Sstevel@tonic-gate rawname(
730Sstevel@tonic-gate 	char	*uname
740Sstevel@tonic-gate )
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	char	*p;
770Sstevel@tonic-gate 	struct stat	sbuf1, sbuf2;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if ((p = getfullrawname(uname)) == NULL) {
800Sstevel@tonic-gate 		return (NULL);
810Sstevel@tonic-gate 	} else if (*p == '\0') {
820Sstevel@tonic-gate 		Free(p);
830Sstevel@tonic-gate 		return (NULL);
840Sstevel@tonic-gate 	} else {
850Sstevel@tonic-gate 		if (stat(uname, &sbuf1) != 0) {
860Sstevel@tonic-gate 			(void) printf(dgettext(TEXT_DOMAIN,
870Sstevel@tonic-gate 			    "device to mount in /etc/vfstab is "
880Sstevel@tonic-gate 			    "invalid for device %s\n"), uname);
890Sstevel@tonic-gate 			exit(1);
900Sstevel@tonic-gate 		}
910Sstevel@tonic-gate 		if (stat(p, &sbuf2) != 0) {
920Sstevel@tonic-gate 			(void) printf(dgettext(TEXT_DOMAIN,
930Sstevel@tonic-gate 			    "device to fsck in /etc/vfstab is "
940Sstevel@tonic-gate 			    "invalid for raw device %s\n"), p);
950Sstevel@tonic-gate 			exit(1);
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 		if (sbuf1.st_rdev != sbuf2.st_rdev) {
980Sstevel@tonic-gate 			(void) printf(dgettext(TEXT_DOMAIN,
990Sstevel@tonic-gate 			    "/etc/vfstab entries inconsistent on "
1000Sstevel@tonic-gate 			    "line containing device %s\n"), uname);
1010Sstevel@tonic-gate 			exit(1);
1020Sstevel@tonic-gate 		}
103871Scasper 		if (!S_ISCHR(sbuf2.st_mode)) {
1040Sstevel@tonic-gate 			(void) printf(dgettext(TEXT_DOMAIN,
1050Sstevel@tonic-gate 			    "/etc/vfstab device to fsck is not a "
1060Sstevel@tonic-gate 			    "raw device for device %s\n"), p);
1070Sstevel@tonic-gate 			exit(1);
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate 		return (p);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate char *
1140Sstevel@tonic-gate blkname(
1150Sstevel@tonic-gate 	char	*uname
1160Sstevel@tonic-gate )
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	char	*p;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if ((p = getfullblkname(uname)) == NULL) {
1210Sstevel@tonic-gate 		return (NULL);
1220Sstevel@tonic-gate 	} else if (*p == '\0') {
1230Sstevel@tonic-gate 		Free(p);
1240Sstevel@tonic-gate 		return (NULL);
1250Sstevel@tonic-gate 	} else {
1260Sstevel@tonic-gate 		return (p);
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate  * FUNCTION:	parse_device()
1320Sstevel@tonic-gate  * INPUT:	sp - pointer to setname struct
1330Sstevel@tonic-gate  *		uname - Name of either a hotspare pool or metadevice
1340Sstevel@tonic-gate  *			This can either be a fully qualified path or
1350Sstevel@tonic-gate  *			in the form [set name/]device
1361623Stw21770  * OUTPUT:	snamep - name of the set that uname is in
1371623Stw21770  *		fnamep - metadevice or hsp with path and set name info stripped
1381623Stw21770  *		    This parameter is dynamically allocated and must be
1391623Stw21770  *		    freed by the calling function.
1400Sstevel@tonic-gate  * PURPOSE:	Parse uname and sp into the set name and device name strings.
1410Sstevel@tonic-gate  *		If the set name is specified as part of uname then use that
1420Sstevel@tonic-gate  *		otherwise attempt to get the set name from sp.
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate static void
1450Sstevel@tonic-gate parse_device(
1460Sstevel@tonic-gate 	mdsetname_t	*sp,
1470Sstevel@tonic-gate 	char		*uname,
1481623Stw21770 	char		**fnamep, /* dynamically alloced - caller must free */
1491623Stw21770 	char		**snamep  /* dynamically alloced - caller must free */
1500Sstevel@tonic-gate )
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	char		setname[FILENAME_MAX+1];
1531623Stw21770 	char		devname[FILENAME_MAX+1];
1540Sstevel@tonic-gate 	char		*tname = Malloc(strlen(uname) + 1);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	int		len;
1570Sstevel@tonic-gate 	char *up;
1580Sstevel@tonic-gate 	char *tp;
1590Sstevel@tonic-gate 	int lcws;	/* last character was slash */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/* Now copy uname to tname by throwing away any duplicate '/' */
1620Sstevel@tonic-gate 	for (lcws = 0, tp = tname, up = uname; *up; up++) {
1630Sstevel@tonic-gate 		if (lcws) {
1640Sstevel@tonic-gate 			if (*up == '/') {
1650Sstevel@tonic-gate 				continue;
1660Sstevel@tonic-gate 			} else {
1670Sstevel@tonic-gate 				lcws = 0;
1680Sstevel@tonic-gate 			}
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 		if (*up == '/') {
1710Sstevel@tonic-gate 			lcws = 1;
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 		*tp++ = *up; /* ++ is done by for loop */
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 	*tp = '\0';
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/* fully-qualified  - local set */
1780Sstevel@tonic-gate 	if (((sscanf(tname, "/dev/md/dsk/%" VAL2STR(FILENAME_MAX) "s%n",
1791623Stw21770 			devname, &len) == 1) && (strlen(tname) == len)) ||
1800Sstevel@tonic-gate 	    ((sscanf(tname, "/dev/md/rdsk/%" VAL2STR(FILENAME_MAX) "s%n",
1811623Stw21770 			devname, &len) == 1) && (strlen(tname) == len))) {
1821623Stw21770 		*snamep = Strdup(MD_LOCAL_NAME);
1831623Stw21770 		*fnamep = Strdup(devname);
1840Sstevel@tonic-gate 		Free(tname);
1850Sstevel@tonic-gate 		return;
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/* with setname specified - either fully qualified and relative spec */
1891623Stw21770 	if (((sscanf(tname, "%[^/]/%" VAL2STR(FILENAME_MAX) "s%n",
1901623Stw21770 		setname, devname, &len) == 2) && (strlen(tname) == len)) ||
1910Sstevel@tonic-gate 	    ((sscanf(tname, "/dev/md/%[^/]/dsk/%" VAL2STR(FILENAME_MAX) "s%n",
1921623Stw21770 		setname, devname, &len) == 2) && (strlen(tname) == len)) ||
1930Sstevel@tonic-gate 	    ((sscanf(tname, "/dev/md/%[^/]/rdsk/%" VAL2STR(FILENAME_MAX) "s%n",
1941623Stw21770 		setname, devname, &len) == 2) && (strlen(tname) == len))) {
1951623Stw21770 
1961623Stw21770 		*snamep = Strdup(setname);
1971623Stw21770 		*fnamep = Strdup(devname);
1980Sstevel@tonic-gate 		Free(tname);
1990Sstevel@tonic-gate 		return;
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/* without setname specified */
2031623Stw21770 	*fnamep = tname;
2041623Stw21770 	if (sp != NULL && !metaislocalset(sp))
2051623Stw21770 		*snamep = Strdup(sp->setname);
2061623Stw21770 	else
2071623Stw21770 		*snamep = NULL;
2081623Stw21770 }
2091623Stw21770 
2101623Stw21770 /*
2111623Stw21770  * check for "all"
2121623Stw21770  */
2131623Stw21770 int
2141623Stw21770 meta_is_all(char *s)
2151623Stw21770 {
2161623Stw21770 	if ((strcoll(s, gettext("all")) == 0) ||
2171623Stw21770 	    (strcoll(s, gettext("ALL")) == 0))
2181623Stw21770 		return (1);
2191623Stw21770 	return (0);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /*
2231623Stw21770  * check for "none"
2240Sstevel@tonic-gate  */
2251623Stw21770 int
2261623Stw21770 meta_is_none(char *s)
2271623Stw21770 {
2281623Stw21770 	if ((strcoll(s, gettext("none")) == 0) ||
2291623Stw21770 	    (strcoll(s, gettext("NONE")) == 0))
2301623Stw21770 		return (1);
2311623Stw21770 	return (0);
2321623Stw21770 }
2331623Stw21770 
2340Sstevel@tonic-gate static int
2351623Stw21770 valid_name_syntax(char *uname)
2360Sstevel@tonic-gate {
2371623Stw21770 	int	i;
2381623Stw21770 	int	uname_len;
2391623Stw21770 
2401623Stw21770 	if (uname == NULL || !isalpha(uname[0]))
2411623Stw21770 		return (0);
2421623Stw21770 
2431623Stw21770 	uname_len = strlen(uname);
2441623Stw21770 	if (uname_len > MAXNAMLEN)
2450Sstevel@tonic-gate 		return (0);
2461623Stw21770 
2471623Stw21770 	/* 'all' and 'none' are reserved */
2481623Stw21770 	if (meta_is_all(uname) || meta_is_none(uname))
2491623Stw21770 		return (0);
2501623Stw21770 
2511623Stw21770 	for (i = 1; i < uname_len; i++) {
2521623Stw21770 		if ((isalnum(uname[i]) || uname[i] == '-' ||
2531623Stw21770 		    uname[i] == '_' || uname[i] == '.'))
2541623Stw21770 			continue;
2551623Stw21770 		break;
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2581623Stw21770 	if (i < uname_len)
2590Sstevel@tonic-gate 		return (0);
2601623Stw21770 
2611623Stw21770 	return (1);
2621623Stw21770 
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate /*
2661623Stw21770  * canonicalize name
2670Sstevel@tonic-gate  */
2680Sstevel@tonic-gate char *
2690Sstevel@tonic-gate meta_canonicalize(
2700Sstevel@tonic-gate 	mdsetname_t	*sp,
2710Sstevel@tonic-gate 	char		*uname
2720Sstevel@tonic-gate )
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	char	*sname = NULL;
2751623Stw21770 	char	*tname = NULL;
2760Sstevel@tonic-gate 	char	*cname;
2770Sstevel@tonic-gate 
2781623Stw21770 	/* return the dev name and set name */
2791623Stw21770 	parse_device(sp, uname, &tname, &sname);
2801623Stw21770 
2811623Stw21770 	if (!valid_name_syntax(tname)) {
2821623Stw21770 		Free(tname);
2831623Stw21770 		if (sname != NULL)
2841623Stw21770 		    Free(sname);
2851623Stw21770 		return (NULL);
2861623Stw21770 	}
2871623Stw21770 
2881623Stw21770 	if ((sname == NULL) || (strcmp(sname, MD_LOCAL_NAME) == 0))
2891623Stw21770 		cname = tname;
2900Sstevel@tonic-gate 	else {
2910Sstevel@tonic-gate 		size_t	cname_len;
2920Sstevel@tonic-gate 
2931623Stw21770 		cname_len = strlen(tname) + strlen(sname) + 2;
2940Sstevel@tonic-gate 		cname = Malloc(cname_len);
2950Sstevel@tonic-gate 		(void) snprintf(
2961623Stw21770 		    cname, cname_len, "%s/%s", sname, tname);
2971623Stw21770 		Free(tname);
2981623Stw21770 	}
2991623Stw21770 
3001623Stw21770 	if (sname != NULL)
3011623Stw21770 	    Free(sname);
3021623Stw21770 
3031623Stw21770 	return (cname);
3041623Stw21770 }
3051623Stw21770 
3061623Stw21770 /*
3071623Stw21770  * canonicalize name and check the set
3081623Stw21770  */
3091623Stw21770 char *
3101623Stw21770 meta_canonicalize_check_set(
3111623Stw21770 	mdsetname_t	**spp,
3121623Stw21770 	char		*uname,
3131623Stw21770 	md_error_t	*ep
3141623Stw21770 )
3151623Stw21770 {
3161623Stw21770 	char		*sname = NULL;
3171623Stw21770 	char		*tname = NULL;
3181623Stw21770 	char		*cname;
3191623Stw21770 
3201623Stw21770 	/* return the dev name and set name */
3211623Stw21770 	parse_device(*spp, uname, &tname, &sname);
3221623Stw21770 
3231623Stw21770 	if (!valid_name_syntax(tname)) {
3241623Stw21770 		(void) mderror(ep, MDE_NAME_ILLEGAL, tname);
3251623Stw21770 		if (sname != NULL)
3261623Stw21770 			Free(sname);
3271623Stw21770 		Free(tname);
3281623Stw21770 		return (NULL);
3291623Stw21770 	}
3301623Stw21770 
3311623Stw21770 	/* check the set name returned from the name for validity */
3321623Stw21770 	if (chksetname(spp, sname, ep) != 0) {
3331623Stw21770 		Free(tname);
3341623Stw21770 		if (sname != NULL)
3351623Stw21770 		    Free(sname);
3361623Stw21770 		return (NULL);
3371623Stw21770 	}
3381623Stw21770 
3391623Stw21770 	if ((sname == NULL) || (strcmp(sname, MD_LOCAL_NAME) == 0))
3401623Stw21770 		cname = tname;
3411623Stw21770 	else {
3421623Stw21770 		size_t	cname_len;
3431623Stw21770 
3441623Stw21770 		cname_len = strlen(tname) + strlen(sname) + 2;
3451623Stw21770 		cname = Malloc(cname_len);
3461623Stw21770 		(void) snprintf(
3471623Stw21770 		    cname, cname_len, "%s/%s", sname, tname);
3481623Stw21770 		Free(tname);
3491623Stw21770 	}
3501623Stw21770 
3511623Stw21770 	if (sname != NULL)
3521623Stw21770 	    Free(sname);
3531623Stw21770 
3541623Stw21770 	return (cname);
3551623Stw21770 }
3561623Stw21770 
3571623Stw21770 /*
3581623Stw21770  * Verify that the name is a valid hsp/metadevice name
3591623Stw21770  */
3601623Stw21770 static int
3611623Stw21770 parse_meta_hsp_name(char *uname)
3621623Stw21770 {
3631623Stw21770 	char	*sname = NULL;
3641623Stw21770 	char	*tname = NULL;
3651623Stw21770 	int	ret;
3661623Stw21770 
3671623Stw21770 	/* return the dev name and set name */
3681623Stw21770 	parse_device(NULL, uname, &tname, &sname);
3691623Stw21770 
3701623Stw21770 	ret = valid_name_syntax(tname);
3711623Stw21770 	if (sname != NULL)
3720Sstevel@tonic-gate 		Free(sname);
3731623Stw21770 	Free(tname);
3741623Stw21770 	return (ret);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate  * check that name is a metadevice
3790Sstevel@tonic-gate  */
3800Sstevel@tonic-gate int
3810Sstevel@tonic-gate is_metaname(
3820Sstevel@tonic-gate 	char	*uname
3830Sstevel@tonic-gate )
3840Sstevel@tonic-gate {
3851623Stw21770 	return (parse_meta_hsp_name(uname));
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate  * check that name is a hotspare pool
3900Sstevel@tonic-gate  */
3910Sstevel@tonic-gate int
3920Sstevel@tonic-gate is_hspname(
3930Sstevel@tonic-gate 	char	*uname
3940Sstevel@tonic-gate )
3950Sstevel@tonic-gate {
3961623Stw21770 	return (parse_meta_hsp_name(uname));
3971623Stw21770 }
3981623Stw21770 
3991623Stw21770 /*
4001623Stw21770  * check to verify that name is an existing metadevice
4011623Stw21770  */
4021623Stw21770 int
4031623Stw21770 is_existing_metadevice(
4041623Stw21770 	mdsetname_t	*sp,
4051623Stw21770 	char		*uname
4061623Stw21770 )
4071623Stw21770 {
4081623Stw21770 	char		*raw_name;
4091623Stw21770 	char		*set_name;
4101623Stw21770 	char		*full_path;
4111623Stw21770 	char		*fname = NULL;
4121623Stw21770 	int		pathlen;
4131623Stw21770 	int		retval = 0;
4141623Stw21770 
4151623Stw21770 	assert(uname != NULL);
4161623Stw21770 	/*
4171623Stw21770 	 * If it is an absolute name of a metadevice, then just call rawname
4181623Stw21770 	 * on the input
4191623Stw21770 	 */
4201623Stw21770 	if (uname[0] == '/') {
4211623Stw21770 		if (strncmp("/dev/md", uname, strlen("/dev/md")) == 0 &&
4221623Stw21770 			(raw_name = rawname(uname)) != NULL) {
4231623Stw21770 		    Free(raw_name);
4241623Stw21770 		    return (1);
4251623Stw21770 		}
4261623Stw21770 		return (0);
4271623Stw21770 	}
4281623Stw21770 
4291623Stw21770 	/* create a fully specified path from the parsed string */
4301623Stw21770 	parse_device(sp, uname, &fname, &set_name);
4311623Stw21770 
4321623Stw21770 	if ((set_name == NULL) || (strcmp(set_name, MD_LOCAL_NAME) == 0)) {
4331623Stw21770 		pathlen = strlen("/dev/md/rdsk/") + strlen(fname) + 1;
4341623Stw21770 		full_path = Zalloc(pathlen);
4351623Stw21770 		(void) snprintf(full_path, pathlen, "/dev/md/rdsk/%s", fname);
4361623Stw21770 	} else {
4371623Stw21770 		pathlen = strlen("/dev/md//rdsk/") + strlen(fname) +
4381623Stw21770 		    strlen(set_name) + 1;
4391623Stw21770 		full_path = Zalloc(pathlen);
4401623Stw21770 		(void) snprintf(full_path, pathlen, "/dev/md/%s/rdsk/%s",
4411623Stw21770 		    set_name, fname);
4421623Stw21770 	}
4431623Stw21770 
4441623Stw21770 	if ((raw_name = rawname(full_path)) != NULL) {
4451623Stw21770 	    Free(raw_name);
4461623Stw21770 	    retval = 1;
4471623Stw21770 	}
4481623Stw21770 
4491623Stw21770 	if (set_name != NULL)
4501623Stw21770 		Free(set_name);
4511623Stw21770 
4521623Stw21770 	Free(fname);
4531623Stw21770 	Free(full_path);
4541623Stw21770 	return (retval);
4551623Stw21770 }
4561623Stw21770 
4571623Stw21770 /*
4581623Stw21770  * check to verify that name is an existing hsp
4591623Stw21770  */
4601623Stw21770 int
4611623Stw21770 is_existing_hsp(
4621623Stw21770 	mdsetname_t	*sp,
4631623Stw21770 	char		*uname
4641623Stw21770 )
4651623Stw21770 {
4661623Stw21770 	md_error_t	status = mdnullerror;
4671623Stw21770 	hsp_t		hsp;
4681623Stw21770 	set_t		cur_set;
4691623Stw21770 
4701623Stw21770 	if (sp != NULL)
4711623Stw21770 		cur_set = sp->setno;
4721623Stw21770 	else
4731623Stw21770 		cur_set = 0;
4741623Stw21770 
4751623Stw21770 	hsp = meta_gethspnmentbyname(cur_set, MD_SIDEWILD, uname, &status);
4761623Stw21770 
4771623Stw21770 	if (hsp == MD_HSP_NONE) {
4781623Stw21770 		mdclrerror(&status);
4791623Stw21770 		return (0);
4801623Stw21770 	}
4811623Stw21770 	return (1);
4821623Stw21770 }
4831623Stw21770 
4841623Stw21770 /*
4851623Stw21770  * check to verify that name is an existing metadevice or hotspare pool
4861623Stw21770  */
4871623Stw21770 int
4881623Stw21770 is_existing_meta_hsp(
4891623Stw21770 	mdsetname_t	*sp,
4901623Stw21770 	char		*uname
4911623Stw21770 )
4921623Stw21770 {
4931623Stw21770 	if (is_existing_metadevice(sp, uname) ||
4941623Stw21770 	    is_existing_hsp(sp, uname))
4950Sstevel@tonic-gate 		return (1);
4961623Stw21770 
4971623Stw21770 	return (0);
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate  *	mdsetname_t stuff
5020Sstevel@tonic-gate  */
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate /*
5050Sstevel@tonic-gate  * initialize setname
5060Sstevel@tonic-gate  */
5070Sstevel@tonic-gate static void
5080Sstevel@tonic-gate metainitsetname(
5090Sstevel@tonic-gate 	mdsetname_t	*sp
5100Sstevel@tonic-gate )
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	(void) memset(sp, '\0', sizeof (*sp));
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate static void
5160Sstevel@tonic-gate metafreesetdesc(md_set_desc *sd)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	md_mnnode_desc	*nd;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if (MD_MNSET_DESC(sd)) {
5210Sstevel@tonic-gate 		nd = sd->sd_nodelist;
5220Sstevel@tonic-gate 		while (nd) {
5230Sstevel@tonic-gate 			sd->sd_nodelist = nd->nd_next;
5240Sstevel@tonic-gate 			Free(nd);
5250Sstevel@tonic-gate 			nd = sd->sd_nodelist;
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 	metafreedrivedesc(&sd->sd_drvs);
5290Sstevel@tonic-gate 	Free(sd);
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate /*
5330Sstevel@tonic-gate  * free allocated setname
5340Sstevel@tonic-gate  */
5350Sstevel@tonic-gate static void
5360Sstevel@tonic-gate metafreesetname(
5370Sstevel@tonic-gate 	mdsetname_t	*sp
5380Sstevel@tonic-gate )
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate 	if (sp->setname != NULL)
5410Sstevel@tonic-gate 		Free(sp->setname);
5420Sstevel@tonic-gate 	if (sp->setdesc != NULL)
5430Sstevel@tonic-gate 		metafreesetdesc(sp->setdesc);
5440Sstevel@tonic-gate 	metainitsetname(sp);
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate  * flush the setname cache
5490Sstevel@tonic-gate  */
5500Sstevel@tonic-gate static void
5510Sstevel@tonic-gate metaflushsetnames()
5520Sstevel@tonic-gate {
5530Sstevel@tonic-gate 	mdsetnamelist_t		*p, *n;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	for (p = setlistp, n = NULL; (p != NULL); p = n) {
5560Sstevel@tonic-gate 		n = p->next;
5570Sstevel@tonic-gate 		metafreesetname(p->sp);
5580Sstevel@tonic-gate 		Free(p->sp);
5590Sstevel@tonic-gate 		Free(p);
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 	setlistp = NULL;
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * get set number
5660Sstevel@tonic-gate  */
5670Sstevel@tonic-gate static int
5680Sstevel@tonic-gate getsetno(
5690Sstevel@tonic-gate 	char		*sname,
5700Sstevel@tonic-gate 	set_t		*setnop,
5710Sstevel@tonic-gate 	md_error_t	*ep
5720Sstevel@tonic-gate )
5730Sstevel@tonic-gate {
5740Sstevel@tonic-gate 	md_set_record	*sr;
5750Sstevel@tonic-gate 	size_t		len;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	/* local set */
5780Sstevel@tonic-gate 	if ((sname == NULL) || (strcmp(sname, MD_LOCAL_NAME) == 0)) {
5790Sstevel@tonic-gate 		*setnop = 0;
5800Sstevel@tonic-gate 		return (0);
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/* shared set */
5840Sstevel@tonic-gate 	if ((sr = getsetbyname(sname, ep)) == NULL) {
5850Sstevel@tonic-gate 		if (mdisrpcerror(ep, RPC_PROGNOTREGISTERED)) {
5860Sstevel@tonic-gate 			char	*p;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 			len = strlen(sname) + 30;
5890Sstevel@tonic-gate 			p = Malloc(len);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 			(void) snprintf(p, len, "setname \"%s\"", sname);
5920Sstevel@tonic-gate 			(void) mderror(ep, MDE_NO_SET, p);
5930Sstevel@tonic-gate 			Free(p);
5940Sstevel@tonic-gate 		}
5950Sstevel@tonic-gate 		return (-1);
5960Sstevel@tonic-gate 	}
5970Sstevel@tonic-gate 	*setnop = sr->sr_setno;
5980Sstevel@tonic-gate 	free_sr(sr);
5990Sstevel@tonic-gate 	return (0);
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate /*
6030Sstevel@tonic-gate  * find setname from name
6040Sstevel@tonic-gate  */
6050Sstevel@tonic-gate mdsetname_t *
6060Sstevel@tonic-gate metasetname(
6070Sstevel@tonic-gate 	char		*sname,
6080Sstevel@tonic-gate 	md_error_t	*ep
6090Sstevel@tonic-gate )
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate 	mdsetnamelist_t	**tail;
6120Sstevel@tonic-gate 	set_t		setno;
6130Sstevel@tonic-gate 	mdsetname_t	*sp;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/* look for cached value first */
6160Sstevel@tonic-gate 	assert(sname != NULL);
6170Sstevel@tonic-gate 	for (tail = &setlistp; (*tail != NULL); tail = &(*tail)->next) {
6180Sstevel@tonic-gate 		sp = (*tail)->sp;
6190Sstevel@tonic-gate 		if (strcmp(sp->setname, sname) == 0) {
6200Sstevel@tonic-gate 			return (sp);
6210Sstevel@tonic-gate 		}
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	/* setup set */
6250Sstevel@tonic-gate 	if (getsetno(sname, &setno, ep) != 0)
6260Sstevel@tonic-gate 		return (NULL);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	/* allocate new list element and setname */
6290Sstevel@tonic-gate 	*tail = Zalloc(sizeof (**tail));
6300Sstevel@tonic-gate 	sp = (*tail)->sp = Zalloc(sizeof (*sp));
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	sp->setname = Strdup(sname);
6330Sstevel@tonic-gate 	sp->setno = setno;
6340Sstevel@tonic-gate 	sp->lockfd = MD_NO_LOCK;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	return (sp);
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate /*
6400Sstevel@tonic-gate  * find setname from setno
6410Sstevel@tonic-gate  */
6420Sstevel@tonic-gate mdsetname_t *
6430Sstevel@tonic-gate metasetnosetname(
6440Sstevel@tonic-gate 	set_t		setno,
6450Sstevel@tonic-gate 	md_error_t	*ep
6460Sstevel@tonic-gate )
6470Sstevel@tonic-gate {
6480Sstevel@tonic-gate 	mdsetnamelist_t	*slp;
6490Sstevel@tonic-gate 	mdsetname_t	*sp;
6500Sstevel@tonic-gate 	md_set_record	*sr;
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	/* look for cached value first */
6530Sstevel@tonic-gate 	for (slp = setlistp; (slp != NULL); slp = slp->next) {
6540Sstevel@tonic-gate 		sp = slp->sp;
6550Sstevel@tonic-gate 		if (sp->setno == setno)
6560Sstevel@tonic-gate 			return (sp);
6570Sstevel@tonic-gate 	}
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	/* local set */
6600Sstevel@tonic-gate 	if (setno == MD_LOCAL_SET)
6610Sstevel@tonic-gate 		return (metasetname(MD_LOCAL_NAME, ep));
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	/* shared set */
6640Sstevel@tonic-gate 	if ((sr = getsetbynum(setno, ep)) == NULL)
6650Sstevel@tonic-gate 		return (NULL);
6660Sstevel@tonic-gate 	sp = metasetname(sr->sr_setname, ep);
6670Sstevel@tonic-gate 	free_sr(sr);
6680Sstevel@tonic-gate 	return (sp);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate mdsetname_t *
6720Sstevel@tonic-gate metafakesetname(
6730Sstevel@tonic-gate 	set_t		setno,
6740Sstevel@tonic-gate 	char		*sname
6750Sstevel@tonic-gate )
6760Sstevel@tonic-gate {
6770Sstevel@tonic-gate 	mdsetnamelist_t	**tail;
6780Sstevel@tonic-gate 	mdsetname_t	*sp;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	/* look for cached value first */
6810Sstevel@tonic-gate 	for (tail = &setlistp; (*tail != NULL); tail = &(*tail)->next) {
6820Sstevel@tonic-gate 		sp = (*tail)->sp;
6830Sstevel@tonic-gate 		if (sp->setno == setno) {
6840Sstevel@tonic-gate 			if ((sp->setname == NULL) && (sname != NULL))
6850Sstevel@tonic-gate 				sp->setname = Strdup(sname);
6860Sstevel@tonic-gate 			return (sp);
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	/* allocate new list element and setname */
6910Sstevel@tonic-gate 	*tail = Zalloc(sizeof (**tail));
6920Sstevel@tonic-gate 	sp = (*tail)->sp = Zalloc(sizeof (*sp));
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	if (sname != NULL)
6950Sstevel@tonic-gate 		sp->setname = Strdup(sname);
6960Sstevel@tonic-gate 	sp->setno = setno;
6970Sstevel@tonic-gate 	sp->lockfd = MD_NO_LOCK;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	return (sp);
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate /*
7040Sstevel@tonic-gate  * setup set record (sr) and cache it in the mdsetname_t struct
7050Sstevel@tonic-gate  */
7060Sstevel@tonic-gate md_set_desc *
7070Sstevel@tonic-gate sr2setdesc(
7080Sstevel@tonic-gate 	md_set_record	*sr
7090Sstevel@tonic-gate )
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	md_set_desc	*sd;
7120Sstevel@tonic-gate 	int		i;
7130Sstevel@tonic-gate 	md_mnset_record	*mnsr;
7140Sstevel@tonic-gate 	md_mnnode_desc	*nd, *nd_prev = 0;
7150Sstevel@tonic-gate 	md_mnnode_record	*nr;
7160Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
7170Sstevel@tonic-gate 	md_error_t	*ep = &status;
7180Sstevel@tonic-gate 	int		nodecnt, nrcnt;
7190Sstevel@tonic-gate 	mndiskset_membershiplist_t *nl, *nl2;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	sd = Zalloc(sizeof (*sd));
7220Sstevel@tonic-gate 	sd->sd_ctime = sr->sr_ctime;
7230Sstevel@tonic-gate 	sd->sd_genid = sr->sr_genid;
7240Sstevel@tonic-gate 	sd->sd_setno = sr->sr_setno;
7250Sstevel@tonic-gate 	sd->sd_flags = sr->sr_flags;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	if (MD_MNSET_DESC(sd)) {
7280Sstevel@tonic-gate 		mnsr = (md_mnset_record *)sr;
7290Sstevel@tonic-gate 		(void) strlcpy(sd->sd_mn_master_nodenm,
7300Sstevel@tonic-gate 		    mnsr->sr_master_nodenm, sizeof (sd->sd_mn_master_nodenm));
7310Sstevel@tonic-gate 		sd->sd_mn_master_nodeid = mnsr->sr_master_nodeid;
7320Sstevel@tonic-gate 		if (strcmp(mnsr->sr_master_nodenm, mynode()) == 0) {
7330Sstevel@tonic-gate 			sd->sd_mn_am_i_master = 1;
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 		/*
7370Sstevel@tonic-gate 		 * Get membershiplist from API routine.  If there's
7380Sstevel@tonic-gate 		 * an error, just use a NULL nodelist.
7390Sstevel@tonic-gate 		 */
7400Sstevel@tonic-gate 		if (meta_read_nodelist(&nodecnt, &nl, ep) == -1) {
7410Sstevel@tonic-gate 			nodecnt = 0;  /* no nodes are alive */
7420Sstevel@tonic-gate 			nl = NULL;
7430Sstevel@tonic-gate 		}
7440Sstevel@tonic-gate 		nr = mnsr->sr_nodechain;
7450Sstevel@tonic-gate 		nrcnt = 0;
7460Sstevel@tonic-gate 		/*
7470Sstevel@tonic-gate 		 * Node descriptor node list must be built in
7480Sstevel@tonic-gate 		 * ascending order of nodeid.  The nodechain
7490Sstevel@tonic-gate 		 * in the mnset record is in ascending order,
7500Sstevel@tonic-gate 		 * so just make them the same.
7510Sstevel@tonic-gate 		 */
7520Sstevel@tonic-gate 		while (nr) {
7530Sstevel@tonic-gate 			nd = Zalloc(sizeof (*nd));
7540Sstevel@tonic-gate 			if (nd_prev) {
7550Sstevel@tonic-gate 				nd_prev->nd_next = nd;
7560Sstevel@tonic-gate 			} else {
7570Sstevel@tonic-gate 				sd->sd_nodelist = nd;
7580Sstevel@tonic-gate 			}
7590Sstevel@tonic-gate 			nd->nd_ctime = nr->nr_ctime;
7600Sstevel@tonic-gate 			nd->nd_genid = nr->nr_genid;
7610Sstevel@tonic-gate 			nd->nd_flags = nr->nr_flags;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 			(void) strlcpy(nd->nd_nodename, nr->nr_nodename,
7640Sstevel@tonic-gate 			    sizeof (nd->nd_nodename));
7650Sstevel@tonic-gate 			nd->nd_nodeid = nr->nr_nodeid;
7660Sstevel@tonic-gate 			if (strcmp(nd->nd_nodename, mynode()) == 0) {
7670Sstevel@tonic-gate 				sd->sd_mn_mynode = nd;
7680Sstevel@tonic-gate 			}
7690Sstevel@tonic-gate 			if (nd->nd_nodeid == sd->sd_mn_master_nodeid) {
7700Sstevel@tonic-gate 				sd->sd_mn_masternode = nd;
7710Sstevel@tonic-gate 			}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 			/*
7740Sstevel@tonic-gate 			 * If node is marked ALIVE, then set priv_ic
7750Sstevel@tonic-gate 			 * from membership list.  During the early part
7760Sstevel@tonic-gate 			 * of a reconfig cycle, the membership list may
7770Sstevel@tonic-gate 			 * have been changed, (a node entering or leaving
7780Sstevel@tonic-gate 			 * the cluster), but rpc.metad hasn't flushed
7790Sstevel@tonic-gate 			 * its data yet.  So, if node is marked alive, but
7800Sstevel@tonic-gate 			 * is no longer in the membership list (node has
7810Sstevel@tonic-gate 			 * left the cluster) then just leave priv_ic to NULL.
7820Sstevel@tonic-gate 			 */
7830Sstevel@tonic-gate 			if (nd->nd_flags & MD_MN_NODE_ALIVE) {
7840Sstevel@tonic-gate 				nl2 = nl;
7850Sstevel@tonic-gate 				while (nl2) {
7860Sstevel@tonic-gate 					if (nl2->msl_node_id == nd->nd_nodeid) {
7870Sstevel@tonic-gate 						(void) strlcpy(nd->nd_priv_ic,
7880Sstevel@tonic-gate 						    nl2->msl_node_addr,
7890Sstevel@tonic-gate 						    sizeof (nd->nd_priv_ic));
7900Sstevel@tonic-gate 						break;
7910Sstevel@tonic-gate 					}
7920Sstevel@tonic-gate 					nl2 = nl2->next;
7930Sstevel@tonic-gate 				}
7940Sstevel@tonic-gate 			}
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 			nr = nr->nr_next;
7970Sstevel@tonic-gate 			nrcnt++;
7980Sstevel@tonic-gate 			nd_prev = nd;
7990Sstevel@tonic-gate 		}
8000Sstevel@tonic-gate 		sd->sd_mn_numnodes = nrcnt;
8010Sstevel@tonic-gate 		if (nodecnt)
8020Sstevel@tonic-gate 			meta_free_nodelist(nl);
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 		/* Just copying to keep consistent view between sr & sd */
8050Sstevel@tonic-gate 		(void) strlcpy(sd->sd_nodes[0], mnsr->sr_nodes_bw_compat[0],
8060Sstevel@tonic-gate 		    sizeof (sd->sd_nodes[0]));
8070Sstevel@tonic-gate 	} else {
8080Sstevel@tonic-gate 		for (i = 0; i < MD_MAXSIDES; i++)
8090Sstevel@tonic-gate 			(void) strlcpy(sd->sd_nodes[i], sr->sr_nodes[i],
8100Sstevel@tonic-gate 			    sizeof (sd->sd_nodes[i]));
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	sd->sd_med = sr->sr_med;		/* structure assignment */
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	return (sd);
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate md_set_desc *
8190Sstevel@tonic-gate metaget_setdesc(
8200Sstevel@tonic-gate 	mdsetname_t	*sp,
8210Sstevel@tonic-gate 	md_error_t	*ep
8220Sstevel@tonic-gate )
8230Sstevel@tonic-gate {
8240Sstevel@tonic-gate 	md_set_record	*sr;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	if (sp->setdesc != NULL)
8270Sstevel@tonic-gate 		return (sp->setdesc);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	if (sp->setname != NULL) {
8300Sstevel@tonic-gate 		if ((sr = getsetbyname(sp->setname, ep)) != NULL) {
8310Sstevel@tonic-gate 			sp->setdesc = sr2setdesc(sr);
8320Sstevel@tonic-gate 			free_sr(sr);
8330Sstevel@tonic-gate 			return (sp->setdesc);
8340Sstevel@tonic-gate 		}
8350Sstevel@tonic-gate 	}
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	if (sp->setno > 0) {
8380Sstevel@tonic-gate 		if ((sr = getsetbynum(sp->setno, ep)) != NULL) {
8390Sstevel@tonic-gate 			sp->setdesc = sr2setdesc(sr);
8400Sstevel@tonic-gate 			free_sr(sr);
8410Sstevel@tonic-gate 			return (sp->setdesc);
8420Sstevel@tonic-gate 		}
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	return (NULL);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate void
8490Sstevel@tonic-gate metaflushsetname(mdsetname_t *sp)
8500Sstevel@tonic-gate {
8510Sstevel@tonic-gate 	if (sp == NULL)
8520Sstevel@tonic-gate 		return;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	if (sp->setdesc == NULL)
8550Sstevel@tonic-gate 		return;
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	metafreesetdesc(sp->setdesc);
8580Sstevel@tonic-gate 	sp->setdesc = NULL;
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate /*
8620Sstevel@tonic-gate  * check for local set
8630Sstevel@tonic-gate  */
8640Sstevel@tonic-gate int
8650Sstevel@tonic-gate metaislocalset(
8660Sstevel@tonic-gate 	mdsetname_t	*sp
8670Sstevel@tonic-gate )
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate 	assert(sp->setname != NULL);
8700Sstevel@tonic-gate 	if (strcmp(sp->setname, MD_LOCAL_NAME) == 0) {
8710Sstevel@tonic-gate 		assert(sp->setno == MD_LOCAL_SET);
8720Sstevel@tonic-gate 		return (1);
8730Sstevel@tonic-gate 	} else {
8740Sstevel@tonic-gate 		assert(sp->setno != MD_LOCAL_SET);
8750Sstevel@tonic-gate 		return (0);
8760Sstevel@tonic-gate 	}
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate /*
8800Sstevel@tonic-gate  * check for same set
8810Sstevel@tonic-gate  */
8820Sstevel@tonic-gate int
8830Sstevel@tonic-gate metaissameset(
8840Sstevel@tonic-gate 	mdsetname_t	*sp1,
8850Sstevel@tonic-gate 	mdsetname_t	*sp2
8860Sstevel@tonic-gate )
8870Sstevel@tonic-gate {
8880Sstevel@tonic-gate 	if (strcmp(sp1->setname, sp2->setname) == 0) {
8890Sstevel@tonic-gate 		assert(sp1->setno == sp2->setno);
8900Sstevel@tonic-gate 		return (1);
8910Sstevel@tonic-gate 	} else {
8920Sstevel@tonic-gate 		assert(sp1->setno != sp2->setno);
8930Sstevel@tonic-gate 		return (0);
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate  * check to see if set changed
8990Sstevel@tonic-gate  */
9000Sstevel@tonic-gate static int
9010Sstevel@tonic-gate chkset(
9020Sstevel@tonic-gate 	mdsetname_t	**spp,
9030Sstevel@tonic-gate 	char		*sname,
9040Sstevel@tonic-gate 	md_error_t	*ep
9050Sstevel@tonic-gate )
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate 	/* if we already have a set, make sure it's the same */
9081623Stw21770 	if (*spp != NULL && !metaislocalset(*spp)) {
9090Sstevel@tonic-gate 		if ((*spp)->setname != sname &&
9100Sstevel@tonic-gate 				strcmp((*spp)->setname, sname) != 0) {
9110Sstevel@tonic-gate 			return (mderror(ep, MDE_SET_DIFF, sname));
9120Sstevel@tonic-gate 		}
9130Sstevel@tonic-gate 		return (0);
9140Sstevel@tonic-gate 	}
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	/* otherwise store new set name and number */
9170Sstevel@tonic-gate 	if ((*spp = metasetname(sname, ep)) == NULL) {
9180Sstevel@tonic-gate 		return (-1);
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	/* return success */
9220Sstevel@tonic-gate 	return (0);
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate /*
9260Sstevel@tonic-gate  * check to see if set changed from default
9270Sstevel@tonic-gate  */
9280Sstevel@tonic-gate static int
9290Sstevel@tonic-gate chksetname(
9300Sstevel@tonic-gate 	mdsetname_t	**spp,
9310Sstevel@tonic-gate 	char		*sname,
9320Sstevel@tonic-gate 	md_error_t	*ep
9330Sstevel@tonic-gate )
9340Sstevel@tonic-gate {
9350Sstevel@tonic-gate 	/* default to *spp's setname, or if that is NULL to MD_LOCAL_NAME */
9360Sstevel@tonic-gate 	if (sname == NULL) {
9370Sstevel@tonic-gate 		if (*spp) {
9381623Stw21770 			return (0);
9390Sstevel@tonic-gate 		} else {
9400Sstevel@tonic-gate 			sname = MD_LOCAL_NAME;
9410Sstevel@tonic-gate 		}
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	/* see if changed */
9450Sstevel@tonic-gate 	return (chkset(spp, sname, ep));
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate /*
9490Sstevel@tonic-gate  * check setname from setno
9500Sstevel@tonic-gate  */
9510Sstevel@tonic-gate static int
9520Sstevel@tonic-gate chksetno(
9530Sstevel@tonic-gate 	mdsetname_t	**spp,
9540Sstevel@tonic-gate 	set_t		setno,
9550Sstevel@tonic-gate 	md_error_t	*ep
9560Sstevel@tonic-gate )
9570Sstevel@tonic-gate {
9580Sstevel@tonic-gate 	md_set_record	*sr;
9590Sstevel@tonic-gate 	int		rval;
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	/* local set */
9620Sstevel@tonic-gate 	if (setno == 0)
9630Sstevel@tonic-gate 		return (chkset(spp, MD_LOCAL_NAME, ep));
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/* shared set */
9660Sstevel@tonic-gate 	if ((sr = getsetbynum(setno, ep)) == NULL)
9670Sstevel@tonic-gate 		return (-1);
9680Sstevel@tonic-gate 	rval = chkset(spp, sr->sr_setname, ep);
9690Sstevel@tonic-gate 	free_sr(sr);
9700Sstevel@tonic-gate 	return (rval);
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate /*
9740Sstevel@tonic-gate  *	mddrivename_t stuff
9750Sstevel@tonic-gate  */
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate /*
9780Sstevel@tonic-gate  * initialize name
9790Sstevel@tonic-gate  */
9800Sstevel@tonic-gate static void
9810Sstevel@tonic-gate metainitname(
9820Sstevel@tonic-gate 	mdname_t	*np
9830Sstevel@tonic-gate )
9840Sstevel@tonic-gate {
9850Sstevel@tonic-gate 	(void) memset(np, 0, sizeof (*np));
9860Sstevel@tonic-gate 	np->dev = NODEV64;
9870Sstevel@tonic-gate 	np->key = MD_KEYBAD;
9880Sstevel@tonic-gate 	np->end_blk = -1;
9890Sstevel@tonic-gate 	np->start_blk = -1;
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate /*
9930Sstevel@tonic-gate  * free allocated name
9940Sstevel@tonic-gate  */
9950Sstevel@tonic-gate static void
9960Sstevel@tonic-gate metafreename(
9970Sstevel@tonic-gate 	mdname_t	*np
9980Sstevel@tonic-gate )
9990Sstevel@tonic-gate {
10000Sstevel@tonic-gate 	if (np->cname != NULL)
10010Sstevel@tonic-gate 		Free(np->cname);
10020Sstevel@tonic-gate 	if (np->bname != NULL)
10030Sstevel@tonic-gate 		Free(np->bname);
10040Sstevel@tonic-gate 	if (np->rname != NULL)
10050Sstevel@tonic-gate 		Free(np->rname);
10060Sstevel@tonic-gate 	if (np->devicesname != NULL)
10070Sstevel@tonic-gate 		Free(np->devicesname);
10080Sstevel@tonic-gate 	metainitname(np);
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate /*
10120Sstevel@tonic-gate  * initialize drive name
10130Sstevel@tonic-gate  */
10140Sstevel@tonic-gate static void
10150Sstevel@tonic-gate metainitdrivename(
10160Sstevel@tonic-gate 	mddrivename_t	*dnp
10170Sstevel@tonic-gate )
10180Sstevel@tonic-gate {
10190Sstevel@tonic-gate 	(void) memset(dnp, 0, sizeof (*dnp));
10200Sstevel@tonic-gate 	dnp->side_names_key = MD_KEYBAD;
10210Sstevel@tonic-gate }
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate /*
10240Sstevel@tonic-gate  * flush side names
10250Sstevel@tonic-gate  */
10260Sstevel@tonic-gate void
10270Sstevel@tonic-gate metaflushsidenames(
10280Sstevel@tonic-gate 	mddrivename_t	*dnp
10290Sstevel@tonic-gate )
10300Sstevel@tonic-gate {
10310Sstevel@tonic-gate 	mdsidenames_t	*p, *n;
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	for (p = dnp->side_names, n = NULL; (p != NULL); p = n) {
10340Sstevel@tonic-gate 		n = p->next;
10350Sstevel@tonic-gate 		if (p->dname != NULL)
10360Sstevel@tonic-gate 			Free(p->dname);
10370Sstevel@tonic-gate 		if (p->cname != NULL)
10380Sstevel@tonic-gate 			Free(p->cname);
10390Sstevel@tonic-gate 		Free(p);
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate 	dnp->side_names = NULL;
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate /*
10450Sstevel@tonic-gate  * free drive name
10460Sstevel@tonic-gate  */
10470Sstevel@tonic-gate void
10480Sstevel@tonic-gate metafreedrivename(
10490Sstevel@tonic-gate 	mddrivename_t	*dnp
10500Sstevel@tonic-gate )
10510Sstevel@tonic-gate {
10520Sstevel@tonic-gate 	uint_t		slice;
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	if (dnp->cname != NULL)
10550Sstevel@tonic-gate 		Free(dnp->cname);
10560Sstevel@tonic-gate 	if (dnp->rname != NULL)
10570Sstevel@tonic-gate 		Free(dnp->rname);
10580Sstevel@tonic-gate 	metafreevtoc(&dnp->vtoc);
10590Sstevel@tonic-gate 	for (slice = 0; (slice < dnp->parts.parts_len); ++slice)
10600Sstevel@tonic-gate 		metafreename(&dnp->parts.parts_val[slice]);
10610Sstevel@tonic-gate 	if (dnp->parts.parts_val != NULL)
10620Sstevel@tonic-gate 		Free(dnp->parts.parts_val);
10630Sstevel@tonic-gate 	metaflushsidenames(dnp);
10640Sstevel@tonic-gate 	if (dnp->miscname != NULL)
10650Sstevel@tonic-gate 		Free(dnp->miscname);
10660Sstevel@tonic-gate 	meta_free_unit(dnp);
10670Sstevel@tonic-gate 	metainitdrivename(dnp);
10680Sstevel@tonic-gate }
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate /*
10710Sstevel@tonic-gate  * flush the drive name cache
10720Sstevel@tonic-gate  */
1073*1945Sjeanm void
10740Sstevel@tonic-gate metaflushdrivenames()
10750Sstevel@tonic-gate {
10760Sstevel@tonic-gate 	mddrivenamelist_t	*p, *n;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	for (p = drivelistp, n = NULL; (p != NULL); p = n) {
10790Sstevel@tonic-gate 		n = p->next;
10800Sstevel@tonic-gate 		metafreedrivename(p->drivenamep);
10810Sstevel@tonic-gate 		Free(p->drivenamep);
10820Sstevel@tonic-gate 		Free(p);
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 	drivelistp = NULL;
10850Sstevel@tonic-gate }
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate /*
10880Sstevel@tonic-gate  * peel off s%u from name
10890Sstevel@tonic-gate  */
10900Sstevel@tonic-gate char *
10910Sstevel@tonic-gate metadiskname(
10920Sstevel@tonic-gate 	char	*name
10930Sstevel@tonic-gate )
10940Sstevel@tonic-gate {
10950Sstevel@tonic-gate 	char	*p, *e;
10960Sstevel@tonic-gate 	char	onmb[BUFSIZ+1], cnmb[BUFSIZ];
10970Sstevel@tonic-gate 	uint_t	d = 0;
10980Sstevel@tonic-gate 	int	l = 0;
10990Sstevel@tonic-gate 	int	cl = strlen(name);
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	/*
11020Sstevel@tonic-gate 	 * Handle old style names, which are of the form /dev/rXXNN[a-h].
11030Sstevel@tonic-gate 	 */
11040Sstevel@tonic-gate 	if (sscanf(name, "/dev/r%" VAL2STR(BUFSIZ) "[^0-9/]%u%*[a-h]%n",
11050Sstevel@tonic-gate 	    onmb, &d, &l) == 2 && l == cl) {
11060Sstevel@tonic-gate 		(void) snprintf(cnmb, sizeof (cnmb), "/dev/r%s%u", onmb, d);
11070Sstevel@tonic-gate 		return (Strdup(cnmb));
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	/*
11110Sstevel@tonic-gate 	 * Handle old style names, which are of the form /dev/XXNN[a-h].
11120Sstevel@tonic-gate 	 */
11130Sstevel@tonic-gate 	if (sscanf(name, "/dev/%" VAL2STR(BUFSIZ) "[^0-9/]%u%*[a-h]%n",
11140Sstevel@tonic-gate 	    onmb, &d, &l) == 2 && l == cl) {
11150Sstevel@tonic-gate 		(void) snprintf(cnmb, sizeof (cnmb), "/dev/%s%u", onmb, d);
11160Sstevel@tonic-gate 		return (Strdup(cnmb));
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	/* gobble number and 's' */
11200Sstevel@tonic-gate 	p = e = name + strlen(name) - 1;
11210Sstevel@tonic-gate 	for (; (p > name); --p) {
11220Sstevel@tonic-gate 		if (!isdigit(*p))
11230Sstevel@tonic-gate 			break;
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 	if ((p == e) || (p <= name))
11260Sstevel@tonic-gate 		return (Strdup(name));
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	if (*p != 's' && strchr("dt", *p) == NULL)
11290Sstevel@tonic-gate 		return (Strdup(name));
11300Sstevel@tonic-gate 	else if (strchr("dt", *p) != NULL)
11310Sstevel@tonic-gate 		return (Strdup(name));
11320Sstevel@tonic-gate 	p--;
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	if ((p <= name) || (!isdigit(*p)))
11350Sstevel@tonic-gate 		return (Strdup(name));
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	*(++p) = '\0';
11380Sstevel@tonic-gate 	e = Strdup(name);
11390Sstevel@tonic-gate 	*p = 's';
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	return (e);
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate /*
11450Sstevel@tonic-gate  * free list of drivenames
11460Sstevel@tonic-gate  */
11470Sstevel@tonic-gate void
11480Sstevel@tonic-gate metafreedrivenamelist(
11490Sstevel@tonic-gate 	mddrivenamelist_t	*dnlp
11500Sstevel@tonic-gate )
11510Sstevel@tonic-gate {
11520Sstevel@tonic-gate 	mddrivenamelist_t	*next = NULL;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	for (/* void */; (dnlp != NULL); dnlp = next) {
11550Sstevel@tonic-gate 		next = dnlp->next;
11560Sstevel@tonic-gate 		Free(dnlp);
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate /*
11610Sstevel@tonic-gate  * build list of drivenames
11620Sstevel@tonic-gate  */
11630Sstevel@tonic-gate int
11640Sstevel@tonic-gate metadrivenamelist(
11650Sstevel@tonic-gate 	mdsetname_t		**spp,
11660Sstevel@tonic-gate 	mddrivenamelist_t	**dnlpp,
11670Sstevel@tonic-gate 	int			argc,
11680Sstevel@tonic-gate 	char			*argv[],
11690Sstevel@tonic-gate 	md_error_t		*ep
11700Sstevel@tonic-gate )
11710Sstevel@tonic-gate {
11720Sstevel@tonic-gate 	mddrivenamelist_t	**tailpp = dnlpp;
11730Sstevel@tonic-gate 	int			count = 0;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	for (*dnlpp = NULL; (argc > 0); ++count, --argc, ++argv) {
11760Sstevel@tonic-gate 		mddrivenamelist_t	*dnlp = Zalloc(sizeof (*dnlp));
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 		if ((dnlp->drivenamep = metadrivename(spp, argv[0],
11790Sstevel@tonic-gate 		    ep)) == NULL) {
11800Sstevel@tonic-gate 			metafreedrivenamelist(*dnlpp);
11810Sstevel@tonic-gate 			*dnlpp = NULL;
11820Sstevel@tonic-gate 			return (-1);
11830Sstevel@tonic-gate 		}
11840Sstevel@tonic-gate 		*tailpp = dnlp;
11850Sstevel@tonic-gate 		tailpp = &dnlp->next;
11860Sstevel@tonic-gate 	}
11870Sstevel@tonic-gate 	return (count);
11880Sstevel@tonic-gate }
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate /*
11910Sstevel@tonic-gate  * append to end of drivename list
11920Sstevel@tonic-gate  */
11930Sstevel@tonic-gate mddrivename_t *
11940Sstevel@tonic-gate metadrivenamelist_append(
11950Sstevel@tonic-gate 	mddrivenamelist_t	**dnlpp,
11960Sstevel@tonic-gate 	mddrivename_t		*dnp
11970Sstevel@tonic-gate )
11980Sstevel@tonic-gate {
11990Sstevel@tonic-gate 	mddrivenamelist_t	*dnlp;
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	/* run to end of list */
12020Sstevel@tonic-gate 	for (; (*dnlpp != NULL); dnlpp = &(*dnlpp)->next)
12030Sstevel@tonic-gate 		;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	/* allocate new list element */
12060Sstevel@tonic-gate 	dnlp = *dnlpp = Zalloc(sizeof (*dnlp));
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	/* append drivename */
12090Sstevel@tonic-gate 	dnlp->drivenamep = dnp;
12100Sstevel@tonic-gate 	return (dnp);
12110Sstevel@tonic-gate }
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate /*
12140Sstevel@tonic-gate  * FUNCTION:	meta_drivenamelist_append_wrapper()
12150Sstevel@tonic-gate  * INPUT:	tailpp	- pointer to the list tail pointer
12160Sstevel@tonic-gate  *		dnp	- name node to be appended to list
12170Sstevel@tonic-gate  * OUTPUT:	none
12180Sstevel@tonic-gate  * RETURNS:	mddrivenamelist_t * - new tail of the list.
12190Sstevel@tonic-gate  * PURPOSE:	wrapper to meta_namelist_append for performance.
12200Sstevel@tonic-gate  *		metanamelist_append finds the tail each time which slows
12210Sstevel@tonic-gate  *		down long lists.  By keeping track of the tail ourselves
12220Sstevel@tonic-gate  *		we can change metadrivenamelist_append into a
12230Sstevel@tonic-gate  *		constant time operation.
12240Sstevel@tonic-gate  */
12250Sstevel@tonic-gate mddrivenamelist_t **
12260Sstevel@tonic-gate meta_drivenamelist_append_wrapper(
12270Sstevel@tonic-gate 	mddrivenamelist_t	**tailpp,
12280Sstevel@tonic-gate 	mddrivename_t	*dnp
12290Sstevel@tonic-gate )
12300Sstevel@tonic-gate {
12310Sstevel@tonic-gate 	(void) metadrivenamelist_append(tailpp, dnp);
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	/* If it's the first item in the list, return it instead of the next */
12340Sstevel@tonic-gate 	if ((*tailpp)->next == NULL)
12350Sstevel@tonic-gate 		return (tailpp);
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	return (&(*tailpp)->next);
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate /*
12420Sstevel@tonic-gate  *	mdname_t stuff
12430Sstevel@tonic-gate  */
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate /*
12460Sstevel@tonic-gate  * check set and get comparison name
12471623Stw21770  *
12481623Stw21770  * NOTE: This function has a side effect of setting *spp if the setname
12491623Stw21770  * has been specified and *spp is not already set.
12500Sstevel@tonic-gate  */
12510Sstevel@tonic-gate char *
12520Sstevel@tonic-gate meta_name_getname(
12531623Stw21770 	mdsetname_t		**spp,
12541623Stw21770 	char			*uname,
12551623Stw21770 	meta_device_type_t	uname_type,
12561623Stw21770 	md_error_t		*ep
12570Sstevel@tonic-gate )
12580Sstevel@tonic-gate {
12591623Stw21770 	if (uname_type == META_DEVICE || uname_type == HSP_DEVICE ||
12601623Stw21770 	    (uname_type == UNKNOWN && is_existing_metadevice(*spp, uname))) {
12611623Stw21770 
12621623Stw21770 		/*
12631623Stw21770 		 * if the setname is specified in uname, *spp is set,
12641623Stw21770 		 * and the set names don't agree then canonical name will be
12651623Stw21770 		 * returned as NULL
12661623Stw21770 		 */
12671623Stw21770 		return (meta_canonicalize_check_set(spp, uname, ep));
12681623Stw21770 	}
12691623Stw21770 
12701623Stw21770 	/* if it is not a meta/hsp and *spp is not set then set it to local */
12711623Stw21770 	if (chksetname(spp, NULL, ep) != 0)
12720Sstevel@tonic-gate 		return (NULL);
12731623Stw21770 
12741623Stw21770 	/* if it is not a meta/hsp name then just return uname */
12751623Stw21770 	return (Strdup(uname));
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate /*
12790Sstevel@tonic-gate  * FUNCTION:	getrname()
12800Sstevel@tonic-gate  * INPUT:	spp	- the setname struct
12810Sstevel@tonic-gate  *		uname	- the possibly unqualified device name
12821623Stw21770  *		type 	- ptr to the device type of uname
12830Sstevel@tonic-gate  * OUTPUT:	ep	- return error pointer
12840Sstevel@tonic-gate  * RETURNS:	char*	- character string containing the fully
12850Sstevel@tonic-gate  *			qualified raw device name
12860Sstevel@tonic-gate  * PURPOSE:	Create the fully qualified raw name for the possibly
12870Sstevel@tonic-gate  *		unqualified device name.  If uname is an absolute
12880Sstevel@tonic-gate  *		path the raw name is derived from the input string.
12890Sstevel@tonic-gate  *		Otherwise, an attempt is made to get the rawname by
12901623Stw21770  *		catting "/dev/md/rdsk" and "/dev/rdsk". If the input
12911623Stw21770  *		value of type is UNKNOWN and it can be successfully
12921623Stw21770  *		determined then update type to the correct value.
12930Sstevel@tonic-gate  */
12941623Stw21770 static	char *
12951623Stw21770 getrname(mdsetname_t **spp, char *uname,
12961623Stw21770     meta_device_type_t *type, md_error_t *ep)
12970Sstevel@tonic-gate {
12981623Stw21770 	char			*rname,
12991623Stw21770 				*fname;
13001623Stw21770 	int			i;
13011623Stw21770 	int 			rname_cnt = 0;
13021623Stw21770 	char			*rname_list[3];
13031623Stw21770 	meta_device_type_t	tmp_type;
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	assert(uname != NULL);
13060Sstevel@tonic-gate 	/* if it is an absolute name then just call rawname on the input */
13070Sstevel@tonic-gate 	if (uname[0] == '/') {
13081623Stw21770 	    if ((rname = rawname(uname)) != NULL) {
13091623Stw21770 		/*
13101623Stw21770 		 * If the returned rname does not match with
13111623Stw21770 		 * the specified uname type, we'll return null.
13121623Stw21770 		 */
13131623Stw21770 		if (strncmp(rname, "/dev/md", strlen("/dev/md")) == 0) {
13141623Stw21770 			if (*type == LOGICAL_DEVICE) {
13151623Stw21770 				(void) mdsyserror(ep, ENOENT, uname);
13161623Stw21770 				return (NULL);
13171623Stw21770 			}
13181623Stw21770 			*type = META_DEVICE;
13191623Stw21770 		} else {
13201623Stw21770 			if (*type == META_DEVICE) {
13211623Stw21770 				(void) mdsyserror(ep, ENOENT, uname);
13221623Stw21770 				return (NULL);
13231623Stw21770 			}
13241623Stw21770 			*type = LOGICAL_DEVICE;
13251623Stw21770 		}
13260Sstevel@tonic-gate 		return (rname);
13271623Stw21770 	    }
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	    /* out of luck */
13300Sstevel@tonic-gate 	    (void) mdsyserror(ep, ENOENT, uname);
13310Sstevel@tonic-gate 	    return (NULL);
13320Sstevel@tonic-gate 	}
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	/*
13351623Stw21770 	 * Get device that matches the requested type. If
13361623Stw21770 	 * a match is found, return immediately. If type is
13371623Stw21770 	 * UNKNOWN, save all the found devices in rname_list
13381623Stw21770 	 * so we can determine later whether the input uname
13391623Stw21770 	 * is ambiguous.
13401623Stw21770 	 *
13410Sstevel@tonic-gate 	 * Check for metadevice before physical device.
13420Sstevel@tonic-gate 	 * With the introduction of softpartitions it is more
13430Sstevel@tonic-gate 	 * likely to be a metadevice.
13440Sstevel@tonic-gate 	 */
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	/* metadevice short form */
13471623Stw21770 	if (*type == META_DEVICE || *type == UNKNOWN) {
13481623Stw21770 		if (metaislocalset(*spp)) {
13491623Stw21770 			fname = Malloc(strlen(uname) +
13501623Stw21770 			    strlen("/dev/md/rdsk/") + 1);
13511623Stw21770 			(void) strcpy(fname, "/dev/md/rdsk/");
13521623Stw21770 			(void) strcat(fname, uname);
13530Sstevel@tonic-gate 		} else {
13541623Stw21770 			char	*p;
13551623Stw21770 			size_t	len;
13561623Stw21770 
13571623Stw21770 			if ((p = strchr(uname, '/')) != NULL) {
13581623Stw21770 				++p;
13591623Stw21770 			} else {
13601623Stw21770 				p = uname;
13611623Stw21770 			}
13621623Stw21770 			len = strlen((*spp)->setname) + strlen(p) +
13631623Stw21770 			    strlen("/dev/md//rdsk/") + 1;
13641623Stw21770 			fname = Malloc(len);
13651623Stw21770 			(void) snprintf(fname, len, "/dev/md/%s/rdsk/%s",
13661623Stw21770 			    (*spp)->setname, p);
13670Sstevel@tonic-gate 		}
13681623Stw21770 		rname = rawname(fname);
13691623Stw21770 
13701623Stw21770 		if (*type == META_DEVICE) {
13711623Stw21770 			/*
13721623Stw21770 			 * Handle the case where we have a new metadevice
13731623Stw21770 			 * that does not yet exist in the name-space(e.g
13741623Stw21770 			 * metarecover in MN sets where /dev/md entry is
13751623Stw21770 			 * not yet created in the non-master nodes). In
13761623Stw21770 			 * this case we return the constructed metadevice
13771623Stw21770 			 * name as that will exist after the metainit call
13781623Stw21770 			 * has created it.
13791623Stw21770 			 */
13801623Stw21770 			if (rname == NULL) {
13811623Stw21770 				rname = Strdup(fname);
13821623Stw21770 			}
13831623Stw21770 
13841623Stw21770 			Free(fname);
13851623Stw21770 			return (rname);
13861623Stw21770 		}
13871623Stw21770 
13881623Stw21770 		Free(fname);
13891623Stw21770 		if ((rname != NULL) && (*type == UNKNOWN)) {
13901623Stw21770 			/* Save this result */
13911623Stw21770 			rname_list[rname_cnt] = rname;
13921623Stw21770 			rname_cnt ++;
13931623Stw21770 		}
13940Sstevel@tonic-gate 	}
13951623Stw21770 
13961623Stw21770 	if (*type == LOGICAL_DEVICE || *type == UNKNOWN) {
13971623Stw21770 		fname = Malloc(strlen(uname) + strlen("/dev/rdsk/") + 1);
13981623Stw21770 		(void) strcpy(fname, "/dev/rdsk/");
13991623Stw21770 		(void) strcat(fname, uname);
14001623Stw21770 		rname = rawname(fname);
14011623Stw21770 
14021623Stw21770 		Free(fname);
14031623Stw21770 		if (rname != NULL) {
14041623Stw21770 			/* Simply return if a logical device was requested */
14051623Stw21770 			if (*type == LOGICAL_DEVICE) {
14061623Stw21770 				return (rname);
14071623Stw21770 			} else {
14081623Stw21770 				rname_list[rname_cnt] = rname;
14091623Stw21770 				rname_cnt ++;
14101623Stw21770 			}
14111623Stw21770 		}
14120Sstevel@tonic-gate 	}
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	/*
14150Sstevel@tonic-gate 	 * If all else fails try the straight uname.
14160Sstevel@tonic-gate 	 * NOTE: This check was at the beginning of getrname instead
14170Sstevel@tonic-gate 	 * of here. It was moved to avoid a conflict with SC3.0. If
14180Sstevel@tonic-gate 	 * a diskset was mounted with the same name it would hang
14190Sstevel@tonic-gate 	 * the cluster in a loop. Example:
14200Sstevel@tonic-gate 	 *
14210Sstevel@tonic-gate 	 *	fubar/d10 -m fubar/d0 fubar/d1
14220Sstevel@tonic-gate 	 *	mount /dev/md/fubar/dsk/d10 /fubar
14230Sstevel@tonic-gate 	 *
14241623Stw21770 	 * When the system was booted SVM would try to take ownership
14250Sstevel@tonic-gate 	 * of diskset fubar. This would cause rawname("fubar/d10") to be
14260Sstevel@tonic-gate 	 * called. rawname() stats the string which caused the cluster
14270Sstevel@tonic-gate 	 * reservation code to try and take ownership which it was already
14280Sstevel@tonic-gate 	 * doing and a deadlock would occur. By moving this final attempt
14290Sstevel@tonic-gate 	 * at resolving the rawname to the end we avoid this deadlock.
14300Sstevel@tonic-gate 	 */
14311623Stw21770 	if (rname = rawname(uname)) {
14321623Stw21770 		/*
14331623Stw21770 		 * It's only possible to get a logical device from this
14341623Stw21770 		 * rawname call since a metadevice would have been
14351623Stw21770 		 * detected earlier.
14361623Stw21770 		 */
14371623Stw21770 		if (*type == LOGICAL_DEVICE &&
14381623Stw21770 		    (strncmp(rname, "/dev/md/", strlen("/dev/md"))) != 1)
14391623Stw21770 			return (rname);
14401623Stw21770 		else {
14411623Stw21770 			rname_list[rname_cnt] = rname;
14421623Stw21770 			rname_cnt++;
14431623Stw21770 		}
14441623Stw21770 	}
14451623Stw21770 
14461623Stw21770 	/*
14471623Stw21770 	 * At this point, we've searched /dev/md/rdsk, /dev/rdsk and
14481623Stw21770 	 * ./ for the specified device. rname_list contains all
14491623Stw21770 	 * the matches we've found and rname_cnt is the number of
14501623Stw21770 	 * matches.
14511623Stw21770 	 *
14521623Stw21770 	 * We know that either we don't have a match if a specific
14531623Stw21770 	 * type was given, in which case we simply return NULL or
14541623Stw21770 	 * we have an UNKNOWN device with 1-3 entries in rname_list.
14551623Stw21770 	 *
14561623Stw21770 	 * If we get 3 entries, rname_cnt == 3, it's ambiguous.
14571623Stw21770 	 * If we only get 1 entry, rname_cnt == 1, return rname_list[0].
14581623Stw21770 	 * If we get 2 entries that are not the same, it's ambigous.
14591623Stw21770 	 */
14601623Stw21770 	rname = NULL;
14611623Stw21770 	if (rname_cnt == 0 || *type != UNKNOWN) {
14621623Stw21770 		/* out of luck */
14631623Stw21770 		(void) mdsyserror(ep, ENOENT, uname);
14641623Stw21770 		return (NULL);
14651623Stw21770 	} else {
14661623Stw21770 		if (rname_cnt == 3) {
14671623Stw21770 			(void) mderror(ep, MDE_AMBIGUOUS_DEV, uname);
14681623Stw21770 			(void) printf(dgettext(TEXT_DOMAIN,
14691623Stw21770 			    "Error: ambiguous device name.\n%s %s %s\n\n"),
14701623Stw21770 			    rname_list[0], rname_list[1], rname_list[2]);
14711623Stw21770 			rname = NULL;
14721623Stw21770 		}
14731623Stw21770 
14741623Stw21770 		/* grab the type in case it is not ambiguous */
14751623Stw21770 		if (strncmp(rname_list[0], "/dev/md", strlen("/dev/md")) == 0)
14761623Stw21770 			tmp_type =  META_DEVICE;
14771623Stw21770 		else
14781623Stw21770 			tmp_type =  LOGICAL_DEVICE;
14791623Stw21770 
14801623Stw21770 		if (rname_cnt == 1) {
14811623Stw21770 			rname = Strdup(rname_list[0]);
14821623Stw21770 			*type = tmp_type;
14831623Stw21770 		} else {
14841623Stw21770 			/*
14851623Stw21770 			 * Prevent the case where the command is run in
14861623Stw21770 			 * either /dev/md/rdsk or /dev/rdsk so the both
14871623Stw21770 			 * rname_list[0] and rname_list[1] are the same.
14881623Stw21770 			 */
14891623Stw21770 			if (strcmp(rname_list[0], rname_list[1]) != 0) {
14901623Stw21770 				(void) mderror(ep, MDE_AMBIGUOUS_DEV, uname);
14911623Stw21770 				if (rname_cnt != 3) {
14921623Stw21770 					/*
14931623Stw21770 					 * For the rname_cnt == 3 case, the
14941623Stw21770 					 * error was printed above.
14951623Stw21770 					 */
14961623Stw21770 					(void) printf(dgettext(TEXT_DOMAIN,
14971623Stw21770 						"Error: ambiguous device "
14981623Stw21770 						"name.\n%s %s\n\n"),
14991623Stw21770 						rname_list[0], rname_list[1]);
15001623Stw21770 				}
15011623Stw21770 				rname = NULL;
15021623Stw21770 			} else {
15031623Stw21770 				rname = Strdup(rname_list[0]);
15041623Stw21770 				*type = tmp_type;
15051623Stw21770 			}
15061623Stw21770 		}
15071623Stw21770 		for (i = 0; i < rname_cnt; i++)
15081623Stw21770 			Free(rname_list[i]);
15090Sstevel@tonic-gate 		return (rname);
15101623Stw21770 	}
15110Sstevel@tonic-gate }
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate /*
15140Sstevel@tonic-gate  * get raw slice and drive names
15150Sstevel@tonic-gate  */
15160Sstevel@tonic-gate static char *
15170Sstevel@tonic-gate getrawnames(
15181623Stw21770 	mdsetname_t		**spp,
15191623Stw21770 	char			*uname,
15201623Stw21770 	char			**dnamep,
15211623Stw21770 	meta_device_type_t	*uname_type,
15221623Stw21770 	md_error_t		*ep
15230Sstevel@tonic-gate )
15240Sstevel@tonic-gate {
15251623Stw21770 	char		*rname = NULL;
15260Sstevel@tonic-gate 	size_t		len;
15270Sstevel@tonic-gate 
15281623Stw21770 	/*
15291623Stw21770 	 * Incorrect code path if type is HSP_DEVICE
15301623Stw21770 	 */
15311623Stw21770 	assert(*uname_type != HSP_DEVICE);
15321623Stw21770 
15330Sstevel@tonic-gate 	/* initialize */
15340Sstevel@tonic-gate 	*dnamep = NULL;
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	/* get slice name */
15371623Stw21770 	if ((rname = getrname(spp, uname, uname_type, ep)) != NULL) {
15380Sstevel@tonic-gate 		*dnamep = metadiskname(rname);
15390Sstevel@tonic-gate 		return (rname);
15400Sstevel@tonic-gate 	}
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	/*
15430Sstevel@tonic-gate 	 * If name cannot be found, if may be because is is not accessible.
15440Sstevel@tonic-gate 	 * If it is an absolute name, try all possible disk name formats and
15451623Stw21770 	 * if it is device name, assume it is /dev/rdsk/..
15461623Stw21770 	 * Since the code below assumes logical devices, if the given
15471623Stw21770 	 * uname_type is META_DEVICE, there's nothing to do.
15480Sstevel@tonic-gate 	 */
15491623Stw21770 	if (mdissyserror(ep, ENOENT) && *uname_type != META_DEVICE) {
15500Sstevel@tonic-gate 		if (uname[0] == '/') {
15510Sstevel@tonic-gate 			/* Absolute name */
15520Sstevel@tonic-gate 			char			*p;
15530Sstevel@tonic-gate 			uint_t			d = 0;
15540Sstevel@tonic-gate 			int			l = 0;
15550Sstevel@tonic-gate 			char			onmb[BUFSIZ+1], snm[BUFSIZ+1];
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 			/*
15580Sstevel@tonic-gate 			 * Handle old style raw names
15590Sstevel@tonic-gate 			 */
15600Sstevel@tonic-gate 			if (sscanf(uname,
15610Sstevel@tonic-gate 			    "/dev/r%" VAL2STR(BUFSIZ) "[^0-9/]%u"
15620Sstevel@tonic-gate 			    "%" VAL2STR(BUFSIZ) "[a-h]%n",
15630Sstevel@tonic-gate 			    onmb, &d, snm, &l) == 3 && l == strlen(uname)) {
15640Sstevel@tonic-gate 				mdclrerror(ep);
15650Sstevel@tonic-gate 				rname = Strdup(uname);
15660Sstevel@tonic-gate 				*dnamep = metadiskname(rname);
15671623Stw21770 				*uname_type = LOGICAL_DEVICE;
15680Sstevel@tonic-gate 				return (rname);
15690Sstevel@tonic-gate 			}
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 			/*
15720Sstevel@tonic-gate 			 * Handle old style block names
15730Sstevel@tonic-gate 			 */
15740Sstevel@tonic-gate 			if (sscanf(uname,
15750Sstevel@tonic-gate 			    "/dev/%" VAL2STR(BUFSIZ) "[^0-9/]%u"
15760Sstevel@tonic-gate 			    "%" VAL2STR(BUFSIZ) "[a-h]%n",
15770Sstevel@tonic-gate 			    onmb, &d, snm, &l) == 3 && l == strlen(uname)) {
15780Sstevel@tonic-gate 				len = strlen(uname) + 1 + 1;
15790Sstevel@tonic-gate 				rname = Malloc(len);
15800Sstevel@tonic-gate 				(void) snprintf(rname, len, "/dev/r%s%u%s",
15810Sstevel@tonic-gate 				    onmb, d, snm);
15820Sstevel@tonic-gate 				*dnamep = metadiskname(rname);
15831623Stw21770 				*uname_type = LOGICAL_DEVICE;
15840Sstevel@tonic-gate 				return (rname);
15850Sstevel@tonic-gate 			}
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 			/* /.../dsk/... */
15880Sstevel@tonic-gate 			if ((p = strstr(uname, "/dsk/")) != NULL) {
15890Sstevel@tonic-gate 				mdclrerror(ep);
15900Sstevel@tonic-gate 				++p;
15910Sstevel@tonic-gate 				rname = Malloc(strlen(uname) + 1 + 1);
15920Sstevel@tonic-gate 				(void) strncpy(rname, uname, (p - uname));
15930Sstevel@tonic-gate 				rname[(p - uname)] = 'r';
15940Sstevel@tonic-gate 				(void) strcpy(&rname[(p - uname) + 1], p);
15950Sstevel@tonic-gate 				*dnamep = metadiskname(rname);
15961623Stw21770 				*uname_type = LOGICAL_DEVICE;
15970Sstevel@tonic-gate 				return (rname);
15980Sstevel@tonic-gate 			}
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 			/* /.../rdsk/... */
16010Sstevel@tonic-gate 			else if (strstr(uname, "/rdsk/") != NULL) {
16020Sstevel@tonic-gate 				mdclrerror(ep);
16030Sstevel@tonic-gate 				rname = Strdup(uname);
16040Sstevel@tonic-gate 				*dnamep = metadiskname(rname);
16051623Stw21770 				*uname_type = LOGICAL_DEVICE;
16060Sstevel@tonic-gate 				return (rname);
16070Sstevel@tonic-gate 			}
16080Sstevel@tonic-gate 		} else {
16090Sstevel@tonic-gate 			/*
16100Sstevel@tonic-gate 			 * If it's not an absolute name but is a valid ctd name,
16110Sstevel@tonic-gate 			 * guess at /dev/rdsk/...
16120Sstevel@tonic-gate 			 */
16130Sstevel@tonic-gate 			uint_t	s;
16140Sstevel@tonic-gate 			if (parse_ctd(uname, &s) == 0) {
16150Sstevel@tonic-gate 				len = strlen(uname) + strlen("/dev/rdsk/") + 1;
16160Sstevel@tonic-gate 				rname = Malloc(len);
16170Sstevel@tonic-gate 				(void) snprintf(rname, len, "/dev/rdsk/%s",
16180Sstevel@tonic-gate 				    uname);
16190Sstevel@tonic-gate 				*dnamep = metadiskname(rname);
16201623Stw21770 				*uname_type = LOGICAL_DEVICE;
16210Sstevel@tonic-gate 				return (rname);
16220Sstevel@tonic-gate 			}
16230Sstevel@tonic-gate 		}
16240Sstevel@tonic-gate 	}
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	/* out of luck */
16271623Stw21770 	if (!mdiserror(ep, MDE_AMBIGUOUS_DEV))
16281623Stw21770 		(void) mderror(ep, MDE_UNIT_NOT_FOUND, uname);
16290Sstevel@tonic-gate 	return (NULL);
16300Sstevel@tonic-gate }
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate /*
16330Sstevel@tonic-gate  * get number of slices for name
16340Sstevel@tonic-gate  */
16350Sstevel@tonic-gate static int
16360Sstevel@tonic-gate getnslice(
16370Sstevel@tonic-gate 	char		*rname,
16380Sstevel@tonic-gate 	char		*dname,
16390Sstevel@tonic-gate 	uint_t		*slicep
16400Sstevel@tonic-gate )
16410Sstevel@tonic-gate {
16420Sstevel@tonic-gate 	char		*srname;
16430Sstevel@tonic-gate 	uint_t		nslice;
16440Sstevel@tonic-gate 	size_t		dl = strlen(dname);
16450Sstevel@tonic-gate 	size_t		rl = strlen(rname);
16460Sstevel@tonic-gate 	size_t		l = 0;
16470Sstevel@tonic-gate 	size_t		len;
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	/*
16500Sstevel@tonic-gate 	 * get our slice number - works only with names that end in s%u -
16510Sstevel@tonic-gate 	 * all others return -1.
16520Sstevel@tonic-gate 	 */
16530Sstevel@tonic-gate 	if (dl >= rl ||
16540Sstevel@tonic-gate 	    sscanf(&rname[dl], "s%u%n", slicep, &l) != 1 || l != rl ||
16550Sstevel@tonic-gate 	    (int)*slicep < 0) {
16560Sstevel@tonic-gate 		return (-1);
16570Sstevel@tonic-gate 	}
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	/*
16600Sstevel@tonic-gate 	 * go find how many slices there really are
16610Sstevel@tonic-gate 	 */
16620Sstevel@tonic-gate 	len = strlen(dname) + 20 + 1;
16630Sstevel@tonic-gate 	srname = Malloc(len);
16640Sstevel@tonic-gate 	for (nslice = 0; /* void */; ++nslice) {
16650Sstevel@tonic-gate 		struct stat	statbuf;
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 		/* build slice name */
16680Sstevel@tonic-gate 		(void) snprintf(srname, len, "%ss%u", dname, nslice);
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 		/* see if it's there */
16710Sstevel@tonic-gate 		if ((meta_stat(srname, &statbuf) != 0) ||
16720Sstevel@tonic-gate 		    (! S_ISCHR(statbuf.st_mode))) {
16730Sstevel@tonic-gate 			break;
16740Sstevel@tonic-gate 		}
16750Sstevel@tonic-gate 	}
16760Sstevel@tonic-gate 	Free(srname);
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	/* Need to make sure that we at least have V_NUMPAR */
16790Sstevel@tonic-gate 	nslice = max(nslice, V_NUMPAR);
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	/* make sure we have at least our slice */
16820Sstevel@tonic-gate 	if (nslice < *slicep)
16830Sstevel@tonic-gate 		return (-1);
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 	/* return number of slices */
16860Sstevel@tonic-gate 	return (nslice);
16870Sstevel@tonic-gate }
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate /*
16900Sstevel@tonic-gate  * Attempt to parse the input string as a c[t]ds specifier
16910Sstevel@tonic-gate  * The target can either be a SCSI target id or if the device
16920Sstevel@tonic-gate  * is in a fabric configuration in a fibre channel setup then
16930Sstevel@tonic-gate  * the target is a standard WWN (world wide name).
16940Sstevel@tonic-gate  *
16950Sstevel@tonic-gate  * if successful	return 0
16960Sstevel@tonic-gate  * if c[t]dp name	return 1
16970Sstevel@tonic-gate  * otherwise		return -1
16980Sstevel@tonic-gate  */
16990Sstevel@tonic-gate int
17000Sstevel@tonic-gate parse_ctd(
17010Sstevel@tonic-gate 	char	*uname,
17020Sstevel@tonic-gate 	uint_t	*slice)
17030Sstevel@tonic-gate {
17040Sstevel@tonic-gate 	uint_t	channel;
17050Sstevel@tonic-gate 	uint_t	target;
17060Sstevel@tonic-gate 	uint_t	device;
17070Sstevel@tonic-gate 	int	has_target = 1;
17080Sstevel@tonic-gate 	uint_t	cl;
17090Sstevel@tonic-gate 	uint_t	target_str_len;
17100Sstevel@tonic-gate 	char	*partial_ctd_str;
17110Sstevel@tonic-gate 	char	*target_str;
17120Sstevel@tonic-gate 	char	*device_start_pos;
17130Sstevel@tonic-gate 	int	l = -1;
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	/* pull off the channel spec and the 't' for the target */
17160Sstevel@tonic-gate 	if (sscanf(uname, "c%ut%n", &channel, &l) != 1 || l == -1) {
17170Sstevel@tonic-gate 		/* check for cds style name */
17180Sstevel@tonic-gate 		if (sscanf(uname, "c%ud%n", &channel, &l) != 1 || l == -1) {
17190Sstevel@tonic-gate 			return (-1);
17200Sstevel@tonic-gate 		} else {
17210Sstevel@tonic-gate 			l--;	/* we want to be on the 'd' */
17220Sstevel@tonic-gate 			has_target = 0;
17230Sstevel@tonic-gate 		}
17240Sstevel@tonic-gate 	}
17250Sstevel@tonic-gate 	partial_ctd_str = uname + l;
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	/* find the beginning of the device specifier */
17280Sstevel@tonic-gate 	device_start_pos = strrchr(partial_ctd_str, 'd');
17290Sstevel@tonic-gate 	if (device_start_pos == NULL) {
17300Sstevel@tonic-gate 		return (-1);
17310Sstevel@tonic-gate 	}
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 	/* check to see if it is a ctd with a WWN or SCSI target */
17340Sstevel@tonic-gate 	if (has_target) {
17350Sstevel@tonic-gate 		/* pull off the target and see if it is a WWN */
17360Sstevel@tonic-gate 		target_str_len = device_start_pos - partial_ctd_str + 2;
17370Sstevel@tonic-gate 		target_str = (char *)Malloc(target_str_len+1);
17380Sstevel@tonic-gate 		(void) strcpy(target_str, "0X");
17390Sstevel@tonic-gate 		(void) strncpy(target_str+2, partial_ctd_str,
17400Sstevel@tonic-gate 		    target_str_len - 2);
17410Sstevel@tonic-gate 		target_str[target_str_len] = '\0';
17420Sstevel@tonic-gate 		if (sscanf(target_str, "%x%n", &target, &l) != 1 ||
17430Sstevel@tonic-gate 		    l != target_str_len) {
17440Sstevel@tonic-gate 			Free(target_str);
17450Sstevel@tonic-gate 			return (-1);
17460Sstevel@tonic-gate 		}
17470Sstevel@tonic-gate 		Free(target_str);
17480Sstevel@tonic-gate 	}
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	/* check the device and slice */
17510Sstevel@tonic-gate 	cl = strlen(device_start_pos);
17520Sstevel@tonic-gate 	if (sscanf(device_start_pos, "d%us%u%n", &device, slice, &l) != 2 ||
17530Sstevel@tonic-gate 			l != cl) {
17540Sstevel@tonic-gate 		/* check the device and partition */
17550Sstevel@tonic-gate 		if (sscanf(device_start_pos, "d%up%u%n", &device, slice, &l)
17560Sstevel@tonic-gate 		    == 2 && l == cl) {
17570Sstevel@tonic-gate 			return (1);
17580Sstevel@tonic-gate 		}
17590Sstevel@tonic-gate 		return (-1);
17600Sstevel@tonic-gate 	}
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	return (0);
17630Sstevel@tonic-gate }
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate /*
17670Sstevel@tonic-gate  * get number of slices for name
17680Sstevel@tonic-gate  */
17690Sstevel@tonic-gate static int
17700Sstevel@tonic-gate uname2sliceno(
17711623Stw21770 	char			*uname,
17721623Stw21770 	meta_device_type_t	uname_type,
17731623Stw21770 	uint_t			*slicep,
17741623Stw21770 	md_error_t		*ep
17750Sstevel@tonic-gate )
17760Sstevel@tonic-gate {
17770Sstevel@tonic-gate 	uint_t			c = 0, t = 0, d = 0;
17780Sstevel@tonic-gate 	int			l = 0, cl = 0;
17790Sstevel@tonic-gate 	int			fd;
17800Sstevel@tonic-gate 	struct dk_cinfo		cinfo;
17810Sstevel@tonic-gate 	char			*p;
17820Sstevel@tonic-gate 	char			*rname = NULL;
17830Sstevel@tonic-gate 
17841623Stw21770 
17851623Stw21770 	if (uname_type == META_DEVICE)
17860Sstevel@tonic-gate 		return (*slicep = 0);
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 	if ((p = strrchr(uname, '/')) != NULL)
17890Sstevel@tonic-gate 		p++;
17900Sstevel@tonic-gate 	else
17910Sstevel@tonic-gate 		p = uname;
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	cl = strlen(p);
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	if (parse_ctd(p, slicep) == 0)
17960Sstevel@tonic-gate 		return (*slicep);
17970Sstevel@tonic-gate 	else if (sscanf(p, "mc%ut%ud%us%u%n", &c, &t, &d, slicep, &l) == 4 &&
17980Sstevel@tonic-gate 	    l == cl)
17990Sstevel@tonic-gate 		return (*slicep);
18000Sstevel@tonic-gate 	else if (sscanf(p, "d%us%u%n", &d, slicep, &l) == 2 && l == cl)
18010Sstevel@tonic-gate 		return (*slicep);
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	/*
18040Sstevel@tonic-gate 	 * If we can't get the slice from the name, then we have to do it the
18050Sstevel@tonic-gate 	 * hard and expensive way.
18060Sstevel@tonic-gate 	 */
18070Sstevel@tonic-gate 	if ((rname = rawname(uname)) == NULL)
18080Sstevel@tonic-gate 		return (-1);
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 	/* get controller info */
18110Sstevel@tonic-gate 	if ((fd = open(rname, (O_RDONLY|O_NDELAY), 0)) < 0) {
18120Sstevel@tonic-gate 		Free(rname);
18130Sstevel@tonic-gate 		return (-1);
18140Sstevel@tonic-gate 	}
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, &cinfo) != 0) {
18170Sstevel@tonic-gate 		int	save = errno;
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 		if (save == ENOTTY)
18200Sstevel@tonic-gate 			(void) mddeverror(ep, MDE_NOT_DISK, NODEV64, rname);
18210Sstevel@tonic-gate 		else
18220Sstevel@tonic-gate 			(void) mdsyserror(ep, save, rname);
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 		Free(rname);
18250Sstevel@tonic-gate 		(void) close(fd);
18260Sstevel@tonic-gate 		return (-1);
18270Sstevel@tonic-gate 	}
18280Sstevel@tonic-gate 	(void) close(fd);	/* sd/ssd bug */
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 	if (cinfo.dki_partition < V_NUMPAR) {
18310Sstevel@tonic-gate 		Free(rname);
18320Sstevel@tonic-gate 		return (*slicep = cinfo.dki_partition);
18330Sstevel@tonic-gate 	}
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	return (mddeverror(ep, MDE_NOT_DISK, NODEV64, rname));
18360Sstevel@tonic-gate }
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate /*
18390Sstevel@tonic-gate  * get partition info
18400Sstevel@tonic-gate  */
18410Sstevel@tonic-gate static int
18420Sstevel@tonic-gate getparts(
18431623Stw21770 	mddrivename_t		*dnp,
18441623Stw21770 	char			*rname,
18451623Stw21770 	char			*dname,
18461623Stw21770 	meta_device_type_t	uname_type,
18471623Stw21770 	uint_t			*npartsp,
18481623Stw21770 	uint_t			*partnop,
18491623Stw21770 	md_error_t		*ep
18500Sstevel@tonic-gate )
18510Sstevel@tonic-gate {
18520Sstevel@tonic-gate 	int		nparts;
18530Sstevel@tonic-gate 	uint_t		partno;
18540Sstevel@tonic-gate 	mdname_t	name;
18550Sstevel@tonic-gate 	mdvtoc_t	*vtocp;
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 	/* metadevice */
18581623Stw21770 	if (uname_type == META_DEVICE) {
18590Sstevel@tonic-gate 		dnp->type = MDT_META;
18600Sstevel@tonic-gate 		nparts = 1;
18610Sstevel@tonic-gate 		partno = 0;
18620Sstevel@tonic-gate 		goto gotit;
18630Sstevel@tonic-gate 	}
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	/* see how many partitions in drive, this is really tricky */
18660Sstevel@tonic-gate 	metainitname(&name);
18670Sstevel@tonic-gate 	name.rname = rname;
18680Sstevel@tonic-gate 	name.drivenamep = dnp;
18690Sstevel@tonic-gate 	if ((vtocp = metagetvtoc(&name, TRUE, &partno, ep)) != NULL) {
18700Sstevel@tonic-gate 		dnp->type = MDT_COMP;
18710Sstevel@tonic-gate 		nparts = vtocp->nparts;
18720Sstevel@tonic-gate 		/* partno already setup */
18730Sstevel@tonic-gate 		/* dname already setup */
18740Sstevel@tonic-gate 		goto gotit;
18750Sstevel@tonic-gate 	}
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 	if ((ep->info.errclass == MDEC_DEV) &&
18780Sstevel@tonic-gate 	    (ep->info.md_error_info_t_u.dev_error.errnum == MDE_TOO_MANY_PARTS))
18790Sstevel@tonic-gate 		return (-1);
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 	/* fallback and try and guess (used to check for just EACCES here) */
18820Sstevel@tonic-gate 	if ((dname != NULL) &&
18830Sstevel@tonic-gate 	    ((nparts = getnslice(rname, dname, &partno)) > 0)) {
18840Sstevel@tonic-gate 		dnp->type = MDT_ACCES;
18850Sstevel@tonic-gate 		if (mdanysyserror(ep)) {
18860Sstevel@tonic-gate 			dnp->errnum =
18870Sstevel@tonic-gate 			    ep->info.md_error_info_t_u.sys_error.errnum;
18880Sstevel@tonic-gate 		} else {
18890Sstevel@tonic-gate 			dnp->errnum = ENOENT;
18900Sstevel@tonic-gate 		}
18910Sstevel@tonic-gate 		mdclrerror(ep);
18920Sstevel@tonic-gate 		/* nparts already setup */
18930Sstevel@tonic-gate 		/* partno already setup */
18940Sstevel@tonic-gate 		/* dname already setup */
18950Sstevel@tonic-gate 		nparts = roundup(nparts, V_NUMPAR);
18960Sstevel@tonic-gate 		goto gotit;
18970Sstevel@tonic-gate 	}
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 	/* nothing worked */
19000Sstevel@tonic-gate 	dnp->type = MDT_UNKNOWN;
19010Sstevel@tonic-gate 	if (mdissyserror(ep, EACCES))
19020Sstevel@tonic-gate 		dnp->type = MDT_ACCES;
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 	if (mdanysyserror(ep)) {
19050Sstevel@tonic-gate 		dnp->errnum = ep->info.md_error_info_t_u.sys_error.errnum;
19060Sstevel@tonic-gate 	} else {
19070Sstevel@tonic-gate 		dnp->errnum = ENOENT;
19080Sstevel@tonic-gate 	}
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 	mdclrerror(ep);
19110Sstevel@tonic-gate 	nparts = V_NUMPAR;
19121623Stw21770 	if (uname2sliceno(rname, uname_type, &partno, ep) < 0) {
19130Sstevel@tonic-gate 		mdclrerror(ep);
19140Sstevel@tonic-gate 		partno = 0;
19150Sstevel@tonic-gate 	}
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	/* return success */
19180Sstevel@tonic-gate gotit:
19190Sstevel@tonic-gate 	assert(nparts > 0);
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	if (partno >= nparts)
19220Sstevel@tonic-gate 		return (mdsyserror(ep, ENOENT, rname));
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	*npartsp = nparts;
19250Sstevel@tonic-gate 	*partnop = partno;
19260Sstevel@tonic-gate 	return (0);
19270Sstevel@tonic-gate }
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate /*
19300Sstevel@tonic-gate  * get block name
19310Sstevel@tonic-gate  */
19320Sstevel@tonic-gate static int
19330Sstevel@tonic-gate getbname(
19340Sstevel@tonic-gate 	mdname_t	*np,
19350Sstevel@tonic-gate 	md_error_t	*ep
19360Sstevel@tonic-gate )
19370Sstevel@tonic-gate {
19380Sstevel@tonic-gate 	char		*rname = np->rname;
19390Sstevel@tonic-gate 	char		*bname;
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	/* fully qualified */
19420Sstevel@tonic-gate 	assert(rname != NULL);
19430Sstevel@tonic-gate 	if ((bname = blkname(rname)) != NULL) {
19440Sstevel@tonic-gate 		if (np->bname)
19450Sstevel@tonic-gate 			Free(np->bname);
19460Sstevel@tonic-gate 		np->bname = bname;
19470Sstevel@tonic-gate 		return (0);
19480Sstevel@tonic-gate 	}
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 	/* out of luck */
19510Sstevel@tonic-gate 	return (mdsyserror(ep, ENOENT, rname));
19520Sstevel@tonic-gate }
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate static void
19550Sstevel@tonic-gate getcname(
19560Sstevel@tonic-gate 	mdsetname_t	*sp,
19570Sstevel@tonic-gate 	mdname_t	*np
19580Sstevel@tonic-gate )
19590Sstevel@tonic-gate {
19600Sstevel@tonic-gate 	char		*sname = sp->setname;
19610Sstevel@tonic-gate 	char		*bname = np->bname;
19620Sstevel@tonic-gate 	char		*p;
19630Sstevel@tonic-gate 	size_t		len;
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	assert(sname != NULL);
19660Sstevel@tonic-gate 	assert(bname != NULL);
19670Sstevel@tonic-gate 	assert(np->drivenamep->type != MDT_FAST_COMP &&
19680Sstevel@tonic-gate 	    np->drivenamep->type != MDT_FAST_META);
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate 	/* regular device */
19710Sstevel@tonic-gate 	if ((strncmp(bname, "/dev/dsk/", strlen("/dev/dsk/")) == 0) &&
19720Sstevel@tonic-gate 	    (strchr((p = bname + strlen("/dev/dsk/")), '/') == NULL)) {
19730Sstevel@tonic-gate 		if (np->cname)
19740Sstevel@tonic-gate 			Free(np->cname);
19750Sstevel@tonic-gate 		np->cname = Strdup(p);
19760Sstevel@tonic-gate 		return;
19770Sstevel@tonic-gate 	}
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 	if ((strncmp(bname, "/dev/ap/dsk/", strlen("/dev/ap/dsk/")) == 0) &&
19800Sstevel@tonic-gate 	    (strchr((p = bname + strlen("/dev/ap/dsk/")), '/') == NULL)) {
19810Sstevel@tonic-gate 		if (np->cname)
19820Sstevel@tonic-gate 			Free(np->cname);
19830Sstevel@tonic-gate 		np->cname = Strdup(p);
19840Sstevel@tonic-gate 		return;
19850Sstevel@tonic-gate 	}
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 	if ((strncmp(bname, "/dev/did/dsk/", strlen("/dev/did/dsk/")) == 0) &&
19880Sstevel@tonic-gate 	    (strchr((p = bname + strlen("/dev/did/dsk/")), '/') == NULL)) {
19890Sstevel@tonic-gate 		if (np->cname)
19900Sstevel@tonic-gate 			Free(np->cname);
19910Sstevel@tonic-gate 		np->cname = Strdup(p);
19920Sstevel@tonic-gate 		return;
19930Sstevel@tonic-gate 	}
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	/* anything else but metadevice */
19960Sstevel@tonic-gate 	if (np->drivenamep->type != MDT_META) {
19970Sstevel@tonic-gate 		if (np->cname)
19980Sstevel@tonic-gate 			Free(np->cname);
19990Sstevel@tonic-gate 		np->cname = Strdup(bname);
20000Sstevel@tonic-gate 		return;
20010Sstevel@tonic-gate 	}
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	/* metadevice */
20040Sstevel@tonic-gate 	p = strrchr(bname, '/');
20050Sstevel@tonic-gate 	assert(p != NULL);
20060Sstevel@tonic-gate 	++p;
20070Sstevel@tonic-gate 	if (metaislocalset(sp)) {
20080Sstevel@tonic-gate 		if (np->cname)
20090Sstevel@tonic-gate 			Free(np->cname);
20100Sstevel@tonic-gate 		np->cname = Strdup(p);
20110Sstevel@tonic-gate 	} else {
20120Sstevel@tonic-gate 		assert(sname[0] != '\0');
20130Sstevel@tonic-gate 		if (np->cname)
20140Sstevel@tonic-gate 			Free(np->cname);
20150Sstevel@tonic-gate 		len = strlen(sname) + 1 + strlen(p) + 1;
20160Sstevel@tonic-gate 		np->cname = Malloc(len);
20170Sstevel@tonic-gate 		(void) snprintf(np->cname, len, "%s/%s", sname, p);
20180Sstevel@tonic-gate 	}
20190Sstevel@tonic-gate }
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate /*
20220Sstevel@tonic-gate  * get dev
20230Sstevel@tonic-gate  */
20240Sstevel@tonic-gate int
20250Sstevel@tonic-gate meta_getdev(
20260Sstevel@tonic-gate 	mdsetname_t	*sp,
20270Sstevel@tonic-gate 	mdname_t	*np,
20280Sstevel@tonic-gate 	md_error_t	*ep
20290Sstevel@tonic-gate )
20300Sstevel@tonic-gate {
20310Sstevel@tonic-gate 	struct stat	statbuf;
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 	/* get dev */
20340Sstevel@tonic-gate 	if (meta_stat(np->rname, &statbuf) != 0)
20350Sstevel@tonic-gate 		return (mdsyserror(ep, errno, np->rname));
20360Sstevel@tonic-gate 	else if (! S_ISCHR(statbuf.st_mode))
20370Sstevel@tonic-gate 		return (mddeverror(ep, MDE_NOT_DISK, NODEV64, np->rname));
20380Sstevel@tonic-gate 	np->dev = meta_expldev(statbuf.st_rdev);
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 	assert(np->drivenamep->type != MDT_FAST_META &&
20410Sstevel@tonic-gate 	    np->drivenamep->type != MDT_FAST_COMP);
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate 	/* check set */
20440Sstevel@tonic-gate 	assert((np->drivenamep->type == MDT_META) ?
20450Sstevel@tonic-gate 	    (sp->setno == MD_MIN2SET(meta_getminor(np->dev))) : 1);
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 	/* return sucess */
20480Sstevel@tonic-gate 	return (0);
20490Sstevel@tonic-gate }
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate /*
20520Sstevel@tonic-gate  * set up names for a slice
20530Sstevel@tonic-gate  */
20540Sstevel@tonic-gate static int
20550Sstevel@tonic-gate getnames(
20560Sstevel@tonic-gate 	mdsetname_t	*sp,
20570Sstevel@tonic-gate 	mdname_t	*np,
20580Sstevel@tonic-gate 	char		*rname,
20590Sstevel@tonic-gate 	md_error_t	*ep
20600Sstevel@tonic-gate )
20610Sstevel@tonic-gate {
20620Sstevel@tonic-gate 	/* get names */
20630Sstevel@tonic-gate 	if (np->rname)
20640Sstevel@tonic-gate 		Free(np->rname);
20650Sstevel@tonic-gate 	np->rname = Strdup(rname);
20660Sstevel@tonic-gate 	if (getbname(np, ep) != 0)
20670Sstevel@tonic-gate 		return (-1);
20680Sstevel@tonic-gate 	getcname(sp, np);
20690Sstevel@tonic-gate 	if (meta_getdev(sp, np, ep) != 0)
20700Sstevel@tonic-gate 		return (-1);
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 	/* return success */
20730Sstevel@tonic-gate 	return (0);
20740Sstevel@tonic-gate }
20750Sstevel@tonic-gate 
20760Sstevel@tonic-gate /*
20770Sstevel@tonic-gate  * fake up names for a slice
20780Sstevel@tonic-gate  */
20790Sstevel@tonic-gate static void
20800Sstevel@tonic-gate getfakenames(
20810Sstevel@tonic-gate 	mdsetname_t	*sp,
20820Sstevel@tonic-gate 	mdname_t	*np,
20830Sstevel@tonic-gate 	char		*rname
20840Sstevel@tonic-gate )
20850Sstevel@tonic-gate {
20860Sstevel@tonic-gate 	char		*p;
20870Sstevel@tonic-gate 	char		onmb[BUFSIZ+1], snm[BUFSIZ+1];
20880Sstevel@tonic-gate 	uint_t		d = 0;
20890Sstevel@tonic-gate 	int		l = 0;
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 	/* fake names */
20920Sstevel@tonic-gate 	if (np->rname != NULL)
20930Sstevel@tonic-gate 		Free(np->rname);
20940Sstevel@tonic-gate 	np->rname = Strdup(rname);
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate 	if (np->bname != NULL)
20970Sstevel@tonic-gate 		Free(np->bname);
20980Sstevel@tonic-gate 	np->bname = Strdup(rname);
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	/*
21010Sstevel@tonic-gate 	 * Fixup old style names
21020Sstevel@tonic-gate 	 */
21030Sstevel@tonic-gate 	if (sscanf(rname, "/dev/r%" VAL2STR(BUFSIZ) "[^0-9/]%u"
21040Sstevel@tonic-gate 	    "%" VAL2STR(BUFSIZ) "[a-h]%n",
21050Sstevel@tonic-gate 	    onmb, &d, snm, &l) == 3 && l == strlen(rname))
21060Sstevel@tonic-gate 		(void) snprintf(np->bname, l, "/dev/%s%u%s", onmb, d, snm);
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	/*
21090Sstevel@tonic-gate 	 * Fixup new style names
21100Sstevel@tonic-gate 	 */
21110Sstevel@tonic-gate 	if ((p = strstr(np->bname, "/rdsk/")) != NULL) {
21120Sstevel@tonic-gate 		for (++p; (*(p + 1) != '\0'); ++p)
21130Sstevel@tonic-gate 			*p = *(p + 1);
21140Sstevel@tonic-gate 		*p = '\0';
21150Sstevel@tonic-gate 	}
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	if (np->cname != NULL)
21180Sstevel@tonic-gate 		Free(np->cname);
21190Sstevel@tonic-gate 	getcname(sp, np);
21200Sstevel@tonic-gate }
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate static mdname_t *
21230Sstevel@tonic-gate setup_slice(
21241623Stw21770 	mdsetname_t		*sp,
21251623Stw21770 	meta_device_type_t	uname_type,
21261623Stw21770 	mddrivename_t		*dnp,
21271623Stw21770 	char			*uname,
21281623Stw21770 	char			*rname,
21291623Stw21770 	char			*dname,
21301623Stw21770 	uint_t			partno,
21311623Stw21770 	md_error_t		*ep
21320Sstevel@tonic-gate )
21330Sstevel@tonic-gate {
21341623Stw21770 	char			*srname = NULL;
21351623Stw21770 	mdname_t		*np;
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 	/* must have a set */
21380Sstevel@tonic-gate 	assert(sp != NULL);
21390Sstevel@tonic-gate 	assert(partno < dnp->parts.parts_len);
21400Sstevel@tonic-gate 	assert(dname != NULL);
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 	np = &dnp->parts.parts_val[partno];
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate 	if (rname)
21450Sstevel@tonic-gate 		srname = rname;
21461623Stw21770 	else if (uname_type == META_DEVICE)
21470Sstevel@tonic-gate 		srname = dname;
21480Sstevel@tonic-gate 	else {
21490Sstevel@tonic-gate 		char	onmb[BUFSIZ+1];
21500Sstevel@tonic-gate 		uint_t	d = 0;
21510Sstevel@tonic-gate 		int	l = 0, cl = strlen(dname);
21520Sstevel@tonic-gate 		size_t	len;
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 		len = cl + 20 + 1;
21550Sstevel@tonic-gate 		srname = Malloc(len);
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 		/*
21580Sstevel@tonic-gate 		 * Handle /dev/rXXNN.
21590Sstevel@tonic-gate 		 */
21600Sstevel@tonic-gate 		if (sscanf(dname, "/dev/r%" VAL2STR(BUFSIZ) "[^0-9/]%u%n",
21610Sstevel@tonic-gate 		    onmb, &d, &l) == 2 && l == cl) {
21620Sstevel@tonic-gate 			(void) snprintf(srname, len, "/dev/r%s%u%c", onmb, d,
21630Sstevel@tonic-gate 			    'a' + partno);
21640Sstevel@tonic-gate 		} else if (sscanf(dname, "/dev/%" VAL2STR(BUFSIZ) "[^0-9/]%u%n",
21650Sstevel@tonic-gate 		    onmb, &d, &l) == 2 && l == cl) {
21660Sstevel@tonic-gate 			    (void) snprintf(srname, len, "/dev/%s%u%c", onmb, d,
21670Sstevel@tonic-gate 				'a' + partno);
21680Sstevel@tonic-gate 		} else {
21690Sstevel@tonic-gate 			/* build the slice that is wanted */
21700Sstevel@tonic-gate 			(void) snprintf(srname, len, "%ss%u", dname, partno);
21710Sstevel@tonic-gate 		}
21720Sstevel@tonic-gate 	}
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 	if (getnames(sp, np, srname, ep) != 0) {
21750Sstevel@tonic-gate 		if (dnp->type == MDT_UNKNOWN) {
21760Sstevel@tonic-gate 			mdclrerror(ep);
21770Sstevel@tonic-gate 			getfakenames(sp, np, srname);
21780Sstevel@tonic-gate 		} else if (dnp->type == MDT_COMP && mdissyserror(ep, ENOENT)) {
21790Sstevel@tonic-gate 			dnp->type = MDT_UNKNOWN;
21800Sstevel@tonic-gate 			if (mdanysyserror(ep)) {
21810Sstevel@tonic-gate 				dnp->errnum =
21820Sstevel@tonic-gate 				    ep->info.md_error_info_t_u.sys_error.errnum;
21830Sstevel@tonic-gate 			} else {
21840Sstevel@tonic-gate 				dnp->errnum = ENOENT;
21850Sstevel@tonic-gate 			}
21860Sstevel@tonic-gate 			mdclrerror(ep);
21870Sstevel@tonic-gate 			getfakenames(sp, np, srname);
21880Sstevel@tonic-gate 		} else {
21890Sstevel@tonic-gate 			mdclrerror(ep);
21900Sstevel@tonic-gate 			if (getnames(sp, np, dname, ep) != 0) {
21910Sstevel@tonic-gate 				np = NULL;
21920Sstevel@tonic-gate 				goto fixup;
21930Sstevel@tonic-gate 			}
21940Sstevel@tonic-gate 		}
21950Sstevel@tonic-gate 	}
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate out:
21980Sstevel@tonic-gate 	if ((srname != rname) && (srname != dname))
21990Sstevel@tonic-gate 		Free(srname);
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate 	/* return name */
22020Sstevel@tonic-gate 	return (np);
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate fixup:
22050Sstevel@tonic-gate 	if (mdanysyserror(ep)) {
22060Sstevel@tonic-gate 		char	*p;
22070Sstevel@tonic-gate 		int	errnum = ep->info.md_error_info_t_u.sys_error.errnum;
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 		mdclrerror(ep);
22100Sstevel@tonic-gate 		if (uname && *uname) {
22110Sstevel@tonic-gate 			if ((p = strrchr(uname, '/')) != NULL)
22120Sstevel@tonic-gate 				(void) mdsyserror(ep, errnum, ++p);
22130Sstevel@tonic-gate 			else
22140Sstevel@tonic-gate 				(void) mdsyserror(ep, errnum, uname);
22150Sstevel@tonic-gate 		} else {
22160Sstevel@tonic-gate 			if ((p = strrchr(srname, '/')) != NULL)
22170Sstevel@tonic-gate 				(void) mdsyserror(ep, errnum, ++p);
22180Sstevel@tonic-gate 			else
22190Sstevel@tonic-gate 				(void) mdsyserror(ep, errnum, srname);
22200Sstevel@tonic-gate 		}
22210Sstevel@tonic-gate 	}
22220Sstevel@tonic-gate 	goto out;
22230Sstevel@tonic-gate }
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate /*
22260Sstevel@tonic-gate  * flush the fast name cache
22270Sstevel@tonic-gate  */
22280Sstevel@tonic-gate static void
22290Sstevel@tonic-gate metafreefastnm(mdname_t **np)
22300Sstevel@tonic-gate {
22310Sstevel@tonic-gate 	mddrivename_t	*dnp;
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate 	assert(np != NULL && *np != NULL);
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 	if ((dnp = (*np)->drivenamep) != NULL) {
22360Sstevel@tonic-gate 		if (dnp->cname != NULL)
22370Sstevel@tonic-gate 			Free(dnp->cname);
22380Sstevel@tonic-gate 		if (dnp->rname != NULL)
22390Sstevel@tonic-gate 			Free(dnp->rname);
22400Sstevel@tonic-gate 		if (dnp->miscname != NULL)
22410Sstevel@tonic-gate 			Free(dnp->miscname);
22420Sstevel@tonic-gate 		meta_free_unit(dnp);
22430Sstevel@tonic-gate 		Free(dnp);
22440Sstevel@tonic-gate 	}
22450Sstevel@tonic-gate 	if ((*np)->cname != NULL)
22460Sstevel@tonic-gate 		Free((*np)->cname);
22470Sstevel@tonic-gate 	if ((*np)->bname != NULL)
22480Sstevel@tonic-gate 		Free((*np)->bname);
22490Sstevel@tonic-gate 	if ((*np)->rname != NULL)
22500Sstevel@tonic-gate 		Free((*np)->rname);
22510Sstevel@tonic-gate 	if ((*np)->devicesname != NULL)
22520Sstevel@tonic-gate 		Free((*np)->devicesname);
22530Sstevel@tonic-gate 	Free(*np);
22540Sstevel@tonic-gate 	*np = NULL;
22550Sstevel@tonic-gate }
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate /*
22580Sstevel@tonic-gate  * flush the fast name cache
22590Sstevel@tonic-gate  */
22600Sstevel@tonic-gate static void
22610Sstevel@tonic-gate metaflushfastnames()
22620Sstevel@tonic-gate {
22630Sstevel@tonic-gate 	mdnamelist_t	*p, *n;
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 	for (p = fastnmlp, n = NULL; (p != NULL); p = n) {
22660Sstevel@tonic-gate 		n = p->next;
22670Sstevel@tonic-gate 		metafreefastnm(&p->namep);
22680Sstevel@tonic-gate 		Free(p);
22690Sstevel@tonic-gate 	}
22700Sstevel@tonic-gate 	fastnmlp = NULL;
22710Sstevel@tonic-gate }
22720Sstevel@tonic-gate static char *
22731623Stw21770 getrname_fast(char *unm, meta_device_type_t uname_type, md_error_t *ep)
22740Sstevel@tonic-gate {
22750Sstevel@tonic-gate 	uint_t			d = 0;
22760Sstevel@tonic-gate 	int			l = 0;
22770Sstevel@tonic-gate 	int			cl = strlen(unm);
22780Sstevel@tonic-gate 	char			onmb[BUFSIZ+1], snm[BUFSIZ+1], cnmb[BUFSIZ];
22790Sstevel@tonic-gate 	char			*rnm;
22800Sstevel@tonic-gate 	char			*p;
22810Sstevel@tonic-gate 	size_t			len;
22820Sstevel@tonic-gate 
22831623Stw21770 	if (uname_type == META_DEVICE) {
22841623Stw21770 		/* fully qualified  - local set */
22851623Stw21770 		if (((sscanf(unm, "/dev/md/dsk/%" VAL2STR(BUFSIZ) "s%n",
22861623Stw21770 				onmb, &len) == 1) && (cl == len)) ||
22871623Stw21770 		    ((sscanf(unm, "/dev/md/rdsk/%" VAL2STR(BUFSIZ) "s%n",
22881623Stw21770 				onmb, &len) == 1) && (cl == len))) {
22891623Stw21770 			len = strlen("/dev/md/rdsk/") +	strlen(onmb) + 1;
22901623Stw21770 			rnm = Zalloc(len);
22911623Stw21770 			(void) snprintf(rnm, len, "/dev/md/rdsk/%s", onmb);
22921623Stw21770 			return (rnm);
22931623Stw21770 		}
22941623Stw21770 
22951623Stw21770 		/* fully qualified - setname specified */
22961623Stw21770 		if (((sscanf(unm, "/dev/md/%[^/]/dsk/%"
22971623Stw21770 				VAL2STR(BUFSIZ) "s%n",
22981623Stw21770 				snm, onmb, &len) == 2) && (cl == len)) ||
22991623Stw21770 		    ((sscanf(unm, "/dev/md/%[^/]/rdsk/%"
23001623Stw21770 				VAL2STR(BUFSIZ) "s%n",
23011623Stw21770 				snm, onmb, &len) == 2) && (cl == len))) {
23021623Stw21770 
23031623Stw21770 			len = strlen("/dev/md//rdsk/") + strlen(snm) +
23041623Stw21770 				strlen(onmb) + 1;
23051623Stw21770 			rnm = Zalloc(len);
23061623Stw21770 			(void) snprintf(rnm, len, "/dev/md/%s/rdsk/%s",
23071623Stw21770 			    snm, onmb);
23080Sstevel@tonic-gate 			return (rnm);
23090Sstevel@tonic-gate 		}
23100Sstevel@tonic-gate 
23111623Stw21770 		/* Fully qualified path - error */
23121623Stw21770 		if (unm[0] == '/') {
23131623Stw21770 			(void) mdsyserror(ep, EINVAL, unm);
23141623Stw21770 			return (NULL);
23151623Stw21770 		}
23161623Stw21770 
23171623Stw21770 		/* setname specified <setname>/<metadev> */
23181623Stw21770 		if (((sscanf(unm, "%[^/]/%" VAL2STR(BUFSIZ) "s%n",
23191623Stw21770 				snm, onmb, &len) == 2) && (cl == len))) {
23201623Stw21770 			/* Not <setname>/<metadev>  - error */
23211623Stw21770 			if (strchr(onmb, '/') != NULL) {
23221623Stw21770 				(void) mdsyserror(ep, EINVAL, unm);
23231623Stw21770 				return (NULL);
23241623Stw21770 			}
23251623Stw21770 
23261623Stw21770 			len = strlen("/dev/md//rdsk/") + strlen(snm) +
23271623Stw21770 				strlen(onmb) + 1;
23281623Stw21770 			rnm = Zalloc(len);
23291623Stw21770 			(void) snprintf(rnm, len, "/dev/md/%s/rdsk/%s",
23301623Stw21770 			    snm, onmb);
23310Sstevel@tonic-gate 			return (rnm);
23320Sstevel@tonic-gate 		}
23330Sstevel@tonic-gate 
23341623Stw21770 		/* Must be simple metaname/hsp pool name */
23351623Stw21770 		len = strlen("/dev/md/rdsk/") + strlen(unm) + 1;
23361623Stw21770 		rnm = Zalloc(len);
23371623Stw21770 		(void) snprintf(rnm, len, "/dev/md/rdsk/%s", unm);
23381623Stw21770 		return (rnm);
23390Sstevel@tonic-gate 	}
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 	/* NOT Fully qualified path, done */
23420Sstevel@tonic-gate 	if (unm[0] != '/') {
23430Sstevel@tonic-gate 		(void) mdsyserror(ep, EINVAL, unm);
23440Sstevel@tonic-gate 		return (NULL);
23450Sstevel@tonic-gate 	}
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	/*
23480Sstevel@tonic-gate 	 * Get slice information from old style names of the form
23490Sstevel@tonic-gate 	 * /dev/rXXNN[a-h] or /dev/XXNN[a-h], must be done before regular
23500Sstevel@tonic-gate 	 * devices, but after metadevices.
23510Sstevel@tonic-gate 	 */
23520Sstevel@tonic-gate 	if ((sscanf(unm, "/dev/r%" VAL2STR(BUFSIZ) "[^0-9/]%u"
23530Sstevel@tonic-gate 	    "%" VAL2STR(BUFSIZ) "[a-h]%n",
23540Sstevel@tonic-gate 	    onmb, &d, snm, &l) == 3 ||
23550Sstevel@tonic-gate 	    sscanf(unm, "/dev/%" VAL2STR(BUFSIZ) "[^0-9/]%u"
23560Sstevel@tonic-gate 	    "%" VAL2STR(BUFSIZ) "[a-h]%n",
23570Sstevel@tonic-gate 	    onmb, &d, snm, &l) == 3) && l == cl) {
23580Sstevel@tonic-gate 		if ((p = strchr("abcdefgh", snm[0])) != NULL) {
23590Sstevel@tonic-gate 			(void) snprintf(cnmb, sizeof (cnmb), "/dev/r%s%u%s",
23600Sstevel@tonic-gate 			    onmb, d, snm);
23610Sstevel@tonic-gate 			return (Strdup(cnmb));
23620Sstevel@tonic-gate 		}
23630Sstevel@tonic-gate 	}
23640Sstevel@tonic-gate 
23650Sstevel@tonic-gate 	if ((p = strstr(unm, "/dsk/")) != NULL) {	/* /.../dsk/... */
23660Sstevel@tonic-gate 		++p;
23670Sstevel@tonic-gate 		rnm = Zalloc(strlen(unm) + 1 + 1);
23680Sstevel@tonic-gate 		(void) strncpy(rnm, unm, (p - unm));
23690Sstevel@tonic-gate 		rnm[(p - unm)] = 'r';
23700Sstevel@tonic-gate 		(void) strcpy(&rnm[(p - unm) + 1], p);
23710Sstevel@tonic-gate 		return (rnm);
23720Sstevel@tonic-gate 	} else if (strstr(unm, "/rdsk/") != NULL) {	/* /.../rdsk/... */
23730Sstevel@tonic-gate 		return (Strdup(unm));
23740Sstevel@tonic-gate 	}
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate 	/*
23770Sstevel@tonic-gate 	 * Shouldn't get here but if we do then we have an unrecognized
23780Sstevel@tonic-gate 	 * fully qualified path - error
23790Sstevel@tonic-gate 	 */
23800Sstevel@tonic-gate 	(void) mdsyserror(ep, EINVAL, unm);
23810Sstevel@tonic-gate 	return (NULL);
23820Sstevel@tonic-gate }
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate static mdname_t *
23850Sstevel@tonic-gate metainitfastname(
23861623Stw21770 	mdsetname_t		*sp,
23871623Stw21770 	char			*uname,
23881623Stw21770 	meta_device_type_t	uname_type,
23891623Stw21770 	md_error_t		*ep
23900Sstevel@tonic-gate )
23910Sstevel@tonic-gate {
23920Sstevel@tonic-gate 	uint_t			c = 0, t = 0, d = 0, s = 0;
23930Sstevel@tonic-gate 	int			l = 0;
23940Sstevel@tonic-gate 	mddrivename_t		*dnp;
23950Sstevel@tonic-gate 	mdname_t		*np;
23960Sstevel@tonic-gate 	mdnamelist_t		**fnlpp;
23971623Stw21770 	char			*cname;
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	for (fnlpp = &fastnmlp; (*fnlpp != NULL); fnlpp = &(*fnlpp)->next) {
24000Sstevel@tonic-gate 		np = (*fnlpp)->namep;
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate 		if (strcmp(np->bname, uname) == 0)
24030Sstevel@tonic-gate 			return (np);
24040Sstevel@tonic-gate 	}
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 	*fnlpp = Zalloc(sizeof (**fnlpp));
24070Sstevel@tonic-gate 	np = (*fnlpp)->namep = Zalloc(sizeof (mdname_t));
24080Sstevel@tonic-gate 	metainitname(np);
24090Sstevel@tonic-gate 	dnp = np->drivenamep = Zalloc(sizeof (mddrivename_t));
24100Sstevel@tonic-gate 	metainitdrivename(dnp);
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate 	/* Metadevices */
24141623Stw21770 	if (uname_type == META_DEVICE &&
24151623Stw21770 	    (cname = meta_canonicalize(sp, uname)) != NULL) {
24161623Stw21770 
24171623Stw21770 		np->cname = cname;
24180Sstevel@tonic-gate 		dnp->type = MDT_FAST_META;
24190Sstevel@tonic-gate 		goto done;
24200Sstevel@tonic-gate 	}
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate 	/* Others */
24230Sstevel@tonic-gate 	dnp->type = MDT_FAST_COMP;
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 	if (((sscanf(uname, "/dev/rdsk/c%ut%ud%us%u%n", &c, &t, &d,
24260Sstevel@tonic-gate 		&s, &l) == 4 ||
24270Sstevel@tonic-gate 	    sscanf(uname, "/dev/dsk/c%ut%ud%us%u%n", &c, &t, &d,
24280Sstevel@tonic-gate 		&s, &l) == 4 ||
24290Sstevel@tonic-gate 	    sscanf(uname, "/dev/ap/rdsk/mc%ut%ud%us%u%n", &c, &t, &d,
24300Sstevel@tonic-gate 		&s, &l) == 4 ||
24310Sstevel@tonic-gate 	    sscanf(uname, "/dev/ap/dsk/mc%ut%ud%us%u%n", &c, &t, &d,
24320Sstevel@tonic-gate 		&s, &l) == 4 ||
24330Sstevel@tonic-gate 	    sscanf(uname, "/dev/did/rdsk/d%us%u%n", &t, &s, &l) == 2 ||
24340Sstevel@tonic-gate 	    sscanf(uname, "/dev/did/dsk/d%us%u%n", &t, &s, &l) == 2||
24350Sstevel@tonic-gate 	    sscanf(uname, "/dev/rdsk/c%ud%us%u%n", &c, &d, &s, &l) == 3 ||
24360Sstevel@tonic-gate 	    sscanf(uname, "/dev/dsk/c%ud%us%u%n", &c, &d, &s, &l) == 3 ||
24370Sstevel@tonic-gate 	    sscanf(uname, "/dev/rdsk/c%ut%ud%u%n", &c, &t, &d, &l) == 3 ||
24380Sstevel@tonic-gate 	    sscanf(uname, "/dev/dsk/c%ut%ud%u%n", &c, &t, &d, &l) == 3 ||
24390Sstevel@tonic-gate 	    sscanf(uname, "/dev/ap/rdsk/mc%ut%ud%u%n", &c, &t, &d, &l) == 3 ||
24400Sstevel@tonic-gate 	    sscanf(uname, "/dev/ap/dsk/mc%ut%ud%u%n", &c, &t, &d, &l) == 3 ||
24410Sstevel@tonic-gate 	    sscanf(uname, "/dev/did/rdsk/d%u%n", &t, &l) == 1 ||
24420Sstevel@tonic-gate 	    sscanf(uname, "/dev/did/dsk/d%u%n", &t, &l) == 1 ||
24430Sstevel@tonic-gate 	    sscanf(uname, "/dev/rdsk/c%ud%u%n", &c, &d, &l) == 2 ||
24440Sstevel@tonic-gate 	    sscanf(uname, "/dev/dsk/c%ud%u%n", &c, &d, &l) == 2) &&
24450Sstevel@tonic-gate 		l == strlen(uname))) {
24460Sstevel@tonic-gate 		if ((np->cname = strrchr(uname, '/')) == NULL)
24470Sstevel@tonic-gate 			np->cname = Strdup(uname);
24480Sstevel@tonic-gate 		else
24490Sstevel@tonic-gate 			np->cname = Strdup(++np->cname);
24500Sstevel@tonic-gate 	} else {
24510Sstevel@tonic-gate 		np->cname = Strdup(uname);
24520Sstevel@tonic-gate 	}
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate done:
24550Sstevel@tonic-gate 	/* Driver always gives us block names */
24560Sstevel@tonic-gate 	np->bname = Strdup(uname);
24570Sstevel@tonic-gate 
24580Sstevel@tonic-gate 	/* canonical disk name */
24590Sstevel@tonic-gate 	if ((dnp->cname = metadiskname(np->cname)) == NULL)
24600Sstevel@tonic-gate 		dnp->cname = Strdup(np->cname);
24610Sstevel@tonic-gate 
24621623Stw21770 	if ((np->rname = getrname_fast(uname, uname_type, ep)) != NULL) {
24630Sstevel@tonic-gate 		if ((dnp->rname = metadiskname(np->rname)) == NULL)
24640Sstevel@tonic-gate 			dnp->rname = Strdup(np->rname);
24650Sstevel@tonic-gate 	} else {
24660Sstevel@tonic-gate 		metafreefastnm(&(*fnlpp)->namep);
24670Sstevel@tonic-gate 		Free(*fnlpp);
24680Sstevel@tonic-gate 		*fnlpp = NULL;
24690Sstevel@tonic-gate 		return (NULL);
24700Sstevel@tonic-gate 	}
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 	/* cleanup, return success */
24730Sstevel@tonic-gate 	return (np);
24740Sstevel@tonic-gate }
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate /*
24770Sstevel@tonic-gate  * set up names for a device
24780Sstevel@tonic-gate  */
24790Sstevel@tonic-gate static mdname_t *
24800Sstevel@tonic-gate metaname_common(
24810Sstevel@tonic-gate 	mdsetname_t	**spp,
24820Sstevel@tonic-gate 	char		*uname,
24830Sstevel@tonic-gate 	int		fast,
24841623Stw21770 	meta_device_type_t	uname_type,
24850Sstevel@tonic-gate 	md_error_t	*ep
24860Sstevel@tonic-gate )
24870Sstevel@tonic-gate {
24880Sstevel@tonic-gate 	mddrivenamelist_t	**tail;
24890Sstevel@tonic-gate 	mddrivename_t		*dnp;
24900Sstevel@tonic-gate 	uint_t			slice;
24910Sstevel@tonic-gate 	mdname_t		*np;
24920Sstevel@tonic-gate 	char			*rname = NULL;
24930Sstevel@tonic-gate 	char			*dname = NULL;
24940Sstevel@tonic-gate 	char			*cname = NULL;
24950Sstevel@tonic-gate 	uint_t			nparts, partno;
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate 	assert(uname != NULL);
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 	/* check setname */
25001623Stw21770 	if ((cname = meta_name_getname(spp, uname, uname_type, ep)) == NULL)
25010Sstevel@tonic-gate 		return (NULL);
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 	assert(*spp != NULL);
25040Sstevel@tonic-gate 	Free(cname);
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 	/* get raw name (rname) of the slice and drive (dname) we have */
25071623Stw21770 	if ((rname = getrawnames(spp, uname,
25081623Stw21770 				&dname, &uname_type, ep)) == NULL) {
25090Sstevel@tonic-gate 		return (NULL);
25100Sstevel@tonic-gate 	}
25110Sstevel@tonic-gate 
25121623Stw21770 	assert(uname_type != UNKNOWN);
25131623Stw21770 
25140Sstevel@tonic-gate 	/* look in cache first */
25150Sstevel@tonic-gate 	for (tail = &drivelistp; (*tail != NULL); tail = &(*tail)->next) {
25160Sstevel@tonic-gate 		dnp = (*tail)->drivenamep;
25170Sstevel@tonic-gate 
25180Sstevel@tonic-gate 		/* check to see if the drive name is already in the cache */
25190Sstevel@tonic-gate 		if ((dnp->rname != NULL) && strcmp(dnp->rname, dname) == 0) {
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 			Free(rname);
25220Sstevel@tonic-gate 			if (dname != NULL)
25230Sstevel@tonic-gate 				Free(dname);
25240Sstevel@tonic-gate 
25251623Stw21770 			if (uname2sliceno(uname, uname_type, &partno, ep) < 0)
25260Sstevel@tonic-gate 				return (NULL);
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 			return (metaslicename(dnp, partno, ep));
25290Sstevel@tonic-gate 		}
25300Sstevel@tonic-gate 	}
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 	/*
25330Sstevel@tonic-gate 	 * If a fast names is OK, then get one, and be done.
25340Sstevel@tonic-gate 	 */
25350Sstevel@tonic-gate 	if (fast) {
25360Sstevel@tonic-gate 		Free(rname);
25370Sstevel@tonic-gate 		if (dname != NULL)
25380Sstevel@tonic-gate 			Free(dname);
25390Sstevel@tonic-gate 
25401623Stw21770 		return (metainitfastname(*spp, uname, uname_type, ep));
25410Sstevel@tonic-gate 	}
25420Sstevel@tonic-gate 
25430Sstevel@tonic-gate 	/* allocate new list element and drive */
25440Sstevel@tonic-gate 	*tail = Zalloc(sizeof (**tail));
25450Sstevel@tonic-gate 	dnp = (*tail)->drivenamep = Zalloc(sizeof (*dnp));
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate 	metainitdrivename(dnp);
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 	/* get parts info */
25501623Stw21770 	if (getparts(dnp, rname, dname, uname_type, &nparts, &partno, ep) != 0)
25510Sstevel@tonic-gate 		goto out;
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 	/*
25540Sstevel@tonic-gate 	 * libmeta needs at least V_NUMPAR partitions.
25550Sstevel@tonic-gate 	 * If we have an EFI partition with less than V_NUMPAR slices,
25560Sstevel@tonic-gate 	 * we nevertheless reserve space for V_NUMPAR
25570Sstevel@tonic-gate 	 */
25580Sstevel@tonic-gate 	if (nparts < V_NUMPAR) {
25590Sstevel@tonic-gate 		nparts = V_NUMPAR;
25600Sstevel@tonic-gate 	}
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	/* allocate and link in parts */
25630Sstevel@tonic-gate 	dnp->parts.parts_len = nparts;
25640Sstevel@tonic-gate 	dnp->parts.parts_val = Zalloc((sizeof (*dnp->parts.parts_val)) *
25650Sstevel@tonic-gate 	    dnp->parts.parts_len);
25660Sstevel@tonic-gate 	for (slice = 0; (slice < nparts); ++slice) {
25670Sstevel@tonic-gate 		np = &dnp->parts.parts_val[slice];
25680Sstevel@tonic-gate 		metainitname(np);
25690Sstevel@tonic-gate 		np->drivenamep = dnp;
25700Sstevel@tonic-gate 	}
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate 	/* setup name_t (or slice) wanted */
25731623Stw21770 	if ((np = setup_slice(*spp, uname_type, dnp, uname, rname,
25741623Stw21770 	    dname, partno, ep)) == NULL)
25750Sstevel@tonic-gate 		goto out;
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 	/* canonical disk name */
25780Sstevel@tonic-gate 	if ((dnp->cname = metadiskname(np->cname)) == NULL)
25790Sstevel@tonic-gate 		dnp->cname = Strdup(np->cname);
25800Sstevel@tonic-gate 	if ((dnp->rname = metadiskname(np->rname)) == NULL)
25810Sstevel@tonic-gate 		dnp->rname = Strdup(np->rname);
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 	/* cleanup, return success */
25840Sstevel@tonic-gate 	if (dname != NULL)
25850Sstevel@tonic-gate 		Free(dname);
25860Sstevel@tonic-gate 	Free(rname);
25870Sstevel@tonic-gate 	return (np);
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 	/* cleanup, return error */
25900Sstevel@tonic-gate out:
25910Sstevel@tonic-gate 	if (dname != NULL)
25920Sstevel@tonic-gate 		Free(dname);
25930Sstevel@tonic-gate 	if (rname != NULL)
25940Sstevel@tonic-gate 		Free(rname);
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 	metafreedrivename(dnp);
25970Sstevel@tonic-gate 	Free(dnp);
25980Sstevel@tonic-gate 	Free(*tail);
25990Sstevel@tonic-gate 	*tail = NULL;
26000Sstevel@tonic-gate 	return (NULL);
26010Sstevel@tonic-gate }
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate mdname_t *
26040Sstevel@tonic-gate metaname(
26050Sstevel@tonic-gate 	mdsetname_t	**spp,
26060Sstevel@tonic-gate 	char		*uname,
26071623Stw21770 	meta_device_type_t	uname_type,
26080Sstevel@tonic-gate 	md_error_t	*ep
26090Sstevel@tonic-gate )
26100Sstevel@tonic-gate {
26111623Stw21770 	return (metaname_common(spp, uname, 0, uname_type, ep));
26120Sstevel@tonic-gate }
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate mdname_t *
26150Sstevel@tonic-gate metaname_fast(
26160Sstevel@tonic-gate 	mdsetname_t	**spp,
26170Sstevel@tonic-gate 	char		*uname,
26181623Stw21770 	meta_device_type_t	uname_type,
26190Sstevel@tonic-gate 	md_error_t	*ep
26200Sstevel@tonic-gate )
26210Sstevel@tonic-gate {
26221623Stw21770 	return (metaname_common(spp, uname, 1, uname_type, ep));
26230Sstevel@tonic-gate }
2624*1945Sjeanm /*
2625*1945Sjeanm  * Get the dnp using the device id.
2626*1945Sjeanm  *
2627*1945Sjeanm  * We have the potential to have more than 1 dnp with the same disk name but
2628*1945Sjeanm  * have different device ids. This would happen in the case of a partial
2629*1945Sjeanm  * diskset. The unavailable disk name is relative to the prior host and could
2630*1945Sjeanm  * possibly be the same as a disk on this system. The only way to tell which
2631*1945Sjeanm  * dnp belongs with this disk is by searching by device id. We have the
2632*1945Sjeanm  * potential to have the case where 1) the disk who's device id we pass in is
2633*1945Sjeanm  * in the system. In this case the name and the device id are both valid for
2634*1945Sjeanm  * the disk. 2) The disk whose device id we've been passed is not in the
2635*1945Sjeanm  * system and no disk with the same name has a dnp on the list. And 3) The
2636*1945Sjeanm  * disk whose device id we've been passed is not on the system but there is
2637*1945Sjeanm  * a disk with the same name (different devid) that is on the system. Here's
2638*1945Sjeanm  * what we return for each of those cases:
2639*1945Sjeanm  * 1) If disk is in system:
2640*1945Sjeanm  * 	disk is found on drivelistp or we create a new drivename and it's
2641*1945Sjeanm  * 	fully populated as expected.
2642*1945Sjeanm  * 2) If disk not in system, no collision
2643*1945Sjeanm  *	Disk with the same devid is not found on drivelistp, we create a new
2644*1945Sjeanm  *	drivename structure and the dnp->devid is filled in not from getparts
2645*1945Sjeanm  *	but from the devidp passed in. No other disk in the system has the
2646*1945Sjeanm  *	same "name" or devid.
2647*1945Sjeanm  *	This situation would be caused by the import of a partial diskset.
2648*1945Sjeanm  * 3) If disk not in system, collision
2649*1945Sjeanm  *	Disk with the same devid is not found on the drivelistp, we create a
2650*1945Sjeanm  *	new drivename struct but getparts will use the information from the
2651*1945Sjeanm  *	name which is actually in reference to another disk of the same name
2652*1945Sjeanm  *	in the system. getparts will fill in the dnp->devid with the value
2653*1945Sjeanm  *	from the other disk and	we overwrite this with the value of this disk.
2654*1945Sjeanm  *	To get into this situation one of the disks is actually unavailable
2655*1945Sjeanm  *	as in the case of a partial import.
2656*1945Sjeanm  */
2657*1945Sjeanm mddrivename_t *
2658*1945Sjeanm meta_getdnp_bydevid(
2659*1945Sjeanm 	mdsetname_t	*sp,
2660*1945Sjeanm 	side_t		sideno,
2661*1945Sjeanm 	ddi_devid_t	devidp,
2662*1945Sjeanm 	mdkey_t		key,
2663*1945Sjeanm 	md_error_t	*ep
2664*1945Sjeanm )
2665*1945Sjeanm {
2666*1945Sjeanm 	ddi_devid_t		dnp_devidp;
2667*1945Sjeanm 	char			*nm;
2668*1945Sjeanm 	mddrivenamelist_t	**tail;
2669*1945Sjeanm 	mddrivename_t		*dnp;
2670*1945Sjeanm 	uint_t			slice;
2671*1945Sjeanm 	mdname_t		*np;
2672*1945Sjeanm 	char			*rname = NULL;
2673*1945Sjeanm 	char			*dname = NULL;
2674*1945Sjeanm 	uint_t			nparts, partno;
2675*1945Sjeanm 	int			ret;
2676*1945Sjeanm 	md_set_desc		*sd = NULL;
2677*1945Sjeanm 	meta_device_type_t	uname_type = LOGICAL_DEVICE;
2678*1945Sjeanm 
2679*1945Sjeanm 	/* look in the cache first */
2680*1945Sjeanm 	for (tail = &drivelistp; (*tail != NULL); tail = &(*tail)->next) {
2681*1945Sjeanm 		dnp = (*tail)->drivenamep;
2682*1945Sjeanm 		if (dnp->type != MDT_COMP)
2683*1945Sjeanm 			continue;
2684*1945Sjeanm 		ret = devid_str_decode(dnp->devid, &dnp_devidp, NULL);
2685*1945Sjeanm 		if (ret != 0) {
2686*1945Sjeanm 			/* unable to decode the devid */
2687*1945Sjeanm 			return (NULL);
2688*1945Sjeanm 		}
2689*1945Sjeanm 		/* compare with the devid passed in. */
2690*1945Sjeanm 		if (devid_compare(devidp, dnp_devidp) == 0) {
2691*1945Sjeanm 			/* match! We have the same disk */
2692*1945Sjeanm 			devid_free(dnp_devidp);
2693*1945Sjeanm 			return (dnp);
2694*1945Sjeanm 		}
2695*1945Sjeanm 		devid_free(dnp_devidp);
2696*1945Sjeanm 	}
2697*1945Sjeanm 
2698*1945Sjeanm 	/* drive not in the cache */
2699*1945Sjeanm 
2700*1945Sjeanm 	if ((sd = metaget_setdesc(sp, ep)) == NULL) {
2701*1945Sjeanm 		return (NULL);
2702*1945Sjeanm 	}
2703*1945Sjeanm 	/* get namespace info */
2704*1945Sjeanm 	if (MD_MNSET_DESC(sd)) {
2705*1945Sjeanm 		if ((nm = meta_getnmbykey(MD_LOCAL_SET, sideno,
2706*1945Sjeanm 		    key, ep)) == NULL)
2707*1945Sjeanm 			return (NULL);
2708*1945Sjeanm 	} else {
2709*1945Sjeanm 		if ((nm = meta_getnmbykey(MD_LOCAL_SET,
2710*1945Sjeanm 		    sideno+SKEW, key, ep)) == NULL)
2711*1945Sjeanm 			return (NULL);
2712*1945Sjeanm 	}
2713*1945Sjeanm 
2714*1945Sjeanm 	/* get raw name (rname) of the slice and drive name (dname) */
2715*1945Sjeanm 	if ((rname = getrawnames(&sp, nm, &dname, &uname_type, ep)) == NULL) {
2716*1945Sjeanm 		return (NULL);
2717*1945Sjeanm 	}
2718*1945Sjeanm 
2719*1945Sjeanm 	/* allocate new list element and drive */
2720*1945Sjeanm 	*tail = Zalloc(sizeof (**tail));
2721*1945Sjeanm 	dnp = (*tail)->drivenamep = Zalloc(sizeof (*dnp));
2722*1945Sjeanm 	metainitdrivename(dnp);
2723*1945Sjeanm 
2724*1945Sjeanm 	/* get parts info */
2725*1945Sjeanm 	/*
2726*1945Sjeanm 	 * Note that if the disk is unavailable this name will point to
2727*1945Sjeanm 	 * either a nonexistent disk and thus the part info and devid will
2728*1945Sjeanm 	 * be empty or the name will point to the wrong disk and this
2729*1945Sjeanm 	 * information will be invalid. Because of this, we overwrite the
2730*1945Sjeanm 	 * dnp->devid with the correct one after getparts returns.
2731*1945Sjeanm 	 */
2732*1945Sjeanm 	if (getparts(dnp, rname, dname, uname_type, &nparts, &partno, ep) != 0)
2733*1945Sjeanm 		goto out;
2734*1945Sjeanm 
2735*1945Sjeanm 	dnp->devid = devid_str_encode(devidp, NULL);
2736*1945Sjeanm 
2737*1945Sjeanm 	/*
2738*1945Sjeanm 	 * libmeta needs at least V_NUMPAR partitions.
2739*1945Sjeanm 	 * If we have an EFI partition with less than V_NUMPAR slices,
2740*1945Sjeanm 	 * we nevertheless reserve space for V_NUMPAR
2741*1945Sjeanm 	 */
2742*1945Sjeanm 	if (nparts < V_NUMPAR) {
2743*1945Sjeanm 		nparts = V_NUMPAR;
2744*1945Sjeanm 	}
2745*1945Sjeanm 
2746*1945Sjeanm 	/* allocate and link in parts */
2747*1945Sjeanm 	dnp->parts.parts_len = nparts;
2748*1945Sjeanm 	dnp->parts.parts_val = Zalloc((sizeof (*dnp->parts.parts_val)) *
2749*1945Sjeanm 	    dnp->parts.parts_len);
2750*1945Sjeanm 
2751*1945Sjeanm 	for (slice = 0; (slice < nparts); ++slice) {
2752*1945Sjeanm 		np = &dnp->parts.parts_val[slice];
2753*1945Sjeanm 		metainitname(np);
2754*1945Sjeanm 		np->drivenamep = dnp;
2755*1945Sjeanm 	}
2756*1945Sjeanm 
2757*1945Sjeanm 	/* setup name_t (or slice) wanted */
2758*1945Sjeanm 	if ((np = setup_slice(sp, uname_type, dnp, nm, rname,
2759*1945Sjeanm 	    dname, partno, ep)) == NULL)
2760*1945Sjeanm 		goto out;
2761*1945Sjeanm 
2762*1945Sjeanm 	/* canonical disk name */
2763*1945Sjeanm 	if ((dnp->cname = metadiskname(np->cname)) == NULL)
2764*1945Sjeanm 		dnp->cname = Strdup(np->cname);
2765*1945Sjeanm 	if ((dnp->rname = metadiskname(np->rname)) == NULL)
2766*1945Sjeanm 		dnp->rname = Strdup(np->rname);
2767*1945Sjeanm 
2768*1945Sjeanm 	if (dname != NULL)
2769*1945Sjeanm 		Free(dname);
2770*1945Sjeanm 	Free(rname);
2771*1945Sjeanm 	return (dnp);
2772*1945Sjeanm 
2773*1945Sjeanm out:
2774*1945Sjeanm 	if (dname != NULL)
2775*1945Sjeanm 		Free(dname);
2776*1945Sjeanm 
2777*1945Sjeanm 	if (rname != NULL)
2778*1945Sjeanm 		Free(rname);
2779*1945Sjeanm 
2780*1945Sjeanm 	metafreedrivename(dnp);
2781*1945Sjeanm 	Free(dnp);
2782*1945Sjeanm 	Free(*tail);
2783*1945Sjeanm 	*tail = NULL;
2784*1945Sjeanm 	return (NULL);
2785*1945Sjeanm }
2786*1945Sjeanm 
2787*1945Sjeanm /*
2788*1945Sjeanm  * Search the drivename list by devid instead of name. If you don't find
2789*1945Sjeanm  * an entry with the same device id, create one for the uname passed in.
2790*1945Sjeanm  */
2791*1945Sjeanm mddrivename_t *
2792*1945Sjeanm metadrivenamebydevid(
2793*1945Sjeanm 	mdsetname_t		**spp,
2794*1945Sjeanm 	char			*devid,
2795*1945Sjeanm 	char			*uname,
2796*1945Sjeanm 	md_error_t		*ep
2797*1945Sjeanm )
2798*1945Sjeanm {
2799*1945Sjeanm 	ddi_devid_t		dnp_devidp, in_devidp;
2800*1945Sjeanm 	mdname_t		*np;
2801*1945Sjeanm 	mddrivenamelist_t	**tail;
2802*1945Sjeanm 	char			*rname = NULL;
2803*1945Sjeanm 	mddrivename_t		*dnp;
2804*1945Sjeanm 	char			*dname;
2805*1945Sjeanm 	int			ret;
2806*1945Sjeanm 	uint_t			nparts, partno;
2807*1945Sjeanm 	uint_t			slice;
2808*1945Sjeanm 	meta_device_type_t	uname_type = LOGICAL_DEVICE;
2809*1945Sjeanm 
2810*1945Sjeanm 	/* look in the cache first */
2811*1945Sjeanm 	for (tail = &drivelistp; (*tail != NULL); tail = &(*tail)->next) {
2812*1945Sjeanm 		dnp = (*tail)->drivenamep;
2813*1945Sjeanm 		if (dnp->type != MDT_COMP)
2814*1945Sjeanm 			continue;
2815*1945Sjeanm 
2816*1945Sjeanm 		/* decode the dnp devid */
2817*1945Sjeanm 		ret = devid_str_decode(dnp->devid, &dnp_devidp, NULL);
2818*1945Sjeanm 		if (ret != 0) {
2819*1945Sjeanm 			/* unable to decode the devid */
2820*1945Sjeanm 			return (NULL);
2821*1945Sjeanm 		}
2822*1945Sjeanm 		/* decode the passed in devid */
2823*1945Sjeanm 		ret = devid_str_decode(devid, &in_devidp, NULL);
2824*1945Sjeanm 		if (ret != 0) {
2825*1945Sjeanm 			/* unable to decode the devid */
2826*1945Sjeanm 			devid_free(dnp_devidp);
2827*1945Sjeanm 			return (NULL);
2828*1945Sjeanm 		}
2829*1945Sjeanm 		/* compare with the devids */
2830*1945Sjeanm 		if (devid_compare(in_devidp, dnp_devidp) == 0) {
2831*1945Sjeanm 			/* match! We have the same disk */
2832*1945Sjeanm 			devid_free(dnp_devidp);
2833*1945Sjeanm 			devid_free(in_devidp);
2834*1945Sjeanm 			return (dnp);
2835*1945Sjeanm 		}
2836*1945Sjeanm 	}
2837*1945Sjeanm 	devid_free(dnp_devidp);
2838*1945Sjeanm 	devid_free(in_devidp);
2839*1945Sjeanm 
2840*1945Sjeanm 	/* not in the cache */
2841*1945Sjeanm 
2842*1945Sjeanm 	/* get raw name (rname) of the slice and drive (dname) we have */
2843*1945Sjeanm 	if ((rname = getrawnames(spp, uname, &dname, &uname_type,
2844*1945Sjeanm 	    ep)) == NULL) {
2845*1945Sjeanm 		return (NULL);
2846*1945Sjeanm 	}
2847*1945Sjeanm 
2848*1945Sjeanm 	/* allocate new list element and drive */
2849*1945Sjeanm 	*tail = Zalloc(sizeof (**tail));
2850*1945Sjeanm 	dnp = (*tail)->drivenamep = Zalloc(sizeof (*dnp));
2851*1945Sjeanm 
2852*1945Sjeanm 	metainitdrivename(dnp);
2853*1945Sjeanm 
2854*1945Sjeanm 	/* get parts info */
2855*1945Sjeanm 	if (getparts(dnp, rname, dname, uname_type, &nparts, &partno, ep) != 0)
2856*1945Sjeanm 		goto out;
2857*1945Sjeanm 
2858*1945Sjeanm 	/*
2859*1945Sjeanm 	 * libmeta needs at least V_NUMPAR partitions.
2860*1945Sjeanm 	 * If we have an EFI partition with less than V_NUMPAR slices,
2861*1945Sjeanm 	 * we nevertheless reserve space for V_NUMPAR
2862*1945Sjeanm 	 */
2863*1945Sjeanm 	if (nparts < V_NUMPAR) {
2864*1945Sjeanm 		nparts = V_NUMPAR;
2865*1945Sjeanm 	}
2866*1945Sjeanm 
2867*1945Sjeanm 	/* allocate and link in parts */
2868*1945Sjeanm 	dnp->parts.parts_len = nparts;
2869*1945Sjeanm 	dnp->parts.parts_val = Zalloc((sizeof (*dnp->parts.parts_val)) *
2870*1945Sjeanm 	    dnp->parts.parts_len);
2871*1945Sjeanm 	for (slice = 0; (slice < nparts); ++slice) {
2872*1945Sjeanm 		np = &dnp->parts.parts_val[slice];
2873*1945Sjeanm 		metainitname(np);
2874*1945Sjeanm 		np->drivenamep = dnp;
2875*1945Sjeanm 	}
2876*1945Sjeanm 
2877*1945Sjeanm 	/* setup name_t (or slice) wanted */
2878*1945Sjeanm 	if ((np = setup_slice(*spp, uname_type, dnp, uname, rname,
2879*1945Sjeanm 	    dname, partno, ep)) == NULL)
2880*1945Sjeanm 		goto out;
2881*1945Sjeanm 
2882*1945Sjeanm 	/* canonical disk name */
2883*1945Sjeanm 	if ((dnp->cname = metadiskname(np->cname)) == NULL)
2884*1945Sjeanm 		dnp->cname = Strdup(np->cname);
2885*1945Sjeanm 	if ((dnp->rname = metadiskname(np->rname)) == NULL)
2886*1945Sjeanm 		dnp->rname = Strdup(np->rname);
2887*1945Sjeanm 
2888*1945Sjeanm 	/* cleanup, return success */
2889*1945Sjeanm 	if (dname != NULL)
2890*1945Sjeanm 		Free(dname);
2891*1945Sjeanm 	Free(rname);
2892*1945Sjeanm 	return (dnp);
2893*1945Sjeanm 
2894*1945Sjeanm 	/* cleanup, return error */
2895*1945Sjeanm out:
2896*1945Sjeanm 	if (dname != NULL)
2897*1945Sjeanm 		Free(dname);
2898*1945Sjeanm 	if (rname != NULL)
2899*1945Sjeanm 		Free(rname);
2900*1945Sjeanm 
2901*1945Sjeanm 	metafreedrivename(dnp);
2902*1945Sjeanm 	Free(dnp);
2903*1945Sjeanm 	Free(*tail);
2904*1945Sjeanm 	*tail = NULL;
2905*1945Sjeanm 	return (NULL);
2906*1945Sjeanm }
29070Sstevel@tonic-gate /*
29080Sstevel@tonic-gate  * set up names for a drive
29090Sstevel@tonic-gate  */
29100Sstevel@tonic-gate mddrivename_t *
29110Sstevel@tonic-gate metadrivename(
29121623Stw21770 	mdsetname_t		**spp,
29131623Stw21770 	char			*uname,
29141623Stw21770 	md_error_t		*ep
29150Sstevel@tonic-gate )
29160Sstevel@tonic-gate {
29170Sstevel@tonic-gate 	char		*slicename;
29180Sstevel@tonic-gate 	mdname_t	*np;
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate 	mddrivenamelist_t **tail;
29210Sstevel@tonic-gate 	mddrivename_t	*dnp;
29220Sstevel@tonic-gate 	char		*dname;
29230Sstevel@tonic-gate 	int		i;
29240Sstevel@tonic-gate 	int		mplen;
29250Sstevel@tonic-gate 	size_t		len;
29260Sstevel@tonic-gate 
29270Sstevel@tonic-gate 	assert(uname != NULL);
29281623Stw21770 
29291623Stw21770 	if ((dname = metadiskname(uname)) == NULL) {
29300Sstevel@tonic-gate 		(void) mdsyserror(ep, ENOENT, uname);
29310Sstevel@tonic-gate 		return (NULL);
29320Sstevel@tonic-gate 	}
29330Sstevel@tonic-gate 
29340Sstevel@tonic-gate 	/* look in cache first */
29350Sstevel@tonic-gate 	for (tail = &drivelistp; (*tail != NULL); tail = &(*tail)->next) {
29360Sstevel@tonic-gate 		dnp = (*tail)->drivenamep;
29370Sstevel@tonic-gate 		if ((dnp->cname != NULL &&
29380Sstevel@tonic-gate 		    (strcmp(dnp->cname, dname) == 0)) ||
29390Sstevel@tonic-gate 		    (dnp->rname != NULL &&
29400Sstevel@tonic-gate 		    (strcmp(dnp->rname, dname) == 0))) {
29410Sstevel@tonic-gate 			Free(dname);
29420Sstevel@tonic-gate 			return (dnp);
29430Sstevel@tonic-gate 		}
29440Sstevel@tonic-gate 	}
29451623Stw21770 	Free(dname);
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 	/* Check each possible slice name based on MD_MAX_PARTS. */
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate 	/*
29500Sstevel@tonic-gate 	 * Figure out how much string space to reserve to fit
29510Sstevel@tonic-gate 	 * (MD_MAX_PARTS - 1) into the name string; the loop will
29520Sstevel@tonic-gate 	 * increment the mplen counter once for each decimal digit in
29530Sstevel@tonic-gate 	 * (MD_MAX_PARTS - 1).
29540Sstevel@tonic-gate 	 */
29550Sstevel@tonic-gate 	for (i = MD_MAX_PARTS - 1, mplen = 0; i; i /= 10, ++mplen);
29560Sstevel@tonic-gate 	len = strlen(uname) + mplen + 2;
29570Sstevel@tonic-gate 	slicename = Malloc(len);
29580Sstevel@tonic-gate 
29590Sstevel@tonic-gate 	/* Check for each slice in turn until we find one */
29600Sstevel@tonic-gate 	for (np = NULL, i = 0; ((np == NULL) && (i < MD_MAX_PARTS)); ++i) {
29610Sstevel@tonic-gate 		(void) snprintf(slicename, len, "%ss%d", uname, i);
29621623Stw21770 		np = metaname(spp, slicename, LOGICAL_DEVICE, ep);
29630Sstevel@tonic-gate 	}
29640Sstevel@tonic-gate 	Free(slicename);
29650Sstevel@tonic-gate 
29660Sstevel@tonic-gate 	if (np == NULL) {
29671623Stw21770 		if ((mdiserror(ep, MDE_UNIT_NOT_FOUND)) &&
29680Sstevel@tonic-gate 		    ((dname = metadiskname(uname)) != NULL)) {
29690Sstevel@tonic-gate 			Free(dname);
29700Sstevel@tonic-gate 			(void) mderror(ep, MDE_NOT_DRIVENAME, uname);
29710Sstevel@tonic-gate 		}
29720Sstevel@tonic-gate 		return (NULL);
29730Sstevel@tonic-gate 	}
29740Sstevel@tonic-gate 	return (np->drivenamep);
29750Sstevel@tonic-gate }
29760Sstevel@tonic-gate 
29770Sstevel@tonic-gate /*
29781623Stw21770  * FUNCTION:	metaslicename_type()
29791623Stw21770  * INPUT:	dnp	- the drivename structure
29801623Stw21770  *		sliceno	- the slice on the drive to return
29811623Stw21770  *		type - LOGICAL_DEVICE or META_DEVICE
29821623Stw21770  * OUTPUT:	ep	- return error pointer
29831623Stw21770  * RETURNS:	mdname_t- pointer the the slice name structure
29841623Stw21770  * PURPOSE:	interface to the parts struct in the drive name struct
29851623Stw21770  *		Since there is no guarantee that the slice name
29861623Stw21770  *		structures are populated users should call this
29871623Stw21770  *		function rather than accessing the structure directly
29881623Stw21770  *		since it will populate the structure values if they
29891623Stw21770  *		haven't already been populated before returning.
29901623Stw21770  */
29911623Stw21770 mdname_t *
29921623Stw21770 metaslicename_type(
29931623Stw21770 	mddrivename_t		*dnp,
29941623Stw21770 	uint_t			sliceno,
29951623Stw21770 	meta_device_type_t	uname_type,
29961623Stw21770 	md_error_t		*ep
29971623Stw21770 )
29981623Stw21770 {
29991623Stw21770 	mdsetname_t	*sp = NULL;
30001623Stw21770 	char		*namep = NULL;
30011623Stw21770 	mdname_t	*np;
30021623Stw21770 
30031623Stw21770 	assert(dnp->type != MDT_FAST_COMP && dnp->type != MDT_FAST_META);
30041623Stw21770 
30051623Stw21770 	if (sliceno >= dnp->parts.parts_len) {
30061623Stw21770 		(void) mderror(ep, MDE_NOSLICE, dnp->cname);
30071623Stw21770 		return (NULL);
30081623Stw21770 	}
30091623Stw21770 
30101623Stw21770 	np = &dnp->parts.parts_val[sliceno];
30111623Stw21770 
30121623Stw21770 	/* check to see if the struct is already populated */
30131623Stw21770 	if (np->cname) {
30141623Stw21770 		return (np);
30151623Stw21770 	}
30161623Stw21770 
30171623Stw21770 	if ((namep = meta_name_getname(&sp, dnp->cname,
30181623Stw21770 					uname_type, ep)) == NULL)
30191623Stw21770 		return (NULL);
30201623Stw21770 
30211623Stw21770 	np = setup_slice(sp, uname_type, dnp, NULL, NULL, dnp->rname,
30221623Stw21770 	    sliceno, ep);
30231623Stw21770 
30241623Stw21770 	Free(namep);
30251623Stw21770 
30261623Stw21770 	return (np);
30271623Stw21770 }
30281623Stw21770 
30291623Stw21770 /*
30300Sstevel@tonic-gate  * FUNCTION:	metaslicename()
30310Sstevel@tonic-gate  * INPUT:	dnp	- the drivename structure
30320Sstevel@tonic-gate  *		sliceno	- the slice on the drive to return
30330Sstevel@tonic-gate  * OUTPUT:	ep	- return error pointer
30340Sstevel@tonic-gate  * RETURNS:	mdname_t- pointer the the slice name structure
30350Sstevel@tonic-gate  * PURPOSE:	interface to the parts struct in the drive name struct
30360Sstevel@tonic-gate  *		Since there is no guarantee that the slice name
30370Sstevel@tonic-gate  *		structures are populated users should call this
30380Sstevel@tonic-gate  *		function rather than accessing the structure directly
30390Sstevel@tonic-gate  *		since it will populate the structure values if they
30400Sstevel@tonic-gate  *		haven't already been populated before returning.
30410Sstevel@tonic-gate  */
30420Sstevel@tonic-gate mdname_t *
30430Sstevel@tonic-gate metaslicename(
30440Sstevel@tonic-gate 	mddrivename_t	*dnp,
30450Sstevel@tonic-gate 	uint_t		sliceno,
30460Sstevel@tonic-gate 	md_error_t	*ep
30470Sstevel@tonic-gate )
30480Sstevel@tonic-gate {
30491623Stw21770 	return (metaslicename_type(dnp, sliceno, LOGICAL_DEVICE, ep));
30500Sstevel@tonic-gate }
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate /*
30530Sstevel@tonic-gate  * set up metadevice name from id
30540Sstevel@tonic-gate  */
30550Sstevel@tonic-gate mdname_t *
30560Sstevel@tonic-gate metamnumname(
30570Sstevel@tonic-gate 	mdsetname_t	**spp,
30580Sstevel@tonic-gate 	minor_t		mnum,
30590Sstevel@tonic-gate 	int		fast,
30600Sstevel@tonic-gate 	md_error_t	*ep
30610Sstevel@tonic-gate )
30620Sstevel@tonic-gate {
30630Sstevel@tonic-gate 	set_t		setno = MD_MIN2SET(mnum);
30640Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
30650Sstevel@tonic-gate 	char		*uname;
30660Sstevel@tonic-gate 	mdname_t	*np;
30671623Stw21770 	md_dev64_t	dev;
30681623Stw21770 	mdkey_t		key;
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 	/* check set first */
30710Sstevel@tonic-gate 	if (spp == NULL)
30720Sstevel@tonic-gate 		spp = &sp;
30730Sstevel@tonic-gate 	if (chksetno(spp, setno, ep) != 0)
30740Sstevel@tonic-gate 		return (NULL);
30750Sstevel@tonic-gate 	assert(*spp != NULL);
30760Sstevel@tonic-gate 	sp = *spp;
30770Sstevel@tonic-gate 
30781623Stw21770 	/* get corresponding device name */
30791623Stw21770 	dev = metamakedev(mnum);
30801623Stw21770 	if ((uname = meta_getnmentbydev(sp->setno, MD_SIDEWILD, dev,
30811623Stw21770 	    NULL, NULL, &key, ep)) == NULL)
30821623Stw21770 		return (NULL);
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 	/* setup name */
30850Sstevel@tonic-gate 	if (fast) {
30861623Stw21770 		np = metaname_fast(spp, uname, META_DEVICE, ep);
30871623Stw21770 		if (np) {
30881623Stw21770 			np->dev = dev;
30891623Stw21770 			np->key = key;
30901623Stw21770 		}
30910Sstevel@tonic-gate 	} else
30921623Stw21770 		np = metaname(spp, uname, META_DEVICE, ep);
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 	Free(uname);
30950Sstevel@tonic-gate 	return (np);
30960Sstevel@tonic-gate }
30970Sstevel@tonic-gate 
30980Sstevel@tonic-gate /*
30990Sstevel@tonic-gate  * return metadevice name
31000Sstevel@tonic-gate  */
31010Sstevel@tonic-gate char *
31020Sstevel@tonic-gate get_mdname(
31031623Stw21770 	mdsetname_t	*sp,
31040Sstevel@tonic-gate 	minor_t		mnum
31050Sstevel@tonic-gate )
31060Sstevel@tonic-gate {
31070Sstevel@tonic-gate 	mdname_t	*np;
31080Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
31091623Stw21770 	mdsetname_t	**spp = NULL;
31101623Stw21770 
31111623Stw21770 	if (sp != NULL)
31121623Stw21770 		spp = &sp;
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 	/* get name */
31151623Stw21770 	if ((np = metamnumname(spp, mnum, 0, &status)) == NULL) {
31160Sstevel@tonic-gate 		return (NULL);
31170Sstevel@tonic-gate 	}
31180Sstevel@tonic-gate 	assert(meta_getminor(np->dev) == mnum);
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate 	/* return name */
31210Sstevel@tonic-gate 	return (np->cname);
31220Sstevel@tonic-gate }
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate /*
31250Sstevel@tonic-gate  * check for device type
31260Sstevel@tonic-gate  */
31270Sstevel@tonic-gate int
31280Sstevel@tonic-gate metaismeta(
31290Sstevel@tonic-gate 	mdname_t	*np
31300Sstevel@tonic-gate )
31310Sstevel@tonic-gate {
31320Sstevel@tonic-gate 	return (np->drivenamep->type == MDT_META ||
31330Sstevel@tonic-gate 		np->drivenamep->type == MDT_FAST_META);
31340Sstevel@tonic-gate }
31350Sstevel@tonic-gate 
31360Sstevel@tonic-gate int
31370Sstevel@tonic-gate metachkmeta(
31380Sstevel@tonic-gate 	mdname_t	*np,
31390Sstevel@tonic-gate 	md_error_t	*ep
31400Sstevel@tonic-gate )
31410Sstevel@tonic-gate {
31420Sstevel@tonic-gate 	if (! metaismeta(np)) {
31430Sstevel@tonic-gate 		return (mddeverror(ep, MDE_NOT_META, np->dev,
31440Sstevel@tonic-gate 		    np->cname));
31450Sstevel@tonic-gate 	}
31460Sstevel@tonic-gate 	return (0);
31470Sstevel@tonic-gate }
31480Sstevel@tonic-gate 
31490Sstevel@tonic-gate int
31500Sstevel@tonic-gate metachkdisk(
31510Sstevel@tonic-gate 	mdname_t	*np,
31520Sstevel@tonic-gate 	md_error_t	*ep
31530Sstevel@tonic-gate )
31540Sstevel@tonic-gate {
31550Sstevel@tonic-gate 	mddrivename_t	*dnp = np->drivenamep;
31560Sstevel@tonic-gate 
31570Sstevel@tonic-gate 	assert(dnp->type != MDT_FAST_COMP && dnp->type != MDT_FAST_META);
31580Sstevel@tonic-gate 
31590Sstevel@tonic-gate 	if ((! metaismeta(np)) && (dnp->type != MDT_COMP)) {
31600Sstevel@tonic-gate 		switch (dnp->type) {
31610Sstevel@tonic-gate 		    case MDT_ACCES:
31620Sstevel@tonic-gate 		    case MDT_UNKNOWN:
31630Sstevel@tonic-gate 			    return (mdsyserror(ep, dnp->errnum, np->bname));
31640Sstevel@tonic-gate 		    default:
31650Sstevel@tonic-gate 			    assert(0);
31660Sstevel@tonic-gate 			    return (mddeverror(ep, MDE_NOT_DISK, np->dev,
31670Sstevel@tonic-gate 				np->cname));
31680Sstevel@tonic-gate 		}
31690Sstevel@tonic-gate 	}
31700Sstevel@tonic-gate 	return (0);
31710Sstevel@tonic-gate }
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate int
31740Sstevel@tonic-gate metachkcomp(
31750Sstevel@tonic-gate 	mdname_t	*np,
31760Sstevel@tonic-gate 	md_error_t	*ep
31770Sstevel@tonic-gate )
31780Sstevel@tonic-gate {
31790Sstevel@tonic-gate 	if (metaismeta(np)) {
31800Sstevel@tonic-gate 		return (mddeverror(ep, MDE_IS_META, np->dev,
31810Sstevel@tonic-gate 		    np->cname));
31820Sstevel@tonic-gate 	}
31830Sstevel@tonic-gate 	return (metachkdisk(np, ep));
31840Sstevel@tonic-gate }
31850Sstevel@tonic-gate 
31860Sstevel@tonic-gate /*
31870Sstevel@tonic-gate  * free list of names
31880Sstevel@tonic-gate  */
31890Sstevel@tonic-gate void
31900Sstevel@tonic-gate metafreenamelist(
31910Sstevel@tonic-gate 	mdnamelist_t	*nlp
31920Sstevel@tonic-gate )
31930Sstevel@tonic-gate {
31940Sstevel@tonic-gate 	mdnamelist_t	*next = NULL;
31950Sstevel@tonic-gate 
31960Sstevel@tonic-gate 	for (/* void */; (nlp != NULL); nlp = next) {
31970Sstevel@tonic-gate 		next = nlp->next;
31980Sstevel@tonic-gate 		Free(nlp);
31990Sstevel@tonic-gate 	}
32000Sstevel@tonic-gate }
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate /*
32030Sstevel@tonic-gate  * build list of names
32040Sstevel@tonic-gate  */
32050Sstevel@tonic-gate int
32060Sstevel@tonic-gate metanamelist(
32070Sstevel@tonic-gate 	mdsetname_t	**spp,
32080Sstevel@tonic-gate 	mdnamelist_t	**nlpp,
32090Sstevel@tonic-gate 	int		argc,
32100Sstevel@tonic-gate 	char		*argv[],
32111623Stw21770 	meta_device_type_t	type,
32120Sstevel@tonic-gate 	md_error_t	*ep
32130Sstevel@tonic-gate )
32140Sstevel@tonic-gate {
32150Sstevel@tonic-gate 	mdnamelist_t	**tailpp = nlpp;
32160Sstevel@tonic-gate 	int		count = 0;
32170Sstevel@tonic-gate 
32180Sstevel@tonic-gate 	for (*nlpp = NULL; (argc > 0); ++count, --argc, ++argv) {
32190Sstevel@tonic-gate 		mdnamelist_t	*nlp = Zalloc(sizeof (*nlp));
32200Sstevel@tonic-gate 
32211623Stw21770 		if ((nlp->namep = metaname(spp, argv[0],
32221623Stw21770 		    type, ep)) == NULL) {
32230Sstevel@tonic-gate 			metafreenamelist(*nlpp);
32240Sstevel@tonic-gate 			*nlpp = NULL;
32250Sstevel@tonic-gate 			return (-1);
32260Sstevel@tonic-gate 		}
32270Sstevel@tonic-gate 		*tailpp = nlp;
32280Sstevel@tonic-gate 		tailpp = &nlp->next;
32290Sstevel@tonic-gate 	}
32300Sstevel@tonic-gate 	return (count);
32310Sstevel@tonic-gate }
32320Sstevel@tonic-gate 
32330Sstevel@tonic-gate /*
32340Sstevel@tonic-gate  * append to end of name list
32350Sstevel@tonic-gate  */
32360Sstevel@tonic-gate mdname_t *
32370Sstevel@tonic-gate metanamelist_append(
32380Sstevel@tonic-gate 	mdnamelist_t	**nlpp,
32390Sstevel@tonic-gate 	mdname_t	*np
32400Sstevel@tonic-gate )
32410Sstevel@tonic-gate {
32420Sstevel@tonic-gate 	mdnamelist_t	*nlp;
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate 	/* run to end of list */
32450Sstevel@tonic-gate 	for (; (*nlpp != NULL); nlpp = &(*nlpp)->next)
32460Sstevel@tonic-gate 		;
32470Sstevel@tonic-gate 
32480Sstevel@tonic-gate 	/* allocate new list element */
32490Sstevel@tonic-gate 	nlp = *nlpp = Zalloc(sizeof (*nlp));
32500Sstevel@tonic-gate 
32510Sstevel@tonic-gate 	/* append name */
32520Sstevel@tonic-gate 	nlp->namep = np;
32530Sstevel@tonic-gate 	return (np);
32540Sstevel@tonic-gate }
32550Sstevel@tonic-gate 
32560Sstevel@tonic-gate /*
32570Sstevel@tonic-gate  * FUNCTION:	meta_namelist_append_wrapper()
32580Sstevel@tonic-gate  * INPUT:	tailpp	- pointer to the list tail pointer
32590Sstevel@tonic-gate  *		np	- name node to be appended to list
32600Sstevel@tonic-gate  * OUTPUT:	none
32610Sstevel@tonic-gate  * RETURNS:	mdnamelist_t * - new tail of the list.
32620Sstevel@tonic-gate  * PURPOSE:	wrapper to meta_namelist_append for performance.
32630Sstevel@tonic-gate  *		metanamelist_append finds the tail each time which slows
32640Sstevel@tonic-gate  *		down long lists.  By keeping track of the tail ourselves
32650Sstevel@tonic-gate  *		we can change metanamelist_append into a constant time
32660Sstevel@tonic-gate  *		operation.
32670Sstevel@tonic-gate  */
32680Sstevel@tonic-gate mdnamelist_t **
32690Sstevel@tonic-gate meta_namelist_append_wrapper(
32700Sstevel@tonic-gate 	mdnamelist_t	**tailpp,
32710Sstevel@tonic-gate 	mdname_t	*np
32720Sstevel@tonic-gate )
32730Sstevel@tonic-gate {
32740Sstevel@tonic-gate 	(void) metanamelist_append(tailpp, np);
32750Sstevel@tonic-gate 
32760Sstevel@tonic-gate 	/* If it's the first item in the list, return it instead of the next */
32770Sstevel@tonic-gate 	if ((*tailpp)->next == NULL)
32780Sstevel@tonic-gate 		return (tailpp);
32790Sstevel@tonic-gate 
32800Sstevel@tonic-gate 	return (&(*tailpp)->next);
32810Sstevel@tonic-gate }
32820Sstevel@tonic-gate 
32830Sstevel@tonic-gate 
32840Sstevel@tonic-gate /*
32850Sstevel@tonic-gate  *	mdhspname_t stuff
32860Sstevel@tonic-gate  */
32870Sstevel@tonic-gate 
32880Sstevel@tonic-gate /*
32890Sstevel@tonic-gate  * initialize hspname
32900Sstevel@tonic-gate  */
32910Sstevel@tonic-gate static void
32920Sstevel@tonic-gate metainithspname(
32930Sstevel@tonic-gate 	mdhspname_t	*hspnamep
32940Sstevel@tonic-gate )
32950Sstevel@tonic-gate {
32960Sstevel@tonic-gate 	(void) memset(hspnamep, '\0', sizeof (*hspnamep));
32970Sstevel@tonic-gate 	hspnamep->hsp = MD_HSP_NONE;
32980Sstevel@tonic-gate }
32990Sstevel@tonic-gate 
33000Sstevel@tonic-gate /*
33010Sstevel@tonic-gate  * free allocated hspname
33020Sstevel@tonic-gate  */
33030Sstevel@tonic-gate static void
33040Sstevel@tonic-gate metafreehspname(
33050Sstevel@tonic-gate 	mdhspname_t	*hspnamep
33060Sstevel@tonic-gate )
33070Sstevel@tonic-gate {
33080Sstevel@tonic-gate 	if (hspnamep->hspname != NULL)
33090Sstevel@tonic-gate 		Free(hspnamep->hspname);
33100Sstevel@tonic-gate 	if (hspnamep->unitp != NULL)
33110Sstevel@tonic-gate 		meta_invalidate_hsp(hspnamep);
33120Sstevel@tonic-gate 	metainithspname(hspnamep);
33130Sstevel@tonic-gate }
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate /*
33160Sstevel@tonic-gate  * clear the hspname cache
33170Sstevel@tonic-gate  */
33180Sstevel@tonic-gate static void
33190Sstevel@tonic-gate metaflushhspnames()
33200Sstevel@tonic-gate {
33210Sstevel@tonic-gate 	mdhspnamelist_t		*p, *n;
33220Sstevel@tonic-gate 
33230Sstevel@tonic-gate 	for (p = hsplistp, n = NULL; (p != NULL); p = n) {
33240Sstevel@tonic-gate 		n = p->next;
33250Sstevel@tonic-gate 		metafreehspname(p->hspnamep);
33260Sstevel@tonic-gate 		Free(p->hspnamep);
33270Sstevel@tonic-gate 		Free(p);
33280Sstevel@tonic-gate 	}
33290Sstevel@tonic-gate 	hsplistp = NULL;
33300Sstevel@tonic-gate }
33310Sstevel@tonic-gate 
33320Sstevel@tonic-gate /*
33330Sstevel@tonic-gate  * check set and get comparison name
33340Sstevel@tonic-gate  */
33350Sstevel@tonic-gate static char *
33360Sstevel@tonic-gate gethspname(
33370Sstevel@tonic-gate 	mdsetname_t	**spp,
33380Sstevel@tonic-gate 	char		*uname,
33390Sstevel@tonic-gate 	md_error_t	*ep
33400Sstevel@tonic-gate )
33410Sstevel@tonic-gate {
33421623Stw21770 	char		*cname = NULL;
33431623Stw21770 
33441623Stw21770 	cname = meta_canonicalize(*spp, uname);
33451623Stw21770 	/* if it is not a meta/hsp name then flag an error */
33461623Stw21770 	if (cname == NULL) {
33471623Stw21770 		(void) mdsyserror(ep, ENOENT, uname);
33481623Stw21770 		return (NULL);
33491623Stw21770 	}
33501623Stw21770 	return (cname);
33511623Stw21770 }
33521623Stw21770 
33531623Stw21770 /*
33541623Stw21770  * set up a hotspare pool name structure using both the name
33551623Stw21770  * and the self id
33561623Stw21770  */
33571623Stw21770 static mdhspname_t *
33581623Stw21770 metahspname_hsp(
33591623Stw21770 	mdsetname_t	**spp,
33601623Stw21770 	char		*uname,
33611623Stw21770 	hsp_t		hsp,
33621623Stw21770 	md_error_t	*ep
33631623Stw21770 )
33641623Stw21770 {
33651623Stw21770 	char		*cname;
33661623Stw21770 	mdhspnamelist_t	**tail;
33671623Stw21770 	mdhspname_t	*hspnp;
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate 	/* check setname */
33700Sstevel@tonic-gate 	assert(uname != NULL);
33711623Stw21770 	if ((cname = gethspname(spp, uname, ep)) == NULL)
33720Sstevel@tonic-gate 		return (NULL);
33731623Stw21770 	assert(*spp != NULL);
33741623Stw21770 
33751623Stw21770 	/* look in cache first */
33761623Stw21770 	for (tail = &hsplistp; (*tail != NULL); tail = &(*tail)->next) {
33771623Stw21770 		hspnp = (*tail)->hspnamep;
33781623Stw21770 		if (strcmp(hspnp->hspname, cname) == 0) {
33791623Stw21770 			Free(cname);
33801623Stw21770 			/* if the hsp value has not been set then set it now */
33811623Stw21770 			if (hspnp->hsp == MD_HSP_NONE)
33821623Stw21770 				hspnp->hsp = hsp;
33831623Stw21770 			return (hspnp);
33841623Stw21770 		}
33850Sstevel@tonic-gate 	}
33861623Stw21770 
33871623Stw21770 	/* if the hsp number isn't specified then attempt to get it */
33881623Stw21770 	if (hsp == MD_HSP_NONE && (hsp = meta_gethspnmentbyname((*spp)->setno,
33891623Stw21770 	    MD_SIDEWILD, cname, ep)) == MD_HSP_NONE) {
33901623Stw21770 		if (! mdisok(ep)) {
33911623Stw21770 			/*
33921623Stw21770 			 * If the error is ENOENT, then we will continue on,
33931623Stw21770 			 * because the device does not yet exist.
33941623Stw21770 			 * For other types of errors, however, we'll bail out.
33951623Stw21770 			 */
33961623Stw21770 			if (! mdissyserror(ep, ENOENT)) {
33971623Stw21770 				Free(cname);
33981623Stw21770 				return (NULL);
33991623Stw21770 			}
34001623Stw21770 			mdclrerror(ep);
34011623Stw21770 		}
34020Sstevel@tonic-gate 	}
34031623Stw21770 
34041623Stw21770 	/* allocate new list element and hspname */
34051623Stw21770 	*tail = Zalloc(sizeof (**tail));
34061623Stw21770 	hspnp = (*tail)->hspnamep = Zalloc(sizeof (*hspnp));
34071623Stw21770 	metainithspname(hspnp);
34081623Stw21770 
34091623Stw21770 	/* save hspname and number */
34101623Stw21770 	hspnp->hspname = cname;
34111623Stw21770 	hspnp->hsp = hsp;
34121623Stw21770 
34131623Stw21770 	/* success */
34141623Stw21770 	return (hspnp);
34150Sstevel@tonic-gate }
34160Sstevel@tonic-gate 
34170Sstevel@tonic-gate /*
34180Sstevel@tonic-gate  * set up names for a hotspare pool
34190Sstevel@tonic-gate  */
34200Sstevel@tonic-gate mdhspname_t *
34210Sstevel@tonic-gate metahspname(
34220Sstevel@tonic-gate 	mdsetname_t	**spp,
34230Sstevel@tonic-gate 	char		*uname,
34240Sstevel@tonic-gate 	md_error_t	*ep
34250Sstevel@tonic-gate )
34260Sstevel@tonic-gate {
34271623Stw21770 	return (metahspname_hsp(spp, uname, MD_HSP_NONE, ep));
34280Sstevel@tonic-gate }
34290Sstevel@tonic-gate 
34300Sstevel@tonic-gate /*
34311623Stw21770  * set up hotspare pool name from key
34320Sstevel@tonic-gate  */
34330Sstevel@tonic-gate mdhspname_t *
34340Sstevel@tonic-gate metahsphspname(
34350Sstevel@tonic-gate 	mdsetname_t	**spp,
34360Sstevel@tonic-gate 	hsp_t		hsp,
34370Sstevel@tonic-gate 	md_error_t	*ep
34380Sstevel@tonic-gate )
34390Sstevel@tonic-gate {
34400Sstevel@tonic-gate 	set_t		setno = HSP_SET(hsp);
34410Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
34420Sstevel@tonic-gate 	char		*uname;
34430Sstevel@tonic-gate 	mdhspname_t	*hspnp;
34440Sstevel@tonic-gate 
34450Sstevel@tonic-gate 	/* check set first */
34460Sstevel@tonic-gate 	if (spp == NULL)
34470Sstevel@tonic-gate 		spp = &sp;
34480Sstevel@tonic-gate 	if (chksetno(spp, setno, ep) != 0)
34490Sstevel@tonic-gate 		return (NULL);
34500Sstevel@tonic-gate 	assert(*spp != NULL);
34510Sstevel@tonic-gate 	sp = *spp;
34520Sstevel@tonic-gate 
34531623Stw21770 	/* get corresponding hotspare pool name */
34541623Stw21770 	if ((uname = meta_gethspnmentbyid(sp->setno,
34551623Stw21770 			MD_SIDEWILD, hsp, ep)) == NULL)
34561623Stw21770 		return (NULL);
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate 	/* setup name */
34591623Stw21770 	hspnp = metahspname_hsp(spp, uname, hsp, ep);
34600Sstevel@tonic-gate 	Free(uname);
34610Sstevel@tonic-gate 	return (hspnp);
34620Sstevel@tonic-gate }
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate /*
34650Sstevel@tonic-gate  * return hotspare pool name
34660Sstevel@tonic-gate  */
34670Sstevel@tonic-gate char *
34681623Stw21770 get_hspname(mdsetname_t *sp, hsp_t hsp)
34690Sstevel@tonic-gate {
34700Sstevel@tonic-gate 	mdhspname_t	*hspnp;
34710Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
34721623Stw21770 	mdsetname_t	**spp = NULL;
34731623Stw21770 
34741623Stw21770 	if (sp != NULL)
34751623Stw21770 		spp = &sp;
34760Sstevel@tonic-gate 
34770Sstevel@tonic-gate 	/* get name */
34781623Stw21770 	if ((hspnp = metahsphspname(spp, hsp, &status)) == NULL) {
34790Sstevel@tonic-gate 		mdclrerror(&status);
34800Sstevel@tonic-gate 		return (NULL);
34810Sstevel@tonic-gate 	}
34820Sstevel@tonic-gate 
34830Sstevel@tonic-gate 	/* return name */
34840Sstevel@tonic-gate 	return (hspnp->hspname);
34850Sstevel@tonic-gate }
34860Sstevel@tonic-gate 
34870Sstevel@tonic-gate /*
34880Sstevel@tonic-gate  * free hotspare pool list
34890Sstevel@tonic-gate  */
34900Sstevel@tonic-gate void
34910Sstevel@tonic-gate metafreehspnamelist(mdhspnamelist_t *hspnlp)
34920Sstevel@tonic-gate {
34930Sstevel@tonic-gate 	mdhspnamelist_t	*next = NULL;
34940Sstevel@tonic-gate 
34950Sstevel@tonic-gate 	for (/* void */; (hspnlp != NULL); hspnlp = next) {
34960Sstevel@tonic-gate 		next = hspnlp->next;
34970Sstevel@tonic-gate 		Free(hspnlp);
34980Sstevel@tonic-gate 	}
34990Sstevel@tonic-gate }
35000Sstevel@tonic-gate 
35010Sstevel@tonic-gate /*
35020Sstevel@tonic-gate  * build list of hotspare pool names
35030Sstevel@tonic-gate  */
35040Sstevel@tonic-gate int
35050Sstevel@tonic-gate metahspnamelist(
35060Sstevel@tonic-gate 	mdsetname_t	**spp,
35070Sstevel@tonic-gate 	mdhspnamelist_t	**hspnlpp,
35080Sstevel@tonic-gate 	int		argc,
35090Sstevel@tonic-gate 	char		*argv[],
35100Sstevel@tonic-gate 	md_error_t	*ep
35110Sstevel@tonic-gate )
35120Sstevel@tonic-gate {
35130Sstevel@tonic-gate 	mdhspnamelist_t	**tailpp = hspnlpp;
35140Sstevel@tonic-gate 	int		count = 0;
35150Sstevel@tonic-gate 
35160Sstevel@tonic-gate 	for (*hspnlpp = NULL; (argc > 0); ++count, --argc, ++argv) {
35170Sstevel@tonic-gate 		mdhspnamelist_t	*hspnlp = Zalloc(sizeof (*hspnlp));
35180Sstevel@tonic-gate 
35190Sstevel@tonic-gate 		if ((hspnlp->hspnamep = metahspname(spp, argv[0],
35200Sstevel@tonic-gate 		    ep)) == NULL) {
35210Sstevel@tonic-gate 			metafreehspnamelist(*hspnlpp);
35220Sstevel@tonic-gate 			*hspnlpp = NULL;
35230Sstevel@tonic-gate 			return (-1);
35240Sstevel@tonic-gate 		}
35250Sstevel@tonic-gate 		*tailpp = hspnlp;
35260Sstevel@tonic-gate 		tailpp = &hspnlp->next;
35270Sstevel@tonic-gate 	}
35280Sstevel@tonic-gate 	return (count);
35290Sstevel@tonic-gate }
35300Sstevel@tonic-gate 
35310Sstevel@tonic-gate /*
35320Sstevel@tonic-gate  * append to end of hotspare pool list
35330Sstevel@tonic-gate  */
35340Sstevel@tonic-gate mdhspname_t *
35350Sstevel@tonic-gate metahspnamelist_append(mdhspnamelist_t **hspnlpp, mdhspname_t *hspnp)
35360Sstevel@tonic-gate {
35370Sstevel@tonic-gate 	mdhspnamelist_t	*hspnlp;
35380Sstevel@tonic-gate 
35390Sstevel@tonic-gate 	/* run to end of list */
35400Sstevel@tonic-gate 	for (; (*hspnlpp != NULL); hspnlpp = &(*hspnlpp)->next)
35410Sstevel@tonic-gate 		;
35420Sstevel@tonic-gate 
35430Sstevel@tonic-gate 	/* allocate new list element */
35440Sstevel@tonic-gate 	hspnlp = *hspnlpp = Zalloc(sizeof (*hspnlp));
35450Sstevel@tonic-gate 
35460Sstevel@tonic-gate 	/* append hotspare pool name */
35470Sstevel@tonic-gate 	hspnlp->hspnamep = hspnp;
35480Sstevel@tonic-gate 	return (hspnp);
35490Sstevel@tonic-gate }
35500Sstevel@tonic-gate 
35510Sstevel@tonic-gate /*
35520Sstevel@tonic-gate  * get name from dev
35530Sstevel@tonic-gate  */
35540Sstevel@tonic-gate mdname_t *
35550Sstevel@tonic-gate metadevname(
35560Sstevel@tonic-gate 	mdsetname_t **spp,
35570Sstevel@tonic-gate 	md_dev64_t dev,
35580Sstevel@tonic-gate 	md_error_t *ep)
35590Sstevel@tonic-gate {
35600Sstevel@tonic-gate 	char		*device_name;
35610Sstevel@tonic-gate 	mdname_t	*namep;
35620Sstevel@tonic-gate 	mdkey_t		key;
35630Sstevel@tonic-gate 
35640Sstevel@tonic-gate 	/* short circuit metadevices */
35650Sstevel@tonic-gate 	assert(dev != NODEV64);
35660Sstevel@tonic-gate 	if (meta_dev_ismeta(dev))
35670Sstevel@tonic-gate 		return (metamnumname(spp, meta_getminor(dev), 0, ep));
35680Sstevel@tonic-gate 
35690Sstevel@tonic-gate 	/* create local set, if necessary */
35700Sstevel@tonic-gate 	if (*spp == NULL) {
35710Sstevel@tonic-gate 		if ((*spp = metasetname(MD_LOCAL_NAME, ep)) == NULL)
35720Sstevel@tonic-gate 			return (NULL);
35730Sstevel@tonic-gate 	}
35740Sstevel@tonic-gate 
35750Sstevel@tonic-gate 	/* get name from namespace */
35760Sstevel@tonic-gate 	if ((device_name = meta_getnmentbydev((*spp)->setno, MD_SIDEWILD,
35770Sstevel@tonic-gate 	    dev, NULL, NULL, &key, ep)) == NULL) {
35780Sstevel@tonic-gate 		return (NULL);
35790Sstevel@tonic-gate 	}
35801623Stw21770 	namep = metaname_fast(spp, device_name, LOGICAL_DEVICE, ep);
35810Sstevel@tonic-gate 	if (namep != NULL)
35820Sstevel@tonic-gate 		namep->key = key;
35830Sstevel@tonic-gate 
35840Sstevel@tonic-gate 	Free(device_name);
35850Sstevel@tonic-gate 	return (namep);
35860Sstevel@tonic-gate }
35870Sstevel@tonic-gate 
35880Sstevel@tonic-gate /*
35890Sstevel@tonic-gate  * return cached name from md_dev64_t
35900Sstevel@tonic-gate  */
35910Sstevel@tonic-gate static char *
35920Sstevel@tonic-gate metadevtocachename(md_dev64_t dev)
35930Sstevel@tonic-gate {
35940Sstevel@tonic-gate 	mddrivenamelist_t	*dnlp;
35950Sstevel@tonic-gate 
35960Sstevel@tonic-gate 	/* look in cache */
35970Sstevel@tonic-gate 	for (dnlp = drivelistp; (dnlp != NULL); dnlp = dnlp->next) {
35980Sstevel@tonic-gate 		mddrivename_t	*dnp = dnlp->drivenamep;
35990Sstevel@tonic-gate 		uint_t		i;
36000Sstevel@tonic-gate 
36010Sstevel@tonic-gate 		for (i = 0; (i < dnp->parts.parts_len); ++i) {
36020Sstevel@tonic-gate 			mdname_t	*np = &dnp->parts.parts_val[i];
36030Sstevel@tonic-gate 
36040Sstevel@tonic-gate 			if (np->dev == dev)
36050Sstevel@tonic-gate 				return (np->cname);
36060Sstevel@tonic-gate 		}
36070Sstevel@tonic-gate 	}
36080Sstevel@tonic-gate 
36090Sstevel@tonic-gate 	/* not found */
36100Sstevel@tonic-gate 	return (NULL);
36110Sstevel@tonic-gate }
36120Sstevel@tonic-gate 
36130Sstevel@tonic-gate /*
36140Sstevel@tonic-gate  * Ask the driver for the name, which has been stored in the
36150Sstevel@tonic-gate  * metadevice state database (on behalf of the utilities).
36160Sstevel@tonic-gate  * (by devno)
36170Sstevel@tonic-gate  */
36180Sstevel@tonic-gate char *
36190Sstevel@tonic-gate get_devname(
36200Sstevel@tonic-gate 	set_t setno,
36210Sstevel@tonic-gate 	md_dev64_t dev)
36220Sstevel@tonic-gate {
36230Sstevel@tonic-gate 	mdsetname_t	*sp;
36240Sstevel@tonic-gate 	mdname_t	*np;
36250Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
36260Sstevel@tonic-gate 
36270Sstevel@tonic-gate 	/* get name */
36280Sstevel@tonic-gate 	if ((setno == MD_SET_BAD) ||
36290Sstevel@tonic-gate 	    ((sp = metasetnosetname(setno, &status)) == NULL) ||
36300Sstevel@tonic-gate 	    ((np = metadevname(&sp, dev, &status)) == NULL)) {
36310Sstevel@tonic-gate 		mdclrerror(&status);
36320Sstevel@tonic-gate 		return (metadevtocachename(dev));
36330Sstevel@tonic-gate 	}
36340Sstevel@tonic-gate 
36350Sstevel@tonic-gate 	/* return name */
36360Sstevel@tonic-gate 	return (np->cname);
36370Sstevel@tonic-gate }
36380Sstevel@tonic-gate 
36390Sstevel@tonic-gate /*
36400Sstevel@tonic-gate  * get name from key
36410Sstevel@tonic-gate  */
36420Sstevel@tonic-gate mdname_t *
36430Sstevel@tonic-gate metakeyname(
36440Sstevel@tonic-gate 	mdsetname_t	**spp,
36450Sstevel@tonic-gate 	mdkey_t		key,
36460Sstevel@tonic-gate 	int		fast,
36470Sstevel@tonic-gate 	md_error_t	*ep
36480Sstevel@tonic-gate )
36490Sstevel@tonic-gate {
36500Sstevel@tonic-gate 	char		*device_name;
36510Sstevel@tonic-gate 	md_dev64_t	dev = NODEV64;
36520Sstevel@tonic-gate 	mdname_t	*namep;
36530Sstevel@tonic-gate 
36540Sstevel@tonic-gate 	/* create local set, if necessary */
36550Sstevel@tonic-gate 	if (*spp == NULL) {
36560Sstevel@tonic-gate 		if ((*spp = metasetname(MD_LOCAL_NAME, ep)) == NULL)
36570Sstevel@tonic-gate 			return (NULL);
36580Sstevel@tonic-gate 	}
36590Sstevel@tonic-gate 
36600Sstevel@tonic-gate 	/* get name from namespace */
36610Sstevel@tonic-gate 	if ((device_name = meta_getnmentbykey((*spp)->setno, MD_SIDEWILD,
36620Sstevel@tonic-gate 	    key, NULL, NULL, &dev, ep)) == NULL) {
36630Sstevel@tonic-gate 		return (NULL);
36640Sstevel@tonic-gate 	}
36650Sstevel@tonic-gate 	if (fast)
36661623Stw21770 		namep = metaname_fast(spp, device_name, UNKNOWN, ep);
36670Sstevel@tonic-gate 	else
36681623Stw21770 		namep = metaname(spp, device_name, UNKNOWN, ep);
36690Sstevel@tonic-gate 
36700Sstevel@tonic-gate 	assert(dev != NODEV64);
36710Sstevel@tonic-gate 	if (namep)
36720Sstevel@tonic-gate 		namep->dev = dev;
36730Sstevel@tonic-gate 	Free(device_name);
36740Sstevel@tonic-gate 	return (namep);
36750Sstevel@tonic-gate }
36760Sstevel@tonic-gate 
36770Sstevel@tonic-gate /*
36781623Stw21770  * completely flush metadev/hsp caches
36791623Stw21770  */
36801623Stw21770 void
36811623Stw21770 metaflushmetanames()
36821623Stw21770 {
36831623Stw21770 	metaflushhspnames();
36841623Stw21770 	metaflushdrivenames();
36851623Stw21770 	metaflushfastnames();
36861623Stw21770 	metaflushstatcache();
36871623Stw21770 }
36881623Stw21770 
36891623Stw21770 /*
36900Sstevel@tonic-gate  * completely flush the caches
36910Sstevel@tonic-gate  */
36920Sstevel@tonic-gate void
36930Sstevel@tonic-gate metaflushnames(int flush_sr_cache)
36940Sstevel@tonic-gate {
36950Sstevel@tonic-gate 	metaflushhspnames();
36960Sstevel@tonic-gate 	metaflushdrivenames();
36970Sstevel@tonic-gate 	metaflushsetnames();
36980Sstevel@tonic-gate 	metaflushctlrcache();
36990Sstevel@tonic-gate 	metaflushfastnames();
37000Sstevel@tonic-gate 	metaflushstatcache();
37010Sstevel@tonic-gate 	if (flush_sr_cache)
37020Sstevel@tonic-gate 		sr_cache_flush(0);
37030Sstevel@tonic-gate }
37040Sstevel@tonic-gate 
37050Sstevel@tonic-gate /*
37060Sstevel@tonic-gate  * meta_get_hotspare_names
37070Sstevel@tonic-gate  *  returns an mdnamelist_t of hot spare names
37080Sstevel@tonic-gate  */
37090Sstevel@tonic-gate 
37100Sstevel@tonic-gate int
37110Sstevel@tonic-gate meta_get_hotspare_names(
37120Sstevel@tonic-gate 	mdsetname_t	*sp,
37130Sstevel@tonic-gate 	mdnamelist_t	**nlpp,
37140Sstevel@tonic-gate 	int		options,
37150Sstevel@tonic-gate 	md_error_t	*ep
37160Sstevel@tonic-gate )
37170Sstevel@tonic-gate {
37180Sstevel@tonic-gate 	mdhspnamelist_t		*hspnlp	= NULL;
37190Sstevel@tonic-gate 	mdhspnamelist_t		*hspp;
37200Sstevel@tonic-gate 	int			cnt = 0;
37210Sstevel@tonic-gate 
37220Sstevel@tonic-gate 	assert(nlpp != NULL);
37230Sstevel@tonic-gate 
37240Sstevel@tonic-gate 	/* get hotspare names */
37250Sstevel@tonic-gate 	if (meta_get_hsp_names(sp, &hspnlp, options, ep) < 0) {
37260Sstevel@tonic-gate 		cnt = -1;
37270Sstevel@tonic-gate 		goto out;
37280Sstevel@tonic-gate 	}
37290Sstevel@tonic-gate 
37300Sstevel@tonic-gate 	/* build name list */
37310Sstevel@tonic-gate 	for (hspp = hspnlp; (hspp != NULL); hspp = hspp->next) {
37320Sstevel@tonic-gate 		md_hsp_t	*hsp;
37330Sstevel@tonic-gate 		int		i;
37340Sstevel@tonic-gate 
37350Sstevel@tonic-gate 		if ((hsp = meta_get_hsp(sp, hspp->hspnamep, ep)) == NULL) {
37360Sstevel@tonic-gate 			cnt = -1;
37370Sstevel@tonic-gate 			goto out;
37380Sstevel@tonic-gate 		}
37390Sstevel@tonic-gate 		for (i = 0; (i < hsp->hotspares.hotspares_len); i++) {
37400Sstevel@tonic-gate 			md_hs_t	*hs = &hsp->hotspares.hotspares_val[i];
37410Sstevel@tonic-gate 
37420Sstevel@tonic-gate 			(void) metanamelist_append(nlpp, hs->hsnamep);
37430Sstevel@tonic-gate 			++cnt;
37440Sstevel@tonic-gate 		}
37450Sstevel@tonic-gate 	}
37460Sstevel@tonic-gate 
37470Sstevel@tonic-gate 	/* cleanup and return count or error */
37480Sstevel@tonic-gate out:
37490Sstevel@tonic-gate 	metafreehspnamelist(hspnlp);
37500Sstevel@tonic-gate 	if ((cnt == -1) && mdisok(ep)) {
37510Sstevel@tonic-gate 		/*
37520Sstevel@tonic-gate 		 * At least try to give some sort of meaningful error
37530Sstevel@tonic-gate 		 */
37540Sstevel@tonic-gate 		(void) mderror(ep, MDE_NO_HSPS, "Generic Hotspare Error");
37550Sstevel@tonic-gate 	}
37560Sstevel@tonic-gate 
37570Sstevel@tonic-gate 	return (cnt);
37580Sstevel@tonic-gate }
37590Sstevel@tonic-gate /*
37600Sstevel@tonic-gate  * meta_create_non_dup_list
37610Sstevel@tonic-gate  *    INPUT: mdnp mdname_t pointer to add to the list if a new name
37620Sstevel@tonic-gate  *           ldevidp list of non-duplicate names.
37630Sstevel@tonic-gate  *    OUTPUT: ldevidp list of non-duplicate names.
37640Sstevel@tonic-gate  * meta_create_non_dup_list will take a mdname_t pointer and if the device
37650Sstevel@tonic-gate  *    is not in the list (ldevidp) will add it to the list.
37660Sstevel@tonic-gate  *    User needs to free allocated memory.
37670Sstevel@tonic-gate  */
37680Sstevel@tonic-gate void
37690Sstevel@tonic-gate meta_create_non_dup_list(
37700Sstevel@tonic-gate 	mdname_t	*mdnp,
37710Sstevel@tonic-gate 	mddevid_t	**ldevidpp
37720Sstevel@tonic-gate )
37730Sstevel@tonic-gate {
37740Sstevel@tonic-gate 	char		*lcname;
37750Sstevel@tonic-gate 	mddevid_t	*tmp;
37760Sstevel@tonic-gate 	mddevid_t	*lastdevidp;
37770Sstevel@tonic-gate 	mddevid_t	*lldevidp;
37780Sstevel@tonic-gate 	char		*ctd, *slice;
37790Sstevel@tonic-gate 	mddevid_t	*ldevidp;
37800Sstevel@tonic-gate 
37810Sstevel@tonic-gate 	if (mdnp == NULL)
37820Sstevel@tonic-gate 		return;
37830Sstevel@tonic-gate 
37840Sstevel@tonic-gate 	ldevidp = *ldevidpp;
37850Sstevel@tonic-gate 	/*
37860Sstevel@tonic-gate 	 * Grab the name of the device and strip off slice information
37870Sstevel@tonic-gate 	 */
37880Sstevel@tonic-gate 	lcname = Strdup(mdnp->cname);
37890Sstevel@tonic-gate 	if (lcname == NULL) {
37900Sstevel@tonic-gate 		return;
37910Sstevel@tonic-gate 	}
37920Sstevel@tonic-gate 	ctd = strrchr(lcname, '/');
37930Sstevel@tonic-gate 	if (ctd != NULL)
37940Sstevel@tonic-gate 		slice = strrchr(ctd, 's');
37950Sstevel@tonic-gate 	else
37960Sstevel@tonic-gate 		slice = strrchr(lcname, 's');
37970Sstevel@tonic-gate 
37980Sstevel@tonic-gate 	if (slice != NULL)
37990Sstevel@tonic-gate 		*slice = '\0';
38000Sstevel@tonic-gate 
38010Sstevel@tonic-gate 	if (ldevidp == NULL) {
38020Sstevel@tonic-gate 		/* first item in list */
38030Sstevel@tonic-gate 		ldevidp = Zalloc(sizeof (mddevid_t));
38040Sstevel@tonic-gate 		ldevidp->ctdname = lcname;
38050Sstevel@tonic-gate 		ldevidp->key = mdnp->key;
38060Sstevel@tonic-gate 		*ldevidpp = ldevidp;
38070Sstevel@tonic-gate 	} else {
38080Sstevel@tonic-gate 		for (tmp = ldevidp; (tmp != NULL); tmp = tmp->next) {
38090Sstevel@tonic-gate 			if (strcmp(tmp->ctdname, lcname) == 0) {
38100Sstevel@tonic-gate 				/* already there so just return */
38110Sstevel@tonic-gate 				Free(lcname);
38120Sstevel@tonic-gate 				return;
38130Sstevel@tonic-gate 			}
38140Sstevel@tonic-gate 			lastdevidp = tmp;
38150Sstevel@tonic-gate 		}
38160Sstevel@tonic-gate 		lldevidp = Zalloc(sizeof (mddevid_t));
38170Sstevel@tonic-gate 		lldevidp->ctdname = lcname;
38180Sstevel@tonic-gate 		lldevidp->key = mdnp->key;
38190Sstevel@tonic-gate 		lastdevidp->next = lldevidp;
38200Sstevel@tonic-gate 	}
38210Sstevel@tonic-gate }
3822