1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <string.h>
31*0Sstevel@tonic-gate #include <sys/vfstab.h>
32*0Sstevel@tonic-gate #include <meta.h>
33*0Sstevel@tonic-gate #include <libsvm.h>
34*0Sstevel@tonic-gate #include <svm.h>
35*0Sstevel@tonic-gate #include <sdssc.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate extern int mod_unload(char *modname);
39*0Sstevel@tonic-gate static int inited = 0;
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate /*
42*0Sstevel@tonic-gate  * FUNCTION: init_metalib
43*0Sstevel@tonic-gate  *	initialize libmeta only once.
44*0Sstevel@tonic-gate  *
45*0Sstevel@tonic-gate  * RETURN VALUES:
46*0Sstevel@tonic-gate  *	0 - SUCCESS
47*0Sstevel@tonic-gate  *     -1 - FAIL
48*0Sstevel@tonic-gate  */
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static int
51*0Sstevel@tonic-gate init_metalib()
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate 	int largc = 1;
54*0Sstevel@tonic-gate 	char *largv = "libsvm";
55*0Sstevel@tonic-gate 	md_error_t status = mdnullerror;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	if (!inited) {
58*0Sstevel@tonic-gate 		if (md_init_nosig(largc, &largv, 0, 1, &status) != 0 ||
59*0Sstevel@tonic-gate 				meta_check_root(&status) != 0) {
60*0Sstevel@tonic-gate 			return (-1);
61*0Sstevel@tonic-gate 		}
62*0Sstevel@tonic-gate 		inited = 1;
63*0Sstevel@tonic-gate 	}
64*0Sstevel@tonic-gate 	return (RET_SUCCESS);
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate  * FUNCTION: reset_metalib
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  * INPUT: ptr to md_error_t
71*0Sstevel@tonic-gate  */
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate static void
74*0Sstevel@tonic-gate reset_metalib(md_error_t *ep)
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	inited = 0;
77*0Sstevel@tonic-gate 	(void) close_admin(ep);
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate /*
81*0Sstevel@tonic-gate  * FUNCTION: metahalt
82*0Sstevel@tonic-gate  *	halt the metadb
83*0Sstevel@tonic-gate  *
84*0Sstevel@tonic-gate  */
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate static void
87*0Sstevel@tonic-gate metahalt()
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate 	mdsetname_t	*sp;
90*0Sstevel@tonic-gate 	md_error_t status = mdnullerror;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	(void) init_metalib();
93*0Sstevel@tonic-gate 	if ((sp = metasetname(MD_LOCAL_NAME, &status)) == NULL) {
94*0Sstevel@tonic-gate 		return;
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, &status)) {
97*0Sstevel@tonic-gate 		return;
98*0Sstevel@tonic-gate 	}
99*0Sstevel@tonic-gate 	if (metaioctl(MD_HALT, NULL, &status, NULL) != 0) {
100*0Sstevel@tonic-gate 		debug_printf("metahalt(): errno %d\n",
101*0Sstevel@tonic-gate 			status.info.md_error_info_t_u.sys_error.errnum);
102*0Sstevel@tonic-gate 	}
103*0Sstevel@tonic-gate 	(void) meta_unlock(sp, &status);
104*0Sstevel@tonic-gate 	reset_metalib(&status);
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate  * FUNCTION: svm_stop
109*0Sstevel@tonic-gate  *	Halt the SDS/SVM configuration and unload md module.
110*0Sstevel@tonic-gate  *
111*0Sstevel@tonic-gate  * RETURN VALUES:
112*0Sstevel@tonic-gate  *	0 - SUCCESS
113*0Sstevel@tonic-gate  *	RET_ERROR
114*0Sstevel@tonic-gate  */
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate #define	MAX_TIMEOUT 1800
117*0Sstevel@tonic-gate int
118*0Sstevel@tonic-gate svm_stop()
119*0Sstevel@tonic-gate {
120*0Sstevel@tonic-gate 	int rval = RET_SUCCESS;
121*0Sstevel@tonic-gate 	int timeval = 0;
122*0Sstevel@tonic-gate 	int sleep_int = 5;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	metahalt();
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	if ((rval = mod_unload(MD_MODULE)) != 0) {
127*0Sstevel@tonic-gate 		timeval += sleep_int;
128*0Sstevel@tonic-gate 		(void) sleep(sleep_int);
129*0Sstevel@tonic-gate 		while (timeval < MAX_TIMEOUT) {
130*0Sstevel@tonic-gate 			if ((rval = mod_unload(MD_MODULE)) == 0) {
131*0Sstevel@tonic-gate 				debug_printf("svm_stop(): mod_unload succeeded."
132*0Sstevel@tonic-gate 						" Time %d\n", timeval);
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 				break;
135*0Sstevel@tonic-gate 			}
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 			debug_printf("svm_stop(): mod_unload failed. Trying "
138*0Sstevel@tonic-gate 				"in  %d s (%d)\n", sleep_int, timeval);
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 			timeval += sleep_int;
141*0Sstevel@tonic-gate 			(void) sleep(sleep_int);
142*0Sstevel@tonic-gate 			metahalt();
143*0Sstevel@tonic-gate 		}
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 		if (rval != 0) {
146*0Sstevel@tonic-gate 			rval = RET_ERROR;
147*0Sstevel@tonic-gate 			debug_printf("svm_stop(): mod_unload FAILED!\n");
148*0Sstevel@tonic-gate 		}
149*0Sstevel@tonic-gate 	}
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	return (rval);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate /*
155*0Sstevel@tonic-gate  * FUNCTION: get_rootmetadevice
156*0Sstevel@tonic-gate  *	parses the vfstab to return the metadevice
157*0Sstevel@tonic-gate  *
158*0Sstevel@tonic-gate  * INPUT:
159*0Sstevel@tonic-gate  *	mount point
160*0Sstevel@tonic-gate  *	mdname	- pointer to string pointer that will contain the
161*0Sstevel@tonic-gate  *		  metadevice name. Caller must free the allocated space.
162*0Sstevel@tonic-gate  * RETURN VALUES:
163*0Sstevel@tonic-gate  *	mdname - md root device name
164*0Sstevel@tonic-gate  *	0 - SUCCESS
165*0Sstevel@tonic-gate  *	!0 - FAIL
166*0Sstevel@tonic-gate  *		> 0 errno
167*0Sstevel@tonic-gate  *		RET_ERROR
168*0Sstevel@tonic-gate  */
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate int
171*0Sstevel@tonic-gate get_rootmetadevice(char *mntpath, char **mdname)
172*0Sstevel@tonic-gate {
173*0Sstevel@tonic-gate 	struct	vfstab v;
174*0Sstevel@tonic-gate 	FILE	*fp;
175*0Sstevel@tonic-gate 	int	rval = RET_SUCCESS;
176*0Sstevel@tonic-gate 	char	*cp;
177*0Sstevel@tonic-gate 	char	vfstab_name[PATH_MAX + 1];
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	if (mdname == NULL)
180*0Sstevel@tonic-gate 		return (EINVAL);
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	*mdname = NULL;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	if (snprintf(vfstab_name, PATH_MAX + 1, "%s%s", mntpath, VFSTAB) < 0)
185*0Sstevel@tonic-gate 		return (ENOMEM);
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	debug_printf("get_rootmetadevice(): mntpath %s %s\n", mntpath,
188*0Sstevel@tonic-gate 		vfstab_name);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	if ((fp = fopen(vfstab_name, "r")) == NULL) {
191*0Sstevel@tonic-gate 		rval = errno;
192*0Sstevel@tonic-gate 		return (rval);
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	if ((rval = getvfsfile(fp, &v, ROOT_MNTPT)) != 0) {
196*0Sstevel@tonic-gate 		goto out;
197*0Sstevel@tonic-gate 	}
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	debug_printf("get_rootmetadevice(): vfs_special %s\n", v.vfs_special);
201*0Sstevel@tonic-gate 	if (strstr(v.vfs_special, ROOT_METADEVICE) == NULL) {
202*0Sstevel@tonic-gate 		/* md device not found */
203*0Sstevel@tonic-gate 		rval = RET_ERROR;
204*0Sstevel@tonic-gate 		goto out;
205*0Sstevel@tonic-gate 	}
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	/* found a match fill it and return */
208*0Sstevel@tonic-gate 	cp = v.vfs_special + strlen(ROOT_METADEVICE);
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	*mdname = (char *)malloc(strlen(cp) + 1);
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	if (*mdname == NULL) {
213*0Sstevel@tonic-gate 		rval = ENOMEM;
214*0Sstevel@tonic-gate 		goto out;
215*0Sstevel@tonic-gate 	}
216*0Sstevel@tonic-gate 	(void) strcpy(*mdname, cp);
217*0Sstevel@tonic-gate 	debug_printf("get_rootmetadevice(): *mdname %s rval %d\n",
218*0Sstevel@tonic-gate 							*mdname, rval);
219*0Sstevel@tonic-gate out:
220*0Sstevel@tonic-gate 	(void) fclose(fp);
221*0Sstevel@tonic-gate 	return (rval);
222*0Sstevel@tonic-gate }
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate /*
225*0Sstevel@tonic-gate  * FUNCTION: create_diskset_links
226*0Sstevel@tonic-gate  * 	Create the diskset name symlinks in /dev/md from the diskset
227*0Sstevel@tonic-gate  *	names found in the set records.  These are normally created
228*0Sstevel@tonic-gate  *	in rpc.metad when you create the set but those symlinks are
229*0Sstevel@tonic-gate  *	sitting out on the real system disk and we're running off the
230*0Sstevel@tonic-gate  *	devfs that got created when we booted off the install image.
231*0Sstevel@tonic-gate  */
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate void
234*0Sstevel@tonic-gate create_diskset_links()
235*0Sstevel@tonic-gate {
236*0Sstevel@tonic-gate 	int		max_sets;
237*0Sstevel@tonic-gate 	int		i;
238*0Sstevel@tonic-gate 	md_error_t	error = mdnullerror;
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	/*
241*0Sstevel@tonic-gate 	 * Resolve the function pointers for libsds_sc so that we can
242*0Sstevel@tonic-gate 	 * snarf the set records.
243*0Sstevel@tonic-gate 	 */
244*0Sstevel@tonic-gate 	(void) sdssc_bind_library();
245*0Sstevel@tonic-gate 	(void) init_metalib();
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	if ((max_sets = get_max_sets(&error)) == 0) {
248*0Sstevel@tonic-gate 		debug_printf("create_diskset_links(): get_max_sets failed\n");
249*0Sstevel@tonic-gate 		mdclrerror(&error);
250*0Sstevel@tonic-gate 		return;
251*0Sstevel@tonic-gate 	}
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	for (i = 1; i < max_sets; i++) {
254*0Sstevel@tonic-gate 		md_set_record	*sr;
255*0Sstevel@tonic-gate 		char		setname[MAXPATHLEN];
256*0Sstevel@tonic-gate 		char		setnum[MAXPATHLEN];
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 		if ((sr = metad_getsetbynum(i, &error)) == NULL) {
259*0Sstevel@tonic-gate 			mdclrerror(&error);
260*0Sstevel@tonic-gate 			continue;
261*0Sstevel@tonic-gate 		}
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 		(void) snprintf(setname, MAXPATHLEN, "/dev/md/%s",
264*0Sstevel@tonic-gate 		    sr->sr_setname);
265*0Sstevel@tonic-gate 		(void) snprintf(setnum, MAXPATHLEN, "shared/%d", i);
266*0Sstevel@tonic-gate 		/*
267*0Sstevel@tonic-gate 		 * Ignore failures to create the symlink.  This could
268*0Sstevel@tonic-gate 		 * happen because suninstall is restartable so the
269*0Sstevel@tonic-gate 		 * symlink might have already been created.
270*0Sstevel@tonic-gate 		 */
271*0Sstevel@tonic-gate 		(void) symlink(setnum, setname);
272*0Sstevel@tonic-gate 	}
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate /*
276*0Sstevel@tonic-gate  * FUNCTION: svm_alloc
277*0Sstevel@tonic-gate  * 	Return a pointer to an opaque piece of zeroed memory.
278*0Sstevel@tonic-gate  *
279*0Sstevel@tonic-gate  * RETURN VALUES:
280*0Sstevel@tonic-gate  *	Non null - SUCCESS
281*0Sstevel@tonic-gate  *	NULL - FAIL
282*0Sstevel@tonic-gate  */
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate svm_info_t *
285*0Sstevel@tonic-gate svm_alloc()
286*0Sstevel@tonic-gate {
287*0Sstevel@tonic-gate 	return ((svm_info_t *)calloc(1, sizeof (svm_info_t)));
288*0Sstevel@tonic-gate }
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate /*
291*0Sstevel@tonic-gate  * FUNCTION: svm_free
292*0Sstevel@tonic-gate  *
293*0Sstevel@tonic-gate  * INPUT: pointer to struct svm_info
294*0Sstevel@tonic-gate  */
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate void
297*0Sstevel@tonic-gate svm_free(svm_info_t *svmp)
298*0Sstevel@tonic-gate {
299*0Sstevel@tonic-gate 	int i;
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	if (svmp == NULL)
302*0Sstevel@tonic-gate 		return;
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	for (i = 0; i < svmp->count; i++) {
305*0Sstevel@tonic-gate 		free(svmp->md_comps[i]);
306*0Sstevel@tonic-gate 	}
307*0Sstevel@tonic-gate 	free(svmp->root_md);
308*0Sstevel@tonic-gate 	free(svmp);
309*0Sstevel@tonic-gate }
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate /*
312*0Sstevel@tonic-gate  * FUNCTION: get_mdcomponents
313*0Sstevel@tonic-gate  *	Given "uname" metadevice, return the physical components
314*0Sstevel@tonic-gate  *      of that metadevice.
315*0Sstevel@tonic-gate  *
316*0Sstevel@tonic-gate  * INPUT:
317*0Sstevel@tonic-gate  *	uname - metadevice name
318*0Sstevel@tonic-gate  *
319*0Sstevel@tonic-gate  * RETURN VALUES:
320*0Sstevel@tonic-gate  *	svmp - structure containing md name and components
321*0Sstevel@tonic-gate  *	RET_SUCCESS
322*0Sstevel@tonic-gate  *	RET_ERROR
323*0Sstevel@tonic-gate  *
324*0Sstevel@tonic-gate  */
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate int
327*0Sstevel@tonic-gate get_mdcomponents(char *uname, svm_info_t **svmpp)
328*0Sstevel@tonic-gate {
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	svm_info_t	*svmp;
331*0Sstevel@tonic-gate 	md_error_t	status, *ep;
332*0Sstevel@tonic-gate 	mdname_t	*namep;
333*0Sstevel@tonic-gate 	mdnamelist_t	*nlp = NULL;
334*0Sstevel@tonic-gate 	mdnamelist_t	*p;
335*0Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
336*0Sstevel@tonic-gate 	char		*strp = NULL;
337*0Sstevel@tonic-gate 	int		rval, cnt;
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 	rval = RET_SUCCESS;
340*0Sstevel@tonic-gate 	cnt = 0;
341*0Sstevel@tonic-gate 	status = mdnullerror;
342*0Sstevel@tonic-gate 	ep = &status;
343*0Sstevel@tonic-gate 	svmp = *svmpp;
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	(void) init_metalib();
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	debug_printf("get_mdcomponents(): Enter unit name %s\n", uname);
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	if (((namep = metaname(&sp, uname, ep)) == NULL) ||
350*0Sstevel@tonic-gate 					(metachkmeta(namep, ep) != 0)) {
351*0Sstevel@tonic-gate 		debug_printf("get_mdcomponents(): "
352*0Sstevel@tonic-gate 				"metaname or metachkmeta failed\n");
353*0Sstevel@tonic-gate 		mdclrerror(ep);
354*0Sstevel@tonic-gate 		return (RET_ERROR);
355*0Sstevel@tonic-gate 	}
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	debug_printf("get_mdcomponents(): meta_getdevs %s\n", namep->cname);
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	if ((meta_getdevs(sp, namep, &nlp, ep)) < 0) {
360*0Sstevel@tonic-gate 		debug_printf("get_mdcomponents(): "
361*0Sstevel@tonic-gate 				"comp %s - meta_getdevs failed\n", uname);
362*0Sstevel@tonic-gate 		metafreenamelist(nlp);
363*0Sstevel@tonic-gate 		mdclrerror(ep);
364*0Sstevel@tonic-gate 		return (RET_ERROR);
365*0Sstevel@tonic-gate 	}
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	/* compute the number of devices */
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	for (p = nlp, cnt = 0; p != NULL;  p = p->next, cnt++)
370*0Sstevel@tonic-gate 		;
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	/*
373*0Sstevel@tonic-gate 	 * Need to add n -1 components since slvmp already has space
374*0Sstevel@tonic-gate 	 * for one device.
375*0Sstevel@tonic-gate 	 */
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	svmp = (svm_info_t *)realloc(svmp, sizeof (svm_info_t) +
378*0Sstevel@tonic-gate 		(sizeof (char *) * (cnt - 1)));
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	if (svmp == NULL) {
381*0Sstevel@tonic-gate 		debug_printf("get_mdcomponents(): realloc of svmp failed\n");
382*0Sstevel@tonic-gate 		metafreenamelist(nlp);
383*0Sstevel@tonic-gate 		return (RET_ERROR);
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	for (p = nlp, cnt = 0; p != NULL; p = p->next, cnt++) {
388*0Sstevel@tonic-gate 		mdname_t	*devnp = p->namep;
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 		if ((strp = strdup(devnp->cname)) == NULL) {
391*0Sstevel@tonic-gate 			rval = RET_ERROR;
392*0Sstevel@tonic-gate 			break;
393*0Sstevel@tonic-gate 		}
394*0Sstevel@tonic-gate 		svmp->md_comps[cnt] = strp;
395*0Sstevel@tonic-gate 	}
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate 	/* count is set to the number of devices in the list */
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	svmp->count = cnt;
400*0Sstevel@tonic-gate 	svmp->root_md = strdup(uname);
401*0Sstevel@tonic-gate 	if (rval == RET_SUCCESS && svmp->root_md != NULL) {
402*0Sstevel@tonic-gate 		debug_printf("get_mdcomponents(): root_md %s count %d \n",
403*0Sstevel@tonic-gate 			svmp->root_md, svmp->count);
404*0Sstevel@tonic-gate 		for (cnt = 0; cnt < svmp->count; cnt++)
405*0Sstevel@tonic-gate 			debug_printf("get_mdcomponents(): %s\n",
406*0Sstevel@tonic-gate 							svmp->md_comps[cnt]);
407*0Sstevel@tonic-gate 	} else {
408*0Sstevel@tonic-gate 		rval = RET_ERROR;
409*0Sstevel@tonic-gate 		svm_free(svmp);
410*0Sstevel@tonic-gate 		svmp = NULL;
411*0Sstevel@tonic-gate 		debug_printf("get_mdcomponents(): malloc failed\n");
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	}
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 	metafreenamelist(nlp);
417*0Sstevel@tonic-gate 	*svmpp = svmp;
418*0Sstevel@tonic-gate 	return (rval);
419*0Sstevel@tonic-gate }
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate /*
423*0Sstevel@tonic-gate  * FUNCTION: svm_get_components
424*0Sstevel@tonic-gate  *	return svm_infop with the components of a metadevice.
425*0Sstevel@tonic-gate  *
426*0Sstevel@tonic-gate  * INPUT:
427*0Sstevel@tonic-gate  *	md_device - eg. /dev/md/dsk/d10, /dev/md/foo/dsk/d10, or
428*0Sstevel@tonic-gate  *			/dev/md/shared/1/dsk/d10
429*0Sstevel@tonic-gate  *
430*0Sstevel@tonic-gate  * RETURN:
431*0Sstevel@tonic-gate  *	0 - SUCCESS
432*0Sstevel@tonic-gate  *     !0 - FAIL
433*0Sstevel@tonic-gate  */
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate int
436*0Sstevel@tonic-gate svm_get_components(char *md_device, svm_info_t **svmpp)
437*0Sstevel@tonic-gate {
438*0Sstevel@tonic-gate 	int	len;
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	/*
441*0Sstevel@tonic-gate 	 * If this is a named diskset with a shared name
442*0Sstevel@tonic-gate 	 * (e.g. /dev/md/shared/1/dsk/d10) call get_mdcomponents with
443*0Sstevel@tonic-gate 	 * the diskset and metadevice name (e.g. foo/d10).
444*0Sstevel@tonic-gate 	 * Otherwise this is a regular name (e.g. /dev/md/dsk/d10 or
445*0Sstevel@tonic-gate 	 * /dev/md/foo/dsk/d10 or d10 or foo/d10) all of which
446*0Sstevel@tonic-gate 	 * get_mdcomponents can handle directly.
447*0Sstevel@tonic-gate 	 */
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 	len = strlen("/dev/md/shared/");
450*0Sstevel@tonic-gate 	if (strncmp(md_device, "/dev/md/shared/", len) == 0) {
451*0Sstevel@tonic-gate 	    int		numlen;
452*0Sstevel@tonic-gate 	    int		setnum;
453*0Sstevel@tonic-gate 	    char	*cp;
454*0Sstevel@tonic-gate 	    char	*slashp;
455*0Sstevel@tonic-gate 	    char	mdname[MAXPATHLEN];
456*0Sstevel@tonic-gate 	    mdsetname_t	*sp;
457*0Sstevel@tonic-gate 	    md_error_t	error = mdnullerror;
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 	    cp = md_device + len;
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	    if ((slashp = strstr(cp, "/")) == NULL)
462*0Sstevel@tonic-gate 		return (RET_ERROR);
463*0Sstevel@tonic-gate 	    numlen = slashp - cp;
464*0Sstevel@tonic-gate 	    if (numlen >= MAXPATHLEN - 1)
465*0Sstevel@tonic-gate 		return (RET_ERROR);
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	    (void) strlcpy(mdname, cp, numlen + 1);
468*0Sstevel@tonic-gate 	    /* setnum now contains the diskset number */
469*0Sstevel@tonic-gate 	    setnum = atoi(mdname);
470*0Sstevel@tonic-gate 	    if ((sp = metasetnosetname(setnum, &error)) == NULL ||
471*0Sstevel@tonic-gate 		!mdisok(&error))
472*0Sstevel@tonic-gate 		return (RET_ERROR);
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate 	    cp = slashp + 1;
475*0Sstevel@tonic-gate 	    /* cp now pointing at dsk/... */
476*0Sstevel@tonic-gate 	    if ((slashp = strstr(cp, "/")) == NULL)
477*0Sstevel@tonic-gate 		return (RET_ERROR);
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	    (void) snprintf(mdname, MAXPATHLEN, "%s/%s", sp->setname,
480*0Sstevel@tonic-gate 		slashp + 1);
481*0Sstevel@tonic-gate 	    /* mdname now contains diskset and metadevice name e.g. foo/d10 */
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	    debug_printf("svm_get_components(): mdname %s\n", mdname);
484*0Sstevel@tonic-gate 	    return (get_mdcomponents(mdname, svmpp));
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 	} else {
487*0Sstevel@tonic-gate 	    debug_printf("svm_get_components(): md_device %s\n", md_device);
488*0Sstevel@tonic-gate 	    return (get_mdcomponents(md_device, svmpp));
489*0Sstevel@tonic-gate 	}
490*0Sstevel@tonic-gate }
491