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 /*
30*0Sstevel@tonic-gate  * Just in case we're not in a build environment, make sure that
31*0Sstevel@tonic-gate  * TEXT_DOMAIN gets set to something.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
34*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
35*0Sstevel@tonic-gate #endif
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * MH ioctl functions
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <meta.h>
42*0Sstevel@tonic-gate #include <metamhd.h>
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include "meta_runtime.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #define	DEFAULTDEV "/dev/rdsk"
48*0Sstevel@tonic-gate /*
49*0Sstevel@tonic-gate  * default timeout values
50*0Sstevel@tonic-gate  */
51*0Sstevel@tonic-gate mhd_mhiargs_t	defmhiargs = {
52*0Sstevel@tonic-gate 	1000,			/* failfast */
53*0Sstevel@tonic-gate 	{ 6000, 6000, 30000 }	/* take ownership */
54*0Sstevel@tonic-gate };
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /* RPC timeouts */
57*0Sstevel@tonic-gate static md_timeval32_t	tk_own_timeout  = { 24 * 60 * 60, 0 };	/* 1 day */
58*0Sstevel@tonic-gate static md_timeval32_t	rel_own_timeout = { 24 * 60 * 60, 0 };	/* 1 day */
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  * RPC handle
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate typedef struct {
64*0Sstevel@tonic-gate 	char	*hostname;
65*0Sstevel@tonic-gate 	CLIENT	*clientp;
66*0Sstevel@tonic-gate } mhd_handle_t;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * close RPC connection
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate static void
72*0Sstevel@tonic-gate close_metamhd(
73*0Sstevel@tonic-gate 	mhd_handle_t	*hp
74*0Sstevel@tonic-gate )
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	assert(hp != NULL);
77*0Sstevel@tonic-gate 	if (hp->hostname != NULL) {
78*0Sstevel@tonic-gate 		Free(hp->hostname);
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate 	if (hp->clientp != NULL) {
81*0Sstevel@tonic-gate 		auth_destroy(hp->clientp->cl_auth);
82*0Sstevel@tonic-gate 		clnt_destroy(hp->clientp);
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 	Free(hp);
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate /*
88*0Sstevel@tonic-gate  * open RPC connection to rpc.metamhd
89*0Sstevel@tonic-gate  */
90*0Sstevel@tonic-gate static mhd_handle_t *
91*0Sstevel@tonic-gate open_metamhd(
92*0Sstevel@tonic-gate 	char		*hostname,
93*0Sstevel@tonic-gate 	md_error_t	*ep
94*0Sstevel@tonic-gate )
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	CLIENT		*clientp;
97*0Sstevel@tonic-gate 	mhd_handle_t	*hp;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	/* default to local host */
100*0Sstevel@tonic-gate 	if ((hostname == NULL) || (*hostname == '\0'))
101*0Sstevel@tonic-gate 		hostname = mynode();
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	/* open RPC connection */
104*0Sstevel@tonic-gate 	assert(hostname != NULL);
105*0Sstevel@tonic-gate 	if ((clientp = meta_client_create(hostname, METAMHD, METAMHD_VERSION,
106*0Sstevel@tonic-gate 	    "tcp")) == NULL) {
107*0Sstevel@tonic-gate 		clnt_pcreateerror(hostname);
108*0Sstevel@tonic-gate 		(void) mdrpccreateerror(ep, hostname, "metamhd clnt_create");
109*0Sstevel@tonic-gate 		return (NULL);
110*0Sstevel@tonic-gate 	} else {
111*0Sstevel@tonic-gate 		auth_destroy(clientp->cl_auth);
112*0Sstevel@tonic-gate 		clientp->cl_auth = authsys_create_default();
113*0Sstevel@tonic-gate 		assert(clientp->cl_auth != NULL);
114*0Sstevel@tonic-gate 	}
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	/* return connection */
117*0Sstevel@tonic-gate 	hp = Zalloc(sizeof (*hp));
118*0Sstevel@tonic-gate 	hp->hostname = Strdup(hostname);
119*0Sstevel@tonic-gate 	hp->clientp = clientp;
120*0Sstevel@tonic-gate 	return (hp);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate /*
124*0Sstevel@tonic-gate  * steal and convert mherror_t
125*0Sstevel@tonic-gate  */
126*0Sstevel@tonic-gate int
127*0Sstevel@tonic-gate mhstealerror(
128*0Sstevel@tonic-gate 	mhd_error_t	*mhep,
129*0Sstevel@tonic-gate 	md_error_t	*ep
130*0Sstevel@tonic-gate )
131*0Sstevel@tonic-gate {
132*0Sstevel@tonic-gate 	int		rval = -1;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	/* no error */
135*0Sstevel@tonic-gate 	if (mhep->errnum == 0) {
136*0Sstevel@tonic-gate 		/* assert(mhep->name == NULL); */
137*0Sstevel@tonic-gate 		rval = 0;
138*0Sstevel@tonic-gate 		goto out;
139*0Sstevel@tonic-gate 	}
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	/* steal error */
142*0Sstevel@tonic-gate 	switch (mhep->errnum) {
143*0Sstevel@tonic-gate 	case MHD_E_MAJORITY:
144*0Sstevel@tonic-gate 		(void) mderror(ep, MDE_TAKE_OWN, mhep->name);
145*0Sstevel@tonic-gate 		break;
146*0Sstevel@tonic-gate 	case MHD_E_RESERVED:
147*0Sstevel@tonic-gate 		(void) mderror(ep, MDE_RESERVED, mhep->name);
148*0Sstevel@tonic-gate 		break;
149*0Sstevel@tonic-gate 	default:
150*0Sstevel@tonic-gate 		(void) mdsyserror(ep, mhep->errnum, mhep->name);
151*0Sstevel@tonic-gate 		break;
152*0Sstevel@tonic-gate 	}
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	/* cleanup, return success */
155*0Sstevel@tonic-gate out:
156*0Sstevel@tonic-gate 	if (mhep->name != NULL)
157*0Sstevel@tonic-gate 		Free(mhep->name);
158*0Sstevel@tonic-gate 	(void) memset(mhep, 0, sizeof (*mhep));
159*0Sstevel@tonic-gate 	return (rval);
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate /*
163*0Sstevel@tonic-gate  * should we do MHIOCTLs ?
164*0Sstevel@tonic-gate  */
165*0Sstevel@tonic-gate static int
166*0Sstevel@tonic-gate do_mhioctl()
167*0Sstevel@tonic-gate {
168*0Sstevel@tonic-gate 	if (getenv("MD_NOMHIOCTL") != NULL) {
169*0Sstevel@tonic-gate 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
170*0Sstevel@tonic-gate 		    "NOT doing MH ioctls\n"));
171*0Sstevel@tonic-gate 		(void) fflush(stderr);
172*0Sstevel@tonic-gate 		return (0);
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 	return (1);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate /*
178*0Sstevel@tonic-gate  * take ownership of drives
179*0Sstevel@tonic-gate  */
180*0Sstevel@tonic-gate int
181*0Sstevel@tonic-gate meta_take_own(
182*0Sstevel@tonic-gate 	char			*sname,
183*0Sstevel@tonic-gate 	mddrivenamelist_t	*dnlp,
184*0Sstevel@tonic-gate 	mhd_mhiargs_t		*mhiargsp,
185*0Sstevel@tonic-gate 	int			partial_set,
186*0Sstevel@tonic-gate 	md_error_t		*ep
187*0Sstevel@tonic-gate )
188*0Sstevel@tonic-gate {
189*0Sstevel@tonic-gate 	mddrivenamelist_t	*p;
190*0Sstevel@tonic-gate 	uint_t			ndev = 0;
191*0Sstevel@tonic-gate 	mhd_tkown_args_t	args;
192*0Sstevel@tonic-gate 	mhd_error_t		mherror;
193*0Sstevel@tonic-gate 	mhd_set_t		*mhsp = &args.set;
194*0Sstevel@tonic-gate 	uint_t			i;
195*0Sstevel@tonic-gate 	char			*e;
196*0Sstevel@tonic-gate 	mhd_handle_t		*hp = NULL;
197*0Sstevel@tonic-gate 	int			rval = -1;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	/*
200*0Sstevel@tonic-gate 	 * RFE 4126509.  Check the runtime parameters to see if
201*0Sstevel@tonic-gate 	 * they're set to disable MHIOCTKOWN ioctl() operations
202*0Sstevel@tonic-gate 	 * on the disks.  If so, return immediately without
203*0Sstevel@tonic-gate 	 * performing the operations.
204*0Sstevel@tonic-gate 	 */
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	if (do_owner_ioctls() == B_FALSE) {
207*0Sstevel@tonic-gate 		return (0);
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	/* count drives, get set */
211*0Sstevel@tonic-gate 	for (p = dnlp; (p != NULL); p = p->next)
212*0Sstevel@tonic-gate 		++ndev;
213*0Sstevel@tonic-gate 	if (ndev == 0)
214*0Sstevel@tonic-gate 		return (0);
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	/* initialize */
217*0Sstevel@tonic-gate 	(void) memset(&args, 0, sizeof (args));
218*0Sstevel@tonic-gate 	(void) memset(&mherror, 0, sizeof (mherror));
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	/* build arguments */
221*0Sstevel@tonic-gate 	mhsp->setname = Strdup(sname);
222*0Sstevel@tonic-gate 	mhsp->drives.drives_len = ndev;
223*0Sstevel@tonic-gate 	mhsp->drives.drives_val
224*0Sstevel@tonic-gate 	    = Calloc(ndev, sizeof (*mhsp->drives.drives_val));
225*0Sstevel@tonic-gate 	for (p = dnlp, i = 0; (i < ndev); p = p->next, ++i) {
226*0Sstevel@tonic-gate 		mhsp->drives.drives_val[i] = Strdup(p->drivenamep->rname);
227*0Sstevel@tonic-gate 	}
228*0Sstevel@tonic-gate 	args.timeouts = *mhiargsp;
229*0Sstevel@tonic-gate 	args.ff_mode = MHD_FF_DRIVER;
230*0Sstevel@tonic-gate 	if (((e = getenv("MD_DEBUG")) != NULL) &&
231*0Sstevel@tonic-gate 	    ((e = strstr(e, "FAILFAST=")) != NULL) &&
232*0Sstevel@tonic-gate 	    ((e = strchr(e, '=')) != NULL)) {
233*0Sstevel@tonic-gate 		++e;
234*0Sstevel@tonic-gate 		if (strcmp(e, "NONE") == 0)
235*0Sstevel@tonic-gate 			args.ff_mode = MHD_FF_NONE;
236*0Sstevel@tonic-gate 		else if (strcmp(e, "DRIVER") == 0)
237*0Sstevel@tonic-gate 			args.ff_mode = MHD_FF_DRIVER;
238*0Sstevel@tonic-gate 		else if (strcmp(e, "DEBUG") == 0)
239*0Sstevel@tonic-gate 			args.ff_mode = MHD_FF_DEBUG;
240*0Sstevel@tonic-gate 		else if (strcmp(e, "HALT") == 0)
241*0Sstevel@tonic-gate 			args.ff_mode = MHD_FF_HALT;
242*0Sstevel@tonic-gate 		else if (strcmp(e, "PANIC") == 0)
243*0Sstevel@tonic-gate 			args.ff_mode = MHD_FF_PANIC;
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate 	if (partial_set)
246*0Sstevel@tonic-gate 		args.options |= MHD_PARTIAL_SET;
247*0Sstevel@tonic-gate 	if (((e = getenv("MD_DEBUG")) != NULL) &&
248*0Sstevel@tonic-gate 	    (strstr(e, "NOTHREAD") != NULL)) {
249*0Sstevel@tonic-gate 		args.options |= MHD_SERIAL;
250*0Sstevel@tonic-gate 	}
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	/* open connection */
253*0Sstevel@tonic-gate 	if ((hp = open_metamhd(NULL, ep)) == NULL)
254*0Sstevel@tonic-gate 		return (-1);
255*0Sstevel@tonic-gate 	clnt_control(hp->clientp, CLSET_TIMEOUT, (char *)&tk_own_timeout);
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	/* take ownership */
258*0Sstevel@tonic-gate 	if (mhd_tkown_1(&args, &mherror, hp->clientp) != RPC_SUCCESS) {
259*0Sstevel@tonic-gate 		(void) mdrpcerror(ep, hp->clientp, hp->hostname,
260*0Sstevel@tonic-gate 		    "metamhd tkown");
261*0Sstevel@tonic-gate 	} else if (mhstealerror(&mherror, ep) == 0) {
262*0Sstevel@tonic-gate 		rval = 0;	/* success */
263*0Sstevel@tonic-gate 	}
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	/* cleanup, return success */
266*0Sstevel@tonic-gate out:
267*0Sstevel@tonic-gate 	xdr_free(xdr_mhd_tkown_args_t, (char *)&args);
268*0Sstevel@tonic-gate 	xdr_free(xdr_mhd_error_t, (char *)&mherror);
269*0Sstevel@tonic-gate 	if (hp != NULL)
270*0Sstevel@tonic-gate 		close_metamhd(hp);
271*0Sstevel@tonic-gate 	return (rval);
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate /*
275*0Sstevel@tonic-gate  * take ownership of drives
276*0Sstevel@tonic-gate  */
277*0Sstevel@tonic-gate int
278*0Sstevel@tonic-gate tk_own_bydd(
279*0Sstevel@tonic-gate 	mdsetname_t		*sp,
280*0Sstevel@tonic-gate 	md_drive_desc		*ddlp,
281*0Sstevel@tonic-gate 	mhd_mhiargs_t		*mhiargsp,
282*0Sstevel@tonic-gate 	int			partial_set,
283*0Sstevel@tonic-gate 	md_error_t		*ep
284*0Sstevel@tonic-gate )
285*0Sstevel@tonic-gate {
286*0Sstevel@tonic-gate 	mddrivenamelist_t	*dnlp = NULL;
287*0Sstevel@tonic-gate 	mddrivenamelist_t	**tailpp = &dnlp;
288*0Sstevel@tonic-gate 	md_drive_desc		*p;
289*0Sstevel@tonic-gate 	int			rval;
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	/*
292*0Sstevel@tonic-gate 	 * Add the drivename struct to the end of the
293*0Sstevel@tonic-gate 	 * drivenamelist but keep a pointer to the last
294*0Sstevel@tonic-gate 	 * element so that we don't incur the overhead
295*0Sstevel@tonic-gate 	 * of traversing the list each time
296*0Sstevel@tonic-gate 	 */
297*0Sstevel@tonic-gate 	for (p = ddlp; (p != NULL); p = p->dd_next)
298*0Sstevel@tonic-gate 		tailpp = meta_drivenamelist_append_wrapper(tailpp, p->dd_dnp);
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	/* take ownership */
301*0Sstevel@tonic-gate 	rval = meta_take_own(sp->setname, dnlp, mhiargsp, partial_set, ep);
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	/* cleanup, return success */
304*0Sstevel@tonic-gate 	metafreedrivenamelist(dnlp);
305*0Sstevel@tonic-gate 	return (rval);
306*0Sstevel@tonic-gate }
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate /*
309*0Sstevel@tonic-gate  * release ownership of drives
310*0Sstevel@tonic-gate  */
311*0Sstevel@tonic-gate int
312*0Sstevel@tonic-gate meta_rel_own(
313*0Sstevel@tonic-gate 	char			*sname,
314*0Sstevel@tonic-gate 	mddrivenamelist_t	*dnlp,
315*0Sstevel@tonic-gate 	int			partial_set,
316*0Sstevel@tonic-gate 	md_error_t		*ep
317*0Sstevel@tonic-gate )
318*0Sstevel@tonic-gate {
319*0Sstevel@tonic-gate 	mddrivenamelist_t	*p;
320*0Sstevel@tonic-gate 	uint_t			ndev = 0;
321*0Sstevel@tonic-gate 	mhd_relown_args_t	args;
322*0Sstevel@tonic-gate 	mhd_error_t		mherror;
323*0Sstevel@tonic-gate 	mhd_set_t		*mhsp = &args.set;
324*0Sstevel@tonic-gate 	uint_t			i;
325*0Sstevel@tonic-gate 	char			*e;
326*0Sstevel@tonic-gate 	mhd_handle_t		*hp = NULL;
327*0Sstevel@tonic-gate 	int			rval = -1;
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	/*
330*0Sstevel@tonic-gate 	 * RFE 4126509.  Check the runtime parameters to see if
331*0Sstevel@tonic-gate 	 * they're set to disable MHIOCRELEASE and MHIOCENFAILFAST
332*0Sstevel@tonic-gate 	 * ioctl() operations on the disks.  If so, return
333*0Sstevel@tonic-gate 	 * immediately without performing the operations.
334*0Sstevel@tonic-gate 	 */
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	if (do_owner_ioctls() == B_FALSE) {
337*0Sstevel@tonic-gate 		return (0);
338*0Sstevel@tonic-gate 	}
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	/*
341*0Sstevel@tonic-gate 	 * if not doing ioctls (HK 98/10/28: the following code tests
342*0Sstevel@tonic-gate 	 * an environment variable, and was apparently inserted to
343*0Sstevel@tonic-gate 	 * make testing easier.)
344*0Sstevel@tonic-gate 	 */
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	if (! do_mhioctl())
347*0Sstevel@tonic-gate 		return (0);
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	/* count drives, get set */
350*0Sstevel@tonic-gate 	for (p = dnlp; (p != NULL); p = p->next)
351*0Sstevel@tonic-gate 		++ndev;
352*0Sstevel@tonic-gate 	if (ndev == 0)
353*0Sstevel@tonic-gate 		return (0);
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 	/* initialize */
356*0Sstevel@tonic-gate 	(void) memset(&args, 0, sizeof (args));
357*0Sstevel@tonic-gate 	(void) memset(&mherror, 0, sizeof (mherror));
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	/* build arguments */
360*0Sstevel@tonic-gate 	mhsp->setname = Strdup(sname);
361*0Sstevel@tonic-gate 	mhsp->drives.drives_len = ndev;
362*0Sstevel@tonic-gate 	mhsp->drives.drives_val
363*0Sstevel@tonic-gate 	    = Calloc(ndev, sizeof (*mhsp->drives.drives_val));
364*0Sstevel@tonic-gate 	for (p = dnlp, i = 0; (i < ndev); p = p->next, ++i) {
365*0Sstevel@tonic-gate 		mhsp->drives.drives_val[i] = Strdup(p->drivenamep->rname);
366*0Sstevel@tonic-gate 	}
367*0Sstevel@tonic-gate 	if (partial_set)
368*0Sstevel@tonic-gate 		args.options |= MHD_PARTIAL_SET;
369*0Sstevel@tonic-gate 	if (((e = getenv("MD_DEBUG")) != NULL) &&
370*0Sstevel@tonic-gate 	    (strstr(e, "NOTHREAD") != NULL)) {
371*0Sstevel@tonic-gate 		args.options |= MHD_SERIAL;
372*0Sstevel@tonic-gate 	}
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	/* open connection */
375*0Sstevel@tonic-gate 	if ((hp = open_metamhd(NULL, ep)) == NULL)
376*0Sstevel@tonic-gate 		return (-1);
377*0Sstevel@tonic-gate 	clnt_control(hp->clientp, CLSET_TIMEOUT, (char *)&rel_own_timeout);
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	/* take ownership */
380*0Sstevel@tonic-gate 	if (mhd_relown_1(&args, &mherror, hp->clientp) != RPC_SUCCESS) {
381*0Sstevel@tonic-gate 		(void) mdrpcerror(ep, hp->clientp, hp->hostname,
382*0Sstevel@tonic-gate 		    "metamhd relown");
383*0Sstevel@tonic-gate 	} else if (mhstealerror(&mherror, ep) == 0) {
384*0Sstevel@tonic-gate 		rval = 0;	/* success */
385*0Sstevel@tonic-gate 	}
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	/* cleanup, return success */
388*0Sstevel@tonic-gate out:
389*0Sstevel@tonic-gate 	xdr_free(xdr_mhd_relown_args_t, (char *)&args);
390*0Sstevel@tonic-gate 	xdr_free(xdr_mhd_error_t, (char *)&mherror);
391*0Sstevel@tonic-gate 	if (hp != NULL)
392*0Sstevel@tonic-gate 		close_metamhd(hp);
393*0Sstevel@tonic-gate 	return (rval);
394*0Sstevel@tonic-gate }
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate /*
397*0Sstevel@tonic-gate  * release ownership of drives
398*0Sstevel@tonic-gate  */
399*0Sstevel@tonic-gate int
400*0Sstevel@tonic-gate rel_own_bydd(
401*0Sstevel@tonic-gate 	mdsetname_t		*sp,
402*0Sstevel@tonic-gate 	md_drive_desc		*ddlp,
403*0Sstevel@tonic-gate 	int			partial_set,
404*0Sstevel@tonic-gate 	md_error_t		*ep
405*0Sstevel@tonic-gate )
406*0Sstevel@tonic-gate {
407*0Sstevel@tonic-gate 	mddrivenamelist_t	*dnlp = NULL;
408*0Sstevel@tonic-gate 	mddrivenamelist_t	**tailpp = &dnlp;
409*0Sstevel@tonic-gate 	md_drive_desc		*p;
410*0Sstevel@tonic-gate 	int			rval;
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 	/*
413*0Sstevel@tonic-gate 	 * Add the drivename struct to the end of the
414*0Sstevel@tonic-gate 	 * drivenamelist but keep a pointer to the last
415*0Sstevel@tonic-gate 	 * element so that we don't incur the overhead
416*0Sstevel@tonic-gate 	 * of traversing the list each time
417*0Sstevel@tonic-gate 	 */
418*0Sstevel@tonic-gate 	for (p = ddlp; (p != NULL); p = p->dd_next)
419*0Sstevel@tonic-gate 		tailpp = meta_drivenamelist_append_wrapper(tailpp, p->dd_dnp);
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	/* release ownership */
422*0Sstevel@tonic-gate 	rval = meta_rel_own(sp->setname, dnlp, partial_set, ep);
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate 	/* cleanup, return success */
425*0Sstevel@tonic-gate 	metafreedrivenamelist(dnlp);
426*0Sstevel@tonic-gate 	return (rval);
427*0Sstevel@tonic-gate }
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate /*
430*0Sstevel@tonic-gate  * get status of drives
431*0Sstevel@tonic-gate  */
432*0Sstevel@tonic-gate int
433*0Sstevel@tonic-gate meta_status_own(
434*0Sstevel@tonic-gate 	char			*sname,
435*0Sstevel@tonic-gate 	md_disk_status_list_t	*dslp,
436*0Sstevel@tonic-gate 	int			partial_set,
437*0Sstevel@tonic-gate 	md_error_t		*ep
438*0Sstevel@tonic-gate )
439*0Sstevel@tonic-gate {
440*0Sstevel@tonic-gate 	md_disk_status_list_t	*p;
441*0Sstevel@tonic-gate 	uint_t			ndev = 0;
442*0Sstevel@tonic-gate 	mhd_status_args_t	args;
443*0Sstevel@tonic-gate 	mhd_status_res_t	results;
444*0Sstevel@tonic-gate 	mhd_error_t		*mhep = &results.status;
445*0Sstevel@tonic-gate 	mhd_set_t		*mhsp = &args.set;
446*0Sstevel@tonic-gate 	uint_t			i;
447*0Sstevel@tonic-gate 	char			*e;
448*0Sstevel@tonic-gate 	mhd_handle_t		*hp = NULL;
449*0Sstevel@tonic-gate 	int			rval = -1;
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 	/* if not doing ioctls */
452*0Sstevel@tonic-gate 	if (! do_mhioctl())
453*0Sstevel@tonic-gate 		return (0);
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 	/* count drives, get set */
456*0Sstevel@tonic-gate 	for (p = dslp; (p != NULL); p = p->next)
457*0Sstevel@tonic-gate 		++ndev;
458*0Sstevel@tonic-gate 	if (ndev == 0)
459*0Sstevel@tonic-gate 		return (0);
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	/* initialize */
462*0Sstevel@tonic-gate 	(void) memset(&args, 0, sizeof (args));
463*0Sstevel@tonic-gate 	(void) memset(&results, 0, sizeof (results));
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	/* build arguments */
466*0Sstevel@tonic-gate 	mhsp->setname = Strdup(sname);
467*0Sstevel@tonic-gate 	mhsp->drives.drives_len = ndev;
468*0Sstevel@tonic-gate 	mhsp->drives.drives_val
469*0Sstevel@tonic-gate 	    = Calloc(ndev, sizeof (*mhsp->drives.drives_val));
470*0Sstevel@tonic-gate 	for (p = dslp, i = 0; (i < ndev); p = p->next, ++i) {
471*0Sstevel@tonic-gate 		mhsp->drives.drives_val[i] = Strdup(p->drivenamep->rname);
472*0Sstevel@tonic-gate 	}
473*0Sstevel@tonic-gate 	if (partial_set)
474*0Sstevel@tonic-gate 		args.options |= MHD_PARTIAL_SET;
475*0Sstevel@tonic-gate 	if (((e = getenv("MD_DEBUG")) != NULL) &&
476*0Sstevel@tonic-gate 	    (strstr(e, "NOTHREAD") != NULL)) {
477*0Sstevel@tonic-gate 		args.options |= MHD_SERIAL;
478*0Sstevel@tonic-gate 	}
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	/* open connection */
481*0Sstevel@tonic-gate 	if ((hp = open_metamhd(NULL, ep)) == NULL)
482*0Sstevel@tonic-gate 		return (-1);
483*0Sstevel@tonic-gate 	clnt_control(hp->clientp, CLSET_TIMEOUT, (char *)&tk_own_timeout);
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	/* get status */
486*0Sstevel@tonic-gate 	if (mhd_status_1(&args, &results, hp->clientp) != RPC_SUCCESS) {
487*0Sstevel@tonic-gate 		(void) mdrpcerror(ep, hp->clientp, hp->hostname,
488*0Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "metamhd status"));
489*0Sstevel@tonic-gate 		goto out;
490*0Sstevel@tonic-gate 	} else if (mhstealerror(mhep, ep) != 0) {
491*0Sstevel@tonic-gate 		goto out;
492*0Sstevel@tonic-gate 	}
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate 	/* do something with it */
495*0Sstevel@tonic-gate 	assert(results.results.results_len == ndev);
496*0Sstevel@tonic-gate 	for (p = dslp, i = 0; (i < ndev); p = p->next, ++i) {
497*0Sstevel@tonic-gate 		mhd_drive_status_t	*resp = &results.results.results_val[i];
498*0Sstevel@tonic-gate 		mddrivename_t		*dp = p->drivenamep;
499*0Sstevel@tonic-gate 		mhd_error_t		mherror;
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 		/* make sure we have the right drive */
502*0Sstevel@tonic-gate 		assert(strcmp(dp->rname, resp->drive) == 0);
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 		/* copy status */
505*0Sstevel@tonic-gate 		if (resp->errnum != 0) {
506*0Sstevel@tonic-gate 			(void) memset(&mherror, 0, sizeof (mherror));
507*0Sstevel@tonic-gate 			mherror.errnum = resp->errnum;
508*0Sstevel@tonic-gate 			mherror.name = Strdup(resp->drive);
509*0Sstevel@tonic-gate 			(void) mhstealerror(&mherror, &p->status);
510*0Sstevel@tonic-gate 		}
511*0Sstevel@tonic-gate 	}
512*0Sstevel@tonic-gate 	rval = 0;		/* success */
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate 	/* cleanup, return success */
515*0Sstevel@tonic-gate out:
516*0Sstevel@tonic-gate 	xdr_free(xdr_mhd_status_args_t, (char *)&args);
517*0Sstevel@tonic-gate 	xdr_free(xdr_mhd_status_res_t, (char *)&results);
518*0Sstevel@tonic-gate 	if (hp != NULL)
519*0Sstevel@tonic-gate 		close_metamhd(hp);
520*0Sstevel@tonic-gate 	return (rval);
521*0Sstevel@tonic-gate }
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate /*
524*0Sstevel@tonic-gate  * build disk status list from drivename list
525*0Sstevel@tonic-gate  */
526*0Sstevel@tonic-gate md_disk_status_list_t *
527*0Sstevel@tonic-gate meta_drive_to_disk_status_list(
528*0Sstevel@tonic-gate 	mddrivenamelist_t	*dnlp
529*0Sstevel@tonic-gate )
530*0Sstevel@tonic-gate {
531*0Sstevel@tonic-gate 	md_disk_status_list_t	*head = NULL;
532*0Sstevel@tonic-gate 	md_disk_status_list_t	**tailp = &head;
533*0Sstevel@tonic-gate 	mddrivenamelist_t	*p;
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate 	/* copy list */
536*0Sstevel@tonic-gate 	for (p = dnlp; (p != NULL); p = p->next) {
537*0Sstevel@tonic-gate 		md_disk_status_list_t	*dsp;
538*0Sstevel@tonic-gate 
539*0Sstevel@tonic-gate 		dsp = *tailp = Zalloc(sizeof (*dsp));
540*0Sstevel@tonic-gate 		tailp = &dsp->next;
541*0Sstevel@tonic-gate 		dsp->drivenamep = p->drivenamep;
542*0Sstevel@tonic-gate 	}
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	/* return list */
545*0Sstevel@tonic-gate 	return (head);
546*0Sstevel@tonic-gate }
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate /*
549*0Sstevel@tonic-gate  * free disk status list
550*0Sstevel@tonic-gate  */
551*0Sstevel@tonic-gate void
552*0Sstevel@tonic-gate meta_free_disk_status_list(
553*0Sstevel@tonic-gate 	md_disk_status_list_t	*dslp
554*0Sstevel@tonic-gate )
555*0Sstevel@tonic-gate {
556*0Sstevel@tonic-gate 	md_disk_status_list_t	*next = NULL;
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	for (/* void */; (dslp != NULL); dslp = next) {
559*0Sstevel@tonic-gate 		next = dslp->next;
560*0Sstevel@tonic-gate 		mdclrerror(&dslp->status);
561*0Sstevel@tonic-gate 		Free(dslp);
562*0Sstevel@tonic-gate 	}
563*0Sstevel@tonic-gate }
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate /*
566*0Sstevel@tonic-gate  * free drive info list
567*0Sstevel@tonic-gate  */
568*0Sstevel@tonic-gate void
569*0Sstevel@tonic-gate meta_free_drive_info_list(
570*0Sstevel@tonic-gate 	mhd_drive_info_list_t	*listp
571*0Sstevel@tonic-gate )
572*0Sstevel@tonic-gate {
573*0Sstevel@tonic-gate 	xdr_free(xdr_mhd_drive_info_list_t, (char *)listp);
574*0Sstevel@tonic-gate 	(void) memset(listp, 0, sizeof (*listp));
575*0Sstevel@tonic-gate }
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate /*
578*0Sstevel@tonic-gate  * sort drive info list
579*0Sstevel@tonic-gate  */
580*0Sstevel@tonic-gate static int
581*0Sstevel@tonic-gate compare_drives(
582*0Sstevel@tonic-gate 	const void		*p1,
583*0Sstevel@tonic-gate 	const void		*p2
584*0Sstevel@tonic-gate )
585*0Sstevel@tonic-gate {
586*0Sstevel@tonic-gate 	const mhd_drive_info_t	*di1 = p1;
587*0Sstevel@tonic-gate 	const mhd_drive_info_t	*di2 = p2;
588*0Sstevel@tonic-gate 	const char		*n1 = di1->dif_name;
589*0Sstevel@tonic-gate 	const char		*n2 = di2->dif_name;
590*0Sstevel@tonic-gate 	uint_t			c1 = 0, t1 = 0, d1 = 0, s1 = 0;
591*0Sstevel@tonic-gate 	uint_t			c2 = 0, t2 = 0, d2 = 0, s2 = 0;
592*0Sstevel@tonic-gate 	uint_t			l, cl;
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	if (n1 == NULL)
595*0Sstevel@tonic-gate 		n1 = "";
596*0Sstevel@tonic-gate 	if (n2 == NULL)
597*0Sstevel@tonic-gate 		n2 = "";
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	/* attempt to sort correctly for c0t1d0s0 .vs. c0t18d0s0 */
600*0Sstevel@tonic-gate 	if ((n1 = strrchr(n1, '/')) == NULL)
601*0Sstevel@tonic-gate 		goto u;
602*0Sstevel@tonic-gate 	n1 += (n1[1] != 'c') ? 2 : 1;
603*0Sstevel@tonic-gate 	cl = strlen(n1);
604*0Sstevel@tonic-gate 	if ((sscanf(n1, "c%ut%ud%us%u%n", &c1, &t1, &d1, &s1, &l) != 4 &&
605*0Sstevel@tonic-gate 	    sscanf(n1, "c%ud%us%u%n", &c1, &d1, &s1, &l) != 3 &&
606*0Sstevel@tonic-gate 	    sscanf(n1, "c%ut%ud%u%n", &c1, &t1, &d1, &l) != 3 &&
607*0Sstevel@tonic-gate 	    sscanf(n1, "c%ud%u%n", &c1, &d1, &l) != 2) || (l != cl))
608*0Sstevel@tonic-gate 		goto u;
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate 	if ((n2 = strrchr(n2, '/')) == NULL)
611*0Sstevel@tonic-gate 		goto u;
612*0Sstevel@tonic-gate 	n2 += (n2[1] != 'c') ? 2 : 1;
613*0Sstevel@tonic-gate 	cl = strlen(n2);
614*0Sstevel@tonic-gate 	if ((sscanf(n2, "c%ut%ud%us%u%n", &c2, &t2, &d2, &s2, &l) != 4 &&
615*0Sstevel@tonic-gate 	    sscanf(n2, "c%ud%us%u%n", &c2, &d2, &s2, &l) != 3 &&
616*0Sstevel@tonic-gate 	    sscanf(n2, "c%ut%ud%u%n", &c2, &t2, &d2, &l) != 3 &&
617*0Sstevel@tonic-gate 	    sscanf(n2, "c%ud%u%n", &c2, &d2, &l) != 2) || (l != cl))
618*0Sstevel@tonic-gate 		goto u;
619*0Sstevel@tonic-gate 	if (c1 != c2)
620*0Sstevel@tonic-gate 		return ((c1 > c2) ? 1 : -1);
621*0Sstevel@tonic-gate 	if (t1 != t2)
622*0Sstevel@tonic-gate 		return ((t1 > t2) ? 1 : -1);
623*0Sstevel@tonic-gate 	if (d1 != d2)
624*0Sstevel@tonic-gate 		return ((d1 > d2) ? 1 : -1);
625*0Sstevel@tonic-gate 	if (s1 != s2)
626*0Sstevel@tonic-gate 		return ((s1 > s2) ? 1 : -1);
627*0Sstevel@tonic-gate 	return (0);
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate u:	return (strcmp(di1->dif_name, di2->dif_name));
630*0Sstevel@tonic-gate }
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate static void
633*0Sstevel@tonic-gate sort_drives(
634*0Sstevel@tonic-gate 	mhd_drive_info_list_t	*listp
635*0Sstevel@tonic-gate )
636*0Sstevel@tonic-gate {
637*0Sstevel@tonic-gate 	qsort(listp->mhd_drive_info_list_t_val,
638*0Sstevel@tonic-gate 	    listp->mhd_drive_info_list_t_len,
639*0Sstevel@tonic-gate 	    sizeof (*listp->mhd_drive_info_list_t_val),
640*0Sstevel@tonic-gate 	    compare_drives);
641*0Sstevel@tonic-gate }
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate /*
644*0Sstevel@tonic-gate  * return list of all drives
645*0Sstevel@tonic-gate  */
646*0Sstevel@tonic-gate int
647*0Sstevel@tonic-gate meta_list_drives(
648*0Sstevel@tonic-gate 	char			*hostname,
649*0Sstevel@tonic-gate 	char			*path,
650*0Sstevel@tonic-gate 	mhd_did_flags_t		flags,
651*0Sstevel@tonic-gate 	mhd_drive_info_list_t	*listp,
652*0Sstevel@tonic-gate 	md_error_t		*ep
653*0Sstevel@tonic-gate )
654*0Sstevel@tonic-gate {
655*0Sstevel@tonic-gate 	mhd_list_args_t		args;
656*0Sstevel@tonic-gate 	mhd_list_res_t		results;
657*0Sstevel@tonic-gate 	mhd_error_t		*mhep = &results.status;
658*0Sstevel@tonic-gate 	mhd_handle_t		*hp = NULL;
659*0Sstevel@tonic-gate 	int			rval = -1;
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate 	/* if not doing ioctls */
662*0Sstevel@tonic-gate 	if (! do_mhioctl())
663*0Sstevel@tonic-gate 		return (0);
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 	/* initialize */
666*0Sstevel@tonic-gate 	(void) memset(&args, 0, sizeof (args));
667*0Sstevel@tonic-gate 	(void) memset(&results, 0, sizeof (results));
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 	/* build arguments */
670*0Sstevel@tonic-gate 	if (path == NULL)
671*0Sstevel@tonic-gate 		path = getenv("MD_DRIVE_ROOT");
672*0Sstevel@tonic-gate 	if ((path != NULL) && (*path != '\0'))
673*0Sstevel@tonic-gate 		args.path = Strdup(path);
674*0Sstevel@tonic-gate 	args.flags = flags;
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate 	/* open connection */
677*0Sstevel@tonic-gate 	if ((hp = open_metamhd(hostname, ep)) == NULL)
678*0Sstevel@tonic-gate 		return (-1);
679*0Sstevel@tonic-gate 	clnt_control(hp->clientp, CLSET_TIMEOUT, (char *)&tk_own_timeout);
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 	/* get list */
682*0Sstevel@tonic-gate 	if (mhd_list_1(&args, &results, hp->clientp) != RPC_SUCCESS) {
683*0Sstevel@tonic-gate 		(void) mdrpcerror(ep, hp->clientp, hp->hostname,
684*0Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "metamhd list"));
685*0Sstevel@tonic-gate 		goto out;
686*0Sstevel@tonic-gate 	} else if (mhstealerror(mhep, ep) != 0) {
687*0Sstevel@tonic-gate 		goto out;
688*0Sstevel@tonic-gate 	}
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate 	/* sort list */
691*0Sstevel@tonic-gate 	sort_drives(&results.results);
692*0Sstevel@tonic-gate 
693*0Sstevel@tonic-gate 	/* steal list */
694*0Sstevel@tonic-gate 	*listp = results.results;
695*0Sstevel@tonic-gate 	results.results.mhd_drive_info_list_t_len = 0;
696*0Sstevel@tonic-gate 	results.results.mhd_drive_info_list_t_val = NULL;
697*0Sstevel@tonic-gate 	rval = listp->mhd_drive_info_list_t_len;	/* success */
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate 	/* cleanup, return success */
700*0Sstevel@tonic-gate out:
701*0Sstevel@tonic-gate 	xdr_free(xdr_mhd_list_args_t, (char *)&args);
702*0Sstevel@tonic-gate 	xdr_free(xdr_mhd_list_res_t, (char *)&results);
703*0Sstevel@tonic-gate 	if (hp != NULL)
704*0Sstevel@tonic-gate 		close_metamhd(hp);
705*0Sstevel@tonic-gate 	return (rval);
706*0Sstevel@tonic-gate }
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate static void
709*0Sstevel@tonic-gate load_paths_to_metamhd()
710*0Sstevel@tonic-gate {
711*0Sstevel@tonic-gate 	FILE			*cfp;		/* config file pointer */
712*0Sstevel@tonic-gate 	char			buf[BUFSIZ],
713*0Sstevel@tonic-gate 				*p,
714*0Sstevel@tonic-gate 				*x;
715*0Sstevel@tonic-gate 	mhd_drive_info_list_t	list;
716*0Sstevel@tonic-gate 	md_error_t		ep;
717*0Sstevel@tonic-gate 	mhd_did_flags_t		flags = MHD_DID_SERIAL;
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 	if ((cfp = fopen(METADEVPATH, "r")) != NULL) {
720*0Sstevel@tonic-gate 		/*
721*0Sstevel@tonic-gate 		 * Read each line from the file. Lines will be either
722*0Sstevel@tonic-gate 		 * comments or path names to pass to rpc.metamhd. If
723*0Sstevel@tonic-gate 		 * path names check to see if their a colon seperate
724*0Sstevel@tonic-gate 		 * list of names which must be processed one at a time.
725*0Sstevel@tonic-gate 		 */
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 		while (fgets(buf, BUFSIZ, cfp) != NULL) {
728*0Sstevel@tonic-gate 			if (buf[0] == '#') {
729*0Sstevel@tonic-gate 				/*
730*0Sstevel@tonic-gate 				 * Ignore comment lines
731*0Sstevel@tonic-gate 				 */
732*0Sstevel@tonic-gate 				continue;
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate 			} else if (strchr(buf, ':') != NULL) {
735*0Sstevel@tonic-gate 				p = buf;
736*0Sstevel@tonic-gate 				while ((x = strchr(p, ':')) != NULL) {
737*0Sstevel@tonic-gate 					*x = '\0';
738*0Sstevel@tonic-gate 					(void) memset(&ep, '\0', sizeof (ep));
739*0Sstevel@tonic-gate 					(void) meta_list_drives(NULL, p, 0,
740*0Sstevel@tonic-gate 					    &list, &ep);
741*0Sstevel@tonic-gate 					meta_free_drive_info_list(&list);
742*0Sstevel@tonic-gate 					p = x + 1;
743*0Sstevel@tonic-gate 				}
744*0Sstevel@tonic-gate 				/*
745*0Sstevel@tonic-gate 				 * We won't pick up the last path name
746*0Sstevel@tonic-gate 				 * because the line ends with a newline
747*0Sstevel@tonic-gate 				 * not a ':'. So p will still point to
748*0Sstevel@tonic-gate 				 * a valid path in this case. Copy the
749*0Sstevel@tonic-gate 				 * data that p points to to the beginning
750*0Sstevel@tonic-gate 				 * of the buf and let the default case
751*0Sstevel@tonic-gate 				 * handle this buffer.
752*0Sstevel@tonic-gate 				 * NOTE:
753*0Sstevel@tonic-gate 				 * If the file does end with a ":\n", p at
754*0Sstevel@tonic-gate 				 * will point to the newline. The default
755*0Sstevel@tonic-gate 				 * cause would then set the newline to a
756*0Sstevel@tonic-gate 				 * NULL which is okay because meta_list_drives
757*0Sstevel@tonic-gate 				 * interprets a null string as /dev/rdsk.
758*0Sstevel@tonic-gate 				 */
759*0Sstevel@tonic-gate 				(void) memcpy(buf, p, strlen(p));
760*0Sstevel@tonic-gate 			}
761*0Sstevel@tonic-gate 			/*
762*0Sstevel@tonic-gate 			 * Remove any newlines in the buffer.
763*0Sstevel@tonic-gate 			 */
764*0Sstevel@tonic-gate 			if ((p = strchr(buf, '\n')) != NULL)
765*0Sstevel@tonic-gate 				*p = '\0';
766*0Sstevel@tonic-gate 			(void) memset(&ep, '\0', sizeof (ep));
767*0Sstevel@tonic-gate 			(void) memset(&list, '\0', sizeof (list));
768*0Sstevel@tonic-gate 			(void) meta_list_drives(NULL, buf, flags, &list, &ep);
769*0Sstevel@tonic-gate 			meta_free_drive_info_list(&list);
770*0Sstevel@tonic-gate 		}
771*0Sstevel@tonic-gate 		(void) fclose(cfp);
772*0Sstevel@tonic-gate 	}
773*0Sstevel@tonic-gate }
774*0Sstevel@tonic-gate 
775*0Sstevel@tonic-gate /*
776*0Sstevel@tonic-gate  * build list of all drives in set
777*0Sstevel@tonic-gate  */
778*0Sstevel@tonic-gate /*ARGSUSED*/
779*0Sstevel@tonic-gate int
780*0Sstevel@tonic-gate meta_get_drive_names(
781*0Sstevel@tonic-gate 	mdsetname_t		*sp,
782*0Sstevel@tonic-gate 	mddrivenamelist_t	**dnlpp,
783*0Sstevel@tonic-gate 	int			options,
784*0Sstevel@tonic-gate 	md_error_t		*ep
785*0Sstevel@tonic-gate )
786*0Sstevel@tonic-gate {
787*0Sstevel@tonic-gate 	mhd_did_flags_t		flags = MHD_DID_SERIAL;
788*0Sstevel@tonic-gate 	mhd_drive_info_list_t	list;
789*0Sstevel@tonic-gate 	mhd_drive_info_t	*mp;
790*0Sstevel@tonic-gate 	uint_t			i;
791*0Sstevel@tonic-gate 	unsigned		cnt = 0;
792*0Sstevel@tonic-gate 	int			rval = -1;
793*0Sstevel@tonic-gate 	mddrivenamelist_t	**tailpp = dnlpp;
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate 	/* must have a set */
796*0Sstevel@tonic-gate 	assert(sp != NULL);
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	load_paths_to_metamhd();
799*0Sstevel@tonic-gate 	(void) memset(&list, 0, sizeof (list));
800*0Sstevel@tonic-gate 	if ((meta_list_drives(NULL, NULL, flags, &list, ep)) < 0)
801*0Sstevel@tonic-gate 		return (-1);
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 	/* find drives in set */
804*0Sstevel@tonic-gate 	for (i = 0; (i < list.mhd_drive_info_list_t_len); ++i) {
805*0Sstevel@tonic-gate 		mddrivename_t		*dnp;
806*0Sstevel@tonic-gate 		mdname_t		*np;
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 		mp = &list.mhd_drive_info_list_t_val[i];
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate 		if (mp->dif_id.did_flags & MHD_DID_DUPLICATE)
811*0Sstevel@tonic-gate 			continue;
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 		/* quietly skip drives which don't conform */
814*0Sstevel@tonic-gate 		if ((dnp = metadrivename(&sp, mp->dif_name, ep)) == NULL) {
815*0Sstevel@tonic-gate 			mdclrerror(ep);
816*0Sstevel@tonic-gate 			continue;
817*0Sstevel@tonic-gate 		}
818*0Sstevel@tonic-gate 
819*0Sstevel@tonic-gate 		/* check in set */
820*0Sstevel@tonic-gate 		if ((np = metaslicename(dnp, MD_SLICE0, ep)) == NULL)
821*0Sstevel@tonic-gate 			goto out;
822*0Sstevel@tonic-gate 		if (meta_check_inset(sp, np, ep) != 0) {
823*0Sstevel@tonic-gate 			mdclrerror(ep);
824*0Sstevel@tonic-gate 			continue;
825*0Sstevel@tonic-gate 		}
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 		/*
828*0Sstevel@tonic-gate 		 * Add the drivename struct to the end of the
829*0Sstevel@tonic-gate 		 * drivenamelist but keep a pointer to the last
830*0Sstevel@tonic-gate 		 * element so that we don't incur the overhead
831*0Sstevel@tonic-gate 		 * of traversing the list each time
832*0Sstevel@tonic-gate 		 */
833*0Sstevel@tonic-gate 		tailpp = meta_drivenamelist_append_wrapper(tailpp, dnp);
834*0Sstevel@tonic-gate 		++cnt;
835*0Sstevel@tonic-gate 	}
836*0Sstevel@tonic-gate 	rval = cnt;
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate 	/* cleanup, return error */
839*0Sstevel@tonic-gate out:
840*0Sstevel@tonic-gate 	meta_free_drive_info_list(&list);
841*0Sstevel@tonic-gate 	return (rval);
842*0Sstevel@tonic-gate }
843