xref: /onnv-gate/usr/src/lib/lvm/libsvm/common/metainterfaces.c (revision 1623:7bac4a816ebe)
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*1623Stw21770  * Common Development and Distribution License (the "License").
6*1623Stw21770  * 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*1623Stw21770  * 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 <stdio.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <sys/vfstab.h>
310Sstevel@tonic-gate #include <meta.h>
320Sstevel@tonic-gate #include <libsvm.h>
330Sstevel@tonic-gate #include <svm.h>
340Sstevel@tonic-gate #include <sdssc.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate 
370Sstevel@tonic-gate extern int mod_unload(char *modname);
380Sstevel@tonic-gate static int inited = 0;
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * FUNCTION: init_metalib
420Sstevel@tonic-gate  *	initialize libmeta only once.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * RETURN VALUES:
450Sstevel@tonic-gate  *	0 - SUCCESS
460Sstevel@tonic-gate  *     -1 - FAIL
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static int
init_metalib()500Sstevel@tonic-gate init_metalib()
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	int largc = 1;
530Sstevel@tonic-gate 	char *largv = "libsvm";
540Sstevel@tonic-gate 	md_error_t status = mdnullerror;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	if (!inited) {
570Sstevel@tonic-gate 		if (md_init_nosig(largc, &largv, 0, 1, &status) != 0 ||
580Sstevel@tonic-gate 				meta_check_root(&status) != 0) {
590Sstevel@tonic-gate 			return (-1);
600Sstevel@tonic-gate 		}
610Sstevel@tonic-gate 		inited = 1;
620Sstevel@tonic-gate 	}
630Sstevel@tonic-gate 	return (RET_SUCCESS);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * FUNCTION: reset_metalib
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * INPUT: ptr to md_error_t
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static void
reset_metalib(md_error_t * ep)730Sstevel@tonic-gate reset_metalib(md_error_t *ep)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	inited = 0;
760Sstevel@tonic-gate 	(void) close_admin(ep);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * FUNCTION: metahalt
810Sstevel@tonic-gate  *	halt the metadb
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static void
metahalt()860Sstevel@tonic-gate metahalt()
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	mdsetname_t	*sp;
890Sstevel@tonic-gate 	md_error_t status = mdnullerror;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	(void) init_metalib();
920Sstevel@tonic-gate 	if ((sp = metasetname(MD_LOCAL_NAME, &status)) == NULL) {
930Sstevel@tonic-gate 		return;
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, &status)) {
960Sstevel@tonic-gate 		return;
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 	if (metaioctl(MD_HALT, NULL, &status, NULL) != 0) {
990Sstevel@tonic-gate 		debug_printf("metahalt(): errno %d\n",
1000Sstevel@tonic-gate 			status.info.md_error_info_t_u.sys_error.errnum);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 	(void) meta_unlock(sp, &status);
1030Sstevel@tonic-gate 	reset_metalib(&status);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * FUNCTION: svm_stop
1080Sstevel@tonic-gate  *	Halt the SDS/SVM configuration and unload md module.
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  * RETURN VALUES:
1110Sstevel@tonic-gate  *	0 - SUCCESS
1120Sstevel@tonic-gate  *	RET_ERROR
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate #define	MAX_TIMEOUT 1800
1160Sstevel@tonic-gate int
svm_stop()1170Sstevel@tonic-gate svm_stop()
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	int rval = RET_SUCCESS;
1200Sstevel@tonic-gate 	int timeval = 0;
1210Sstevel@tonic-gate 	int sleep_int = 5;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	metahalt();
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if ((rval = mod_unload(MD_MODULE)) != 0) {
1260Sstevel@tonic-gate 		timeval += sleep_int;
1270Sstevel@tonic-gate 		(void) sleep(sleep_int);
1280Sstevel@tonic-gate 		while (timeval < MAX_TIMEOUT) {
1290Sstevel@tonic-gate 			if ((rval = mod_unload(MD_MODULE)) == 0) {
1300Sstevel@tonic-gate 				debug_printf("svm_stop(): mod_unload succeeded."
1310Sstevel@tonic-gate 						" Time %d\n", timeval);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 				break;
1340Sstevel@tonic-gate 			}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 			debug_printf("svm_stop(): mod_unload failed. Trying "
1370Sstevel@tonic-gate 				"in  %d s (%d)\n", sleep_int, timeval);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 			timeval += sleep_int;
1400Sstevel@tonic-gate 			(void) sleep(sleep_int);
1410Sstevel@tonic-gate 			metahalt();
1420Sstevel@tonic-gate 		}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 		if (rval != 0) {
1450Sstevel@tonic-gate 			rval = RET_ERROR;
1460Sstevel@tonic-gate 			debug_printf("svm_stop(): mod_unload FAILED!\n");
1470Sstevel@tonic-gate 		}
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	return (rval);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate  * FUNCTION: get_rootmetadevice
1550Sstevel@tonic-gate  *	parses the vfstab to return the metadevice
1560Sstevel@tonic-gate  *
1570Sstevel@tonic-gate  * INPUT:
1580Sstevel@tonic-gate  *	mount point
1590Sstevel@tonic-gate  *	mdname	- pointer to string pointer that will contain the
1600Sstevel@tonic-gate  *		  metadevice name. Caller must free the allocated space.
1610Sstevel@tonic-gate  * RETURN VALUES:
1620Sstevel@tonic-gate  *	mdname - md root device name
1630Sstevel@tonic-gate  *	0 - SUCCESS
1640Sstevel@tonic-gate  *	!0 - FAIL
1650Sstevel@tonic-gate  *		> 0 errno
1660Sstevel@tonic-gate  *		RET_ERROR
1670Sstevel@tonic-gate  */
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate int
get_rootmetadevice(char * mntpath,char ** mdname)1700Sstevel@tonic-gate get_rootmetadevice(char *mntpath, char **mdname)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	struct	vfstab v;
1730Sstevel@tonic-gate 	FILE	*fp;
1740Sstevel@tonic-gate 	int	rval = RET_SUCCESS;
1750Sstevel@tonic-gate 	char	*cp;
1760Sstevel@tonic-gate 	char	vfstab_name[PATH_MAX + 1];
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	if (mdname == NULL)
1790Sstevel@tonic-gate 		return (EINVAL);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	*mdname = NULL;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (snprintf(vfstab_name, PATH_MAX + 1, "%s%s", mntpath, VFSTAB) < 0)
1840Sstevel@tonic-gate 		return (ENOMEM);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	debug_printf("get_rootmetadevice(): mntpath %s %s\n", mntpath,
1870Sstevel@tonic-gate 		vfstab_name);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if ((fp = fopen(vfstab_name, "r")) == NULL) {
1900Sstevel@tonic-gate 		rval = errno;
1910Sstevel@tonic-gate 		return (rval);
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if ((rval = getvfsfile(fp, &v, ROOT_MNTPT)) != 0) {
1950Sstevel@tonic-gate 		goto out;
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	debug_printf("get_rootmetadevice(): vfs_special %s\n", v.vfs_special);
2000Sstevel@tonic-gate 	if (strstr(v.vfs_special, ROOT_METADEVICE) == NULL) {
2010Sstevel@tonic-gate 		/* md device not found */
2020Sstevel@tonic-gate 		rval = RET_ERROR;
2030Sstevel@tonic-gate 		goto out;
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	/* found a match fill it and return */
2070Sstevel@tonic-gate 	cp = v.vfs_special + strlen(ROOT_METADEVICE);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	*mdname = (char *)malloc(strlen(cp) + 1);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (*mdname == NULL) {
2120Sstevel@tonic-gate 		rval = ENOMEM;
2130Sstevel@tonic-gate 		goto out;
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 	(void) strcpy(*mdname, cp);
2160Sstevel@tonic-gate 	debug_printf("get_rootmetadevice(): *mdname %s rval %d\n",
2170Sstevel@tonic-gate 							*mdname, rval);
2180Sstevel@tonic-gate out:
2190Sstevel@tonic-gate 	(void) fclose(fp);
2200Sstevel@tonic-gate 	return (rval);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate  * FUNCTION: create_diskset_links
2250Sstevel@tonic-gate  * 	Create the diskset name symlinks in /dev/md from the diskset
2260Sstevel@tonic-gate  *	names found in the set records.  These are normally created
2270Sstevel@tonic-gate  *	in rpc.metad when you create the set but those symlinks are
2280Sstevel@tonic-gate  *	sitting out on the real system disk and we're running off the
2290Sstevel@tonic-gate  *	devfs that got created when we booted off the install image.
2300Sstevel@tonic-gate  */
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate void
create_diskset_links()2330Sstevel@tonic-gate create_diskset_links()
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	int		max_sets;
2360Sstevel@tonic-gate 	int		i;
2370Sstevel@tonic-gate 	md_error_t	error = mdnullerror;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/*
2400Sstevel@tonic-gate 	 * Resolve the function pointers for libsds_sc so that we can
2410Sstevel@tonic-gate 	 * snarf the set records.
2420Sstevel@tonic-gate 	 */
2430Sstevel@tonic-gate 	(void) sdssc_bind_library();
2440Sstevel@tonic-gate 	(void) init_metalib();
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if ((max_sets = get_max_sets(&error)) == 0) {
2470Sstevel@tonic-gate 		debug_printf("create_diskset_links(): get_max_sets failed\n");
2480Sstevel@tonic-gate 		mdclrerror(&error);
2490Sstevel@tonic-gate 		return;
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	for (i = 1; i < max_sets; i++) {
2530Sstevel@tonic-gate 		md_set_record	*sr;
2540Sstevel@tonic-gate 		char		setname[MAXPATHLEN];
2550Sstevel@tonic-gate 		char		setnum[MAXPATHLEN];
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 		if ((sr = metad_getsetbynum(i, &error)) == NULL) {
2580Sstevel@tonic-gate 			mdclrerror(&error);
2590Sstevel@tonic-gate 			continue;
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 		(void) snprintf(setname, MAXPATHLEN, "/dev/md/%s",
2630Sstevel@tonic-gate 		    sr->sr_setname);
2640Sstevel@tonic-gate 		(void) snprintf(setnum, MAXPATHLEN, "shared/%d", i);
2650Sstevel@tonic-gate 		/*
2660Sstevel@tonic-gate 		 * Ignore failures to create the symlink.  This could
2670Sstevel@tonic-gate 		 * happen because suninstall is restartable so the
2680Sstevel@tonic-gate 		 * symlink might have already been created.
2690Sstevel@tonic-gate 		 */
2700Sstevel@tonic-gate 		(void) symlink(setnum, setname);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate  * FUNCTION: svm_alloc
2760Sstevel@tonic-gate  * 	Return a pointer to an opaque piece of zeroed memory.
2770Sstevel@tonic-gate  *
2780Sstevel@tonic-gate  * RETURN VALUES:
2790Sstevel@tonic-gate  *	Non null - SUCCESS
2800Sstevel@tonic-gate  *	NULL - FAIL
2810Sstevel@tonic-gate  */
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate svm_info_t *
svm_alloc()2840Sstevel@tonic-gate svm_alloc()
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	return ((svm_info_t *)calloc(1, sizeof (svm_info_t)));
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate  * FUNCTION: svm_free
2910Sstevel@tonic-gate  *
2920Sstevel@tonic-gate  * INPUT: pointer to struct svm_info
2930Sstevel@tonic-gate  */
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate void
svm_free(svm_info_t * svmp)2960Sstevel@tonic-gate svm_free(svm_info_t *svmp)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	int i;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (svmp == NULL)
3010Sstevel@tonic-gate 		return;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	for (i = 0; i < svmp->count; i++) {
3040Sstevel@tonic-gate 		free(svmp->md_comps[i]);
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 	free(svmp->root_md);
3070Sstevel@tonic-gate 	free(svmp);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * FUNCTION: get_mdcomponents
3120Sstevel@tonic-gate  *	Given "uname" metadevice, return the physical components
3130Sstevel@tonic-gate  *      of that metadevice.
3140Sstevel@tonic-gate  *
3150Sstevel@tonic-gate  * INPUT:
3160Sstevel@tonic-gate  *	uname - metadevice name
3170Sstevel@tonic-gate  *
3180Sstevel@tonic-gate  * RETURN VALUES:
3190Sstevel@tonic-gate  *	svmp - structure containing md name and components
3200Sstevel@tonic-gate  *	RET_SUCCESS
3210Sstevel@tonic-gate  *	RET_ERROR
3220Sstevel@tonic-gate  *
3230Sstevel@tonic-gate  */
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate int
get_mdcomponents(char * uname,svm_info_t ** svmpp)3260Sstevel@tonic-gate get_mdcomponents(char *uname, svm_info_t **svmpp)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	svm_info_t	*svmp;
3300Sstevel@tonic-gate 	md_error_t	status, *ep;
3310Sstevel@tonic-gate 	mdname_t	*namep;
3320Sstevel@tonic-gate 	mdnamelist_t	*nlp = NULL;
3330Sstevel@tonic-gate 	mdnamelist_t	*p;
3340Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
3350Sstevel@tonic-gate 	char		*strp = NULL;
3360Sstevel@tonic-gate 	int		rval, cnt;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	rval = RET_SUCCESS;
3390Sstevel@tonic-gate 	cnt = 0;
3400Sstevel@tonic-gate 	status = mdnullerror;
3410Sstevel@tonic-gate 	ep = &status;
3420Sstevel@tonic-gate 	svmp = *svmpp;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	(void) init_metalib();
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	debug_printf("get_mdcomponents(): Enter unit name %s\n", uname);
3470Sstevel@tonic-gate 
348*1623Stw21770 	if (((namep = metaname(&sp, uname, META_DEVICE, ep)) == NULL) ||
3490Sstevel@tonic-gate 					(metachkmeta(namep, ep) != 0)) {
3500Sstevel@tonic-gate 		debug_printf("get_mdcomponents(): "
3510Sstevel@tonic-gate 				"metaname or metachkmeta failed\n");
3520Sstevel@tonic-gate 		mdclrerror(ep);
3530Sstevel@tonic-gate 		return (RET_ERROR);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	debug_printf("get_mdcomponents(): meta_getdevs %s\n", namep->cname);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	if ((meta_getdevs(sp, namep, &nlp, ep)) < 0) {
3590Sstevel@tonic-gate 		debug_printf("get_mdcomponents(): "
3600Sstevel@tonic-gate 				"comp %s - meta_getdevs failed\n", uname);
3610Sstevel@tonic-gate 		metafreenamelist(nlp);
3620Sstevel@tonic-gate 		mdclrerror(ep);
3630Sstevel@tonic-gate 		return (RET_ERROR);
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/* compute the number of devices */
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	for (p = nlp, cnt = 0; p != NULL;  p = p->next, cnt++)
3690Sstevel@tonic-gate 		;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	/*
3720Sstevel@tonic-gate 	 * Need to add n -1 components since slvmp already has space
3730Sstevel@tonic-gate 	 * for one device.
3740Sstevel@tonic-gate 	 */
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	svmp = (svm_info_t *)realloc(svmp, sizeof (svm_info_t) +
3770Sstevel@tonic-gate 		(sizeof (char *) * (cnt - 1)));
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if (svmp == NULL) {
3800Sstevel@tonic-gate 		debug_printf("get_mdcomponents(): realloc of svmp failed\n");
3810Sstevel@tonic-gate 		metafreenamelist(nlp);
3820Sstevel@tonic-gate 		return (RET_ERROR);
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	for (p = nlp, cnt = 0; p != NULL; p = p->next, cnt++) {
3870Sstevel@tonic-gate 		mdname_t	*devnp = p->namep;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 		if ((strp = strdup(devnp->cname)) == NULL) {
3900Sstevel@tonic-gate 			rval = RET_ERROR;
3910Sstevel@tonic-gate 			break;
3920Sstevel@tonic-gate 		}
3930Sstevel@tonic-gate 		svmp->md_comps[cnt] = strp;
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	/* count is set to the number of devices in the list */
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	svmp->count = cnt;
3990Sstevel@tonic-gate 	svmp->root_md = strdup(uname);
4000Sstevel@tonic-gate 	if (rval == RET_SUCCESS && svmp->root_md != NULL) {
4010Sstevel@tonic-gate 		debug_printf("get_mdcomponents(): root_md %s count %d \n",
4020Sstevel@tonic-gate 			svmp->root_md, svmp->count);
4030Sstevel@tonic-gate 		for (cnt = 0; cnt < svmp->count; cnt++)
4040Sstevel@tonic-gate 			debug_printf("get_mdcomponents(): %s\n",
4050Sstevel@tonic-gate 							svmp->md_comps[cnt]);
4060Sstevel@tonic-gate 	} else {
4070Sstevel@tonic-gate 		rval = RET_ERROR;
4080Sstevel@tonic-gate 		svm_free(svmp);
4090Sstevel@tonic-gate 		svmp = NULL;
4100Sstevel@tonic-gate 		debug_printf("get_mdcomponents(): malloc failed\n");
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	metafreenamelist(nlp);
4160Sstevel@tonic-gate 	*svmpp = svmp;
4170Sstevel@tonic-gate 	return (rval);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate  * FUNCTION: svm_get_components
4230Sstevel@tonic-gate  *	return svm_infop with the components of a metadevice.
4240Sstevel@tonic-gate  *
4250Sstevel@tonic-gate  * INPUT:
4260Sstevel@tonic-gate  *	md_device - eg. /dev/md/dsk/d10, /dev/md/foo/dsk/d10, or
4270Sstevel@tonic-gate  *			/dev/md/shared/1/dsk/d10
4280Sstevel@tonic-gate  *
4290Sstevel@tonic-gate  * RETURN:
4300Sstevel@tonic-gate  *	0 - SUCCESS
4310Sstevel@tonic-gate  *     !0 - FAIL
4320Sstevel@tonic-gate  */
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate int
svm_get_components(char * md_device,svm_info_t ** svmpp)4350Sstevel@tonic-gate svm_get_components(char *md_device, svm_info_t **svmpp)
4360Sstevel@tonic-gate {
4370Sstevel@tonic-gate 	int	len;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	/*
4400Sstevel@tonic-gate 	 * If this is a named diskset with a shared name
4410Sstevel@tonic-gate 	 * (e.g. /dev/md/shared/1/dsk/d10) call get_mdcomponents with
4420Sstevel@tonic-gate 	 * the diskset and metadevice name (e.g. foo/d10).
4430Sstevel@tonic-gate 	 * Otherwise this is a regular name (e.g. /dev/md/dsk/d10 or
4440Sstevel@tonic-gate 	 * /dev/md/foo/dsk/d10 or d10 or foo/d10) all of which
4450Sstevel@tonic-gate 	 * get_mdcomponents can handle directly.
4460Sstevel@tonic-gate 	 */
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	len = strlen("/dev/md/shared/");
4490Sstevel@tonic-gate 	if (strncmp(md_device, "/dev/md/shared/", len) == 0) {
4500Sstevel@tonic-gate 	    int		numlen;
4510Sstevel@tonic-gate 	    int		setnum;
4520Sstevel@tonic-gate 	    char	*cp;
4530Sstevel@tonic-gate 	    char	*slashp;
4540Sstevel@tonic-gate 	    char	mdname[MAXPATHLEN];
4550Sstevel@tonic-gate 	    mdsetname_t	*sp;
4560Sstevel@tonic-gate 	    md_error_t	error = mdnullerror;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	    cp = md_device + len;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	    if ((slashp = strstr(cp, "/")) == NULL)
4610Sstevel@tonic-gate 		return (RET_ERROR);
4620Sstevel@tonic-gate 	    numlen = slashp - cp;
4630Sstevel@tonic-gate 	    if (numlen >= MAXPATHLEN - 1)
4640Sstevel@tonic-gate 		return (RET_ERROR);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	    (void) strlcpy(mdname, cp, numlen + 1);
4670Sstevel@tonic-gate 	    /* setnum now contains the diskset number */
4680Sstevel@tonic-gate 	    setnum = atoi(mdname);
4690Sstevel@tonic-gate 	    if ((sp = metasetnosetname(setnum, &error)) == NULL ||
4700Sstevel@tonic-gate 		!mdisok(&error))
4710Sstevel@tonic-gate 		return (RET_ERROR);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	    cp = slashp + 1;
4740Sstevel@tonic-gate 	    /* cp now pointing at dsk/... */
4750Sstevel@tonic-gate 	    if ((slashp = strstr(cp, "/")) == NULL)
4760Sstevel@tonic-gate 		return (RET_ERROR);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	    (void) snprintf(mdname, MAXPATHLEN, "%s/%s", sp->setname,
4790Sstevel@tonic-gate 		slashp + 1);
4800Sstevel@tonic-gate 	    /* mdname now contains diskset and metadevice name e.g. foo/d10 */
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	    debug_printf("svm_get_components(): mdname %s\n", mdname);
4830Sstevel@tonic-gate 	    return (get_mdcomponents(mdname, svmpp));
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	} else {
4860Sstevel@tonic-gate 	    debug_printf("svm_get_components(): md_device %s\n", md_device);
4870Sstevel@tonic-gate 	    return (get_mdcomponents(md_device, svmpp));
4880Sstevel@tonic-gate 	}
4890Sstevel@tonic-gate }
490