xref: /onnv-gate/usr/src/cmd/lvm/rpc.metamhd/mhd_set.c (revision 0:68f95e015346)
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 2003 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 "mhd_local.h"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate /*
32*0Sstevel@tonic-gate  * manipulate set list
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate /*
36*0Sstevel@tonic-gate  * global set list
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate static	mutex_t		mhd_set_mx = DEFAULTMUTEX;
39*0Sstevel@tonic-gate static	uint_t		mhd_nset = 0;
40*0Sstevel@tonic-gate static	mhd_drive_set_t	**mhd_sets = NULL;
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * add drive to set
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate void
mhd_add_drive_to_set(mhd_drive_set_t * sp,mhd_drive_t * dp)46*0Sstevel@tonic-gate mhd_add_drive_to_set(
47*0Sstevel@tonic-gate 	mhd_drive_set_t		*sp,
48*0Sstevel@tonic-gate 	mhd_drive_t		*dp
49*0Sstevel@tonic-gate )
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	mhd_drive_list_t	*dlp = &sp->sr_drives;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	/* check locks */
54*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&mhd_set_mx));
55*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&sp->sr_mx));
56*0Sstevel@tonic-gate 	assert(DRIVE_IS_IDLE(dp));
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	/* add to set */
59*0Sstevel@tonic-gate 	mhd_add_drive(dlp, dp);
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	/* adjust backlink */
62*0Sstevel@tonic-gate 	dp->dr_sp = sp;
63*0Sstevel@tonic-gate }
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate  * delete drive from set
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate void
mhd_del_drive_from_set(mhd_drive_t * dp)69*0Sstevel@tonic-gate mhd_del_drive_from_set(
70*0Sstevel@tonic-gate 	mhd_drive_t		*dp
71*0Sstevel@tonic-gate )
72*0Sstevel@tonic-gate {
73*0Sstevel@tonic-gate 	mhd_drive_set_t		*sp = dp->dr_sp;
74*0Sstevel@tonic-gate 	mhd_drive_list_t	*dlp = &sp->sr_drives;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	/* check locks */
77*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&mhd_set_mx));
78*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&sp->sr_mx));
79*0Sstevel@tonic-gate 	assert(DRIVE_IS_IDLE(dp));
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	/* delete from set */
82*0Sstevel@tonic-gate 	mhd_del_drive(dlp, dp);
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	/* adjust backlink */
85*0Sstevel@tonic-gate 	dp->dr_sp = NULL;
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate /*
89*0Sstevel@tonic-gate  * find set in list
90*0Sstevel@tonic-gate  */
91*0Sstevel@tonic-gate static mhd_drive_set_t *
mhd_find_set(char * setname)92*0Sstevel@tonic-gate mhd_find_set(
93*0Sstevel@tonic-gate 	char	*setname
94*0Sstevel@tonic-gate )
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	uint_t	i;
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	/* check lock */
99*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&mhd_set_mx));
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	/* look for set */
102*0Sstevel@tonic-gate 	for (i = 0; (i < mhd_nset); ++i) {
103*0Sstevel@tonic-gate 		mhd_drive_set_t	*sp = mhd_sets[i];
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 		if (strcmp(setname, sp->sr_name) == 0)
106*0Sstevel@tonic-gate 			return (sp);
107*0Sstevel@tonic-gate 	}
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	/* not found */
110*0Sstevel@tonic-gate 	return (NULL);
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate /*
114*0Sstevel@tonic-gate  * wait for operation to complete
115*0Sstevel@tonic-gate  */
116*0Sstevel@tonic-gate static void
mhd_wait_set(mhd_drive_set_t * sp,mhd_drive_list_t * dlp,mhd_state_t state)117*0Sstevel@tonic-gate mhd_wait_set(
118*0Sstevel@tonic-gate 	mhd_drive_set_t		*sp,
119*0Sstevel@tonic-gate 	mhd_drive_list_t	*dlp,
120*0Sstevel@tonic-gate 	mhd_state_t		state
121*0Sstevel@tonic-gate )
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate 	/* check lock */
124*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&mhd_set_mx));
125*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&sp->sr_mx));
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	/* wait for complete */
128*0Sstevel@tonic-gate 	for (;;) {
129*0Sstevel@tonic-gate 		uint_t	cnt = 0;
130*0Sstevel@tonic-gate 		uint_t	i;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 		/* kick threads */
133*0Sstevel@tonic-gate 		for (i = 0; (i < dlp->dl_ndrive); ++i) {
134*0Sstevel@tonic-gate 			mhd_drive_t	*dp = dlp->dl_drives[i];
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 			/* IDLE or ERRORED */
137*0Sstevel@tonic-gate 			if (state == DRIVE_IDLE) {
138*0Sstevel@tonic-gate 				if (DRIVE_IS_IDLE(dp))
139*0Sstevel@tonic-gate 					continue;
140*0Sstevel@tonic-gate 			}
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 			/* operation complete */
143*0Sstevel@tonic-gate 			else {
144*0Sstevel@tonic-gate 				if (! (dp->dr_state & state))
145*0Sstevel@tonic-gate 					continue;
146*0Sstevel@tonic-gate 			}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 			/* kick thread */
149*0Sstevel@tonic-gate 			mhd_cv_broadcast(&dp->dr_cv);
150*0Sstevel@tonic-gate 			++cnt;
151*0Sstevel@tonic-gate 		}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 		/* if complete, quit */
154*0Sstevel@tonic-gate 		if (cnt == 0)
155*0Sstevel@tonic-gate 			break;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 		/* wait for something to happen */
158*0Sstevel@tonic-gate 		(void) mhd_cv_wait(&sp->sr_cv, &sp->sr_mx);
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate /*
163*0Sstevel@tonic-gate  * idle set
164*0Sstevel@tonic-gate  */
165*0Sstevel@tonic-gate static int
mhd_idle_set(mhd_drive_set_t * sp,mhd_drive_list_t * dlp,mhd_error_t * mhep)166*0Sstevel@tonic-gate mhd_idle_set(
167*0Sstevel@tonic-gate 	mhd_drive_set_t		*sp,
168*0Sstevel@tonic-gate 	mhd_drive_list_t	*dlp,
169*0Sstevel@tonic-gate 	mhd_error_t		*mhep
170*0Sstevel@tonic-gate )
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate 	uint_t			i;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	/* check lock */
175*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&mhd_set_mx));
176*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&sp->sr_mx));
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	/* disarm any failfast */
179*0Sstevel@tonic-gate 	if (dlp->dl_ndrive >= sp->sr_drives.dl_ndrive) {
180*0Sstevel@tonic-gate 		if (mhd_ff_disarm(sp, mhep) != 0)
181*0Sstevel@tonic-gate 			return (-1);
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	/* set IDLING */
185*0Sstevel@tonic-gate 	for (i = 0; (i < dlp->dl_ndrive); ++i) {
186*0Sstevel@tonic-gate 		mhd_drive_t	*dp = dlp->dl_drives[i];
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 		if (! DRIVE_IS_IDLE(dp)) {
189*0Sstevel@tonic-gate 			if (mhd_state(dp, DRIVE_IDLING, mhep) != 0)
190*0Sstevel@tonic-gate 				return (-1);
191*0Sstevel@tonic-gate 		}
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	/* wait for IDLE */
195*0Sstevel@tonic-gate 	mhd_wait_set(sp, dlp, DRIVE_IDLE);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	/* return success */
198*0Sstevel@tonic-gate 	return (0);
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate /*
202*0Sstevel@tonic-gate  * create or update new set
203*0Sstevel@tonic-gate  */
204*0Sstevel@tonic-gate mhd_drive_set_t *
mhd_create_set(mhd_set_t * mhsp,mhd_opts_t options,mhd_drive_list_t * dlp,mhd_error_t * mhep)205*0Sstevel@tonic-gate mhd_create_set(
206*0Sstevel@tonic-gate 	mhd_set_t		*mhsp,
207*0Sstevel@tonic-gate 	mhd_opts_t		options,
208*0Sstevel@tonic-gate 	mhd_drive_list_t	*dlp,
209*0Sstevel@tonic-gate 	mhd_error_t		*mhep
210*0Sstevel@tonic-gate )
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate 	char			*setname;
213*0Sstevel@tonic-gate 	mhd_drive_set_t		*sp;
214*0Sstevel@tonic-gate 	mhd_drive_list_t	*sp_dlp;
215*0Sstevel@tonic-gate 	mhd_drive_set_t		*null_sp;
216*0Sstevel@tonic-gate 	uint_t			i;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	/* check locks */
219*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&mhd_set_mx));
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	/* get setname */
222*0Sstevel@tonic-gate 	if (mhsp == NULL)
223*0Sstevel@tonic-gate 		setname = "";
224*0Sstevel@tonic-gate 	else
225*0Sstevel@tonic-gate 		setname = mhsp->setname;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	/* find or create set */
228*0Sstevel@tonic-gate 	if ((sp = mhd_find_set(setname)) == NULL) {
229*0Sstevel@tonic-gate 		/* allocate and initialize set */
230*0Sstevel@tonic-gate 		sp = Zalloc(sizeof (*sp));
231*0Sstevel@tonic-gate 		sp->sr_name = Strdup(setname);
232*0Sstevel@tonic-gate 		mhd_mx_init(&sp->sr_mx);
233*0Sstevel@tonic-gate 		mhd_cv_init(&sp->sr_cv);
234*0Sstevel@tonic-gate 		sp->sr_ff = -1;
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 		/* append to set list */
237*0Sstevel@tonic-gate 		++mhd_nset;
238*0Sstevel@tonic-gate 		mhd_sets = Realloc(mhd_sets, (mhd_nset * sizeof (*mhd_sets)));
239*0Sstevel@tonic-gate 		mhd_sets[mhd_nset - 1] = sp;
240*0Sstevel@tonic-gate 	}
241*0Sstevel@tonic-gate 	sp_dlp = &sp->sr_drives;
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	/* if just grabbing null set, return */
244*0Sstevel@tonic-gate 	if (mhsp == NULL)
245*0Sstevel@tonic-gate 		return (sp);
246*0Sstevel@tonic-gate 	assert(strcmp(setname, "") != 0);
247*0Sstevel@tonic-gate 	assert(mhep != NULL);
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	/* get null set */
250*0Sstevel@tonic-gate 	null_sp = mhd_create_set(NULL, 0, NULL, NULL);
251*0Sstevel@tonic-gate 	assert(null_sp != NULL);
252*0Sstevel@tonic-gate 	assert(sp != null_sp);
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	/* grab set lock */
255*0Sstevel@tonic-gate 	mhd_mx_lock(&sp->sr_mx);
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	/* save options */
258*0Sstevel@tonic-gate 	if (options & MHD_SERIAL)
259*0Sstevel@tonic-gate 		sp->sr_options |= MHD_SERIAL;
260*0Sstevel@tonic-gate 	else
261*0Sstevel@tonic-gate 		sp->sr_options &= ~MHD_SERIAL;
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	/* move drives no longer in set to null set */
264*0Sstevel@tonic-gate 	if (! (options & MHD_PARTIAL_SET)) {
265*0Sstevel@tonic-gate 		for (i = 0; (i < sp_dlp->dl_ndrive); /* void */) {
266*0Sstevel@tonic-gate 			mhd_drive_t	*dp = sp_dlp->dl_drives[i];
267*0Sstevel@tonic-gate 			uint_t		j;
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 			/* check still there */
270*0Sstevel@tonic-gate 			for (j = 0; (j < mhsp->drives.drives_len); ++j) {
271*0Sstevel@tonic-gate 				mhd_drivename_t	mhdp;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 				mhdp = mhsp->drives.drives_val[j];
274*0Sstevel@tonic-gate 				if (strcmp(dp->dr_rname, mhdp) == 0)
275*0Sstevel@tonic-gate 					break;
276*0Sstevel@tonic-gate 			}
277*0Sstevel@tonic-gate 			if (j < mhsp->drives.drives_len) {
278*0Sstevel@tonic-gate 				++i;
279*0Sstevel@tonic-gate 				continue;
280*0Sstevel@tonic-gate 			}
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 			/* idle the drive */
283*0Sstevel@tonic-gate 			if (mhd_idle(dp, mhep) != 0)
284*0Sstevel@tonic-gate 				mhd_clrerror(mhep);
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 			/* move to null set */
287*0Sstevel@tonic-gate 			mhd_del_drive_from_set(dp);
288*0Sstevel@tonic-gate 			mhd_mx_unlock(&sp->sr_mx);
289*0Sstevel@tonic-gate 			mhd_mx_lock(&null_sp->sr_mx);
290*0Sstevel@tonic-gate 			mhd_add_drive_to_set(null_sp, dp);
291*0Sstevel@tonic-gate 			mhd_mx_unlock(&null_sp->sr_mx);
292*0Sstevel@tonic-gate 			mhd_mx_lock(&sp->sr_mx);
293*0Sstevel@tonic-gate 		}
294*0Sstevel@tonic-gate 	}
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	/* add new drives to lists */
297*0Sstevel@tonic-gate 	for (i = 0; (i < mhsp->drives.drives_len); ++i) {
298*0Sstevel@tonic-gate 		mhd_drivename_t	mhdp = mhsp->drives.drives_val[i];
299*0Sstevel@tonic-gate 		uint_t		j;
300*0Sstevel@tonic-gate 		mhd_drive_t	*dp;
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 		/* check already there */
303*0Sstevel@tonic-gate 		for (j = 0; (j < dlp->dl_ndrive); ++j) {
304*0Sstevel@tonic-gate 			dp = dlp->dl_drives[j];
305*0Sstevel@tonic-gate 			if (strcmp(mhdp, dp->dr_rname) == 0)
306*0Sstevel@tonic-gate 				break;
307*0Sstevel@tonic-gate 		}
308*0Sstevel@tonic-gate 		if (j < dlp->dl_ndrive) {
309*0Sstevel@tonic-gate 			mhd_add_drive(dlp, dp);
310*0Sstevel@tonic-gate 			continue;
311*0Sstevel@tonic-gate 		}
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 		/* add drive to set */
314*0Sstevel@tonic-gate 		if ((dp = mhd_create_drive(sp, mhdp, NULL, mhep)) == NULL) {
315*0Sstevel@tonic-gate 			mhde_perror(mhep, "mhd_create_drive: %s", mhdp);
316*0Sstevel@tonic-gate 			continue;
317*0Sstevel@tonic-gate 		}
318*0Sstevel@tonic-gate 		mhd_add_drive(dlp, dp);
319*0Sstevel@tonic-gate 	}
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 	/* debug */
322*0Sstevel@tonic-gate #ifdef	MHD_DEBUG
323*0Sstevel@tonic-gate 	if (mhd_debug > 0) {
324*0Sstevel@tonic-gate 		for (i = 0; (i < mhd_nset); ++i) {
325*0Sstevel@tonic-gate 			mhd_drive_set_t		*sp = mhd_sets[i];
326*0Sstevel@tonic-gate 			mhd_drive_list_t	*dlp = &sp->sr_drives;
327*0Sstevel@tonic-gate 			char			buf[10240];
328*0Sstevel@tonic-gate 			uint_t			j;
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "set '%s':",
331*0Sstevel@tonic-gate 			    sp->sr_name);
332*0Sstevel@tonic-gate 			for (j = 0; (j < dlp->dl_ndrive); ++j) {
333*0Sstevel@tonic-gate 				mhd_drive_t	*dp = dlp->dl_drives[j];
334*0Sstevel@tonic-gate 				char		*p;
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 				if ((p = strrchr(dp->dr_rname, '/')) != NULL)
337*0Sstevel@tonic-gate 					++p;
338*0Sstevel@tonic-gate 				else
339*0Sstevel@tonic-gate 					p = dp->dr_rname;
340*0Sstevel@tonic-gate 				(void) strncat(buf, " ", sizeof (buf));
341*0Sstevel@tonic-gate 				(void) strncat(buf, p, sizeof (buf));
342*0Sstevel@tonic-gate 			}
343*0Sstevel@tonic-gate 			buf[sizeof (buf) - 1] = '\0';
344*0Sstevel@tonic-gate 			mhd_eprintf("%s\n", buf);
345*0Sstevel@tonic-gate 		}
346*0Sstevel@tonic-gate 	}
347*0Sstevel@tonic-gate #endif	/* MHD_DEBUG */
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	/* unlock, return set */
350*0Sstevel@tonic-gate 	mhd_mx_unlock(&sp->sr_mx);
351*0Sstevel@tonic-gate 	return (sp);
352*0Sstevel@tonic-gate }
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate /*
355*0Sstevel@tonic-gate  * find drive
356*0Sstevel@tonic-gate  */
357*0Sstevel@tonic-gate mhd_drive_t *
mhd_find_drive(char * rname)358*0Sstevel@tonic-gate mhd_find_drive(
359*0Sstevel@tonic-gate 	char		*rname
360*0Sstevel@tonic-gate )
361*0Sstevel@tonic-gate {
362*0Sstevel@tonic-gate 	uint_t		i;
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	/* check locks */
365*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&mhd_set_mx));
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	/* for each set */
368*0Sstevel@tonic-gate 	for (i = 0; (i < mhd_nset); ++i) {
369*0Sstevel@tonic-gate 		mhd_drive_set_t		*sp = mhd_sets[i];
370*0Sstevel@tonic-gate 		mhd_drive_list_t	*dlp = &sp->sr_drives;
371*0Sstevel@tonic-gate 		uint_t			j;
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 		/* for each drive */
374*0Sstevel@tonic-gate 		for (j = 0; (j < dlp->dl_ndrive); ++j) {
375*0Sstevel@tonic-gate 			mhd_drive_t	*dp = dlp->dl_drives[j];
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 			if (strcmp(rname, dp->dr_rname) == 0)
378*0Sstevel@tonic-gate 				return (dp);
379*0Sstevel@tonic-gate 		}
380*0Sstevel@tonic-gate 	}
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	/* not found */
383*0Sstevel@tonic-gate 	return (NULL);
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate /*
387*0Sstevel@tonic-gate  * list all the drives
388*0Sstevel@tonic-gate  */
389*0Sstevel@tonic-gate int
mhd_list_drives(char * path,mhd_did_flags_t flags,mhd_list_res_t * resultsp,mhd_error_t * mhep)390*0Sstevel@tonic-gate mhd_list_drives(
391*0Sstevel@tonic-gate 	char		*path,
392*0Sstevel@tonic-gate 	mhd_did_flags_t	flags,
393*0Sstevel@tonic-gate 	mhd_list_res_t	*resultsp,
394*0Sstevel@tonic-gate 	mhd_error_t	*mhep
395*0Sstevel@tonic-gate )
396*0Sstevel@tonic-gate {
397*0Sstevel@tonic-gate 	mhd_state_t	state;
398*0Sstevel@tonic-gate 	uint_t		ndrive, i, j, c;
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	/* grab lock */
401*0Sstevel@tonic-gate 	mhd_mx_lock(&mhd_set_mx);
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	/* add path to list */
404*0Sstevel@tonic-gate 	if (mhd_create_drives(path, mhep) != 0) {
405*0Sstevel@tonic-gate 		mhd_mx_unlock(&mhd_set_mx);
406*0Sstevel@tonic-gate 		return (-1);
407*0Sstevel@tonic-gate 	}
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	/* get what we want */
410*0Sstevel@tonic-gate 	state = 0;
411*0Sstevel@tonic-gate 	if (flags & MHD_DID_SERIAL)
412*0Sstevel@tonic-gate 		state |= DRIVE_SERIALING;
413*0Sstevel@tonic-gate 	if (flags & MHD_DID_TIME)
414*0Sstevel@tonic-gate 		state |= DRIVE_VTOCING;
415*0Sstevel@tonic-gate 	if (flags & MHD_DID_CINFO)
416*0Sstevel@tonic-gate 		state |= DRIVE_CINFOING;
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 	/* ident and count drives */
419*0Sstevel@tonic-gate 	for (ndrive = 0, i = 0; (i < mhd_nset); ++i) {
420*0Sstevel@tonic-gate 		mhd_drive_set_t		*sp = mhd_sets[i];
421*0Sstevel@tonic-gate 		mhd_drive_list_t	*dlp = &sp->sr_drives;
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 		/* count drives */
424*0Sstevel@tonic-gate 		ndrive += dlp->dl_ndrive;
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 		/* ident drives */
427*0Sstevel@tonic-gate 		if (state != 0) {
428*0Sstevel@tonic-gate 			mhd_mx_lock(&sp->sr_mx);
429*0Sstevel@tonic-gate 			for (j = 0; (j < dlp->dl_ndrive); ++j) {
430*0Sstevel@tonic-gate 				mhd_drive_t	*dp = dlp->dl_drives[j];
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 				if (mhd_state_set(dp, state, mhep) != 0) {
433*0Sstevel@tonic-gate 					mhd_mx_unlock(&sp->sr_mx);
434*0Sstevel@tonic-gate 					mhd_mx_unlock(&mhd_set_mx);
435*0Sstevel@tonic-gate 					return (-1);
436*0Sstevel@tonic-gate 				}
437*0Sstevel@tonic-gate 			}
438*0Sstevel@tonic-gate 			mhd_wait_set(sp, dlp, state);
439*0Sstevel@tonic-gate 			mhd_mx_unlock(&sp->sr_mx);
440*0Sstevel@tonic-gate 		}
441*0Sstevel@tonic-gate 	}
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 	/* build list */
444*0Sstevel@tonic-gate 	assert(resultsp->results.mhd_drive_info_list_t_len == 0);
445*0Sstevel@tonic-gate 	assert(resultsp->results.mhd_drive_info_list_t_val == NULL);
446*0Sstevel@tonic-gate 	resultsp->results.mhd_drive_info_list_t_len = ndrive;
447*0Sstevel@tonic-gate 	resultsp->results.mhd_drive_info_list_t_val = Zalloc(
448*0Sstevel@tonic-gate 	    ndrive * sizeof (*resultsp->results.mhd_drive_info_list_t_val));
449*0Sstevel@tonic-gate 	for (c = 0, i = 0; (i < mhd_nset); ++i) {
450*0Sstevel@tonic-gate 		mhd_drive_set_t		*sp = mhd_sets[i];
451*0Sstevel@tonic-gate 		mhd_drive_list_t	*dlp = &sp->sr_drives;
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 		mhd_mx_lock(&sp->sr_mx);
454*0Sstevel@tonic-gate 		for (j = 0; (j < dlp->dl_ndrive); ++j) {
455*0Sstevel@tonic-gate 			mhd_drive_t	*dp = dlp->dl_drives[j];
456*0Sstevel@tonic-gate 			mhd_drive_info_t *ip =
457*0Sstevel@tonic-gate 			    &resultsp->results.mhd_drive_info_list_t_val[c++];
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 			ip->dif_name = Strdup(dp->dr_rname);
460*0Sstevel@tonic-gate 			ip->dif_id = dp->dr_drive_id;
461*0Sstevel@tonic-gate 		}
462*0Sstevel@tonic-gate 		mhd_mx_unlock(&sp->sr_mx);
463*0Sstevel@tonic-gate 	}
464*0Sstevel@tonic-gate 	assert(c == ndrive);
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 	/* unlock, return count */
467*0Sstevel@tonic-gate 	mhd_mx_unlock(&mhd_set_mx);
468*0Sstevel@tonic-gate 	return (ndrive);
469*0Sstevel@tonic-gate }
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate /*
472*0Sstevel@tonic-gate  * release drives
473*0Sstevel@tonic-gate  */
474*0Sstevel@tonic-gate static int
mhd_release_set(mhd_drive_set_t * sp,mhd_drive_list_t * dlp,mhd_error_t * mhep)475*0Sstevel@tonic-gate mhd_release_set(
476*0Sstevel@tonic-gate 	mhd_drive_set_t		*sp,
477*0Sstevel@tonic-gate 	mhd_drive_list_t	*dlp,
478*0Sstevel@tonic-gate 	mhd_error_t		*mhep
479*0Sstevel@tonic-gate )
480*0Sstevel@tonic-gate {
481*0Sstevel@tonic-gate 	uint_t			i;
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	/* check locks */
484*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&mhd_set_mx));
485*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&sp->sr_mx));
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 	/* idle set */
488*0Sstevel@tonic-gate 	if (mhd_idle_set(sp, dlp, mhep) != 0)
489*0Sstevel@tonic-gate 		return (-1);
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	/* release drives */
492*0Sstevel@tonic-gate 	for (i = 0; (i < dlp->dl_ndrive); i++) {
493*0Sstevel@tonic-gate 		mhd_drive_t	*dp = dlp->dl_drives[i];
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 		if (mhd_state(dp, DRIVE_RELEASING, mhep) != 0)
496*0Sstevel@tonic-gate 			return (-1);
497*0Sstevel@tonic-gate 	}
498*0Sstevel@tonic-gate 	mhd_wait_set(sp, dlp, DRIVE_IDLE);
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 	/* return success */
501*0Sstevel@tonic-gate 	return (0);
502*0Sstevel@tonic-gate }
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate /*
505*0Sstevel@tonic-gate  * release drives in set
506*0Sstevel@tonic-gate  */
507*0Sstevel@tonic-gate int
mhd_release_drives(mhd_set_t * mhsp,mhd_opts_t options,mhd_error_t * mhep)508*0Sstevel@tonic-gate mhd_release_drives(
509*0Sstevel@tonic-gate 	mhd_set_t		*mhsp,
510*0Sstevel@tonic-gate 	mhd_opts_t		options,
511*0Sstevel@tonic-gate 	mhd_error_t		*mhep
512*0Sstevel@tonic-gate )
513*0Sstevel@tonic-gate {
514*0Sstevel@tonic-gate 	mhd_drive_list_t	dl = mhd_null_list;
515*0Sstevel@tonic-gate 	mhd_drive_set_t		*sp;
516*0Sstevel@tonic-gate 	int			rval;
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate 	/* grab global lock */
519*0Sstevel@tonic-gate 	mhd_mx_lock(&mhd_set_mx);
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 	/* create or update set */
522*0Sstevel@tonic-gate 	if ((sp = mhd_create_set(mhsp, options, &dl, mhep)) == NULL) {
523*0Sstevel@tonic-gate 		mhd_mx_unlock(&mhd_set_mx);
524*0Sstevel@tonic-gate 		mhd_free_list(&dl);
525*0Sstevel@tonic-gate 		return (-1);
526*0Sstevel@tonic-gate 	}
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 	/* lock set */
529*0Sstevel@tonic-gate 	mhd_mx_lock(&sp->sr_mx);
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	/* release drives */
532*0Sstevel@tonic-gate 	rval = mhd_release_set(sp, &dl, mhep);
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 	/* unlock, return success */
535*0Sstevel@tonic-gate out:
536*0Sstevel@tonic-gate 	mhd_mx_unlock(&sp->sr_mx);
537*0Sstevel@tonic-gate 	mhd_mx_unlock(&mhd_set_mx);
538*0Sstevel@tonic-gate 	mhd_free_list(&dl);
539*0Sstevel@tonic-gate 	return (rval);
540*0Sstevel@tonic-gate }
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate /*
543*0Sstevel@tonic-gate  * reserve drives
544*0Sstevel@tonic-gate  */
545*0Sstevel@tonic-gate static int
mhd_reserve_set(mhd_drive_set_t * sp,mhd_drive_list_t * dlp,mhd_error_t * mhep)546*0Sstevel@tonic-gate mhd_reserve_set(
547*0Sstevel@tonic-gate 	mhd_drive_set_t		*sp,
548*0Sstevel@tonic-gate 	mhd_drive_list_t	*dlp,
549*0Sstevel@tonic-gate 	mhd_error_t		*mhep
550*0Sstevel@tonic-gate )
551*0Sstevel@tonic-gate {
552*0Sstevel@tonic-gate 	mhd_msec_t		ff = sp->sr_timeouts.mh_ff;
553*0Sstevel@tonic-gate 	uint_t			retry, i, ok;
554*0Sstevel@tonic-gate 	int			rval = 0;
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	/* check locks */
557*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&mhd_set_mx));
558*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&sp->sr_mx));
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate 	/* idle set, idle everyone if cancelling failfast */
561*0Sstevel@tonic-gate 	if (ff == 0) {
562*0Sstevel@tonic-gate 		if (mhd_idle_set(sp, &sp->sr_drives, mhep) != 0)
563*0Sstevel@tonic-gate 			return (-1);
564*0Sstevel@tonic-gate 	} else {
565*0Sstevel@tonic-gate 		if (mhd_idle_set(sp, dlp, mhep) != 0)
566*0Sstevel@tonic-gate 			return (-1);
567*0Sstevel@tonic-gate 	}
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 	/*
570*0Sstevel@tonic-gate 	 * Try to take ownership of the drives twice. This helps
571*0Sstevel@tonic-gate 	 * to avoid the situation where the other machine retakes
572*0Sstevel@tonic-gate 	 * ownership of a majority drives back, but then kills itself
573*0Sstevel@tonic-gate 	 * leaving no owners.
574*0Sstevel@tonic-gate 	 */
575*0Sstevel@tonic-gate 	for (retry = 0; (retry < 2); ++retry) {
576*0Sstevel@tonic-gate 		for (i = 0; (i < dlp->dl_ndrive); i++) {
577*0Sstevel@tonic-gate 			mhd_drive_t	*dp = dlp->dl_drives[i];
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate 			if ((retry == 0) ||
580*0Sstevel@tonic-gate 			    ((dp->dr_state == DRIVE_ERRORED) &&
581*0Sstevel@tonic-gate 			    (dp->dr_errnum == EACCES))) {
582*0Sstevel@tonic-gate 				if (mhd_state(dp, DRIVE_RESERVING, mhep) != 0)
583*0Sstevel@tonic-gate 					return (-1);
584*0Sstevel@tonic-gate 			}
585*0Sstevel@tonic-gate 		}
586*0Sstevel@tonic-gate 		mhd_wait_set(sp, dlp, DRIVE_IDLE);
587*0Sstevel@tonic-gate 	}
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate 	/*
590*0Sstevel@tonic-gate 	 * Did the take ownership succeed on a majority of the drives?
591*0Sstevel@tonic-gate 	 */
592*0Sstevel@tonic-gate 	ok = 0;
593*0Sstevel@tonic-gate 	for (i = 0; (i < dlp->dl_ndrive); ++i) {
594*0Sstevel@tonic-gate 		mhd_drive_t	*dp = dlp->dl_drives[i];
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 		if (dp->dr_state == DRIVE_IDLE)
597*0Sstevel@tonic-gate 			++ok;
598*0Sstevel@tonic-gate 	}
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate 	/*
601*0Sstevel@tonic-gate 	 * Let the replica majority be the deciding factor, if able to get
602*0Sstevel@tonic-gate 	 * at least a single drive reserved.
603*0Sstevel@tonic-gate 	 */
604*0Sstevel@tonic-gate 	if (ok == 0) {
605*0Sstevel@tonic-gate 		rval = mhd_error(mhep, MHD_E_MAJORITY, sp->sr_name);
606*0Sstevel@tonic-gate 		goto out;
607*0Sstevel@tonic-gate 	}
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 	/*
610*0Sstevel@tonic-gate 	 * Enable the failfast probes if we haven't given up yet.
611*0Sstevel@tonic-gate 	 */
612*0Sstevel@tonic-gate 	switch (sp->sr_ff_mode) {
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 	/* do nothing */
615*0Sstevel@tonic-gate 	default:
616*0Sstevel@tonic-gate 		assert(0);
617*0Sstevel@tonic-gate 		/* FALLTHROUGH */
618*0Sstevel@tonic-gate 	case MHD_FF_NONE:
619*0Sstevel@tonic-gate 		goto out;
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	/* old style per drive failfast */
622*0Sstevel@tonic-gate 	case MHD_FF_DRIVER:
623*0Sstevel@tonic-gate 		for (i = 0; (i < dlp->dl_ndrive); i++) {
624*0Sstevel@tonic-gate 			mhd_drive_t	*dp = dlp->dl_drives[i];
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate 			if (dp->dr_state != DRIVE_ERRORED) {
627*0Sstevel@tonic-gate 				if (mhd_state(dp, DRIVE_FAILFASTING,
628*0Sstevel@tonic-gate 				    mhep) != 0) {
629*0Sstevel@tonic-gate 					rval = -1;
630*0Sstevel@tonic-gate 					goto out;
631*0Sstevel@tonic-gate 				}
632*0Sstevel@tonic-gate 			}
633*0Sstevel@tonic-gate 		}
634*0Sstevel@tonic-gate 		mhd_wait_set(sp, dlp, DRIVE_IDLE);
635*0Sstevel@tonic-gate 		break;
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 	/* failfast probe threads */
638*0Sstevel@tonic-gate 	case MHD_FF_DEBUG:
639*0Sstevel@tonic-gate 	case MHD_FF_HALT:
640*0Sstevel@tonic-gate 	case MHD_FF_PANIC:
641*0Sstevel@tonic-gate 		if (ff != 0) {
642*0Sstevel@tonic-gate 			if (mhd_ff_open(sp, mhep) != 0) {
643*0Sstevel@tonic-gate 				rval = -1;
644*0Sstevel@tonic-gate 				goto out;
645*0Sstevel@tonic-gate 			}
646*0Sstevel@tonic-gate 			for (i = 0; (i < dlp->dl_ndrive); i++) {
647*0Sstevel@tonic-gate 				mhd_drive_t	*dp = dlp->dl_drives[i];
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 				if (mhd_state_set(dp, DRIVE_PROBING,
650*0Sstevel@tonic-gate 				    mhep) != 0) {
651*0Sstevel@tonic-gate 					rval = -1;
652*0Sstevel@tonic-gate 					goto out;
653*0Sstevel@tonic-gate 				}
654*0Sstevel@tonic-gate 				dp->dr_time = mhd_time();
655*0Sstevel@tonic-gate 			}
656*0Sstevel@tonic-gate 			(void) mhd_ff_rearm(sp, mhep);
657*0Sstevel@tonic-gate 		}
658*0Sstevel@tonic-gate 		break;
659*0Sstevel@tonic-gate 	}
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate 	/* cleanup, return success */
662*0Sstevel@tonic-gate out:
663*0Sstevel@tonic-gate 	if (rval != 0) {
664*0Sstevel@tonic-gate 		mhd_error_t	status = mhd_null_error;
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 		(void) mhd_release_set(sp, dlp, &status);
667*0Sstevel@tonic-gate 		mhd_clrerror(&status);
668*0Sstevel@tonic-gate 	}
669*0Sstevel@tonic-gate 	return (rval);
670*0Sstevel@tonic-gate }
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate /*
673*0Sstevel@tonic-gate  * reserve drives in set
674*0Sstevel@tonic-gate  */
675*0Sstevel@tonic-gate int
mhd_reserve_drives(mhd_set_t * mhsp,mhd_mhiargs_t * timeoutp,mhd_ff_mode_t ff_mode,mhd_opts_t options,mhd_error_t * mhep)676*0Sstevel@tonic-gate mhd_reserve_drives(
677*0Sstevel@tonic-gate 	mhd_set_t		*mhsp,
678*0Sstevel@tonic-gate 	mhd_mhiargs_t		*timeoutp,
679*0Sstevel@tonic-gate 	mhd_ff_mode_t		ff_mode,
680*0Sstevel@tonic-gate 	mhd_opts_t		options,
681*0Sstevel@tonic-gate 	mhd_error_t		*mhep
682*0Sstevel@tonic-gate )
683*0Sstevel@tonic-gate {
684*0Sstevel@tonic-gate 	mhd_drive_list_t	dl = mhd_null_list;
685*0Sstevel@tonic-gate 	mhd_drive_set_t		*sp;
686*0Sstevel@tonic-gate 	int			rval;
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 	/* grab global lock */
689*0Sstevel@tonic-gate 	mhd_mx_lock(&mhd_set_mx);
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 	/* create or update set */
692*0Sstevel@tonic-gate 	if ((sp = mhd_create_set(mhsp, options, &dl, mhep)) == NULL) {
693*0Sstevel@tonic-gate 		mhd_mx_unlock(&mhd_set_mx);
694*0Sstevel@tonic-gate 		mhd_free_list(&dl);
695*0Sstevel@tonic-gate 		return (-1);
696*0Sstevel@tonic-gate 	}
697*0Sstevel@tonic-gate 
698*0Sstevel@tonic-gate 	/* lock set */
699*0Sstevel@tonic-gate 	mhd_mx_lock(&sp->sr_mx);
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 	/* can't change mode or timeouts of partial set */
702*0Sstevel@tonic-gate 	if ((dl.dl_ndrive != sp->sr_drives.dl_ndrive) &&
703*0Sstevel@tonic-gate 	    (options & MHD_PARTIAL_SET)) {
704*0Sstevel@tonic-gate 		if (ff_mode != sp->sr_ff_mode) {
705*0Sstevel@tonic-gate 			mhd_eprintf("%s: invalid ff_mode %d now %d\n",
706*0Sstevel@tonic-gate 			    sp->sr_name, ff_mode, sp->sr_ff_mode);
707*0Sstevel@tonic-gate 			ff_mode = sp->sr_ff_mode;
708*0Sstevel@tonic-gate 		}
709*0Sstevel@tonic-gate 		if (timeoutp->mh_ff < sp->sr_timeouts.mh_ff) {
710*0Sstevel@tonic-gate 			mhd_eprintf("%s: invalid mh_ff %d now %d\n",
711*0Sstevel@tonic-gate 			    sp->sr_name, timeoutp->mh_ff,
712*0Sstevel@tonic-gate 			    sp->sr_timeouts.mh_ff);
713*0Sstevel@tonic-gate 			timeoutp->mh_ff = sp->sr_timeouts.mh_ff;
714*0Sstevel@tonic-gate 		}
715*0Sstevel@tonic-gate 	}
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	/* save timouts and mode */
718*0Sstevel@tonic-gate 	sp->sr_timeouts = *timeoutp;
719*0Sstevel@tonic-gate 	sp->sr_ff_mode = ff_mode;
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 	/* reserve drives */
722*0Sstevel@tonic-gate 	rval = mhd_reserve_set(sp, &dl, mhep);
723*0Sstevel@tonic-gate 
724*0Sstevel@tonic-gate 	/* unlock, return success */
725*0Sstevel@tonic-gate out:
726*0Sstevel@tonic-gate 	mhd_mx_unlock(&sp->sr_mx);
727*0Sstevel@tonic-gate 	mhd_mx_unlock(&mhd_set_mx);
728*0Sstevel@tonic-gate 	mhd_free_list(&dl);
729*0Sstevel@tonic-gate 	return (rval);
730*0Sstevel@tonic-gate }
731*0Sstevel@tonic-gate 
732*0Sstevel@tonic-gate /*
733*0Sstevel@tonic-gate  * status drives
734*0Sstevel@tonic-gate  */
735*0Sstevel@tonic-gate static int
mhd_status_set(mhd_drive_set_t * sp,mhd_drive_list_t * dlp,mhd_error_t * mhep)736*0Sstevel@tonic-gate mhd_status_set(
737*0Sstevel@tonic-gate 	mhd_drive_set_t		*sp,
738*0Sstevel@tonic-gate 	mhd_drive_list_t	*dlp,
739*0Sstevel@tonic-gate 	mhd_error_t		*mhep
740*0Sstevel@tonic-gate )
741*0Sstevel@tonic-gate {
742*0Sstevel@tonic-gate 	uint_t			i;
743*0Sstevel@tonic-gate 
744*0Sstevel@tonic-gate 	/* check locks */
745*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&mhd_set_mx));
746*0Sstevel@tonic-gate 	assert(MUTEX_HELD(&sp->sr_mx));
747*0Sstevel@tonic-gate 
748*0Sstevel@tonic-gate 	/* status drives */
749*0Sstevel@tonic-gate 	for (i = 0; (i < dlp->dl_ndrive); i++) {
750*0Sstevel@tonic-gate 		mhd_drive_t	*dp = dlp->dl_drives[i];
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 		if (mhd_state_set(dp, DRIVE_STATUSING, mhep) != 0)
753*0Sstevel@tonic-gate 			return (-1);
754*0Sstevel@tonic-gate 	}
755*0Sstevel@tonic-gate 	mhd_wait_set(sp, dlp, DRIVE_STATUSING);
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate 	/* return success */
758*0Sstevel@tonic-gate 	return (0);
759*0Sstevel@tonic-gate }
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate /*
762*0Sstevel@tonic-gate  * status drives in set
763*0Sstevel@tonic-gate  */
764*0Sstevel@tonic-gate int
mhd_status_drives(mhd_set_t * mhsp,mhd_opts_t options,mhd_drive_status_t ** status,mhd_error_t * mhep)765*0Sstevel@tonic-gate mhd_status_drives(
766*0Sstevel@tonic-gate 	mhd_set_t		*mhsp,
767*0Sstevel@tonic-gate 	mhd_opts_t		options,
768*0Sstevel@tonic-gate 	mhd_drive_status_t	**status,
769*0Sstevel@tonic-gate 	mhd_error_t		*mhep
770*0Sstevel@tonic-gate )
771*0Sstevel@tonic-gate {
772*0Sstevel@tonic-gate 	mhd_drive_list_t	dl = mhd_null_list;
773*0Sstevel@tonic-gate 	mhd_drive_list_t	*dlp = &dl;
774*0Sstevel@tonic-gate 	mhd_drive_set_t		*sp;
775*0Sstevel@tonic-gate 	uint_t			i;
776*0Sstevel@tonic-gate 	int			rval = 0;
777*0Sstevel@tonic-gate 
778*0Sstevel@tonic-gate 	/* grab global lock */
779*0Sstevel@tonic-gate 	mhd_mx_lock(&mhd_set_mx);
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 	/* create or update set */
782*0Sstevel@tonic-gate 	if ((sp = mhd_create_set(mhsp, options, &dl, mhep)) == NULL) {
783*0Sstevel@tonic-gate 		mhd_mx_unlock(&mhd_set_mx);
784*0Sstevel@tonic-gate 		mhd_free_list(&dl);
785*0Sstevel@tonic-gate 		return (-1);
786*0Sstevel@tonic-gate 	}
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	/* lock set */
789*0Sstevel@tonic-gate 	mhd_mx_lock(&sp->sr_mx);
790*0Sstevel@tonic-gate 
791*0Sstevel@tonic-gate 	/* status drives */
792*0Sstevel@tonic-gate 	if (mhd_status_set(sp, &dl, mhep) != 0) {
793*0Sstevel@tonic-gate 		rval = -1;
794*0Sstevel@tonic-gate 		goto out;
795*0Sstevel@tonic-gate 	}
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 	/* build list */
798*0Sstevel@tonic-gate 	*status = Zalloc(dlp->dl_ndrive * sizeof (**status));
799*0Sstevel@tonic-gate 	for (i = 0; (i < dlp->dl_ndrive); ++i) {
800*0Sstevel@tonic-gate 		mhd_drive_t		*dp = dlp->dl_drives[i];
801*0Sstevel@tonic-gate 		mhd_drive_status_t	*statusp = &(*status)[i];
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 		statusp->drive = Strdup(dp->dr_rname);
804*0Sstevel@tonic-gate 		statusp->errnum = dp->dr_errnum;
805*0Sstevel@tonic-gate 	}
806*0Sstevel@tonic-gate 	assert(i == dlp->dl_ndrive);
807*0Sstevel@tonic-gate 	rval = dlp->dl_ndrive;
808*0Sstevel@tonic-gate 
809*0Sstevel@tonic-gate 	/* unlock, return count */
810*0Sstevel@tonic-gate out:
811*0Sstevel@tonic-gate 	mhd_mx_unlock(&sp->sr_mx);
812*0Sstevel@tonic-gate 	mhd_mx_unlock(&mhd_set_mx);
813*0Sstevel@tonic-gate 	mhd_free_list(&dl);
814*0Sstevel@tonic-gate 	return (rval);
815*0Sstevel@tonic-gate }
816