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
5*1896Sjkennedy  * Common Development and Distribution License (the "License").
6*1896Sjkennedy  * 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 /*
22*1896Sjkennedy  * 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 <dlfcn.h>
290Sstevel@tonic-gate #include <meta.h>
300Sstevel@tonic-gate #include <metadyn.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <dirent.h>
330Sstevel@tonic-gate #include <devid.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/scsi/impl/uscsi.h>
360Sstevel@tonic-gate #include <sys/scsi/generic/commands.h>
370Sstevel@tonic-gate #include <sys/scsi/generic/inquiry.h>
380Sstevel@tonic-gate #include <sys/efi_partition.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #define	MD_EFI_FG_HEADS		128
410Sstevel@tonic-gate #define	MD_EFI_FG_SECTORS	256
420Sstevel@tonic-gate #define	MD_EFI_FG_RPM		7200
430Sstevel@tonic-gate #define	MD_EFI_FG_WRI		1
440Sstevel@tonic-gate #define	MD_EFI_FG_RRI		1
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 
470Sstevel@tonic-gate typedef struct ctlr_cache {
480Sstevel@tonic-gate 	char			*ctlr_nm;
490Sstevel@tonic-gate 	int			ctlr_ty;
500Sstevel@tonic-gate 	struct	ctlr_cache	*ctlr_nx;
510Sstevel@tonic-gate } ctlr_cache_t;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static	ctlr_cache_t	*ctlr_cache = NULL;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * return set for a device
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate mdsetname_t *
600Sstevel@tonic-gate metagetset(
610Sstevel@tonic-gate 	mdname_t	*np,
620Sstevel@tonic-gate 	int		bypass_daemon,
630Sstevel@tonic-gate 	md_error_t	*ep
640Sstevel@tonic-gate )
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	mdsetname_t	*sp;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	/* metadevice */
690Sstevel@tonic-gate 	if (metaismeta(np))
700Sstevel@tonic-gate 		return (metasetnosetname(MD_MIN2SET(meta_getminor(np->dev)),
710Sstevel@tonic-gate 						ep));
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	/* regular device */
740Sstevel@tonic-gate 	if (meta_is_drive_in_anyset(np->drivenamep, &sp, bypass_daemon,
750Sstevel@tonic-gate 	    ep) != 0)
760Sstevel@tonic-gate 		return (NULL);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	if (sp != NULL)
790Sstevel@tonic-gate 		return (sp);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	return (metasetnosetname(MD_LOCAL_SET, ep));
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * convert system to md types
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate static void
880Sstevel@tonic-gate meta_geom_to_md(
890Sstevel@tonic-gate 	struct dk_geom	*gp,
900Sstevel@tonic-gate 	mdgeom_t	*mdgp
910Sstevel@tonic-gate )
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	(void) memset(mdgp, '\0', sizeof (*mdgp));
940Sstevel@tonic-gate 	mdgp->ncyl = gp->dkg_ncyl;
950Sstevel@tonic-gate 	mdgp->nhead = gp->dkg_nhead;
960Sstevel@tonic-gate 	mdgp->nsect = gp->dkg_nsect;
970Sstevel@tonic-gate 	mdgp->rpm = gp->dkg_rpm;
980Sstevel@tonic-gate 	mdgp->write_reinstruct = gp->dkg_write_reinstruct;
990Sstevel@tonic-gate 	mdgp->read_reinstruct = gp->dkg_read_reinstruct;
1000Sstevel@tonic-gate 	mdgp->blk_sz = DEV_BSIZE;
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * convert efi to md types
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate static void
1070Sstevel@tonic-gate meta_efi_to_mdgeom(struct dk_gpt *gpt, mdgeom_t	*mdgp)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	(void) memset(mdgp, '\0', sizeof (*mdgp));
1100Sstevel@tonic-gate 	mdgp->ncyl = (gpt->efi_last_u_lba - gpt->efi_first_u_lba) /
1110Sstevel@tonic-gate 					(MD_EFI_FG_HEADS * MD_EFI_FG_SECTORS);
1120Sstevel@tonic-gate 	mdgp->nhead = MD_EFI_FG_HEADS;
1130Sstevel@tonic-gate 	mdgp->nsect = MD_EFI_FG_SECTORS;
1140Sstevel@tonic-gate 	mdgp->rpm = MD_EFI_FG_RPM;
1150Sstevel@tonic-gate 	mdgp->write_reinstruct = MD_EFI_FG_WRI;
1160Sstevel@tonic-gate 	mdgp->read_reinstruct = MD_EFI_FG_RRI;
1170Sstevel@tonic-gate 	mdgp->blk_sz = DEV_BSIZE;
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate static void
1210Sstevel@tonic-gate meta_efi_to_mdvtoc(struct dk_gpt *gpt, mdvtoc_t *mdvp)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	char		typename[EFI_PART_NAME_LEN];
1240Sstevel@tonic-gate 	uint_t		i;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	(void) memset(mdvp, '\0', sizeof (*mdvp));
1270Sstevel@tonic-gate 	mdvp->nparts = gpt->efi_nparts;
1280Sstevel@tonic-gate 	if (mdvp->nparts > MD_MAX_PARTS)
1290Sstevel@tonic-gate 		return;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	mdvp->first_lba = gpt->efi_first_u_lba;
1320Sstevel@tonic-gate 	mdvp->last_lba = gpt->efi_last_u_lba;
1330Sstevel@tonic-gate 	mdvp->lbasize = gpt->efi_lbasize;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	for (i = 0; (i < gpt->efi_nparts); ++i) {
1360Sstevel@tonic-gate 		mdvp->parts[i].start = gpt->efi_parts[i].p_start;
1370Sstevel@tonic-gate 		mdvp->parts[i].size = gpt->efi_parts[i].p_size;
1380Sstevel@tonic-gate 		mdvp->parts[i].tag = gpt->efi_parts[i].p_tag;
1390Sstevel@tonic-gate 		mdvp->parts[i].flag = gpt->efi_parts[i].p_flag;
1400Sstevel@tonic-gate 		/*
141*1896Sjkennedy 		 * It is possible to present an efi label but be using vtoc
142*1896Sjkennedy 		 * disks to create a > 1 TB metadevice.  In case the first
143*1896Sjkennedy 		 * disk in the underlying metadevice is a vtoc disk and starts
144*1896Sjkennedy 		 * at the beginning of the disk it is necessary to convey this
145*1896Sjkennedy 		 * information to the user.
146*1896Sjkennedy 		 */
147*1896Sjkennedy 		if (mdvp->parts[i].size > 0 &&
148*1896Sjkennedy 		    mdvp->parts[i].start != 0 && mdvp->nparts == 1) {
149*1896Sjkennedy 			mdvp->parts[i].label = btodb(DK_LABEL_SIZE);
150*1896Sjkennedy 			mdvp->parts[i].start = 0;
151*1896Sjkennedy 		}
152*1896Sjkennedy 
153*1896Sjkennedy 		/*
1540Sstevel@tonic-gate 		 * Due to the lack of a label for the entire partition table,
1550Sstevel@tonic-gate 		 * we use p_name of the reserved partition
1560Sstevel@tonic-gate 		 */
1570Sstevel@tonic-gate 		if ((gpt->efi_parts[i].p_tag == V_RESERVED) &&
1580Sstevel@tonic-gate 		    (gpt->efi_parts[i].p_name != NULL)) {
1590Sstevel@tonic-gate 			(void) strlcpy(typename, gpt->efi_parts[i].p_name,
1600Sstevel@tonic-gate 					EFI_PART_NAME_LEN);
1610Sstevel@tonic-gate 			/* Stop at first (if any) space or tab */
1620Sstevel@tonic-gate 			(void) strtok(typename, " \t");
1630Sstevel@tonic-gate 			mdvp->typename = Strdup(typename);
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static void
1690Sstevel@tonic-gate meta_mdvtoc_to_efi(mdvtoc_t *mdvp, struct dk_gpt **gpt)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	char		typename[EFI_PART_NAME_LEN];
1720Sstevel@tonic-gate 	uint_t		i;
1730Sstevel@tonic-gate 	uint_t		lastpart;
1740Sstevel@tonic-gate 	size_t		size;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/* first we count how many partitions we have to send */
1770Sstevel@tonic-gate 	for (i = 0; i < MD_MAX_PARTS; i++) {
1780Sstevel@tonic-gate 		if ((mdvp->parts[i].start == 0) &&
1790Sstevel@tonic-gate 		    (mdvp->parts[i].size == 0) &&
1800Sstevel@tonic-gate 		    (mdvp->parts[i].tag != V_RESERVED)) {
1810Sstevel@tonic-gate 			continue;
1820Sstevel@tonic-gate 		}
1830Sstevel@tonic-gate 		/* if we are here, we know the partition is really used */
1840Sstevel@tonic-gate 		lastpart = i;
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 	size = sizeof (struct dk_gpt) + (sizeof (struct dk_part) * lastpart);
1870Sstevel@tonic-gate 	*gpt = calloc(size, sizeof (char));
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	(*gpt)->efi_nparts = lastpart + 1;
1900Sstevel@tonic-gate 	(*gpt)->efi_first_u_lba = mdvp->first_lba;
1910Sstevel@tonic-gate 	(*gpt)->efi_last_u_lba = mdvp->last_lba;
1920Sstevel@tonic-gate 	(*gpt)->efi_lbasize = mdvp->lbasize;
1930Sstevel@tonic-gate 	for (i = 0; (i < (*gpt)->efi_nparts); ++i) {
1940Sstevel@tonic-gate 		(*gpt)->efi_parts[i].p_start = mdvp->parts[i].start;
1950Sstevel@tonic-gate 		(*gpt)->efi_parts[i].p_size = mdvp->parts[i].size;
1960Sstevel@tonic-gate 		(*gpt)->efi_parts[i].p_tag = mdvp->parts[i].tag;
1970Sstevel@tonic-gate 		(*gpt)->efi_parts[i].p_flag = mdvp->parts[i].flag;
1980Sstevel@tonic-gate 		/*
1990Sstevel@tonic-gate 		 * Due to the lack of a label for the entire partition table,
2000Sstevel@tonic-gate 		 * we use p_name of the reserved partition
2010Sstevel@tonic-gate 		 */
2020Sstevel@tonic-gate 		if (((*gpt)->efi_parts[i].p_tag == V_RESERVED) &&
2030Sstevel@tonic-gate 			(mdvp->typename != NULL)) {
2040Sstevel@tonic-gate 			(void) strlcpy((*gpt)->efi_parts[i].p_name, typename,
2050Sstevel@tonic-gate 				EFI_PART_NAME_LEN);
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate void
2120Sstevel@tonic-gate ctlr_cache_add(char *nm, int ty)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	ctlr_cache_t	**ccpp;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	for (ccpp = &ctlr_cache; *ccpp != NULL; ccpp = &(*ccpp)->ctlr_nx)
2170Sstevel@tonic-gate 		if (strcmp((*ccpp)->ctlr_nm, nm) == 0)
2180Sstevel@tonic-gate 			return;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	*ccpp = Zalloc(sizeof (ctlr_cache_t));
2210Sstevel@tonic-gate 	(*ccpp)->ctlr_nm = Strdup(nm);
2220Sstevel@tonic-gate 	(*ccpp)->ctlr_ty = ty;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate int
2260Sstevel@tonic-gate ctlr_cache_look(char *nm)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	ctlr_cache_t	*tcp;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	for (tcp = ctlr_cache; tcp != NULL; tcp = tcp->ctlr_nx)
2310Sstevel@tonic-gate 		if (strcmp(tcp->ctlr_nm, nm) == 0)
2320Sstevel@tonic-gate 			return (tcp->ctlr_ty);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	return (-1);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate void
2390Sstevel@tonic-gate metaflushctlrcache(void)
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate 	ctlr_cache_t	*cp, *np;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	for (cp = ctlr_cache, np = NULL; cp != NULL; cp = np) {
2440Sstevel@tonic-gate 		np = cp->ctlr_nx;
2450Sstevel@tonic-gate 		Free(cp->ctlr_nm);
2460Sstevel@tonic-gate 		Free(cp);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 	ctlr_cache = NULL;
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * getdrvnode -- return the driver name based on mdname_t->bname
2530Sstevel@tonic-gate  *	Need to free pointer when finished.
2540Sstevel@tonic-gate  */
2550Sstevel@tonic-gate char *
2560Sstevel@tonic-gate getdrvnode(mdname_t *np, md_error_t *ep)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	char	*devicespath,
2590Sstevel@tonic-gate 		*drvnode,
2600Sstevel@tonic-gate 		*cp;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	if ((devicespath = metagetdevicesname(np, ep)) == NULL)
2630Sstevel@tonic-gate 		return (NULL);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	/*
2660Sstevel@tonic-gate 	 * At this point devicespath should be like the following
2670Sstevel@tonic-gate 	 * "/devices/<unknow_and_dont_care>/xxxx@vvvv"
2680Sstevel@tonic-gate 	 *
2690Sstevel@tonic-gate 	 * There's a couple of 'if' statements below which could
2700Sstevel@tonic-gate 	 * return an error condition, but I've decide to allow
2710Sstevel@tonic-gate 	 * a more open approach regarding the mapping so as to
2720Sstevel@tonic-gate 	 * not restrict possible future projects.
2730Sstevel@tonic-gate 	 */
2740Sstevel@tonic-gate 	if (drvnode = strrchr(devicespath, '/'))
2750Sstevel@tonic-gate 		/*
2760Sstevel@tonic-gate 		 * drvnode now just "xxxx@vvvv"
2770Sstevel@tonic-gate 		 */
2780Sstevel@tonic-gate 		drvnode++;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	if (cp = strrchr(drvnode, '@'))
2810Sstevel@tonic-gate 		/*
2820Sstevel@tonic-gate 		 * Now drvnode is just the driver name "xxxx"
2830Sstevel@tonic-gate 		 */
2840Sstevel@tonic-gate 		*cp = '\0';
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	cp = Strdup(drvnode);
2870Sstevel@tonic-gate 	Free(devicespath);
2880Sstevel@tonic-gate 	np->devicesname = NULL;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	return (cp);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate  * meta_load_dl -- open dynamic library using LDLIBRARYPATH, a debug
2950Sstevel@tonic-gate  *    environment variable METALDPATH, or the default location.
2960Sstevel@tonic-gate  */
2970Sstevel@tonic-gate static void *
2980Sstevel@tonic-gate meta_load_dl(mdname_t *np, md_error_t *ep)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	char	*drvnode,
3010Sstevel@tonic-gate 		newpath[MAXPATHLEN],
3020Sstevel@tonic-gate 		*p;
3030Sstevel@tonic-gate 	void	*cookie;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	if ((drvnode = getdrvnode(np, ep)) != NULL) {
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		/*
3080Sstevel@tonic-gate 		 * Library seach algorithm:
3090Sstevel@tonic-gate 		 * 1) Use LDLIBRARYPATH which is implied when a non-absolute
3100Sstevel@tonic-gate 		 *    path name is passed to dlopen()
3110Sstevel@tonic-gate 		 * 2) Use the value of METALDPATH as the directory. Mainly
3120Sstevel@tonic-gate 		 *    used for debugging
3130Sstevel@tonic-gate 		 * 3) Last search the default location of "/usr/lib"
3140Sstevel@tonic-gate 		 */
3150Sstevel@tonic-gate 		(void) snprintf(newpath, sizeof (newpath), "lib%s.so.1",
3160Sstevel@tonic-gate 		    drvnode);
3170Sstevel@tonic-gate 		if ((cookie = dlopen(newpath, RTLD_LAZY)) == NULL) {
3180Sstevel@tonic-gate 			if ((p = getenv("METALDPATH")) == NULL)
3190Sstevel@tonic-gate 				p = METALDPATH_DEFAULT;
3200Sstevel@tonic-gate 			(void) snprintf(newpath, sizeof (newpath),
3210Sstevel@tonic-gate 			    "%s/lib%s.so.1", p, drvnode);
3220Sstevel@tonic-gate 			Free(drvnode);
3230Sstevel@tonic-gate 			if ((cookie = dlopen(newpath, RTLD_LAZY)) != NULL) {
3240Sstevel@tonic-gate 				/*
3250Sstevel@tonic-gate 				 * Common failure here would be failing to
3260Sstevel@tonic-gate 				 * find a libXX.so.1 such as libsd.so.1
3270Sstevel@tonic-gate 				 * Some controllers will not have a library
3280Sstevel@tonic-gate 				 * because there's no enclosure or name
3290Sstevel@tonic-gate 				 * translation required.
3300Sstevel@tonic-gate 				 */
3310Sstevel@tonic-gate 				return (cookie);
3320Sstevel@tonic-gate 			}
3330Sstevel@tonic-gate 		} else {
3340Sstevel@tonic-gate 			Free(drvnode);
3350Sstevel@tonic-gate 			return (cookie);
3360Sstevel@tonic-gate 		}
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 	return (NULL);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate  * meta_match_names -- possibly convert the driver names returned by CINFO
3430Sstevel@tonic-gate  */
3440Sstevel@tonic-gate static void
3450Sstevel@tonic-gate meta_match_names(mdname_t *np, struct dk_cinfo *cp, mdcinfo_t *mdcp,
3460Sstevel@tonic-gate     md_error_t *ep)
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate 	void		*cookie;
3490Sstevel@tonic-gate 	meta_convert_e	((*fptr)(mdname_t *, struct dk_cinfo *, mdcinfo_t *,
3500Sstevel@tonic-gate 			    md_error_t *));
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	if ((cookie = meta_load_dl(np, ep)) != NULL) {
3530Sstevel@tonic-gate 		fptr = (meta_convert_e (*)(mdname_t *, struct dk_cinfo *,
3540Sstevel@tonic-gate 		    mdcinfo_t *, md_error_t *))dlsym(cookie, "convert_path");
3550Sstevel@tonic-gate 		if (fptr != NULL)
3560Sstevel@tonic-gate 			(void) (*fptr)(np, cp, mdcp, ep);
3570Sstevel@tonic-gate 		(void) dlclose(cookie);
3580Sstevel@tonic-gate 	}
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate /*
3620Sstevel@tonic-gate  * meta_match_enclosure -- return any enclosure info if found
3630Sstevel@tonic-gate  */
3640Sstevel@tonic-gate int
3650Sstevel@tonic-gate meta_match_enclosure(mdname_t *np, mdcinfo_t *mdcp, md_error_t *ep)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 	meta_enclosure_e	e,
3680Sstevel@tonic-gate 				((*fptr)(mdname_t *, mdcinfo_t *,
3690Sstevel@tonic-gate 				    md_error_t *));
3700Sstevel@tonic-gate 	void			*cookie;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	if ((cookie = meta_load_dl(np, ep)) != NULL) {
3730Sstevel@tonic-gate 		fptr = (meta_enclosure_e (*)(mdname_t *, mdcinfo_t *,
3740Sstevel@tonic-gate 		    md_error_t *))dlsym(cookie, "get_enclosure");
3750Sstevel@tonic-gate 		if (fptr != NULL) {
3760Sstevel@tonic-gate 			e = (*fptr)(np, mdcp, ep);
3770Sstevel@tonic-gate 			switch (e) {
3780Sstevel@tonic-gate 			case Enclosure_Error:
3790Sstevel@tonic-gate 				/*
3800Sstevel@tonic-gate 				 * Looks like this library wanted to handle
3810Sstevel@tonic-gate 				 * our device and had an internal error.
3820Sstevel@tonic-gate 				 */
3830Sstevel@tonic-gate 				return (1);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 			case Enclosure_Okay:
3860Sstevel@tonic-gate 				/*
3870Sstevel@tonic-gate 				 * Found a library to handle the request so
3880Sstevel@tonic-gate 				 * just return with data provided.
3890Sstevel@tonic-gate 				 */
3900Sstevel@tonic-gate 				return (0);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 			case Enclosure_Noop:
3930Sstevel@tonic-gate 				/*
3940Sstevel@tonic-gate 				 * Need to continue the search
3950Sstevel@tonic-gate 				 */
3960Sstevel@tonic-gate 				break;
3970Sstevel@tonic-gate 			}
3980Sstevel@tonic-gate 		}
3990Sstevel@tonic-gate 		(void) dlclose(cookie);
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 	return (0);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate static int
4050Sstevel@tonic-gate meta_cinfo_to_md(mdname_t *np, struct dk_cinfo *cp, mdcinfo_t *mdcp,
4060Sstevel@tonic-gate     md_error_t *ep)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	/* default */
4090Sstevel@tonic-gate 	(void) memset(mdcp, '\0', sizeof (*mdcp));
4100Sstevel@tonic-gate 	(void) strncpy(mdcp->cname, cp->dki_cname,
4110Sstevel@tonic-gate 	    min((sizeof (mdcp->cname) - 1), sizeof (cp->dki_cname)));
4120Sstevel@tonic-gate 	mdcp->ctype = MHD_CTLR_GENERIC;
4130Sstevel@tonic-gate 	mdcp->cnum = cp->dki_cnum;
4140Sstevel@tonic-gate 	(void) strncpy(mdcp->dname, cp->dki_dname,
4150Sstevel@tonic-gate 	    min((sizeof (mdcp->dname) - 1), sizeof (cp->dki_dname)));
4160Sstevel@tonic-gate 	mdcp->unit = cp->dki_unit;
4170Sstevel@tonic-gate 	mdcp->maxtransfer = cp->dki_maxtransfer;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	/*
4200Sstevel@tonic-gate 	 * See if the driver name returned from DKIOCINFO
4210Sstevel@tonic-gate 	 * is valid or not. In somecases, such as the ap_dmd
4220Sstevel@tonic-gate 	 * driver, we need to modify the name that's return
4230Sstevel@tonic-gate 	 * for everything to work.
4240Sstevel@tonic-gate 	 */
4250Sstevel@tonic-gate 	meta_match_names(np, cp, mdcp, ep);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	if (meta_match_enclosure(np, mdcp, ep))
4280Sstevel@tonic-gate 		return (-1);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	/* return success */
4310Sstevel@tonic-gate 	return (0);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate static void
4350Sstevel@tonic-gate meta_vtoc_to_md(
4360Sstevel@tonic-gate 	struct vtoc	*vp,
4370Sstevel@tonic-gate 	mdvtoc_t	*mdvp
4380Sstevel@tonic-gate )
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	char		typename[sizeof (vp->v_asciilabel) + 1];
4410Sstevel@tonic-gate 	uint_t		i;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	(void) memset(mdvp, '\0', sizeof (*mdvp));
4440Sstevel@tonic-gate 	(void) strncpy(typename, vp->v_asciilabel,
4450Sstevel@tonic-gate 	    sizeof (vp->v_asciilabel));
4460Sstevel@tonic-gate 	typename[sizeof (typename) - 1] = '\0';
4470Sstevel@tonic-gate 	for (i = 0; ((i < sizeof (typename)) && (typename[i] != '\0')); ++i) {
4480Sstevel@tonic-gate 		if ((typename[i] == ' ') || (typename[i] == '\t')) {
4490Sstevel@tonic-gate 			typename[i] = '\0';
4500Sstevel@tonic-gate 			break;
4510Sstevel@tonic-gate 		}
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 	mdvp->typename = Strdup(typename);
4540Sstevel@tonic-gate 	mdvp->nparts = vp->v_nparts;
4550Sstevel@tonic-gate 	for (i = 0; (i < vp->v_nparts); ++i) {
4560Sstevel@tonic-gate 		mdvp->parts[i].start = vp->v_part[i].p_start;
4570Sstevel@tonic-gate 		mdvp->parts[i].size = vp->v_part[i].p_size;
4580Sstevel@tonic-gate 		mdvp->parts[i].tag = vp->v_part[i].p_tag;
4590Sstevel@tonic-gate 		mdvp->parts[i].flag = vp->v_part[i].p_flag;
4600Sstevel@tonic-gate 		if (vp->v_part[i].p_start == 0 && vp->v_part[i].p_size > 0)
4610Sstevel@tonic-gate 			mdvp->parts[i].label = btodb(DK_LABEL_SIZE);
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate  * free allocations in vtoc
4670Sstevel@tonic-gate  */
4680Sstevel@tonic-gate void
4690Sstevel@tonic-gate metafreevtoc(
4700Sstevel@tonic-gate 	mdvtoc_t	*vtocp
4710Sstevel@tonic-gate )
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate 	if (vtocp->typename != NULL)
4740Sstevel@tonic-gate 		Free(vtocp->typename);
4750Sstevel@tonic-gate 	(void) memset(vtocp, 0, sizeof (*vtocp));
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate /*
4790Sstevel@tonic-gate  * return md types
4800Sstevel@tonic-gate  */
4810Sstevel@tonic-gate mdvtoc_t *
4820Sstevel@tonic-gate metagetvtoc(
4830Sstevel@tonic-gate 	mdname_t	*np,	/* only rname, drivenamep, are setup */
4840Sstevel@tonic-gate 	int		nocache,
4850Sstevel@tonic-gate 	uint_t		*partnop,
4860Sstevel@tonic-gate 	md_error_t	*ep
4870Sstevel@tonic-gate )
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate 	mddrivename_t	*dnp = np->drivenamep;
4900Sstevel@tonic-gate 	struct dk_geom	geom;
4910Sstevel@tonic-gate 	char		*minor_name = NULL;
4920Sstevel@tonic-gate 	char		*rname = np->rname;
4930Sstevel@tonic-gate 	int		fd;
4940Sstevel@tonic-gate 	int		partno;
4950Sstevel@tonic-gate 	int		err = 0;	    /* saves errno from ioctl */
4960Sstevel@tonic-gate 	ddi_devid_t	devid;
4970Sstevel@tonic-gate 	char		*p;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	/* short circuit */
5000Sstevel@tonic-gate 	if ((! nocache) && (dnp->vtoc.nparts != 0)) {
5010Sstevel@tonic-gate 		if (partnop != NULL) {
5020Sstevel@tonic-gate 			/*
5030Sstevel@tonic-gate 			 * the following assigment works because the
5040Sstevel@tonic-gate 			 * mdname_t structs are always created as part
5050Sstevel@tonic-gate 			 * of the drivenamep struct.  When a user
5060Sstevel@tonic-gate 			 * creates an mdname_t struct it either
5070Sstevel@tonic-gate 			 * uses an existing drivenamep struct or creates
5080Sstevel@tonic-gate 			 * a new one and then adds the mdname_t struct
5090Sstevel@tonic-gate 			 * as part of its parts_val array.  So what is
5100Sstevel@tonic-gate 			 * being computed below is the slice offset in
5110Sstevel@tonic-gate 			 * the parts_val array.
5120Sstevel@tonic-gate 			 */
5130Sstevel@tonic-gate 			*partnop = np - np->drivenamep->parts.parts_val;
5140Sstevel@tonic-gate 			assert(*partnop < dnp->parts.parts_len);
5150Sstevel@tonic-gate 		}
5160Sstevel@tonic-gate 		return (&dnp->vtoc);
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	/* can't get vtoc */
5200Sstevel@tonic-gate 	if (! nocache) {
5210Sstevel@tonic-gate 		switch (dnp->type) {
5220Sstevel@tonic-gate 		case MDT_ACCES:
5230Sstevel@tonic-gate 		case MDT_UNKNOWN:
5240Sstevel@tonic-gate 			(void) mdsyserror(ep, dnp->errnum, rname);
5250Sstevel@tonic-gate 			return (NULL);
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	/* get all the info */
5300Sstevel@tonic-gate 	if ((fd = open(rname, (O_RDONLY|O_NDELAY), 0)) < 0) {
5310Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, rname);
5320Sstevel@tonic-gate 		return (NULL);
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	/*
5360Sstevel@tonic-gate 	 * The disk is open so this is a good point to get the devid
5370Sstevel@tonic-gate 	 * otherwise it will need to be done at another time which
5380Sstevel@tonic-gate 	 * means reopening it.
5390Sstevel@tonic-gate 	 */
5400Sstevel@tonic-gate 	if (devid_get(fd, &devid) != 0) {
5410Sstevel@tonic-gate 		/* there is no devid for the disk */
5420Sstevel@tonic-gate 		if (((p = getenv("MD_DEBUG")) != NULL) &&
5430Sstevel@tonic-gate 		    (strstr(p, "DEVID") != NULL)) {
5440Sstevel@tonic-gate 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
5450Sstevel@tonic-gate 			    "%s has no device id\n"), np->rname);
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 		np->minor_name = (char *)NULL;
5480Sstevel@tonic-gate 		dnp->devid = NULL;
5490Sstevel@tonic-gate 	} else {
5500Sstevel@tonic-gate 		(void) devid_get_minor_name(fd, &minor_name);
5510Sstevel@tonic-gate 		/*
5520Sstevel@tonic-gate 		 * The minor name could be NULL if the underlying
5530Sstevel@tonic-gate 		 * device driver does not support 'minor names'.
5540Sstevel@tonic-gate 		 * This means we do not use devid's for this device.
5550Sstevel@tonic-gate 		 * SunCluster did driver does not support minor names.
5560Sstevel@tonic-gate 		 */
5570Sstevel@tonic-gate 		if (minor_name != NULL) {
5580Sstevel@tonic-gate 			np->minor_name = Strdup(minor_name);
5590Sstevel@tonic-gate 			devid_str_free(minor_name);
5600Sstevel@tonic-gate 			dnp->devid = devid_str_encode(devid, NULL);
5610Sstevel@tonic-gate 		} else {
5620Sstevel@tonic-gate 			np->minor_name = (char *)NULL;
5630Sstevel@tonic-gate 			dnp->devid = NULL;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 			if (((p = getenv("MD_DEBUG")) != NULL) &&
5660Sstevel@tonic-gate 			    (strstr(p, "DEVID") != NULL)) {
5670Sstevel@tonic-gate 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
5680Sstevel@tonic-gate 				    "%s no minor name (no devid)\n"),
5690Sstevel@tonic-gate 				    np->rname);
5700Sstevel@tonic-gate 			}
5710Sstevel@tonic-gate 		}
5720Sstevel@tonic-gate 		devid_free(devid);
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	/*
5760Sstevel@tonic-gate 	 * if our drivenamep points to a device not supporting DKIOCGGEOM,
5770Sstevel@tonic-gate 	 * it's likely to have an EFI label.
5780Sstevel@tonic-gate 	 */
5790Sstevel@tonic-gate 	(void) memset(&geom, 0, sizeof (geom));
5800Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGGEOM, &geom) != 0) {
5810Sstevel@tonic-gate 		err = errno;
5820Sstevel@tonic-gate 		if (err == ENOTTY) {
5830Sstevel@tonic-gate 			(void) mddeverror(ep, MDE_NOT_DISK, NODEV, rname);
5840Sstevel@tonic-gate 			(void) close(fd);
5850Sstevel@tonic-gate 			return (NULL);
5860Sstevel@tonic-gate 		} else if (err != ENOTSUP) {
5870Sstevel@tonic-gate 			(void) mdsyserror(ep, err, rname);
5880Sstevel@tonic-gate 			(void) close(fd);
5890Sstevel@tonic-gate 			return (NULL);
5900Sstevel@tonic-gate 		}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 	/*
5940Sstevel@tonic-gate 	 * If we are here, there was either no failure on DKIOCGGEOM or
5950Sstevel@tonic-gate 	 * the failure was ENOTSUP
5960Sstevel@tonic-gate 	 */
5970Sstevel@tonic-gate 	if (err == ENOTSUP) {
5980Sstevel@tonic-gate 		/* DKIOCGGEOM yielded ENOTSUP => try efi_alloc_and_read */
5990Sstevel@tonic-gate 		struct dk_gpt	*gpt;
6000Sstevel@tonic-gate 		int		save_errno;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 		/* this also sets errno */
6030Sstevel@tonic-gate 		partno = efi_alloc_and_read(fd, &gpt);
6040Sstevel@tonic-gate 		save_errno = errno;
6050Sstevel@tonic-gate 		(void) close(fd);
6060Sstevel@tonic-gate 		if (partno < 0) {
6070Sstevel@tonic-gate 			efi_free(gpt);
6080Sstevel@tonic-gate 			(void) mdsyserror(ep, save_errno, rname);
6090Sstevel@tonic-gate 			return (NULL);
6100Sstevel@tonic-gate 		}
6110Sstevel@tonic-gate 		if (partno >= gpt->efi_nparts) {
6120Sstevel@tonic-gate 			efi_free(gpt);
6130Sstevel@tonic-gate 			(void) mddeverror(ep, MDE_INVALID_PART, NODEV64,
6140Sstevel@tonic-gate 						rname);
6150Sstevel@tonic-gate 			return (NULL);
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 		/* convert to our format */
6190Sstevel@tonic-gate 		metafreevtoc(&dnp->vtoc);
6200Sstevel@tonic-gate 		meta_efi_to_mdvtoc(gpt, &dnp->vtoc);
6210Sstevel@tonic-gate 		if (dnp->vtoc.nparts > MD_MAX_PARTS) {
6220Sstevel@tonic-gate 			(void) mddeverror(ep, MDE_TOO_MANY_PARTS, NODEV64,
6230Sstevel@tonic-gate 			    rname);
6240Sstevel@tonic-gate 			return (NULL);
6250Sstevel@tonic-gate 		}
6260Sstevel@tonic-gate 		/*
6270Sstevel@tonic-gate 		 * libmeta needs at least V_NUMPAR partitions.
6280Sstevel@tonic-gate 		 * If we have an EFI partition with less than V_NUMPAR slices,
6290Sstevel@tonic-gate 		 * we nevertheless reserve space for V_NUMPAR
6300Sstevel@tonic-gate 		 */
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 		if (dnp->vtoc.nparts < V_NUMPAR) {
6330Sstevel@tonic-gate 			dnp->vtoc.nparts = V_NUMPAR;
6340Sstevel@tonic-gate 		}
6350Sstevel@tonic-gate 		meta_efi_to_mdgeom(gpt, &dnp->geom);
6360Sstevel@tonic-gate 		efi_free(gpt);
6370Sstevel@tonic-gate 	} else {
6380Sstevel@tonic-gate 		/* no error on DKIOCGGEOM, try meta_getvtoc */
6390Sstevel@tonic-gate 		struct vtoc	vtoc;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 		if (meta_getvtoc(fd, np->cname, &vtoc, &partno, ep) < 0) {
6420Sstevel@tonic-gate 			(void) close(fd);
6430Sstevel@tonic-gate 			return (NULL);
6440Sstevel@tonic-gate 		}
6450Sstevel@tonic-gate 		(void) close(fd);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 		/* convert to our format */
6480Sstevel@tonic-gate 		meta_geom_to_md(&geom, &dnp->geom);
6490Sstevel@tonic-gate 		metafreevtoc(&dnp->vtoc);
6500Sstevel@tonic-gate 		meta_vtoc_to_md(&vtoc, &dnp->vtoc);
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/* fix up any drives which are now accessible */
6540Sstevel@tonic-gate 	if ((nocache) && (dnp->type == MDT_ACCES) &&
6550Sstevel@tonic-gate 	    (dnp->vtoc.nparts == dnp->parts.parts_len)) {
6560Sstevel@tonic-gate 		dnp->type = MDT_COMP;
6570Sstevel@tonic-gate 		dnp->errnum = 0;
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	/* save partno */
6610Sstevel@tonic-gate 	assert(partno < dnp->vtoc.nparts);
6620Sstevel@tonic-gate 	if (partnop != NULL)
6630Sstevel@tonic-gate 		*partnop = partno;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	/* return info */
6660Sstevel@tonic-gate 	return (&dnp->vtoc);
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate static void
6700Sstevel@tonic-gate meta_mdvtoc_to_vtoc(
6710Sstevel@tonic-gate 	mdvtoc_t	*mdvp,
6720Sstevel@tonic-gate 	struct vtoc	*vp
6730Sstevel@tonic-gate )
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate 	uint_t		i;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	(void) memset(&vp->v_part, '\0', sizeof (vp->v_part));
6780Sstevel@tonic-gate 	vp->v_nparts = (ushort_t)mdvp->nparts;
6790Sstevel@tonic-gate 	for (i = 0; (i < mdvp->nparts); ++i) {
6800Sstevel@tonic-gate 		vp->v_part[i].p_start = (daddr32_t)mdvp->parts[i].start;
6810Sstevel@tonic-gate 		vp->v_part[i].p_size  = (daddr32_t)mdvp->parts[i].size;
6820Sstevel@tonic-gate 		vp->v_part[i].p_tag   = mdvp->parts[i].tag;
6830Sstevel@tonic-gate 		vp->v_part[i].p_flag  = mdvp->parts[i].flag;
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate /*
6880Sstevel@tonic-gate  * Set the vtoc, but use the cached copy to get the info from.
6890Sstevel@tonic-gate  * We write np->drivenamep->vtoc to disk.
6900Sstevel@tonic-gate  * Before we can do this we read the vtoc in.
6910Sstevel@tonic-gate  * if we're dealing with a metadevice and this metadevice is a 64 bit device
6920Sstevel@tonic-gate  *	we can use meta_getmdvtoc/meta_setmdvtoc
6930Sstevel@tonic-gate  * else
6940Sstevel@tonic-gate  * 	we use meta_getvtoc/meta_setvtoc but than we first have to convert
6950Sstevel@tonic-gate  *	dnp->vtoc (actually being a mdvtoc_t) into a vtoc_t
6960Sstevel@tonic-gate  */
6970Sstevel@tonic-gate int
6980Sstevel@tonic-gate metasetvtoc(
6990Sstevel@tonic-gate 	mdname_t	*np,
7000Sstevel@tonic-gate 	md_error_t	*ep
7010Sstevel@tonic-gate )
7020Sstevel@tonic-gate {
7030Sstevel@tonic-gate 	char		*rname = np->rname;
7040Sstevel@tonic-gate 	mddrivename_t	*dnp = np->drivenamep;
7050Sstevel@tonic-gate 	int		fd;
7060Sstevel@tonic-gate 	int		err;
7070Sstevel@tonic-gate 	int 		save_errno;
7080Sstevel@tonic-gate 	struct dk_geom	geom;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	if ((fd = open(rname, (O_RDONLY | O_NDELAY), 0)) < 0)
7110Sstevel@tonic-gate 		return (mdsyserror(ep, errno, rname));
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	err = ioctl(fd, DKIOCGGEOM, &geom);
7140Sstevel@tonic-gate 	save_errno = errno;
7150Sstevel@tonic-gate 	if (err == 0) {
7160Sstevel@tonic-gate 		struct vtoc	vtoc;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		if (meta_getvtoc(fd, np->cname, &vtoc, NULL, ep) < 0) {
7190Sstevel@tonic-gate 			(void) close(fd);
7200Sstevel@tonic-gate 			return (-1);
7210Sstevel@tonic-gate 		}
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		meta_mdvtoc_to_vtoc(&dnp->vtoc, &vtoc);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 		if (meta_setvtoc(fd, np->cname, &vtoc, ep) < 0) {
7260Sstevel@tonic-gate 			(void) close(fd);
7270Sstevel@tonic-gate 			return (-1);
7280Sstevel@tonic-gate 		}
7290Sstevel@tonic-gate 	} else if (save_errno == ENOTSUP) {
7300Sstevel@tonic-gate 		struct dk_gpt	*gpt;
7310Sstevel@tonic-gate 		int		ret;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 		/* allocation of gpt is done in meta_mdvtoc_to_efi */
7340Sstevel@tonic-gate 		meta_mdvtoc_to_efi(&dnp->vtoc, &gpt);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 		ret = efi_write(fd, gpt);
7370Sstevel@tonic-gate 		save_errno = errno;
7380Sstevel@tonic-gate 		free(gpt);
7390Sstevel@tonic-gate 		if (ret != 0) {
7400Sstevel@tonic-gate 			(void) close(fd);
7410Sstevel@tonic-gate 			return (mdsyserror(ep, save_errno, rname));
7420Sstevel@tonic-gate 		} else {
7430Sstevel@tonic-gate 			(void) close(fd);
7440Sstevel@tonic-gate 			return (0);
7450Sstevel@tonic-gate 		}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	} else {
7480Sstevel@tonic-gate 		(void) close(fd);
7490Sstevel@tonic-gate 		return (mdsyserror(ep, save_errno, rname));
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	(void) close(fd);
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	return (0);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate mdgeom_t *
7580Sstevel@tonic-gate metagetgeom(
7590Sstevel@tonic-gate 	mdname_t	*np,	/* only rname, drivenamep, are setup */
7600Sstevel@tonic-gate 	md_error_t	*ep
7610Sstevel@tonic-gate )
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate 	if (metagetvtoc(np, FALSE, NULL, ep) == NULL)
7640Sstevel@tonic-gate 		return (NULL);
7650Sstevel@tonic-gate 	return (&np->drivenamep->geom);
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate mdcinfo_t *
7690Sstevel@tonic-gate metagetcinfo(
7700Sstevel@tonic-gate 	mdname_t	*np,	/* only rname, drivenamep, are setup */
7710Sstevel@tonic-gate 	md_error_t	*ep
7720Sstevel@tonic-gate )
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate 	char			*rname = np->rname;
7750Sstevel@tonic-gate 	mddrivename_t		*dnp = np->drivenamep;
7760Sstevel@tonic-gate 	int			fd;
7770Sstevel@tonic-gate 	struct dk_cinfo		cinfo;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	/* short circuit */
7800Sstevel@tonic-gate 	if (dnp->cinfo.cname[0] != '\0')
7810Sstevel@tonic-gate 		return (&dnp->cinfo);
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	/* get controller info */
7840Sstevel@tonic-gate 	if ((fd = open(rname, (O_RDONLY|O_NDELAY), 0)) < 0) {
7850Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, rname);
7860Sstevel@tonic-gate 		return (NULL);
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, &cinfo) != 0) {
7890Sstevel@tonic-gate 		int	save = errno;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 		(void) close(fd);
7920Sstevel@tonic-gate 		if (save == ENOTTY) {
7930Sstevel@tonic-gate 			(void) mddeverror(ep, MDE_NOT_DISK, NODEV64, rname);
7940Sstevel@tonic-gate 		} else {
7950Sstevel@tonic-gate 			(void) mdsyserror(ep, save, rname);
7960Sstevel@tonic-gate 		}
7970Sstevel@tonic-gate 		return (NULL);
7980Sstevel@tonic-gate 	}
7990Sstevel@tonic-gate 	(void) close(fd);	/* sd/ssd bug */
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	/* convert to our format */
8020Sstevel@tonic-gate 	if (meta_cinfo_to_md(np, &cinfo, &dnp->cinfo, ep) != 0)
8030Sstevel@tonic-gate 		return (NULL);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	/* return info */
8060Sstevel@tonic-gate 	return (&dnp->cinfo);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate /*
8100Sstevel@tonic-gate  * get partition number
8110Sstevel@tonic-gate  */
8120Sstevel@tonic-gate int
8130Sstevel@tonic-gate metagetpartno(
8140Sstevel@tonic-gate 	mdname_t	*np,
8150Sstevel@tonic-gate 	md_error_t	*ep
8160Sstevel@tonic-gate )
8170Sstevel@tonic-gate {
8180Sstevel@tonic-gate 	mdvtoc_t	*vtocp;
8190Sstevel@tonic-gate 	uint_t		partno;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	if ((vtocp = metagetvtoc(np, FALSE, &partno, ep)) == NULL)
8220Sstevel@tonic-gate 		return (-1);
8230Sstevel@tonic-gate 	assert(partno < vtocp->nparts);
8240Sstevel@tonic-gate 	return (partno);
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate /*
8280Sstevel@tonic-gate  * get size of device
8290Sstevel@tonic-gate  */
8300Sstevel@tonic-gate diskaddr_t
8310Sstevel@tonic-gate metagetsize(
8320Sstevel@tonic-gate 	mdname_t	*np,
8330Sstevel@tonic-gate 	md_error_t	*ep
8340Sstevel@tonic-gate )
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	mdvtoc_t	*vtocp;
8370Sstevel@tonic-gate 	uint_t		partno;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	if ((vtocp = metagetvtoc(np, FALSE, &partno, ep)) == NULL)
8400Sstevel@tonic-gate 		return (MD_DISKADDR_ERROR);
8410Sstevel@tonic-gate 	assert(partno < vtocp->nparts);
8420Sstevel@tonic-gate 	return (vtocp->parts[partno].size);
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate /*
8460Sstevel@tonic-gate  * get label of device
8470Sstevel@tonic-gate  */
8480Sstevel@tonic-gate diskaddr_t
8490Sstevel@tonic-gate metagetlabel(
8500Sstevel@tonic-gate 	mdname_t	*np,
8510Sstevel@tonic-gate 	md_error_t	*ep
8520Sstevel@tonic-gate )
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate 	mdvtoc_t	*vtocp;
8550Sstevel@tonic-gate 	uint_t		partno;
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	if ((vtocp = metagetvtoc(np, FALSE, &partno, ep)) == NULL)
8580Sstevel@tonic-gate 		return (MD_DISKADDR_ERROR);
8590Sstevel@tonic-gate 	assert(partno < vtocp->nparts);
8600Sstevel@tonic-gate 	return (vtocp->parts[partno].label);
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate /*
8640Sstevel@tonic-gate  * find out where database replicas end
8650Sstevel@tonic-gate  */
8660Sstevel@tonic-gate static int
8670Sstevel@tonic-gate mddb_getendblk(
8680Sstevel@tonic-gate 	mdsetname_t		*sp,
8690Sstevel@tonic-gate 	mdname_t		*np,
8700Sstevel@tonic-gate 	diskaddr_t		*endblkp,
8710Sstevel@tonic-gate 	md_error_t		*ep
8720Sstevel@tonic-gate )
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate 	md_replicalist_t	*rlp = NULL;
8750Sstevel@tonic-gate 	md_replicalist_t	*rl;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	/* make sure we have a component */
8780Sstevel@tonic-gate 	*endblkp = 0;
8790Sstevel@tonic-gate 	if (metaismeta(np))
8800Sstevel@tonic-gate 		return (0);
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	/* get replicas, quit if none */
8830Sstevel@tonic-gate 	if (metareplicalist(sp, MD_BASICNAME_OK | PRINT_FAST, &rlp, ep) < 0) {
8840Sstevel@tonic-gate 		if (! mdismddberror(ep, MDE_DB_NODB))
8850Sstevel@tonic-gate 			return (-1);
8860Sstevel@tonic-gate 		mdclrerror(ep);
8870Sstevel@tonic-gate 		return (0);
8880Sstevel@tonic-gate 	} else if (rlp == NULL)
8890Sstevel@tonic-gate 		return (0);
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	/* go through all the replicas */
8920Sstevel@tonic-gate 	for (rl = rlp; (rl != NULL); rl = rl->rl_next) {
8930Sstevel@tonic-gate 		md_replica_t	*rp = rl->rl_repp;
8940Sstevel@tonic-gate 		mdname_t	*repnamep = rp->r_namep;
8950Sstevel@tonic-gate 		diskaddr_t	dbend;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 		if (np->dev != repnamep->dev)
8980Sstevel@tonic-gate 			continue;
8990Sstevel@tonic-gate 		dbend = rp->r_blkno + rp->r_nblk - 1;
9000Sstevel@tonic-gate 		if (dbend > *endblkp)
9010Sstevel@tonic-gate 			*endblkp = dbend;
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	/* cleanup, return success */
9050Sstevel@tonic-gate 	metafreereplicalist(rlp);
9060Sstevel@tonic-gate 	return (0);
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate /*
9100Sstevel@tonic-gate  * return cached start block
9110Sstevel@tonic-gate  */
9120Sstevel@tonic-gate static diskaddr_t
9130Sstevel@tonic-gate metagetend(
9140Sstevel@tonic-gate 	mdsetname_t	*sp,
9150Sstevel@tonic-gate 	mdname_t	*np,
9160Sstevel@tonic-gate 	md_error_t	*ep
9170Sstevel@tonic-gate )
9180Sstevel@tonic-gate {
9190Sstevel@tonic-gate 	diskaddr_t	end_blk = MD_DISKADDR_ERROR;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	/* short circuit */
9220Sstevel@tonic-gate 	if (np->end_blk != MD_DISKADDR_ERROR)
9230Sstevel@tonic-gate 		return (np->end_blk);
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	/* look for database locations */
9260Sstevel@tonic-gate 	if (mddb_getendblk(sp, np, &end_blk, ep) != 0)
9270Sstevel@tonic-gate 		return (MD_DISKADDR_ERROR);
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	/* success */
9300Sstevel@tonic-gate 	np->end_blk = end_blk;
9310Sstevel@tonic-gate 	return (end_blk);
9320Sstevel@tonic-gate }
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate /*
9350Sstevel@tonic-gate  * does device have a metadb
9360Sstevel@tonic-gate  */
9370Sstevel@tonic-gate int
9380Sstevel@tonic-gate metahasmddb(
9390Sstevel@tonic-gate 	mdsetname_t	*sp,
9400Sstevel@tonic-gate 	mdname_t	*np,
9410Sstevel@tonic-gate 	md_error_t	*ep
9420Sstevel@tonic-gate )
9430Sstevel@tonic-gate {
9440Sstevel@tonic-gate 	if (metagetend(sp, np, ep) == MD_DISKADDR_ERROR)
9450Sstevel@tonic-gate 		return (-1);
9460Sstevel@tonic-gate 	else if (np->end_blk > 0)
9470Sstevel@tonic-gate 		return (1);
9480Sstevel@tonic-gate 	else
9490Sstevel@tonic-gate 		return (0);
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate /*
9530Sstevel@tonic-gate  * return cached start block
9540Sstevel@tonic-gate  */
9550Sstevel@tonic-gate diskaddr_t
9560Sstevel@tonic-gate metagetstart(
9570Sstevel@tonic-gate 	mdsetname_t	*sp,
9580Sstevel@tonic-gate 	mdname_t	*np,
9590Sstevel@tonic-gate 	md_error_t	*ep
9600Sstevel@tonic-gate )
9610Sstevel@tonic-gate {
9620Sstevel@tonic-gate 	diskaddr_t	start_blk = MD_DISKADDR_ERROR;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	/* short circuit */
9650Sstevel@tonic-gate 	if (np->start_blk != MD_DISKADDR_ERROR)
9660Sstevel@tonic-gate 		return (np->start_blk);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	/* look for database locations */
9690Sstevel@tonic-gate 	if ((start_blk = metagetend(sp, np, ep)) == MD_DISKADDR_ERROR)
9700Sstevel@tonic-gate 		return (MD_DISKADDR_ERROR);
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	/* check for label */
9730Sstevel@tonic-gate 	if (start_blk == 0) {
9740Sstevel@tonic-gate 		start_blk = metagetlabel(np, ep);
9750Sstevel@tonic-gate 		if (start_blk == MD_DISKADDR_ERROR) {
9760Sstevel@tonic-gate 			return (MD_DISKADDR_ERROR);
9770Sstevel@tonic-gate 		}
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	/* roundup to next cylinder */
9810Sstevel@tonic-gate 	if (start_blk != 0) {
9820Sstevel@tonic-gate 		mdgeom_t	*geomp;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 		if ((geomp = metagetgeom(np, ep)) == NULL)
9850Sstevel@tonic-gate 			return (MD_DISKADDR_ERROR);
9860Sstevel@tonic-gate 		start_blk = roundup(start_blk, (geomp->nhead * geomp->nsect));
9870Sstevel@tonic-gate 	}
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	/* success */
9900Sstevel@tonic-gate 	np->start_blk = start_blk;
9910Sstevel@tonic-gate 	return (start_blk);
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate /*
9950Sstevel@tonic-gate  * return cached devices name
9960Sstevel@tonic-gate  */
9970Sstevel@tonic-gate char *
9980Sstevel@tonic-gate metagetdevicesname(
9990Sstevel@tonic-gate 	mdname_t	*np,
10000Sstevel@tonic-gate 	md_error_t	*ep
10010Sstevel@tonic-gate )
10020Sstevel@tonic-gate {
10030Sstevel@tonic-gate 	char		path[MAXPATHLEN + 1];
10040Sstevel@tonic-gate 	int		len;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	/* short circuit */
10070Sstevel@tonic-gate 	if (np->devicesname != NULL)
10080Sstevel@tonic-gate 		return (np->devicesname);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	/* follow symlink */
10110Sstevel@tonic-gate 	if ((len = readlink(np->bname, path, (sizeof (path) - 1))) < 0) {
10120Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, np->bname);
10130Sstevel@tonic-gate 		return (NULL);
10140Sstevel@tonic-gate 	} else if (len >= sizeof (path)) {
10150Sstevel@tonic-gate 		(void) mdsyserror(ep, ENAMETOOLONG, np->bname);
10160Sstevel@tonic-gate 		return (NULL);
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 	path[len] = '\0';
10190Sstevel@tonic-gate 	if ((len = strfind(path, "/devices/")) < 0) {
10200Sstevel@tonic-gate 		(void) mddeverror(ep, MDE_DEVICES_NAME, np->dev, np->bname);
10210Sstevel@tonic-gate 		return (NULL);
10220Sstevel@tonic-gate 	}
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	/* return name */
10250Sstevel@tonic-gate 	np->devicesname = Strdup(path + len + strlen("/devices"));
10260Sstevel@tonic-gate 	return (np->devicesname);
10270Sstevel@tonic-gate }
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate /*
10300Sstevel@tonic-gate  * get metadevice misc name
10310Sstevel@tonic-gate  */
10320Sstevel@tonic-gate char *
10330Sstevel@tonic-gate metagetmiscname(
10340Sstevel@tonic-gate 	mdname_t		*np,
10350Sstevel@tonic-gate 	md_error_t		*ep
10360Sstevel@tonic-gate )
10370Sstevel@tonic-gate {
10380Sstevel@tonic-gate 	mddrivename_t		*dnp = np->drivenamep;
10390Sstevel@tonic-gate 	md_i_driverinfo_t	mid;
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	/* short circuit */
10420Sstevel@tonic-gate 	if (dnp->miscname != NULL)
10430Sstevel@tonic-gate 		return (dnp->miscname);
10440Sstevel@tonic-gate 	if (metachkmeta(np, ep) != 0)
10450Sstevel@tonic-gate 		return (NULL);
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	/* get misc module from driver */
10480Sstevel@tonic-gate 	(void) memset(&mid, 0, sizeof (mid));
10490Sstevel@tonic-gate 	mid.mnum = meta_getminor(np->dev);
10500Sstevel@tonic-gate 	if (metaioctl(MD_IOCGET_DRVNM, &mid, &mid.mde, np->cname) != 0) {
10510Sstevel@tonic-gate 		(void) mdstealerror(ep, &mid.mde);
10520Sstevel@tonic-gate 		return (NULL);
10530Sstevel@tonic-gate 	}
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	/* return miscname */
10560Sstevel@tonic-gate 	dnp->miscname = Strdup(MD_PNTDRIVERNAME(&mid));
10570Sstevel@tonic-gate 	return (dnp->miscname);
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate /*
10610Sstevel@tonic-gate  * get unit structure from driver
10620Sstevel@tonic-gate  */
10630Sstevel@tonic-gate md_unit_t *
10640Sstevel@tonic-gate meta_get_mdunit(
10650Sstevel@tonic-gate 	mdsetname_t	*sp,
10660Sstevel@tonic-gate 	mdname_t	*np,
10670Sstevel@tonic-gate 	md_error_t	*ep
10680Sstevel@tonic-gate )
10690Sstevel@tonic-gate {
10700Sstevel@tonic-gate 	md_i_get_t	mig;
10710Sstevel@tonic-gate 	char		*miscname = NULL;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	/* should have a set */
10740Sstevel@tonic-gate 	assert(sp != NULL);
10750Sstevel@tonic-gate 	assert(sp->setno == MD_MIN2SET(meta_getminor(np->dev)));
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	/* get size of unit structure */
10780Sstevel@tonic-gate 	if (metachkmeta(np, ep) != 0)
10790Sstevel@tonic-gate 		return (NULL);
10800Sstevel@tonic-gate 	if ((miscname = metagetmiscname(np, ep)) == NULL)
10810Sstevel@tonic-gate 		return (NULL);
10820Sstevel@tonic-gate 	(void) memset(&mig, '\0', sizeof (mig));
10830Sstevel@tonic-gate 	MD_SETDRIVERNAME(&mig, miscname, sp->setno);
10840Sstevel@tonic-gate 	mig.id = meta_getminor(np->dev);
10850Sstevel@tonic-gate 	if (metaioctl(MD_IOCGET, &mig, &mig.mde, np->cname) != 0) {
10860Sstevel@tonic-gate 		(void) mdstealerror(ep, &mig.mde);
10870Sstevel@tonic-gate 		return (NULL);
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	/* get actual unit structure */
10910Sstevel@tonic-gate 	assert(mig.size > 0);
10920Sstevel@tonic-gate 	mig.mdp = (uintptr_t)Zalloc(mig.size);
10930Sstevel@tonic-gate 	if (metaioctl(MD_IOCGET, &mig, &mig.mde, np->cname) != 0) {
10940Sstevel@tonic-gate 		(void) mdstealerror(ep, &mig.mde);
109562Sjeanm 		Free((void *)(uintptr_t)mig.mdp);
10960Sstevel@tonic-gate 		return (NULL);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
109962Sjeanm 	return ((md_unit_t *)(uintptr_t)mig.mdp);
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate /*
11030Sstevel@tonic-gate  * free metadevice unit
11040Sstevel@tonic-gate  */
11050Sstevel@tonic-gate void
11060Sstevel@tonic-gate meta_free_unit(
11070Sstevel@tonic-gate 	mddrivename_t	*dnp
11080Sstevel@tonic-gate )
11090Sstevel@tonic-gate {
11100Sstevel@tonic-gate 	if (dnp->unitp != NULL) {
11110Sstevel@tonic-gate 		switch (dnp->unitp->type) {
11120Sstevel@tonic-gate 		case MD_DEVICE:
11130Sstevel@tonic-gate 			meta_free_stripe((md_stripe_t *)dnp->unitp);
11140Sstevel@tonic-gate 			break;
11150Sstevel@tonic-gate 		case MD_METAMIRROR:
11160Sstevel@tonic-gate 			meta_free_mirror((md_mirror_t *)dnp->unitp);
11170Sstevel@tonic-gate 			break;
11180Sstevel@tonic-gate 		case MD_METATRANS:
11190Sstevel@tonic-gate 			meta_free_trans((md_trans_t *)dnp->unitp);
11200Sstevel@tonic-gate 			break;
11210Sstevel@tonic-gate 		case MD_METARAID:
11220Sstevel@tonic-gate 			meta_free_raid((md_raid_t *)dnp->unitp);
11230Sstevel@tonic-gate 			break;
11240Sstevel@tonic-gate 		case MD_METASP:
11250Sstevel@tonic-gate 			meta_free_sp((md_sp_t *)dnp->unitp);
11260Sstevel@tonic-gate 			break;
11270Sstevel@tonic-gate 		default:
11280Sstevel@tonic-gate 			assert(0);
11290Sstevel@tonic-gate 			break;
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 		dnp->unitp = NULL;
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate /*
11360Sstevel@tonic-gate  * free metadevice name info
11370Sstevel@tonic-gate  */
11380Sstevel@tonic-gate void
11390Sstevel@tonic-gate meta_invalidate_name(
11400Sstevel@tonic-gate 	mdname_t	*namep
11410Sstevel@tonic-gate )
11420Sstevel@tonic-gate {
11430Sstevel@tonic-gate 	mddrivename_t	*dnp = namep->drivenamep;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	/* get rid of cached name info */
11460Sstevel@tonic-gate 	if (namep->devicesname != NULL) {
11470Sstevel@tonic-gate 		Free(namep->devicesname);
11480Sstevel@tonic-gate 		namep->devicesname = NULL;
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 	namep->key = MD_KEYBAD;
11510Sstevel@tonic-gate 	namep->start_blk = -1;
11520Sstevel@tonic-gate 	namep->end_blk = -1;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	/* get rid of cached drivename info */
11550Sstevel@tonic-gate 	(void) memset(&dnp->geom, 0, sizeof (dnp->geom));
11560Sstevel@tonic-gate 	(void) memset(&dnp->cinfo, 0, sizeof (dnp->cinfo));
11570Sstevel@tonic-gate 	metafreevtoc(&dnp->vtoc);
11580Sstevel@tonic-gate 	metaflushsidenames(dnp);
11590Sstevel@tonic-gate 	dnp->side_names_key = MD_KEYBAD;
11600Sstevel@tonic-gate 	if (dnp->miscname != NULL) {
11610Sstevel@tonic-gate 		Free(dnp->miscname);
11620Sstevel@tonic-gate 		dnp->miscname = NULL;
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 	meta_free_unit(dnp);
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate /*
11680Sstevel@tonic-gate  * get metadevice unit
11690Sstevel@tonic-gate  */
11700Sstevel@tonic-gate md_common_t *
11710Sstevel@tonic-gate meta_get_unit(
11720Sstevel@tonic-gate 	mdsetname_t	*sp,
11730Sstevel@tonic-gate 	mdname_t	*np,
11740Sstevel@tonic-gate 	md_error_t	*ep
11750Sstevel@tonic-gate )
11760Sstevel@tonic-gate {
11770Sstevel@tonic-gate 	char		*miscname;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	/* short circuit */
11800Sstevel@tonic-gate 	if (np->drivenamep->unitp != NULL)
11810Sstevel@tonic-gate 		return (np->drivenamep->unitp);
11820Sstevel@tonic-gate 	if (metachkmeta(np, ep) != 0)
11830Sstevel@tonic-gate 		return (NULL);
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	/* dispatch */
11860Sstevel@tonic-gate 	if ((miscname = metagetmiscname(np, ep)) == NULL)
11870Sstevel@tonic-gate 		return (NULL);
11880Sstevel@tonic-gate 	else if (strcmp(miscname, MD_STRIPE) == 0)
11890Sstevel@tonic-gate 		return ((md_common_t *)meta_get_stripe(sp, np, ep));
11900Sstevel@tonic-gate 	else if (strcmp(miscname, MD_MIRROR) == 0)
11910Sstevel@tonic-gate 		return ((md_common_t *)meta_get_mirror(sp, np, ep));
11920Sstevel@tonic-gate 	else if (strcmp(miscname, MD_TRANS) == 0)
11930Sstevel@tonic-gate 		return ((md_common_t *)meta_get_trans(sp, np, ep));
11940Sstevel@tonic-gate 	else if (strcmp(miscname, MD_RAID) == 0)
11950Sstevel@tonic-gate 		return ((md_common_t *)meta_get_raid(sp, np, ep));
11960Sstevel@tonic-gate 	else if (strcmp(miscname, MD_SP) == 0)
11970Sstevel@tonic-gate 		return ((md_common_t *)meta_get_sp(sp, np, ep));
11980Sstevel@tonic-gate 	else {
11990Sstevel@tonic-gate 		(void) mdmderror(ep, MDE_UNKNOWN_TYPE, meta_getminor(np->dev),
12000Sstevel@tonic-gate 		    np->cname);
12010Sstevel@tonic-gate 		return (NULL);
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate int
12070Sstevel@tonic-gate meta_isopen(
12080Sstevel@tonic-gate 	mdsetname_t	*sp,
12090Sstevel@tonic-gate 	mdname_t	*np,
12100Sstevel@tonic-gate 	md_error_t	*ep,
12110Sstevel@tonic-gate 	mdcmdopts_t	options
12120Sstevel@tonic-gate )
12130Sstevel@tonic-gate {
12140Sstevel@tonic-gate 	md_isopen_t	d;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	if (metachkmeta(np, ep) != 0)
12170Sstevel@tonic-gate 		return (-1);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	(void) memset(&d, '\0', sizeof (d));
12200Sstevel@tonic-gate 	d.dev = np->dev;
12210Sstevel@tonic-gate 	if (metaioctl(MD_IOCISOPEN, &d, &d.mde, np->cname) != 0)
12220Sstevel@tonic-gate 		return (mdstealerror(ep, &d.mde));
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	/*
12250Sstevel@tonic-gate 	 * shortcut: if the device is open, no need to check on other nodes,
12260Sstevel@tonic-gate 	 * even in case of a mn metadevice
12270Sstevel@tonic-gate 	 * Also return in case we're told not to check on other nodes.
12280Sstevel@tonic-gate 	 */
12290Sstevel@tonic-gate 	if ((d.isopen != 0) || ((options & MDCMD_MN_OPEN_CHECK) == 0)) {
12300Sstevel@tonic-gate 		return (d.isopen);
12310Sstevel@tonic-gate 	}
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	/*
12340Sstevel@tonic-gate 	 * If the device is closed locally, but it's a mn device,
12350Sstevel@tonic-gate 	 * check on all other nodes, too
12360Sstevel@tonic-gate 	 */
12370Sstevel@tonic-gate 	if (sp->setno != MD_LOCAL_SET) {
12380Sstevel@tonic-gate 		(void) metaget_setdesc(sp, ep); /* not supposed to fail */
12390Sstevel@tonic-gate 		if (sp->setdesc->sd_flags & MD_SR_MN) {
12400Sstevel@tonic-gate 			int		err = 0;
12410Sstevel@tonic-gate 			md_mn_result_t *resp;
12420Sstevel@tonic-gate 			/*
12430Sstevel@tonic-gate 			 * This message is never directly issued.
12440Sstevel@tonic-gate 			 * So we launch it with a suspend override flag.
12450Sstevel@tonic-gate 			 * If the commd is suspended, and this message comes
12460Sstevel@tonic-gate 			 * along it must be sent due to replaying a metainit or
12470Sstevel@tonic-gate 			 * similar. In that case we don't want this message to
12480Sstevel@tonic-gate 			 * be blocked.
12490Sstevel@tonic-gate 			 * If the commd is not suspended, the flag does no harm.
12500Sstevel@tonic-gate 			 * Additionally we don't want the result of the message
12510Sstevel@tonic-gate 			 * cached in the MCT, because we want uptodate results,
12520Sstevel@tonic-gate 			 * and the message doesn't need being logged either.
12530Sstevel@tonic-gate 			 * Hence NO_LOG and NO_MCT
12540Sstevel@tonic-gate 			 */
12550Sstevel@tonic-gate 			err = mdmn_send_message(
12560Sstevel@tonic-gate 				sp->setno,
12570Sstevel@tonic-gate 				MD_MN_MSG_CLU_CHECK,
12580Sstevel@tonic-gate 				MD_MSGF_NO_MCT | MD_MSGF_STOP_ON_ERROR |
12590Sstevel@tonic-gate 				MD_MSGF_NO_LOG | MD_MSGF_OVERRIDE_SUSPEND,
12600Sstevel@tonic-gate 				(char *)&d, sizeof (md_isopen_t),
12610Sstevel@tonic-gate 				&resp, ep);
12620Sstevel@tonic-gate 			if (err == 0) {
12630Sstevel@tonic-gate 				d.isopen = resp->mmr_exitval;
12640Sstevel@tonic-gate 			} else {
12650Sstevel@tonic-gate 				/*
12660Sstevel@tonic-gate 				 * in case some error occurred,
12670Sstevel@tonic-gate 				 * we better say the device is open
12680Sstevel@tonic-gate 				 */
12690Sstevel@tonic-gate 				d.isopen = 1;
12700Sstevel@tonic-gate 			}
12710Sstevel@tonic-gate 			if (resp != (md_mn_result_t *)NULL) {
12720Sstevel@tonic-gate 				free_result(resp);
12730Sstevel@tonic-gate 			}
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 		}
12760Sstevel@tonic-gate 	}
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	return (d.isopen);
12790Sstevel@tonic-gate }
1280