xref: /titanic_41/usr/src/cmd/lvm/util/metaparam.c (revision d7cd82522afdd890a66c7600b499590ad44e84bd)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*d7cd8252Stw21770  * Common Development and Distribution License (the "License").
6*d7cd8252Stw21770  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d7cd8252Stw21770  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * change metadevice parameters
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <meta.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sdssc.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * print usage message
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate static void
usage(mdsetname_t * sp,int eval)407c478bd9Sstevel@tonic-gate usage(
417c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp,
427c478bd9Sstevel@tonic-gate 	int		eval
437c478bd9Sstevel@tonic-gate )
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\
467c478bd9Sstevel@tonic-gate usage:	%s [-s setname] [options] concat/stripe | RAID\n\
477c478bd9Sstevel@tonic-gate 	%s [-s setname] [options] mirror\n\
487c478bd9Sstevel@tonic-gate \n\
497c478bd9Sstevel@tonic-gate Concat/Stripe or RAID options:\n\
507c478bd9Sstevel@tonic-gate -h	hotspare_pool | \"none\"\n\
517c478bd9Sstevel@tonic-gate \n\
527c478bd9Sstevel@tonic-gate Mirror options:\n\
537c478bd9Sstevel@tonic-gate -r	roundrobin | geometric | first\n\
547c478bd9Sstevel@tonic-gate -w	parallel | serial\n\
557c478bd9Sstevel@tonic-gate -p	0-%d\n"), myname, myname, MD_PASS_MAX);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	md_exit(sp, eval);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * do mirror parameters
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate static int
mirror_params(mdsetname_t * sp,mdname_t * mirnp,int argc,char * argv[],md_error_t * ep)647c478bd9Sstevel@tonic-gate mirror_params(
657c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp,
667c478bd9Sstevel@tonic-gate 	mdname_t	*mirnp,
677c478bd9Sstevel@tonic-gate 	int		argc,
687c478bd9Sstevel@tonic-gate 	char		*argv[],
697c478bd9Sstevel@tonic-gate 	md_error_t	*ep
707c478bd9Sstevel@tonic-gate )
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	mm_params_t	mmp;
737c478bd9Sstevel@tonic-gate 	int		modified = 0;
747c478bd9Sstevel@tonic-gate 	int		c;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/* we must have a set */
777c478bd9Sstevel@tonic-gate 	assert(sp != NULL);
787c478bd9Sstevel@tonic-gate 	assert(sp->setno == MD_MIN2SET(meta_getminor(mirnp->dev)));
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	/* initialize */
817c478bd9Sstevel@tonic-gate 	(void) memset(&mmp, '\0', sizeof (mmp));
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	/* reset and parse args */
847c478bd9Sstevel@tonic-gate 	optind = 1;
857c478bd9Sstevel@tonic-gate 	opterr = 1;
867c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:r:w:p:")) != -1) {
877c478bd9Sstevel@tonic-gate 		switch (c) {
887c478bd9Sstevel@tonic-gate 		case 's':
897c478bd9Sstevel@tonic-gate 			break;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 		case 'r':
927c478bd9Sstevel@tonic-gate 			if (name_to_rd_opt(mirnp->cname, optarg,
937c478bd9Sstevel@tonic-gate 			    &mmp.read_option, ep) != 0) {
947c478bd9Sstevel@tonic-gate 				return (-1);
957c478bd9Sstevel@tonic-gate 			}
967c478bd9Sstevel@tonic-gate 			mmp.change_read_option = 1;
977c478bd9Sstevel@tonic-gate 			modified = 1;
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 		case 'w':
1017c478bd9Sstevel@tonic-gate 			if (name_to_wr_opt(mirnp->cname, optarg,
1027c478bd9Sstevel@tonic-gate 			    &mmp.write_option, ep) != 0) {
1037c478bd9Sstevel@tonic-gate 				return (-1);
1047c478bd9Sstevel@tonic-gate 			}
1057c478bd9Sstevel@tonic-gate 			mmp.change_write_option = 1;
1067c478bd9Sstevel@tonic-gate 			modified = 1;
1077c478bd9Sstevel@tonic-gate 			break;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		case 'p':
1107c478bd9Sstevel@tonic-gate 			if (name_to_pass_num(mirnp->cname, optarg,
1117c478bd9Sstevel@tonic-gate 			    &mmp.pass_num, ep) != 0) {
1127c478bd9Sstevel@tonic-gate 				return (-1);
1137c478bd9Sstevel@tonic-gate 			}
1147c478bd9Sstevel@tonic-gate 			mmp.change_pass_num = 1;
1157c478bd9Sstevel@tonic-gate 			modified = 1;
1167c478bd9Sstevel@tonic-gate 			break;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		default:
1197c478bd9Sstevel@tonic-gate 			usage(sp, 1);
1207c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
1217c478bd9Sstevel@tonic-gate 			break;
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	argc -= optind;
1267c478bd9Sstevel@tonic-gate 	argv += optind;
1277c478bd9Sstevel@tonic-gate 	if (argc != 1)
1287c478bd9Sstevel@tonic-gate 		usage(sp, 1);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/* if just printing */
1317c478bd9Sstevel@tonic-gate 	if (! modified) {
1327c478bd9Sstevel@tonic-gate 		if (meta_mirror_get_params(sp, mirnp, &mmp, ep) != 0)
1337c478bd9Sstevel@tonic-gate 			return (-1);
1347c478bd9Sstevel@tonic-gate 		(void) printf(
1357c478bd9Sstevel@tonic-gate 		    gettext(
1367c478bd9Sstevel@tonic-gate 		    "%s: Mirror current parameters are:\n"),
1377c478bd9Sstevel@tonic-gate 		    mirnp->cname);
1387c478bd9Sstevel@tonic-gate 		if (meta_print_mirror_options(mmp.read_option,
1397c478bd9Sstevel@tonic-gate 		    mmp.write_option, mmp.pass_num, 0, NULL,
1407c478bd9Sstevel@tonic-gate 		    sp, stdout, ep) != 0) {
1417c478bd9Sstevel@tonic-gate 			return (-1);
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/* otherwise, change parameters */
1467c478bd9Sstevel@tonic-gate 	else {
1477c478bd9Sstevel@tonic-gate 		if (meta_mirror_set_params(sp, mirnp, &mmp, ep) != 0)
1487c478bd9Sstevel@tonic-gate 			return (-1);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		/* update md.cf */
1517c478bd9Sstevel@tonic-gate 		if (meta_update_md_cf(sp, ep) != 0)
1527c478bd9Sstevel@tonic-gate 			return (-1);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/* return success */
1567c478bd9Sstevel@tonic-gate 	return (0);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * do stripe parameters
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate static int
stripe_params(mdsetname_t * sp,mdname_t * stripenp,int argc,char * argv[],md_error_t * ep)1637c478bd9Sstevel@tonic-gate stripe_params(
1647c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp,
1657c478bd9Sstevel@tonic-gate 	mdname_t	*stripenp,
1667c478bd9Sstevel@tonic-gate 	int		argc,
1677c478bd9Sstevel@tonic-gate 	char		*argv[],
1687c478bd9Sstevel@tonic-gate 	md_error_t	*ep
1697c478bd9Sstevel@tonic-gate )
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	ms_params_t	msp;
1727c478bd9Sstevel@tonic-gate 	int		modified = 0;
1737c478bd9Sstevel@tonic-gate 	mdhspname_t	*hspnp;
1747c478bd9Sstevel@tonic-gate 	int		c;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/* we must have a set */
1777c478bd9Sstevel@tonic-gate 	assert(sp != NULL);
1787c478bd9Sstevel@tonic-gate 	assert(sp->setno == MD_MIN2SET(meta_getminor(stripenp->dev)));
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/* initialize */
1817c478bd9Sstevel@tonic-gate 	(void) memset(&msp, '\0', sizeof (msp));
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/* reset and parse args */
1847c478bd9Sstevel@tonic-gate 	optind = 1;
1857c478bd9Sstevel@tonic-gate 	opterr = 1;
1867c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:h:")) != -1) {
1877c478bd9Sstevel@tonic-gate 		switch (c) {
1887c478bd9Sstevel@tonic-gate 		case 's':
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		case 'h':
192*d7cd8252Stw21770 			if (meta_is_none(optarg)) {
1937c478bd9Sstevel@tonic-gate 				msp.hsp_id = MD_HSP_NONE;
1947c478bd9Sstevel@tonic-gate 			} else if ((hspnp = metahspname(&sp, optarg,
1957c478bd9Sstevel@tonic-gate 			    ep)) == NULL) {
1967c478bd9Sstevel@tonic-gate 				return (-1);
1977c478bd9Sstevel@tonic-gate 			} else if (metachkhsp(sp, hspnp, ep) != 0) {
1987c478bd9Sstevel@tonic-gate 				return (-1);
1997c478bd9Sstevel@tonic-gate 			} else {
2007c478bd9Sstevel@tonic-gate 				msp.hsp_id = hspnp->hsp;
2017c478bd9Sstevel@tonic-gate 			}
2027c478bd9Sstevel@tonic-gate 			msp.change_hsp_id = 1;
2037c478bd9Sstevel@tonic-gate 			modified = 1;
2047c478bd9Sstevel@tonic-gate 			break;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		default:
2077c478bd9Sstevel@tonic-gate 			usage(sp, 1);
2087c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	argc -= optind;
2137c478bd9Sstevel@tonic-gate 	argv += optind;
2147c478bd9Sstevel@tonic-gate 	if (argc != 1)
2157c478bd9Sstevel@tonic-gate 		usage(sp, 1);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/* if just printing */
2187c478bd9Sstevel@tonic-gate 	if (! modified) {
2197c478bd9Sstevel@tonic-gate 		if (meta_stripe_get_params(sp, stripenp, &msp, ep) != 0)
2207c478bd9Sstevel@tonic-gate 			return (-1);
2217c478bd9Sstevel@tonic-gate 		if (msp.hsp_id == MD_HSP_NONE)
2227c478bd9Sstevel@tonic-gate 			hspnp = NULL;
2237c478bd9Sstevel@tonic-gate 		else if ((hspnp = metahsphspname(&sp, msp.hsp_id, ep)) == NULL)
2247c478bd9Sstevel@tonic-gate 			return (-1);
2257c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
2267c478bd9Sstevel@tonic-gate 		    "%s: Concat/Stripe current parameters are:\n"),
2277c478bd9Sstevel@tonic-gate 		    stripenp->cname);
2287c478bd9Sstevel@tonic-gate 		if (meta_print_stripe_options(hspnp, NULL, stdout, ep) != 0)
2297c478bd9Sstevel@tonic-gate 			return (-1);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/* otherwise, change parameters */
2337c478bd9Sstevel@tonic-gate 	else {
2347c478bd9Sstevel@tonic-gate 		if (meta_stripe_set_params(sp, stripenp, &msp, ep) != 0)
2357c478bd9Sstevel@tonic-gate 			return (-1);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		/* update md.cf */
2387c478bd9Sstevel@tonic-gate 		if (meta_update_md_cf(sp, ep) != 0)
2397c478bd9Sstevel@tonic-gate 			return (-1);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/* return success */
2437c478bd9Sstevel@tonic-gate 	return (0);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * do raid parameters
2487c478bd9Sstevel@tonic-gate  */
2497c478bd9Sstevel@tonic-gate static int
raid_params(mdsetname_t * sp,mdname_t * raidnp,int argc,char * argv[],md_error_t * ep)2507c478bd9Sstevel@tonic-gate raid_params(
2517c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp,
2527c478bd9Sstevel@tonic-gate 	mdname_t	*raidnp,
2537c478bd9Sstevel@tonic-gate 	int		argc,
2547c478bd9Sstevel@tonic-gate 	char		*argv[],
2557c478bd9Sstevel@tonic-gate 	md_error_t	*ep
2567c478bd9Sstevel@tonic-gate )
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	mr_params_t	msp;
2597c478bd9Sstevel@tonic-gate 	int		modified = 0;
2607c478bd9Sstevel@tonic-gate 	mdhspname_t	*hspnp;
2617c478bd9Sstevel@tonic-gate 	int		c;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/* we must have a set */
2647c478bd9Sstevel@tonic-gate 	assert(sp != NULL);
2657c478bd9Sstevel@tonic-gate 	assert(sp->setno == MD_MIN2SET(meta_getminor(raidnp->dev)));
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/* initialize */
2687c478bd9Sstevel@tonic-gate 	(void) memset(&msp, '\0', sizeof (msp));
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	/* reset and parse args */
2717c478bd9Sstevel@tonic-gate 	optind = 1;
2727c478bd9Sstevel@tonic-gate 	opterr = 1;
2737c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:h:")) != -1) {
2747c478bd9Sstevel@tonic-gate 		switch (c) {
2757c478bd9Sstevel@tonic-gate 		case 's':
2767c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		case 'h':
279*d7cd8252Stw21770 			if (meta_is_none(optarg)) {
2807c478bd9Sstevel@tonic-gate 				msp.hsp_id = MD_HSP_NONE;
2817c478bd9Sstevel@tonic-gate 			} else if ((hspnp = metahspname(&sp, optarg,
2827c478bd9Sstevel@tonic-gate 			    ep)) == NULL) {
2837c478bd9Sstevel@tonic-gate 				return (-1);
2847c478bd9Sstevel@tonic-gate 			} else if (metachkhsp(sp, hspnp, ep) != 0) {
2857c478bd9Sstevel@tonic-gate 				return (-1);
2867c478bd9Sstevel@tonic-gate 			} else {
2877c478bd9Sstevel@tonic-gate 				msp.hsp_id = hspnp->hsp;
2887c478bd9Sstevel@tonic-gate 			}
2897c478bd9Sstevel@tonic-gate 			msp.change_hsp_id = 1;
2907c478bd9Sstevel@tonic-gate 			modified = 1;
2917c478bd9Sstevel@tonic-gate 			break;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		default:
2947c478bd9Sstevel@tonic-gate 			usage(sp, 1);
2957c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2967c478bd9Sstevel@tonic-gate 			break;
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	argc -= optind;
3007c478bd9Sstevel@tonic-gate 	argv += optind;
3017c478bd9Sstevel@tonic-gate 	if (argc != 1)
3027c478bd9Sstevel@tonic-gate 		usage(sp, 1);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* if just printing */
3057c478bd9Sstevel@tonic-gate 	if (! modified) {
3067c478bd9Sstevel@tonic-gate 		if (meta_raid_get_params(sp, raidnp, &msp, ep) != 0)
3077c478bd9Sstevel@tonic-gate 			return (-1);
3087c478bd9Sstevel@tonic-gate 		if (msp.hsp_id == MD_HSP_NONE)
3097c478bd9Sstevel@tonic-gate 			hspnp = NULL;
3107c478bd9Sstevel@tonic-gate 		else if ((hspnp = metahsphspname(&sp, msp.hsp_id, ep)) == NULL)
3117c478bd9Sstevel@tonic-gate 			return (-1);
3127c478bd9Sstevel@tonic-gate 		(void) printf(gettext(
3137c478bd9Sstevel@tonic-gate 		    "%s: RAID current parameters are:\n"),
3147c478bd9Sstevel@tonic-gate 		    raidnp->cname);
3157c478bd9Sstevel@tonic-gate 		if (meta_print_raid_options(hspnp, NULL, stdout, ep) != 0)
3167c478bd9Sstevel@tonic-gate 			return (-1);
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/* otherwise, change parameters */
3207c478bd9Sstevel@tonic-gate 	else {
3217c478bd9Sstevel@tonic-gate 		if (meta_raid_set_params(sp, raidnp, &msp, ep) != 0)
3227c478bd9Sstevel@tonic-gate 			return (-1);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 		/* update md.cf */
3257c478bd9Sstevel@tonic-gate 		if (meta_update_md_cf(sp, ep) != 0)
3267c478bd9Sstevel@tonic-gate 			return (-1);
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	/* return success */
3307c478bd9Sstevel@tonic-gate 	return (0);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  * parse args and doit
3357c478bd9Sstevel@tonic-gate  */
3367c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)3377c478bd9Sstevel@tonic-gate main(
3387c478bd9Sstevel@tonic-gate 	int		argc,
3397c478bd9Sstevel@tonic-gate 	char		**argv
3407c478bd9Sstevel@tonic-gate )
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	char		*sname = NULL;
3437c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
3447c478bd9Sstevel@tonic-gate 	mdname_t	*np;
3457c478bd9Sstevel@tonic-gate 	char		*miscname;
3467c478bd9Sstevel@tonic-gate 	int		c;
3477c478bd9Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
3487c478bd9Sstevel@tonic-gate 	md_error_t	*ep = &status;
3497c478bd9Sstevel@tonic-gate 	int		error;
3507c478bd9Sstevel@tonic-gate 	bool_t		called_thru_rpc = FALSE;
3517c478bd9Sstevel@tonic-gate 	char		*cp;
3527c478bd9Sstevel@tonic-gate 	char		*firstarg = NULL;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	/*
3567c478bd9Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
3577c478bd9Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
3587c478bd9Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
3597c478bd9Sstevel@tonic-gate 	 * something.
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
3627c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
3637c478bd9Sstevel@tonic-gate #endif
3647c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3657c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
3687c478bd9Sstevel@tonic-gate 		if (sdssc_bind_library() == SDSSC_OKAY)
3697c478bd9Sstevel@tonic-gate 			if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
3707c478bd9Sstevel@tonic-gate 						&error) == SDSSC_PROXY_DONE)
3717c478bd9Sstevel@tonic-gate 				exit(error);
3727c478bd9Sstevel@tonic-gate 	} else {
3737c478bd9Sstevel@tonic-gate 		*cp = '\0'; /* cut off ".rpc_call" */
3747c478bd9Sstevel@tonic-gate 		called_thru_rpc = TRUE;
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/* initialize */
3797c478bd9Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
3807c478bd9Sstevel@tonic-gate 			meta_check_root(ep) != 0) {
3817c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
3827c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/* find set and metadevice first */
3867c478bd9Sstevel@tonic-gate 	optind = 1;
3877c478bd9Sstevel@tonic-gate 	opterr = 1;
3887c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:h:p:r:w:o:?")) != -1) {
3897c478bd9Sstevel@tonic-gate 		switch (c) {
3907c478bd9Sstevel@tonic-gate 		case 's':
3917c478bd9Sstevel@tonic-gate 			sname = optarg;
3927c478bd9Sstevel@tonic-gate 			break;
3937c478bd9Sstevel@tonic-gate 		case 'h':
3947c478bd9Sstevel@tonic-gate 			firstarg = optarg;
3957c478bd9Sstevel@tonic-gate 			break;
3967c478bd9Sstevel@tonic-gate 		case '?':
3977c478bd9Sstevel@tonic-gate 			if (optopt == '?')
3987c478bd9Sstevel@tonic-gate 				usage(sp, 0);
3997c478bd9Sstevel@tonic-gate 			break;
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 	if ((argc - optind) <= 0)
4037c478bd9Sstevel@tonic-gate 		usage(sp, 1);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (sname != NULL) {
4067c478bd9Sstevel@tonic-gate 		if ((sp = metasetname(sname, ep)) == NULL) {
4077c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
4087c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 	if (firstarg == NULL)
4127c478bd9Sstevel@tonic-gate 		firstarg = argv[optind];
4137c478bd9Sstevel@tonic-gate 	if ((called_thru_rpc == FALSE) &&
4147c478bd9Sstevel@tonic-gate 	    meta_is_mn_name(&sp, firstarg, ep)) {
4157c478bd9Sstevel@tonic-gate 		/*
4167c478bd9Sstevel@tonic-gate 		 * If we are dealing with a MN set and we were not
4177c478bd9Sstevel@tonic-gate 		 * called thru an rpc call, we are just to send this
4187c478bd9Sstevel@tonic-gate 		 * command string to the master of the set and let it
4197c478bd9Sstevel@tonic-gate 		 * deal with it.
4207c478bd9Sstevel@tonic-gate 		 * Note that if sp is NULL, meta_is_mn_name() derives sp
4217c478bd9Sstevel@tonic-gate 		 * from firstarg which is the metadevice arg
4227c478bd9Sstevel@tonic-gate 		 * If this fails, the master must panic as the mddb may be
4237c478bd9Sstevel@tonic-gate 		 * inconsistent
4247c478bd9Sstevel@tonic-gate 		 */
4257c478bd9Sstevel@tonic-gate 		int result;
4267c478bd9Sstevel@tonic-gate 		result = meta_mn_send_command(sp, argc, argv, MD_DISP_STDERR |
4277c478bd9Sstevel@tonic-gate 		    MD_PANIC_WHEN_INCONSISTENT, NO_CONTEXT_STRING, ep);
4287c478bd9Sstevel@tonic-gate 		/* No further action required */
4297c478bd9Sstevel@tonic-gate 		md_exit(sp, result);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
432*d7cd8252Stw21770 	if ((np = metaname(&sp, argv[optind], META_DEVICE, ep)) == NULL) {
4337c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
4347c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 	assert(sp != NULL);
4377c478bd9Sstevel@tonic-gate 	/* grab set lock */
4387c478bd9Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, ep)) {
4397c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
4407c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
4447c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
4457c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 	if ((miscname = metagetmiscname(np, ep)) == NULL) {
4487c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
4497c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	/* dispatch based on device type */
4537c478bd9Sstevel@tonic-gate 	if (strcmp(miscname, MD_STRIPE) == 0) {
4547c478bd9Sstevel@tonic-gate 		if (stripe_params(sp, np, argc, argv, ep) != 0) {
4557c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
4567c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 	} else 	if (strcmp(miscname, MD_MIRROR) == 0) {
4597c478bd9Sstevel@tonic-gate 		if (mirror_params(sp, np, argc, argv, ep) != 0) {
4607c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
4617c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 	} else 	if (strcmp(miscname, MD_RAID) == 0) {
4647c478bd9Sstevel@tonic-gate 		if (raid_params(sp, np, argc, argv, ep) != 0) {
4657c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
4667c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate 	} else {
4697c478bd9Sstevel@tonic-gate 		md_eprintf(gettext(
4707c478bd9Sstevel@tonic-gate 		    "%s: invalid metadevice type %s\n"),
4717c478bd9Sstevel@tonic-gate 		    np->cname, miscname);
4727c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/* return success */
4767c478bd9Sstevel@tonic-gate 	md_exit(sp, 0);
4777c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
4787c478bd9Sstevel@tonic-gate 	return (0);
4797c478bd9Sstevel@tonic-gate }
480