xref: /titanic_41/usr/src/cmd/lvm/util/metahs.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  * hotspare maintenance
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <meta.h>
337c478bd9Sstevel@tonic-gate #include <sdssc.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * possible actions
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate enum metahs_op {
397c478bd9Sstevel@tonic-gate 	NONE,
407c478bd9Sstevel@tonic-gate 	ADD_A_HS,
417c478bd9Sstevel@tonic-gate 	DELETE_A_HS,
427c478bd9Sstevel@tonic-gate 	ENABLE_A_HS,
437c478bd9Sstevel@tonic-gate 	REPLACE_A_HS,
447c478bd9Sstevel@tonic-gate 	STATUS_A_HSP
457c478bd9Sstevel@tonic-gate };
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * report status of a hotspare pool
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate static int
status_hsp(mdsetname_t * sp,mdhspname_t * hspnp,md_error_t * ep)517c478bd9Sstevel@tonic-gate status_hsp(
527c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp,
537c478bd9Sstevel@tonic-gate 	mdhspname_t	*hspnp,
547c478bd9Sstevel@tonic-gate 	md_error_t	*ep
557c478bd9Sstevel@tonic-gate )
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	mdprtopts_t	options = (PRINT_HEADER | PRINT_SUBDEVS | PRINT_DEVID);
587c478bd9Sstevel@tonic-gate 	mdnamelist_t	*nlp = NULL;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	/* must have set */
617c478bd9Sstevel@tonic-gate 	assert(sp != NULL);
62*d7cd8252Stw21770 	assert(hspnp->hsp == MD_HSP_NONE || sp->setno == HSP_SET(hspnp->hsp));
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/* print status */
657c478bd9Sstevel@tonic-gate 	if (meta_hsp_print(sp, hspnp, &nlp, NULL, stdout, options, ep) != 0)
667c478bd9Sstevel@tonic-gate 		return (-1);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/* return success */
697c478bd9Sstevel@tonic-gate 	return (0);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * print usage message
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate static void
usage(mdsetname_t * sp,int eval)767c478bd9Sstevel@tonic-gate usage(
777c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp,
787c478bd9Sstevel@tonic-gate 	int		eval
797c478bd9Sstevel@tonic-gate )
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\
827c478bd9Sstevel@tonic-gate usage:	%s [-s setname] -a hot_spare_pool [component...]\n\
837c478bd9Sstevel@tonic-gate 	%s [-s setname] -a \"all\" component...\n\
847c478bd9Sstevel@tonic-gate 	%s [-s setname] -d hot_spare_pool [component...]\n\
857c478bd9Sstevel@tonic-gate 	%s [-s setname] -d \"all\" component...\n\
867c478bd9Sstevel@tonic-gate 	%s [-s setname] -e component...\n\
877c478bd9Sstevel@tonic-gate 	%s [-s setname] -r hot_spare_pool component_old component_new\n\
887c478bd9Sstevel@tonic-gate 	%s [-s setname] -r \"all\" component_old component_new\n\
897c478bd9Sstevel@tonic-gate 	%s [-s setname] -i [hot_spare_pool...]\n"),
907c478bd9Sstevel@tonic-gate 	    myname, myname, myname, myname, myname, myname, myname, myname);
917c478bd9Sstevel@tonic-gate 	md_exit(sp, eval);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * parse args and add hotspares
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate static int
add_hotspares(mdsetname_t ** spp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)987c478bd9Sstevel@tonic-gate add_hotspares(
997c478bd9Sstevel@tonic-gate 	mdsetname_t	**spp,
1007c478bd9Sstevel@tonic-gate 	int		argc,
1017c478bd9Sstevel@tonic-gate 	char		*argv[],
1027c478bd9Sstevel@tonic-gate 	mdcmdopts_t	options,
1037c478bd9Sstevel@tonic-gate 	md_error_t	*ep
1047c478bd9Sstevel@tonic-gate )
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	mdhspnamelist_t	*hspnlp = NULL;
1077c478bd9Sstevel@tonic-gate 	mdnamelist_t	*nlp = NULL;
1087c478bd9Sstevel@tonic-gate 	int		cnt;
1097c478bd9Sstevel@tonic-gate 	mdhspnamelist_t	*p;
1107c478bd9Sstevel@tonic-gate 	int		rval = -1;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/* get hotspare pool name(s) */
1137c478bd9Sstevel@tonic-gate 	if (argc < 1)
1147c478bd9Sstevel@tonic-gate 		usage(*spp, 1);
115*d7cd8252Stw21770 	if ((argc > 1) && meta_is_all(argv[0])) {
1167c478bd9Sstevel@tonic-gate 		/* check for ownership */
1177c478bd9Sstevel@tonic-gate 		assert(*spp != NULL);
1187c478bd9Sstevel@tonic-gate 		if (meta_check_ownership(*spp, ep) != 0)
1197c478bd9Sstevel@tonic-gate 			return (-1);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		if ((cnt = meta_get_hsp_names(*spp, &hspnlp, 0, ep)) < 0) {
1227c478bd9Sstevel@tonic-gate 			return (-1);
1237c478bd9Sstevel@tonic-gate 		} else if (cnt == 0) {
1247c478bd9Sstevel@tonic-gate 			return (mderror(ep, MDE_NO_HSPS, NULL));
1257c478bd9Sstevel@tonic-gate 		}
126*d7cd8252Stw21770 	} else { /* create the hsp nmlist from the specified hsp name */
127*d7cd8252Stw21770 		if (!is_hspname(argv[0]))
128*d7cd8252Stw21770 			return (mderror(ep, MDE_NAME_ILLEGAL, argv[0]));
129*d7cd8252Stw21770 
130*d7cd8252Stw21770 		if ((cnt = metahspnamelist(spp, &hspnlp, 1, &argv[0], ep)) < 0)
1317c478bd9Sstevel@tonic-gate 			return (-1);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	assert(cnt > 0);
1347c478bd9Sstevel@tonic-gate 	--argc, ++argv;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	assert(*spp != NULL);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/* grab set lock */
1397c478bd9Sstevel@tonic-gate 	if (meta_lock(*spp, TRUE, ep))
1407c478bd9Sstevel@tonic-gate 		return (-1);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/* check for ownership */
1437c478bd9Sstevel@tonic-gate 	if (meta_check_ownership(*spp, ep) != 0)
1447c478bd9Sstevel@tonic-gate 		return (-1);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/* get hotspares */
147*d7cd8252Stw21770 	if (metanamelist(spp, &nlp, argc, argv,
148*d7cd8252Stw21770 	    LOGICAL_DEVICE, ep) < 0) {
1497c478bd9Sstevel@tonic-gate 		goto out;
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/* add hotspares */
1537c478bd9Sstevel@tonic-gate 	for (p = hspnlp; (p != NULL); p = p->next) {
1547c478bd9Sstevel@tonic-gate 		mdhspname_t	*hspnp = p->hspnamep;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		if (meta_hs_add(*spp, hspnp, nlp, options, ep) != 0)
1577c478bd9Sstevel@tonic-gate 			goto out;
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	rval = 0;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/* cleanup, return success */
1627c478bd9Sstevel@tonic-gate out:
1637c478bd9Sstevel@tonic-gate 	if (hspnlp != NULL)
1647c478bd9Sstevel@tonic-gate 		metafreehspnamelist(hspnlp);
1657c478bd9Sstevel@tonic-gate 	if (nlp != NULL)
1667c478bd9Sstevel@tonic-gate 		metafreenamelist(nlp);
1677c478bd9Sstevel@tonic-gate 	return (rval);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * parse args and delete hotspares
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate static int
delete_hotspares(mdsetname_t ** spp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)1747c478bd9Sstevel@tonic-gate delete_hotspares(
1757c478bd9Sstevel@tonic-gate 	mdsetname_t	**spp,
1767c478bd9Sstevel@tonic-gate 	int		argc,
1777c478bd9Sstevel@tonic-gate 	char		*argv[],
1787c478bd9Sstevel@tonic-gate 	mdcmdopts_t	options,
1797c478bd9Sstevel@tonic-gate 	md_error_t	*ep
1807c478bd9Sstevel@tonic-gate )
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	mdhspnamelist_t	*hspnlp = NULL;
1837c478bd9Sstevel@tonic-gate 	mdnamelist_t	*nlp = NULL;
1847c478bd9Sstevel@tonic-gate 	int		cnt;
1857c478bd9Sstevel@tonic-gate 	mdhspnamelist_t	*p;
1867c478bd9Sstevel@tonic-gate 	int		rval = -1;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/* get hotspare pool name(s) */
1897c478bd9Sstevel@tonic-gate 	if (argc < 1)
1907c478bd9Sstevel@tonic-gate 		usage(*spp, 1);
191*d7cd8252Stw21770 	if ((argc > 1) && meta_is_all(argv[0])) {
1927c478bd9Sstevel@tonic-gate 		/* check for ownership */
1937c478bd9Sstevel@tonic-gate 		assert(*spp != NULL);
1947c478bd9Sstevel@tonic-gate 		if (meta_check_ownership(*spp, ep) != 0)
1957c478bd9Sstevel@tonic-gate 			return (-1);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		if ((cnt = meta_get_hsp_names(*spp, &hspnlp, 0, ep)) < 0) {
1987c478bd9Sstevel@tonic-gate 			return (-1);
1997c478bd9Sstevel@tonic-gate 		} else if (cnt == 0) {
2007c478bd9Sstevel@tonic-gate 			return (mderror(ep, MDE_NO_HSPS, NULL));
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 	} else if ((cnt = metahspnamelist(spp, &hspnlp, 1, &argv[0],
2037c478bd9Sstevel@tonic-gate 	    ep)) < 0) {
2047c478bd9Sstevel@tonic-gate 		return (-1);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 	assert(cnt > 0);
2077c478bd9Sstevel@tonic-gate 	--argc, ++argv;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	assert(*spp != NULL);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/* grab set lock */
2127c478bd9Sstevel@tonic-gate 	if (meta_lock(*spp, TRUE, ep))
2137c478bd9Sstevel@tonic-gate 		return (-1);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/* check for ownership */
2167c478bd9Sstevel@tonic-gate 	if (meta_check_ownership(*spp, ep) != 0)
2177c478bd9Sstevel@tonic-gate 		return (-1);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/* get hotspares */
220*d7cd8252Stw21770 	if (metanamelist(spp, &nlp, argc, argv,
221*d7cd8252Stw21770 	    LOGICAL_DEVICE, ep) < 0) {
2227c478bd9Sstevel@tonic-gate 		goto out;
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/* delete hotspares */
2267c478bd9Sstevel@tonic-gate 	cnt = 0;
2277c478bd9Sstevel@tonic-gate 	for (p = hspnlp; (p != NULL); p = p->next) {
2287c478bd9Sstevel@tonic-gate 		mdhspname_t	*hspnp = p->hspnamep;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		if (meta_hs_delete(*spp, hspnp, nlp, options, ep) != 0) {
2317c478bd9Sstevel@tonic-gate 			if (mdisdeverror(ep, MDE_INVAL_HS))
2327c478bd9Sstevel@tonic-gate 				mdclrerror(ep);
2337c478bd9Sstevel@tonic-gate 			else
2347c478bd9Sstevel@tonic-gate 				goto out;
2357c478bd9Sstevel@tonic-gate 		} else {
2367c478bd9Sstevel@tonic-gate 			++cnt;
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/* make sure we got some */
2417c478bd9Sstevel@tonic-gate 	if ((nlp != NULL) && (cnt == 0)) {
2427c478bd9Sstevel@tonic-gate 		(void) mddeverror(ep, MDE_INVAL_HS, nlp->namep->dev,
2437c478bd9Sstevel@tonic-gate 		    nlp->namep->cname);
2447c478bd9Sstevel@tonic-gate 		goto out;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/* success */
2487c478bd9Sstevel@tonic-gate 	rval = 0;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/* cleanup, return success */
2517c478bd9Sstevel@tonic-gate out:
2527c478bd9Sstevel@tonic-gate 	if (hspnlp != NULL)
2537c478bd9Sstevel@tonic-gate 		metafreehspnamelist(hspnlp);
2547c478bd9Sstevel@tonic-gate 	if (nlp != NULL)
2557c478bd9Sstevel@tonic-gate 		metafreenamelist(nlp);
2567c478bd9Sstevel@tonic-gate 	return (rval);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  * parse args and enable hotspares
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate static int
enable_hotspares(mdsetname_t ** spp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)2637c478bd9Sstevel@tonic-gate enable_hotspares(
2647c478bd9Sstevel@tonic-gate 	mdsetname_t	**spp,
2657c478bd9Sstevel@tonic-gate 	int		argc,
2667c478bd9Sstevel@tonic-gate 	char		*argv[],
2677c478bd9Sstevel@tonic-gate 	mdcmdopts_t	options,
2687c478bd9Sstevel@tonic-gate 	md_error_t	*ep
2697c478bd9Sstevel@tonic-gate )
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	mdnamelist_t	*nlp = NULL;
2727c478bd9Sstevel@tonic-gate 	int		rval = -1;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/* enable hotspares */
2757c478bd9Sstevel@tonic-gate 	if (argc < 1)
2767c478bd9Sstevel@tonic-gate 		usage(*spp, 1);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/* get list of hotspares */
279*d7cd8252Stw21770 	if (metanamelist(spp, &nlp, argc, argv,
280*d7cd8252Stw21770 	    LOGICAL_DEVICE, ep) < 0)
2817c478bd9Sstevel@tonic-gate 		goto out;
2827c478bd9Sstevel@tonic-gate 	assert(nlp != NULL);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	assert(*spp != NULL);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/* grab set lock */
2877c478bd9Sstevel@tonic-gate 	if (meta_lock(*spp, TRUE, ep))
2887c478bd9Sstevel@tonic-gate 		return (-1);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/* check for ownership */
2917c478bd9Sstevel@tonic-gate 	if (meta_check_ownership(*spp, ep) != 0)
2927c478bd9Sstevel@tonic-gate 		return (-1);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	/* enable hotspares */
2957c478bd9Sstevel@tonic-gate 	rval = meta_hs_enable(*spp, nlp, options, ep);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/* cleanup, return success */
2987c478bd9Sstevel@tonic-gate out:
2997c478bd9Sstevel@tonic-gate 	metafreenamelist(nlp);
3007c478bd9Sstevel@tonic-gate 	return (rval);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * parse args and replace hotspares
3057c478bd9Sstevel@tonic-gate  */
3067c478bd9Sstevel@tonic-gate static int
replace_hotspares(mdsetname_t ** spp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)3077c478bd9Sstevel@tonic-gate replace_hotspares(
3087c478bd9Sstevel@tonic-gate 	mdsetname_t	**spp,
3097c478bd9Sstevel@tonic-gate 	int		argc,
3107c478bd9Sstevel@tonic-gate 	char		*argv[],
3117c478bd9Sstevel@tonic-gate 	mdcmdopts_t	options,
3127c478bd9Sstevel@tonic-gate 	md_error_t	*ep
3137c478bd9Sstevel@tonic-gate )
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	mdhspnamelist_t	*hspnlp = NULL;
3167c478bd9Sstevel@tonic-gate 	int		cnt;
3177c478bd9Sstevel@tonic-gate 	mdname_t	*oldnp;
3187c478bd9Sstevel@tonic-gate 	mdname_t	*newnp;
3197c478bd9Sstevel@tonic-gate 	mdhspnamelist_t	*p;
3207c478bd9Sstevel@tonic-gate 	int		rval = -1;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/* get hotspare pool name(s) */
3237c478bd9Sstevel@tonic-gate 	if (argc != 3)
3247c478bd9Sstevel@tonic-gate 		usage(*spp, 1);
325*d7cd8252Stw21770 	if (meta_is_all(argv[0])) {
3267c478bd9Sstevel@tonic-gate 		/* check for ownership */
3277c478bd9Sstevel@tonic-gate 		assert(*spp != NULL);
3287c478bd9Sstevel@tonic-gate 		if (meta_check_ownership(*spp, ep) != 0)
3297c478bd9Sstevel@tonic-gate 			return (-1);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		if ((cnt = meta_get_hsp_names(*spp, &hspnlp, 0, ep)) < 0) {
3327c478bd9Sstevel@tonic-gate 			return (-1);
3337c478bd9Sstevel@tonic-gate 		} else if (cnt == 0) {
3347c478bd9Sstevel@tonic-gate 			return (mderror(ep, MDE_NO_HSPS, NULL));
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 	} else if ((cnt = metahspnamelist(spp, &hspnlp, 1, &argv[0],
3377c478bd9Sstevel@tonic-gate 	    ep)) < 0) {
3387c478bd9Sstevel@tonic-gate 		return (-1);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 	assert(cnt > 0);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	assert(*spp != NULL);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/* grab set lock */
3457c478bd9Sstevel@tonic-gate 	if (meta_lock(*spp, TRUE, ep))
3467c478bd9Sstevel@tonic-gate 		return (-1);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/* check for ownership */
3497c478bd9Sstevel@tonic-gate 	if (meta_check_ownership(*spp, ep) != 0)
3507c478bd9Sstevel@tonic-gate 		return (-1);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	/* get old component */
353*d7cd8252Stw21770 	if ((oldnp = metaname(spp, argv[1], LOGICAL_DEVICE, ep)) == NULL)
3547c478bd9Sstevel@tonic-gate 		goto out;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/* get new component */
357*d7cd8252Stw21770 	if ((newnp = metaname(spp, argv[2], LOGICAL_DEVICE, ep)) == NULL)
3587c478bd9Sstevel@tonic-gate 		goto out;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/* replace hotspares */
3617c478bd9Sstevel@tonic-gate 	cnt = 0;
3627c478bd9Sstevel@tonic-gate 	for (p = hspnlp; (p != NULL); p = p->next) {
3637c478bd9Sstevel@tonic-gate 		mdhspname_t	*hspnp = p->hspnamep;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		if (meta_hs_replace(*spp, hspnp, oldnp, newnp, options, ep)
3667c478bd9Sstevel@tonic-gate 		    != 0) {
3677c478bd9Sstevel@tonic-gate 			if (mdisdeverror(ep, MDE_INVAL_HS))
3687c478bd9Sstevel@tonic-gate 				mdclrerror(ep);
3697c478bd9Sstevel@tonic-gate 			else
3707c478bd9Sstevel@tonic-gate 				goto out;
3717c478bd9Sstevel@tonic-gate 		} else {
3727c478bd9Sstevel@tonic-gate 			++cnt;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/* make sure we got some */
3777c478bd9Sstevel@tonic-gate 	if (cnt == 0) {
3787c478bd9Sstevel@tonic-gate 		(void) mddeverror(ep, MDE_INVAL_HS, oldnp->dev, oldnp->cname);
3797c478bd9Sstevel@tonic-gate 		goto out;
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/* success */
3837c478bd9Sstevel@tonic-gate 	rval = 0;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/* cleanup, return success */
3867c478bd9Sstevel@tonic-gate out:
3877c478bd9Sstevel@tonic-gate 	if (hspnlp != NULL)
3887c478bd9Sstevel@tonic-gate 		metafreehspnamelist(hspnlp);
3897c478bd9Sstevel@tonic-gate 	return (rval);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate /*
3937c478bd9Sstevel@tonic-gate  * print_hsp_devid will collect the information for each underlying
3947c478bd9Sstevel@tonic-gate  * physical device for all the hotspare pools and print out the
3957c478bd9Sstevel@tonic-gate  * device relocation information
3967c478bd9Sstevel@tonic-gate  * INPUT:
3977c478bd9Sstevel@tonic-gate  *	mdsetname_t *sp			set the hsp is in
3987c478bd9Sstevel@tonic-gate  *	mdhspnamelist_t *hspnlp		list of hsp
3997c478bd9Sstevel@tonic-gate  *	FILE	*fp			where to print to
4007c478bd9Sstevel@tonic-gate  *	md_error_t	*ep		errors
4017c478bd9Sstevel@tonic-gate  * RETURN:
4027c478bd9Sstevel@tonic-gate  *	0 	SUCCESS
4037c478bd9Sstevel@tonic-gate  *	-1	ERROR
4047c478bd9Sstevel@tonic-gate  */
4057c478bd9Sstevel@tonic-gate static int
print_hsp_devid(mdsetname_t * sp,mdhspnamelist_t * hspnlp,FILE * fp,md_error_t * ep)4067c478bd9Sstevel@tonic-gate print_hsp_devid(
4077c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp,
4087c478bd9Sstevel@tonic-gate 	mdhspnamelist_t *hspnlp,
4097c478bd9Sstevel@tonic-gate 	FILE		*fp,
4107c478bd9Sstevel@tonic-gate 	md_error_t	*ep
4117c478bd9Sstevel@tonic-gate )
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	mddevid_t	*ldevidp = NULL;
4147c478bd9Sstevel@tonic-gate 	int		retval = 0;
4157c478bd9Sstevel@tonic-gate 	mdhspnamelist_t	*p;
4167c478bd9Sstevel@tonic-gate 	mddevid_t	*nextp;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/* for all hotspare pools */
4197c478bd9Sstevel@tonic-gate 	for (p = hspnlp; (p != NULL); p = p->next) {
4207c478bd9Sstevel@tonic-gate 		mdhspname_t	*hspnp = p->hspnamep;
4217c478bd9Sstevel@tonic-gate 		uint_t		hsi;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		/* for all hotspares within a pool */
4247c478bd9Sstevel@tonic-gate 		for (hsi = 0;
4257c478bd9Sstevel@tonic-gate 		    hsi < hspnp->unitp->hotspares.hotspares_len; hsi++) {
4267c478bd9Sstevel@tonic-gate 			mdname_t	*hsname;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 			hsname =
4297c478bd9Sstevel@tonic-gate 			    hspnp->unitp->hotspares.hotspares_val[hsi].hsnamep;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 			meta_create_non_dup_list(hsname, &ldevidp);
4327c478bd9Sstevel@tonic-gate 		}
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	retval = meta_print_devid(sp, fp, ldevidp, ep);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/* cleanup */
4387c478bd9Sstevel@tonic-gate 	for (nextp = ldevidp; nextp != NULL; ldevidp = nextp) {
4397c478bd9Sstevel@tonic-gate 		Free(ldevidp->ctdname);
4407c478bd9Sstevel@tonic-gate 		nextp = ldevidp->next;
4417c478bd9Sstevel@tonic-gate 		Free(ldevidp);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 	return (retval);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate  * parse args and status hotspares
4487c478bd9Sstevel@tonic-gate  */
4497c478bd9Sstevel@tonic-gate static int
status_hotspares(mdsetname_t ** spp,int argc,char * argv[],md_error_t * ep)4507c478bd9Sstevel@tonic-gate status_hotspares(
4517c478bd9Sstevel@tonic-gate 	mdsetname_t	**spp,
4527c478bd9Sstevel@tonic-gate 	int		argc,
4537c478bd9Sstevel@tonic-gate 	char		*argv[],
4547c478bd9Sstevel@tonic-gate 	md_error_t	*ep
4557c478bd9Sstevel@tonic-gate )
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate 	mdhspnamelist_t	*hspnlp = NULL;
4587c478bd9Sstevel@tonic-gate 	int		cnt;
4597c478bd9Sstevel@tonic-gate 	mdhspnamelist_t	*p;
4607c478bd9Sstevel@tonic-gate 	int		rval = -1;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/* get hotspare pool name(s) */
4637c478bd9Sstevel@tonic-gate 	if (argc == 0) {
4647c478bd9Sstevel@tonic-gate 		/* check for ownership */
4657c478bd9Sstevel@tonic-gate 		assert(*spp != NULL);
4667c478bd9Sstevel@tonic-gate 		if (meta_check_ownership(*spp, ep) != 0)
4677c478bd9Sstevel@tonic-gate 			return (-1);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		if ((cnt = meta_get_hsp_names(*spp, &hspnlp, 0, ep)) < 0) {
4707c478bd9Sstevel@tonic-gate 			return (-1);
4717c478bd9Sstevel@tonic-gate 		} else if (cnt == 0) {
4727c478bd9Sstevel@tonic-gate 			return (mderror(ep, MDE_NO_HSPS, NULL));
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 	} else if ((cnt = metahspnamelist(spp, &hspnlp, argc, argv, ep)) < 0) {
4757c478bd9Sstevel@tonic-gate 		return (-1);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 	assert(cnt > 0);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/* check for ownership */
4807c478bd9Sstevel@tonic-gate 	assert(*spp != NULL);
4817c478bd9Sstevel@tonic-gate 	if (meta_check_ownership(*spp, ep) != 0)
4827c478bd9Sstevel@tonic-gate 		return (-1);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/* status hotspare pools */
4857c478bd9Sstevel@tonic-gate 	for (p = hspnlp; (p != NULL); p = p->next) {
4867c478bd9Sstevel@tonic-gate 		mdhspname_t	*hspnp = p->hspnamep;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		if (status_hsp(*spp, hspnp, ep) != 0)
4897c478bd9Sstevel@tonic-gate 			goto out;
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if (print_hsp_devid(*spp, hspnlp, stdout, ep) == 0) {
4937c478bd9Sstevel@tonic-gate 		rval = 0;
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/* cleanup, return success */
4977c478bd9Sstevel@tonic-gate out:
4987c478bd9Sstevel@tonic-gate 	if (hspnlp != NULL)
4997c478bd9Sstevel@tonic-gate 		metafreehspnamelist(hspnlp);
5007c478bd9Sstevel@tonic-gate 	return (rval);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * parse args and doit
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)5077c478bd9Sstevel@tonic-gate main(
5087c478bd9Sstevel@tonic-gate 	int		argc,
5097c478bd9Sstevel@tonic-gate 	char		**argv
5107c478bd9Sstevel@tonic-gate )
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
5137c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
5147c478bd9Sstevel@tonic-gate 	enum metahs_op	which_op = NONE;
5157c478bd9Sstevel@tonic-gate 	mdcmdopts_t	options = (MDCMD_PRINT | MDCMD_DOIT);
5167c478bd9Sstevel@tonic-gate 	int		c;
5177c478bd9Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
5187c478bd9Sstevel@tonic-gate 	md_error_t	*ep = &status;
5197c478bd9Sstevel@tonic-gate 	int		error;
5207c478bd9Sstevel@tonic-gate 	bool_t		called_thru_rpc = FALSE;
5217c478bd9Sstevel@tonic-gate 	char		*cp;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/*
5247c478bd9Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
5257c478bd9Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
5267c478bd9Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
5277c478bd9Sstevel@tonic-gate 	 * something.
5287c478bd9Sstevel@tonic-gate 	 */
5297c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
5307c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
5317c478bd9Sstevel@tonic-gate #endif
5327c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
5337c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
5377c478bd9Sstevel@tonic-gate 		if (sdssc_bind_library() == SDSSC_OKAY)
5387c478bd9Sstevel@tonic-gate 			if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
5397c478bd9Sstevel@tonic-gate 						&error) == SDSSC_PROXY_DONE)
5407c478bd9Sstevel@tonic-gate 				exit(error);
5417c478bd9Sstevel@tonic-gate 	} else {
5427c478bd9Sstevel@tonic-gate 		*cp = '\0'; /* cut off ".rpc_call" */
5437c478bd9Sstevel@tonic-gate 		called_thru_rpc = TRUE;
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	/* initialize */
5477c478bd9Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0) {
5487c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
5497c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/* parse args */
5537c478bd9Sstevel@tonic-gate 	optind = 1;
5547c478bd9Sstevel@tonic-gate 	opterr = 1;
5557c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "hs:aderin?")) != -1) {
5567c478bd9Sstevel@tonic-gate 		switch (c) {
5577c478bd9Sstevel@tonic-gate 		case 'h':
5587c478bd9Sstevel@tonic-gate 			usage(sp, 0);
5597c478bd9Sstevel@tonic-gate 			break;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		case 's':
5627c478bd9Sstevel@tonic-gate 			sname = optarg;
5637c478bd9Sstevel@tonic-gate 			break;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		case 'a':
5667c478bd9Sstevel@tonic-gate 			if (which_op != NONE)
5677c478bd9Sstevel@tonic-gate 				usage(sp, 1);
5687c478bd9Sstevel@tonic-gate 			which_op = ADD_A_HS;
5697c478bd9Sstevel@tonic-gate 			break;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		case 'd':
5727c478bd9Sstevel@tonic-gate 			if (which_op != NONE)
5737c478bd9Sstevel@tonic-gate 				usage(sp, 1);
5747c478bd9Sstevel@tonic-gate 			which_op = DELETE_A_HS;
5757c478bd9Sstevel@tonic-gate 			break;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		case 'e':
5787c478bd9Sstevel@tonic-gate 			if (which_op != NONE)
5797c478bd9Sstevel@tonic-gate 				usage(sp, 1);
5807c478bd9Sstevel@tonic-gate 			which_op = ENABLE_A_HS;
5817c478bd9Sstevel@tonic-gate 			break;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		case 'r':
5847c478bd9Sstevel@tonic-gate 			if (which_op != NONE)
5857c478bd9Sstevel@tonic-gate 				usage(sp, 1);
5867c478bd9Sstevel@tonic-gate 			which_op = REPLACE_A_HS;
5877c478bd9Sstevel@tonic-gate 			break;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		case 'i':
5907c478bd9Sstevel@tonic-gate 			if (which_op != NONE)
5917c478bd9Sstevel@tonic-gate 				usage(sp, 1);
5927c478bd9Sstevel@tonic-gate 			which_op = STATUS_A_HSP;
5937c478bd9Sstevel@tonic-gate 			break;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		case 'n':
5967c478bd9Sstevel@tonic-gate 			if (called_thru_rpc == TRUE) {
5977c478bd9Sstevel@tonic-gate 				options &= ~MDCMD_DOIT;
5987c478bd9Sstevel@tonic-gate 			} else {
5997c478bd9Sstevel@tonic-gate 				usage(sp, 1);
6007c478bd9Sstevel@tonic-gate 			}
6017c478bd9Sstevel@tonic-gate 			break;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		case '?':
6057c478bd9Sstevel@tonic-gate 			if (optopt == '?')
6067c478bd9Sstevel@tonic-gate 				usage(sp, 0);
6077c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
6087c478bd9Sstevel@tonic-gate 		default:
6097c478bd9Sstevel@tonic-gate 			usage(sp, 1);
6107c478bd9Sstevel@tonic-gate 			break;
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	/* get set context */
6157c478bd9Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
6167c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
6177c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/*
6217c478bd9Sstevel@tonic-gate 	 * Send the command to all nodes if the -s argument refers to a MN
6227c478bd9Sstevel@tonic-gate 	 * set or the next argument refers to MN set hotspare name ( argc
6237c478bd9Sstevel@tonic-gate 	 * greater than optind if there is a next argument)
6247c478bd9Sstevel@tonic-gate 	 */
6257c478bd9Sstevel@tonic-gate 	if ((called_thru_rpc == FALSE) &&
6267c478bd9Sstevel@tonic-gate 	    (meta_is_mn_set(sp, ep) || ((argc > optind) &&
6277c478bd9Sstevel@tonic-gate 	    meta_is_mn_name(&sp, argv[optind], ep)))) {
6287c478bd9Sstevel@tonic-gate 		int	i;
6297c478bd9Sstevel@tonic-gate 		int	newargc;
6307c478bd9Sstevel@tonic-gate 		int	result;
6317c478bd9Sstevel@tonic-gate 		char	**newargv;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 		/*
6347c478bd9Sstevel@tonic-gate 		 * If we are dealing with a MN set and we were not
6357c478bd9Sstevel@tonic-gate 		 * called thru an rpc call, we are just to send this
6367c478bd9Sstevel@tonic-gate 		 * command string to the master of the set and let it
6377c478bd9Sstevel@tonic-gate 		 * deal with it.
6387c478bd9Sstevel@tonic-gate 		 * First we send out a dryrun version of this command.
6397c478bd9Sstevel@tonic-gate 		 * If that returns success, we know it succeeded on all
6407c478bd9Sstevel@tonic-gate 		 * nodes and it is safe to do the real command now.
6417c478bd9Sstevel@tonic-gate 		 */
6427c478bd9Sstevel@tonic-gate 		newargv = calloc(argc+1, sizeof (char *));
6437c478bd9Sstevel@tonic-gate 		newargv[0] = "metahs";
6447c478bd9Sstevel@tonic-gate 		newargv[1] = "-n"; /* always do "-n" first */
6457c478bd9Sstevel@tonic-gate 		newargc = 2;
6467c478bd9Sstevel@tonic-gate 		for (i = 1; i < argc; i++, newargc++)
6477c478bd9Sstevel@tonic-gate 			newargv[newargc] = argv[i];
6487c478bd9Sstevel@tonic-gate 		result = meta_mn_send_command(sp, newargc, newargv,
6497c478bd9Sstevel@tonic-gate 		    MD_DISP_STDERR | MD_DRYRUN, NO_CONTEXT_STRING, ep);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 		/* If we found a problem don't do it for real */
6527c478bd9Sstevel@tonic-gate 		if (result != 0) {
6537c478bd9Sstevel@tonic-gate 			md_exit(sp, result);
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		/*
6577c478bd9Sstevel@tonic-gate 		 * Do it for real now. Remove "-n" from the arguments and
6587c478bd9Sstevel@tonic-gate 		 * MD_DRYRUN from the flags. If this fails the master must panic
6597c478bd9Sstevel@tonic-gate 		 * as the mddbs may be inconsistent.
6607c478bd9Sstevel@tonic-gate 		 */
6617c478bd9Sstevel@tonic-gate 		newargv[1] = ""; /* this was "-n" before */
6627c478bd9Sstevel@tonic-gate 		result = meta_mn_send_command(sp, newargc, newargv,
6637c478bd9Sstevel@tonic-gate 		    MD_DISP_STDERR | MD_RETRY_BUSY | MD_PANIC_WHEN_INCONSISTENT,
6647c478bd9Sstevel@tonic-gate 		    NO_CONTEXT_STRING, ep);
6657c478bd9Sstevel@tonic-gate 		free(newargv);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		/* No further action required */
6687c478bd9Sstevel@tonic-gate 		md_exit(sp, result);
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	argc -= optind;
6727c478bd9Sstevel@tonic-gate 	argv += optind;
6737c478bd9Sstevel@tonic-gate 	if (which_op == NONE)
6747c478bd9Sstevel@tonic-gate 		usage(sp, 1);
6757c478bd9Sstevel@tonic-gate 
676*d7cd8252Stw21770 	/*
677*d7cd8252Stw21770 	 * if a hot spare pool was specified by name then
678*d7cd8252Stw21770 	 * get the canonical form of the name and set up
679*d7cd8252Stw21770 	 * sp if the name was specified in the form 'set/hsp'
680*d7cd8252Stw21770 	 * unless 'all' is specified or the request is made to
681*d7cd8252Stw21770 	 * enable a hs which means that argv[0] will be a component
682*d7cd8252Stw21770 	 */
683*d7cd8252Stw21770 	if (argc > 0 && !meta_is_all(argv[0]) && which_op != ENABLE_A_HS) {
684*d7cd8252Stw21770 		char *cname = NULL;
685*d7cd8252Stw21770 
686*d7cd8252Stw21770 		cname = meta_name_getname(&sp, argv[0], HSP_DEVICE, ep);
687*d7cd8252Stw21770 		if (cname == NULL) {
688*d7cd8252Stw21770 			mde_perror(ep, "");
689*d7cd8252Stw21770 			md_exit(sp, 1);
690*d7cd8252Stw21770 		}
691*d7cd8252Stw21770 		Free(cname);
692*d7cd8252Stw21770 	}
693*d7cd8252Stw21770 
6947c478bd9Sstevel@tonic-gate 	if (which_op == STATUS_A_HSP) {
6957c478bd9Sstevel@tonic-gate 		if (status_hotspares(&sp, argc, argv, ep) != 0) {
6967c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
6977c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 		md_exit(sp, 0);
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (meta_check_root(ep) != 0) {
7037c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
7047c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	/* dispatch */
7097c478bd9Sstevel@tonic-gate 	switch (which_op) {
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	case ADD_A_HS:
7127c478bd9Sstevel@tonic-gate 		if (add_hotspares(&sp, argc, argv, options, ep) != 0) {
7137c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
7147c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 		break;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	case DELETE_A_HS:
7197c478bd9Sstevel@tonic-gate 		if (delete_hotspares(&sp, argc, argv, options, ep) != 0) {
7207c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
7217c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 		break;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	case ENABLE_A_HS:
7267c478bd9Sstevel@tonic-gate 		if (enable_hotspares(&sp, argc, argv, options, ep) != 0) {
7277c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
7287c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
7297c478bd9Sstevel@tonic-gate 		}
7307c478bd9Sstevel@tonic-gate 		break;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	case REPLACE_A_HS:
7337c478bd9Sstevel@tonic-gate 		if (replace_hotspares(&sp, argc, argv, options, ep) != 0) {
7347c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
7357c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 		break;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	default:
7407c478bd9Sstevel@tonic-gate 		assert(0);
7417c478bd9Sstevel@tonic-gate 		break;
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	/* update md.cf */
7457c478bd9Sstevel@tonic-gate out:
7467c478bd9Sstevel@tonic-gate 	if (meta_update_md_cf(sp, ep) != 0) {
7477c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
7487c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 	md_exit(sp, 0);
7517c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
7527c478bd9Sstevel@tonic-gate 	return (0);
7537c478bd9Sstevel@tonic-gate }
754