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  * change metadevice parameters
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <meta.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <sdssc.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * check for "none"
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate static int
41*0Sstevel@tonic-gate is_none(char *s)
42*0Sstevel@tonic-gate {
43*0Sstevel@tonic-gate 	if ((strcoll(s, gettext("none")) == 0) ||
44*0Sstevel@tonic-gate 	    (strcoll(s, gettext("NONE")) == 0))
45*0Sstevel@tonic-gate 		return (1);
46*0Sstevel@tonic-gate 	return (0);
47*0Sstevel@tonic-gate }
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate  * print usage message
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate static void
53*0Sstevel@tonic-gate usage(
54*0Sstevel@tonic-gate 	mdsetname_t	*sp,
55*0Sstevel@tonic-gate 	int		eval
56*0Sstevel@tonic-gate )
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\
59*0Sstevel@tonic-gate usage:	%s [-s setname] [options] concat/stripe | RAID\n\
60*0Sstevel@tonic-gate 	%s [-s setname] [options] mirror\n\
61*0Sstevel@tonic-gate \n\
62*0Sstevel@tonic-gate Concat/Stripe or RAID options:\n\
63*0Sstevel@tonic-gate -h	hotspare_pool | \"none\"\n\
64*0Sstevel@tonic-gate \n\
65*0Sstevel@tonic-gate Mirror options:\n\
66*0Sstevel@tonic-gate -r	roundrobin | geometric | first\n\
67*0Sstevel@tonic-gate -w	parallel | serial\n\
68*0Sstevel@tonic-gate -p	0-%d\n"), myname, myname, MD_PASS_MAX);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	md_exit(sp, eval);
71*0Sstevel@tonic-gate }
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * do mirror parameters
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate static int
77*0Sstevel@tonic-gate mirror_params(
78*0Sstevel@tonic-gate 	mdsetname_t	*sp,
79*0Sstevel@tonic-gate 	mdname_t	*mirnp,
80*0Sstevel@tonic-gate 	int		argc,
81*0Sstevel@tonic-gate 	char		*argv[],
82*0Sstevel@tonic-gate 	md_error_t	*ep
83*0Sstevel@tonic-gate )
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	mm_params_t	mmp;
86*0Sstevel@tonic-gate 	int		modified = 0;
87*0Sstevel@tonic-gate 	int		c;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	/* we must have a set */
90*0Sstevel@tonic-gate 	assert(sp != NULL);
91*0Sstevel@tonic-gate 	assert(sp->setno == MD_MIN2SET(meta_getminor(mirnp->dev)));
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	/* initialize */
94*0Sstevel@tonic-gate 	(void) memset(&mmp, '\0', sizeof (mmp));
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	/* reset and parse args */
97*0Sstevel@tonic-gate 	optind = 1;
98*0Sstevel@tonic-gate 	opterr = 1;
99*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:r:w:p:")) != -1) {
100*0Sstevel@tonic-gate 		switch (c) {
101*0Sstevel@tonic-gate 		case 's':
102*0Sstevel@tonic-gate 			break;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 		case 'r':
105*0Sstevel@tonic-gate 			if (name_to_rd_opt(mirnp->cname, optarg,
106*0Sstevel@tonic-gate 			    &mmp.read_option, ep) != 0) {
107*0Sstevel@tonic-gate 				return (-1);
108*0Sstevel@tonic-gate 			}
109*0Sstevel@tonic-gate 			mmp.change_read_option = 1;
110*0Sstevel@tonic-gate 			modified = 1;
111*0Sstevel@tonic-gate 			break;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 		case 'w':
114*0Sstevel@tonic-gate 			if (name_to_wr_opt(mirnp->cname, optarg,
115*0Sstevel@tonic-gate 			    &mmp.write_option, ep) != 0) {
116*0Sstevel@tonic-gate 				return (-1);
117*0Sstevel@tonic-gate 			}
118*0Sstevel@tonic-gate 			mmp.change_write_option = 1;
119*0Sstevel@tonic-gate 			modified = 1;
120*0Sstevel@tonic-gate 			break;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 		case 'p':
123*0Sstevel@tonic-gate 			if (name_to_pass_num(mirnp->cname, optarg,
124*0Sstevel@tonic-gate 			    &mmp.pass_num, ep) != 0) {
125*0Sstevel@tonic-gate 				return (-1);
126*0Sstevel@tonic-gate 			}
127*0Sstevel@tonic-gate 			mmp.change_pass_num = 1;
128*0Sstevel@tonic-gate 			modified = 1;
129*0Sstevel@tonic-gate 			break;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 		default:
132*0Sstevel@tonic-gate 			usage(sp, 1);
133*0Sstevel@tonic-gate 			/*NOTREACHED*/
134*0Sstevel@tonic-gate 			break;
135*0Sstevel@tonic-gate 		}
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	argc -= optind;
139*0Sstevel@tonic-gate 	argv += optind;
140*0Sstevel@tonic-gate 	if (argc != 1)
141*0Sstevel@tonic-gate 		usage(sp, 1);
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	/* if just printing */
144*0Sstevel@tonic-gate 	if (! modified) {
145*0Sstevel@tonic-gate 		if (meta_mirror_get_params(sp, mirnp, &mmp, ep) != 0)
146*0Sstevel@tonic-gate 			return (-1);
147*0Sstevel@tonic-gate 		(void) printf(
148*0Sstevel@tonic-gate 		    gettext(
149*0Sstevel@tonic-gate 		    "%s: Mirror current parameters are:\n"),
150*0Sstevel@tonic-gate 		    mirnp->cname);
151*0Sstevel@tonic-gate 		if (meta_print_mirror_options(mmp.read_option,
152*0Sstevel@tonic-gate 		    mmp.write_option, mmp.pass_num, 0, NULL,
153*0Sstevel@tonic-gate 		    sp, stdout, ep) != 0) {
154*0Sstevel@tonic-gate 			return (-1);
155*0Sstevel@tonic-gate 		}
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	/* otherwise, change parameters */
159*0Sstevel@tonic-gate 	else {
160*0Sstevel@tonic-gate 		if (meta_mirror_set_params(sp, mirnp, &mmp, ep) != 0)
161*0Sstevel@tonic-gate 			return (-1);
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 		/* update md.cf */
164*0Sstevel@tonic-gate 		if (meta_update_md_cf(sp, ep) != 0)
165*0Sstevel@tonic-gate 			return (-1);
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	/* return success */
169*0Sstevel@tonic-gate 	return (0);
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate /*
173*0Sstevel@tonic-gate  * do stripe parameters
174*0Sstevel@tonic-gate  */
175*0Sstevel@tonic-gate static int
176*0Sstevel@tonic-gate stripe_params(
177*0Sstevel@tonic-gate 	mdsetname_t	*sp,
178*0Sstevel@tonic-gate 	mdname_t	*stripenp,
179*0Sstevel@tonic-gate 	int		argc,
180*0Sstevel@tonic-gate 	char		*argv[],
181*0Sstevel@tonic-gate 	md_error_t	*ep
182*0Sstevel@tonic-gate )
183*0Sstevel@tonic-gate {
184*0Sstevel@tonic-gate 	ms_params_t	msp;
185*0Sstevel@tonic-gate 	int		modified = 0;
186*0Sstevel@tonic-gate 	mdhspname_t	*hspnp;
187*0Sstevel@tonic-gate 	int		c;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	/* we must have a set */
190*0Sstevel@tonic-gate 	assert(sp != NULL);
191*0Sstevel@tonic-gate 	assert(sp->setno == MD_MIN2SET(meta_getminor(stripenp->dev)));
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	/* initialize */
194*0Sstevel@tonic-gate 	(void) memset(&msp, '\0', sizeof (msp));
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	/* reset and parse args */
197*0Sstevel@tonic-gate 	optind = 1;
198*0Sstevel@tonic-gate 	opterr = 1;
199*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:h:")) != -1) {
200*0Sstevel@tonic-gate 		switch (c) {
201*0Sstevel@tonic-gate 		case 's':
202*0Sstevel@tonic-gate 			break;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 		case 'h':
205*0Sstevel@tonic-gate 			if (is_none(optarg)) {
206*0Sstevel@tonic-gate 				msp.hsp_id = MD_HSP_NONE;
207*0Sstevel@tonic-gate 			} else if ((hspnp = metahspname(&sp, optarg,
208*0Sstevel@tonic-gate 			    ep)) == NULL) {
209*0Sstevel@tonic-gate 				return (-1);
210*0Sstevel@tonic-gate 			} else if (metachkhsp(sp, hspnp, ep) != 0) {
211*0Sstevel@tonic-gate 				return (-1);
212*0Sstevel@tonic-gate 			} else {
213*0Sstevel@tonic-gate 				msp.hsp_id = hspnp->hsp;
214*0Sstevel@tonic-gate 			}
215*0Sstevel@tonic-gate 			msp.change_hsp_id = 1;
216*0Sstevel@tonic-gate 			modified = 1;
217*0Sstevel@tonic-gate 			break;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 		default:
220*0Sstevel@tonic-gate 			usage(sp, 1);
221*0Sstevel@tonic-gate 			/*NOTREACHED*/
222*0Sstevel@tonic-gate 			break;
223*0Sstevel@tonic-gate 		}
224*0Sstevel@tonic-gate 	}
225*0Sstevel@tonic-gate 	argc -= optind;
226*0Sstevel@tonic-gate 	argv += optind;
227*0Sstevel@tonic-gate 	if (argc != 1)
228*0Sstevel@tonic-gate 		usage(sp, 1);
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	/* if just printing */
231*0Sstevel@tonic-gate 	if (! modified) {
232*0Sstevel@tonic-gate 		if (meta_stripe_get_params(sp, stripenp, &msp, ep) != 0)
233*0Sstevel@tonic-gate 			return (-1);
234*0Sstevel@tonic-gate 		if (msp.hsp_id == MD_HSP_NONE)
235*0Sstevel@tonic-gate 			hspnp = NULL;
236*0Sstevel@tonic-gate 		else if ((hspnp = metahsphspname(&sp, msp.hsp_id, ep)) == NULL)
237*0Sstevel@tonic-gate 			return (-1);
238*0Sstevel@tonic-gate 		(void) printf(gettext(
239*0Sstevel@tonic-gate 		    "%s: Concat/Stripe current parameters are:\n"),
240*0Sstevel@tonic-gate 		    stripenp->cname);
241*0Sstevel@tonic-gate 		if (meta_print_stripe_options(hspnp, NULL, stdout, ep) != 0)
242*0Sstevel@tonic-gate 			return (-1);
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	/* otherwise, change parameters */
246*0Sstevel@tonic-gate 	else {
247*0Sstevel@tonic-gate 		if (meta_stripe_set_params(sp, stripenp, &msp, ep) != 0)
248*0Sstevel@tonic-gate 			return (-1);
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 		/* update md.cf */
251*0Sstevel@tonic-gate 		if (meta_update_md_cf(sp, ep) != 0)
252*0Sstevel@tonic-gate 			return (-1);
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	/* return success */
256*0Sstevel@tonic-gate 	return (0);
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate /*
260*0Sstevel@tonic-gate  * do raid parameters
261*0Sstevel@tonic-gate  */
262*0Sstevel@tonic-gate static int
263*0Sstevel@tonic-gate raid_params(
264*0Sstevel@tonic-gate 	mdsetname_t	*sp,
265*0Sstevel@tonic-gate 	mdname_t	*raidnp,
266*0Sstevel@tonic-gate 	int		argc,
267*0Sstevel@tonic-gate 	char		*argv[],
268*0Sstevel@tonic-gate 	md_error_t	*ep
269*0Sstevel@tonic-gate )
270*0Sstevel@tonic-gate {
271*0Sstevel@tonic-gate 	mr_params_t	msp;
272*0Sstevel@tonic-gate 	int		modified = 0;
273*0Sstevel@tonic-gate 	mdhspname_t	*hspnp;
274*0Sstevel@tonic-gate 	int		c;
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	/* we must have a set */
277*0Sstevel@tonic-gate 	assert(sp != NULL);
278*0Sstevel@tonic-gate 	assert(sp->setno == MD_MIN2SET(meta_getminor(raidnp->dev)));
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	/* initialize */
281*0Sstevel@tonic-gate 	(void) memset(&msp, '\0', sizeof (msp));
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	/* reset and parse args */
284*0Sstevel@tonic-gate 	optind = 1;
285*0Sstevel@tonic-gate 	opterr = 1;
286*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:h:")) != -1) {
287*0Sstevel@tonic-gate 		switch (c) {
288*0Sstevel@tonic-gate 		case 's':
289*0Sstevel@tonic-gate 			break;
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 		case 'h':
292*0Sstevel@tonic-gate 			if (is_none(optarg)) {
293*0Sstevel@tonic-gate 				msp.hsp_id = MD_HSP_NONE;
294*0Sstevel@tonic-gate 			} else if ((hspnp = metahspname(&sp, optarg,
295*0Sstevel@tonic-gate 			    ep)) == NULL) {
296*0Sstevel@tonic-gate 				return (-1);
297*0Sstevel@tonic-gate 			} else if (metachkhsp(sp, hspnp, ep) != 0) {
298*0Sstevel@tonic-gate 				return (-1);
299*0Sstevel@tonic-gate 			} else {
300*0Sstevel@tonic-gate 				msp.hsp_id = hspnp->hsp;
301*0Sstevel@tonic-gate 			}
302*0Sstevel@tonic-gate 			msp.change_hsp_id = 1;
303*0Sstevel@tonic-gate 			modified = 1;
304*0Sstevel@tonic-gate 			break;
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 		default:
307*0Sstevel@tonic-gate 			usage(sp, 1);
308*0Sstevel@tonic-gate 			/*NOTREACHED*/
309*0Sstevel@tonic-gate 			break;
310*0Sstevel@tonic-gate 		}
311*0Sstevel@tonic-gate 	}
312*0Sstevel@tonic-gate 	argc -= optind;
313*0Sstevel@tonic-gate 	argv += optind;
314*0Sstevel@tonic-gate 	if (argc != 1)
315*0Sstevel@tonic-gate 		usage(sp, 1);
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	/* if just printing */
318*0Sstevel@tonic-gate 	if (! modified) {
319*0Sstevel@tonic-gate 		if (meta_raid_get_params(sp, raidnp, &msp, ep) != 0)
320*0Sstevel@tonic-gate 			return (-1);
321*0Sstevel@tonic-gate 		if (msp.hsp_id == MD_HSP_NONE)
322*0Sstevel@tonic-gate 			hspnp = NULL;
323*0Sstevel@tonic-gate 		else if ((hspnp = metahsphspname(&sp, msp.hsp_id, ep)) == NULL)
324*0Sstevel@tonic-gate 			return (-1);
325*0Sstevel@tonic-gate 		(void) printf(gettext(
326*0Sstevel@tonic-gate 		    "%s: RAID current parameters are:\n"),
327*0Sstevel@tonic-gate 		    raidnp->cname);
328*0Sstevel@tonic-gate 		if (meta_print_raid_options(hspnp, NULL, stdout, ep) != 0)
329*0Sstevel@tonic-gate 			return (-1);
330*0Sstevel@tonic-gate 	}
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 	/* otherwise, change parameters */
333*0Sstevel@tonic-gate 	else {
334*0Sstevel@tonic-gate 		if (meta_raid_set_params(sp, raidnp, &msp, ep) != 0)
335*0Sstevel@tonic-gate 			return (-1);
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 		/* update md.cf */
338*0Sstevel@tonic-gate 		if (meta_update_md_cf(sp, ep) != 0)
339*0Sstevel@tonic-gate 			return (-1);
340*0Sstevel@tonic-gate 	}
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	/* return success */
343*0Sstevel@tonic-gate 	return (0);
344*0Sstevel@tonic-gate }
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate /*
347*0Sstevel@tonic-gate  * parse args and doit
348*0Sstevel@tonic-gate  */
349*0Sstevel@tonic-gate int
350*0Sstevel@tonic-gate main(
351*0Sstevel@tonic-gate 	int		argc,
352*0Sstevel@tonic-gate 	char		**argv
353*0Sstevel@tonic-gate )
354*0Sstevel@tonic-gate {
355*0Sstevel@tonic-gate 	char		*sname = NULL;
356*0Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
357*0Sstevel@tonic-gate 	mdname_t	*np;
358*0Sstevel@tonic-gate 	char		*miscname;
359*0Sstevel@tonic-gate 	int		c;
360*0Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
361*0Sstevel@tonic-gate 	md_error_t	*ep = &status;
362*0Sstevel@tonic-gate 	int		error;
363*0Sstevel@tonic-gate 	bool_t		called_thru_rpc = FALSE;
364*0Sstevel@tonic-gate 	char		*cp;
365*0Sstevel@tonic-gate 	char		*firstarg = NULL;
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	/*
369*0Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
370*0Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
371*0Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
372*0Sstevel@tonic-gate 	 * something.
373*0Sstevel@tonic-gate 	 */
374*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
375*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
376*0Sstevel@tonic-gate #endif
377*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
378*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
381*0Sstevel@tonic-gate 		if (sdssc_bind_library() == SDSSC_OKAY)
382*0Sstevel@tonic-gate 			if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
383*0Sstevel@tonic-gate 						&error) == SDSSC_PROXY_DONE)
384*0Sstevel@tonic-gate 				exit(error);
385*0Sstevel@tonic-gate 	} else {
386*0Sstevel@tonic-gate 		*cp = '\0'; /* cut off ".rpc_call" */
387*0Sstevel@tonic-gate 		called_thru_rpc = TRUE;
388*0Sstevel@tonic-gate 	}
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	/* initialize */
392*0Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
393*0Sstevel@tonic-gate 			meta_check_root(ep) != 0) {
394*0Sstevel@tonic-gate 		mde_perror(ep, "");
395*0Sstevel@tonic-gate 		md_exit(sp, 1);
396*0Sstevel@tonic-gate 	}
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	/* find set and metadevice first */
399*0Sstevel@tonic-gate 	optind = 1;
400*0Sstevel@tonic-gate 	opterr = 1;
401*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:h:p:r:w:o:?")) != -1) {
402*0Sstevel@tonic-gate 		switch (c) {
403*0Sstevel@tonic-gate 		case 's':
404*0Sstevel@tonic-gate 			sname = optarg;
405*0Sstevel@tonic-gate 			break;
406*0Sstevel@tonic-gate 		case 'h':
407*0Sstevel@tonic-gate 			firstarg = optarg;
408*0Sstevel@tonic-gate 			break;
409*0Sstevel@tonic-gate 		case '?':
410*0Sstevel@tonic-gate 			if (optopt == '?')
411*0Sstevel@tonic-gate 				usage(sp, 0);
412*0Sstevel@tonic-gate 			break;
413*0Sstevel@tonic-gate 		}
414*0Sstevel@tonic-gate 	}
415*0Sstevel@tonic-gate 	if ((argc - optind) <= 0)
416*0Sstevel@tonic-gate 		usage(sp, 1);
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 	if (sname != NULL) {
419*0Sstevel@tonic-gate 		if ((sp = metasetname(sname, ep)) == NULL) {
420*0Sstevel@tonic-gate 			mde_perror(ep, "");
421*0Sstevel@tonic-gate 			md_exit(sp, 1);
422*0Sstevel@tonic-gate 		}
423*0Sstevel@tonic-gate 	}
424*0Sstevel@tonic-gate 	if (firstarg == NULL)
425*0Sstevel@tonic-gate 		firstarg = argv[optind];
426*0Sstevel@tonic-gate 	if ((called_thru_rpc == FALSE) &&
427*0Sstevel@tonic-gate 	    meta_is_mn_name(&sp, firstarg, ep)) {
428*0Sstevel@tonic-gate 		/*
429*0Sstevel@tonic-gate 		 * If we are dealing with a MN set and we were not
430*0Sstevel@tonic-gate 		 * called thru an rpc call, we are just to send this
431*0Sstevel@tonic-gate 		 * command string to the master of the set and let it
432*0Sstevel@tonic-gate 		 * deal with it.
433*0Sstevel@tonic-gate 		 * Note that if sp is NULL, meta_is_mn_name() derives sp
434*0Sstevel@tonic-gate 		 * from firstarg which is the metadevice arg
435*0Sstevel@tonic-gate 		 * If this fails, the master must panic as the mddb may be
436*0Sstevel@tonic-gate 		 * inconsistent
437*0Sstevel@tonic-gate 		 */
438*0Sstevel@tonic-gate 		int result;
439*0Sstevel@tonic-gate 		result = meta_mn_send_command(sp, argc, argv, MD_DISP_STDERR |
440*0Sstevel@tonic-gate 		    MD_PANIC_WHEN_INCONSISTENT, NO_CONTEXT_STRING, ep);
441*0Sstevel@tonic-gate 		/* No further action required */
442*0Sstevel@tonic-gate 		md_exit(sp, result);
443*0Sstevel@tonic-gate 	}
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	if ((np = metaname(&sp, argv[optind], ep)) == NULL) {
446*0Sstevel@tonic-gate 		mde_perror(ep, "");
447*0Sstevel@tonic-gate 		md_exit(sp, 1);
448*0Sstevel@tonic-gate 	}
449*0Sstevel@tonic-gate 	assert(sp != NULL);
450*0Sstevel@tonic-gate 	/* grab set lock */
451*0Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, ep)) {
452*0Sstevel@tonic-gate 		mde_perror(ep, "");
453*0Sstevel@tonic-gate 		md_exit(sp, 1);
454*0Sstevel@tonic-gate 	}
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
457*0Sstevel@tonic-gate 		mde_perror(ep, "");
458*0Sstevel@tonic-gate 		md_exit(sp, 1);
459*0Sstevel@tonic-gate 	}
460*0Sstevel@tonic-gate 	if ((miscname = metagetmiscname(np, ep)) == NULL) {
461*0Sstevel@tonic-gate 		mde_perror(ep, "");
462*0Sstevel@tonic-gate 		md_exit(sp, 1);
463*0Sstevel@tonic-gate 	}
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	/* dispatch based on device type */
466*0Sstevel@tonic-gate 	if (strcmp(miscname, MD_STRIPE) == 0) {
467*0Sstevel@tonic-gate 		if (stripe_params(sp, np, argc, argv, ep) != 0) {
468*0Sstevel@tonic-gate 			mde_perror(ep, "");
469*0Sstevel@tonic-gate 			md_exit(sp, 1);
470*0Sstevel@tonic-gate 		}
471*0Sstevel@tonic-gate 	} else 	if (strcmp(miscname, MD_MIRROR) == 0) {
472*0Sstevel@tonic-gate 		if (mirror_params(sp, np, argc, argv, ep) != 0) {
473*0Sstevel@tonic-gate 			mde_perror(ep, "");
474*0Sstevel@tonic-gate 			md_exit(sp, 1);
475*0Sstevel@tonic-gate 		}
476*0Sstevel@tonic-gate 	} else 	if (strcmp(miscname, MD_RAID) == 0) {
477*0Sstevel@tonic-gate 		if (raid_params(sp, np, argc, argv, ep) != 0) {
478*0Sstevel@tonic-gate 			mde_perror(ep, "");
479*0Sstevel@tonic-gate 			md_exit(sp, 1);
480*0Sstevel@tonic-gate 		}
481*0Sstevel@tonic-gate 	} else {
482*0Sstevel@tonic-gate 		md_eprintf(gettext(
483*0Sstevel@tonic-gate 		    "%s: invalid metadevice type %s\n"),
484*0Sstevel@tonic-gate 		    np->cname, miscname);
485*0Sstevel@tonic-gate 		md_exit(sp, 1);
486*0Sstevel@tonic-gate 	}
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 	/* return success */
489*0Sstevel@tonic-gate 	md_exit(sp, 0);
490*0Sstevel@tonic-gate 	/*NOTREACHED*/
491*0Sstevel@tonic-gate 	return (0);
492*0Sstevel@tonic-gate }
493