xref: /onnv-gate/usr/src/cmd/lvm/util/metahs.c (revision 1623:7bac4a816ebe)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1623Stw21770  * Common Development and Distribution License (the "License").
6*1623Stw21770  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1623Stw21770  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * hotspare maintenance
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <meta.h>
330Sstevel@tonic-gate #include <sdssc.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate  * possible actions
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate enum metahs_op {
390Sstevel@tonic-gate 	NONE,
400Sstevel@tonic-gate 	ADD_A_HS,
410Sstevel@tonic-gate 	DELETE_A_HS,
420Sstevel@tonic-gate 	ENABLE_A_HS,
430Sstevel@tonic-gate 	REPLACE_A_HS,
440Sstevel@tonic-gate 	STATUS_A_HSP
450Sstevel@tonic-gate };
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * report status of a hotspare pool
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate static int
status_hsp(mdsetname_t * sp,mdhspname_t * hspnp,md_error_t * ep)510Sstevel@tonic-gate status_hsp(
520Sstevel@tonic-gate 	mdsetname_t	*sp,
530Sstevel@tonic-gate 	mdhspname_t	*hspnp,
540Sstevel@tonic-gate 	md_error_t	*ep
550Sstevel@tonic-gate )
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	mdprtopts_t	options = (PRINT_HEADER | PRINT_SUBDEVS | PRINT_DEVID);
580Sstevel@tonic-gate 	mdnamelist_t	*nlp = NULL;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	/* must have set */
610Sstevel@tonic-gate 	assert(sp != NULL);
62*1623Stw21770 	assert(hspnp->hsp == MD_HSP_NONE || sp->setno == HSP_SET(hspnp->hsp));
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	/* print status */
650Sstevel@tonic-gate 	if (meta_hsp_print(sp, hspnp, &nlp, NULL, stdout, options, ep) != 0)
660Sstevel@tonic-gate 		return (-1);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	/* return success */
690Sstevel@tonic-gate 	return (0);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * print usage message
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate static void
usage(mdsetname_t * sp,int eval)760Sstevel@tonic-gate usage(
770Sstevel@tonic-gate 	mdsetname_t	*sp,
780Sstevel@tonic-gate 	int		eval
790Sstevel@tonic-gate )
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\
820Sstevel@tonic-gate usage:	%s [-s setname] -a hot_spare_pool [component...]\n\
830Sstevel@tonic-gate 	%s [-s setname] -a \"all\" component...\n\
840Sstevel@tonic-gate 	%s [-s setname] -d hot_spare_pool [component...]\n\
850Sstevel@tonic-gate 	%s [-s setname] -d \"all\" component...\n\
860Sstevel@tonic-gate 	%s [-s setname] -e component...\n\
870Sstevel@tonic-gate 	%s [-s setname] -r hot_spare_pool component_old component_new\n\
880Sstevel@tonic-gate 	%s [-s setname] -r \"all\" component_old component_new\n\
890Sstevel@tonic-gate 	%s [-s setname] -i [hot_spare_pool...]\n"),
900Sstevel@tonic-gate 	    myname, myname, myname, myname, myname, myname, myname, myname);
910Sstevel@tonic-gate 	md_exit(sp, eval);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * parse args and add hotspares
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate static int
add_hotspares(mdsetname_t ** spp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)980Sstevel@tonic-gate add_hotspares(
990Sstevel@tonic-gate 	mdsetname_t	**spp,
1000Sstevel@tonic-gate 	int		argc,
1010Sstevel@tonic-gate 	char		*argv[],
1020Sstevel@tonic-gate 	mdcmdopts_t	options,
1030Sstevel@tonic-gate 	md_error_t	*ep
1040Sstevel@tonic-gate )
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	mdhspnamelist_t	*hspnlp = NULL;
1070Sstevel@tonic-gate 	mdnamelist_t	*nlp = NULL;
1080Sstevel@tonic-gate 	int		cnt;
1090Sstevel@tonic-gate 	mdhspnamelist_t	*p;
1100Sstevel@tonic-gate 	int		rval = -1;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	/* get hotspare pool name(s) */
1130Sstevel@tonic-gate 	if (argc < 1)
1140Sstevel@tonic-gate 		usage(*spp, 1);
115*1623Stw21770 	if ((argc > 1) && meta_is_all(argv[0])) {
1160Sstevel@tonic-gate 		/* check for ownership */
1170Sstevel@tonic-gate 		assert(*spp != NULL);
1180Sstevel@tonic-gate 		if (meta_check_ownership(*spp, ep) != 0)
1190Sstevel@tonic-gate 			return (-1);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 		if ((cnt = meta_get_hsp_names(*spp, &hspnlp, 0, ep)) < 0) {
1220Sstevel@tonic-gate 			return (-1);
1230Sstevel@tonic-gate 		} else if (cnt == 0) {
1240Sstevel@tonic-gate 			return (mderror(ep, MDE_NO_HSPS, NULL));
1250Sstevel@tonic-gate 		}
126*1623Stw21770 	} else { /* create the hsp nmlist from the specified hsp name */
127*1623Stw21770 		if (!is_hspname(argv[0]))
128*1623Stw21770 			return (mderror(ep, MDE_NAME_ILLEGAL, argv[0]));
129*1623Stw21770 
130*1623Stw21770 		if ((cnt = metahspnamelist(spp, &hspnlp, 1, &argv[0], ep)) < 0)
131*1623Stw21770 			return (-1);
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	assert(cnt > 0);
1340Sstevel@tonic-gate 	--argc, ++argv;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	assert(*spp != NULL);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	/* grab set lock */
1390Sstevel@tonic-gate 	if (meta_lock(*spp, TRUE, ep))
1400Sstevel@tonic-gate 		return (-1);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/* check for ownership */
1430Sstevel@tonic-gate 	if (meta_check_ownership(*spp, ep) != 0)
1440Sstevel@tonic-gate 		return (-1);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	/* get hotspares */
147*1623Stw21770 	if (metanamelist(spp, &nlp, argc, argv,
148*1623Stw21770 	    LOGICAL_DEVICE, ep) < 0) {
1490Sstevel@tonic-gate 		goto out;
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/* add hotspares */
1530Sstevel@tonic-gate 	for (p = hspnlp; (p != NULL); p = p->next) {
1540Sstevel@tonic-gate 		mdhspname_t	*hspnp = p->hspnamep;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 		if (meta_hs_add(*spp, hspnp, nlp, options, ep) != 0)
1570Sstevel@tonic-gate 			goto out;
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 	rval = 0;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/* cleanup, return success */
1620Sstevel@tonic-gate out:
1630Sstevel@tonic-gate 	if (hspnlp != NULL)
1640Sstevel@tonic-gate 		metafreehspnamelist(hspnlp);
1650Sstevel@tonic-gate 	if (nlp != NULL)
1660Sstevel@tonic-gate 		metafreenamelist(nlp);
1670Sstevel@tonic-gate 	return (rval);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * parse args and delete hotspares
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate static int
delete_hotspares(mdsetname_t ** spp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)1740Sstevel@tonic-gate delete_hotspares(
1750Sstevel@tonic-gate 	mdsetname_t	**spp,
1760Sstevel@tonic-gate 	int		argc,
1770Sstevel@tonic-gate 	char		*argv[],
1780Sstevel@tonic-gate 	mdcmdopts_t	options,
1790Sstevel@tonic-gate 	md_error_t	*ep
1800Sstevel@tonic-gate )
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	mdhspnamelist_t	*hspnlp = NULL;
1830Sstevel@tonic-gate 	mdnamelist_t	*nlp = NULL;
1840Sstevel@tonic-gate 	int		cnt;
1850Sstevel@tonic-gate 	mdhspnamelist_t	*p;
1860Sstevel@tonic-gate 	int		rval = -1;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/* get hotspare pool name(s) */
1890Sstevel@tonic-gate 	if (argc < 1)
1900Sstevel@tonic-gate 		usage(*spp, 1);
191*1623Stw21770 	if ((argc > 1) && meta_is_all(argv[0])) {
1920Sstevel@tonic-gate 		/* check for ownership */
1930Sstevel@tonic-gate 		assert(*spp != NULL);
1940Sstevel@tonic-gate 		if (meta_check_ownership(*spp, ep) != 0)
1950Sstevel@tonic-gate 			return (-1);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		if ((cnt = meta_get_hsp_names(*spp, &hspnlp, 0, ep)) < 0) {
1980Sstevel@tonic-gate 			return (-1);
1990Sstevel@tonic-gate 		} else if (cnt == 0) {
2000Sstevel@tonic-gate 			return (mderror(ep, MDE_NO_HSPS, NULL));
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 	} else if ((cnt = metahspnamelist(spp, &hspnlp, 1, &argv[0],
2030Sstevel@tonic-gate 	    ep)) < 0) {
2040Sstevel@tonic-gate 		return (-1);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 	assert(cnt > 0);
2070Sstevel@tonic-gate 	--argc, ++argv;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	assert(*spp != NULL);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/* grab set lock */
2120Sstevel@tonic-gate 	if (meta_lock(*spp, TRUE, ep))
2130Sstevel@tonic-gate 		return (-1);
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	/* check for ownership */
2160Sstevel@tonic-gate 	if (meta_check_ownership(*spp, ep) != 0)
2170Sstevel@tonic-gate 		return (-1);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	/* get hotspares */
220*1623Stw21770 	if (metanamelist(spp, &nlp, argc, argv,
221*1623Stw21770 	    LOGICAL_DEVICE, ep) < 0) {
2220Sstevel@tonic-gate 		goto out;
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/* delete hotspares */
2260Sstevel@tonic-gate 	cnt = 0;
2270Sstevel@tonic-gate 	for (p = hspnlp; (p != NULL); p = p->next) {
2280Sstevel@tonic-gate 		mdhspname_t	*hspnp = p->hspnamep;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		if (meta_hs_delete(*spp, hspnp, nlp, options, ep) != 0) {
2310Sstevel@tonic-gate 			if (mdisdeverror(ep, MDE_INVAL_HS))
2320Sstevel@tonic-gate 				mdclrerror(ep);
2330Sstevel@tonic-gate 			else
2340Sstevel@tonic-gate 				goto out;
2350Sstevel@tonic-gate 		} else {
2360Sstevel@tonic-gate 			++cnt;
2370Sstevel@tonic-gate 		}
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/* make sure we got some */
2410Sstevel@tonic-gate 	if ((nlp != NULL) && (cnt == 0)) {
2420Sstevel@tonic-gate 		(void) mddeverror(ep, MDE_INVAL_HS, nlp->namep->dev,
2430Sstevel@tonic-gate 		    nlp->namep->cname);
2440Sstevel@tonic-gate 		goto out;
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	/* success */
2480Sstevel@tonic-gate 	rval = 0;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	/* cleanup, return success */
2510Sstevel@tonic-gate out:
2520Sstevel@tonic-gate 	if (hspnlp != NULL)
2530Sstevel@tonic-gate 		metafreehspnamelist(hspnlp);
2540Sstevel@tonic-gate 	if (nlp != NULL)
2550Sstevel@tonic-gate 		metafreenamelist(nlp);
2560Sstevel@tonic-gate 	return (rval);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate  * parse args and enable hotspares
2610Sstevel@tonic-gate  */
2620Sstevel@tonic-gate static int
enable_hotspares(mdsetname_t ** spp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)2630Sstevel@tonic-gate enable_hotspares(
2640Sstevel@tonic-gate 	mdsetname_t	**spp,
2650Sstevel@tonic-gate 	int		argc,
2660Sstevel@tonic-gate 	char		*argv[],
2670Sstevel@tonic-gate 	mdcmdopts_t	options,
2680Sstevel@tonic-gate 	md_error_t	*ep
2690Sstevel@tonic-gate )
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	mdnamelist_t	*nlp = NULL;
2720Sstevel@tonic-gate 	int		rval = -1;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/* enable hotspares */
2750Sstevel@tonic-gate 	if (argc < 1)
2760Sstevel@tonic-gate 		usage(*spp, 1);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/* get list of hotspares */
279*1623Stw21770 	if (metanamelist(spp, &nlp, argc, argv,
280*1623Stw21770 	    LOGICAL_DEVICE, ep) < 0)
2810Sstevel@tonic-gate 		goto out;
2820Sstevel@tonic-gate 	assert(nlp != NULL);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	assert(*spp != NULL);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/* grab set lock */
2870Sstevel@tonic-gate 	if (meta_lock(*spp, TRUE, ep))
2880Sstevel@tonic-gate 		return (-1);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/* check for ownership */
2910Sstevel@tonic-gate 	if (meta_check_ownership(*spp, ep) != 0)
2920Sstevel@tonic-gate 		return (-1);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/* enable hotspares */
2950Sstevel@tonic-gate 	rval = meta_hs_enable(*spp, nlp, options, ep);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/* cleanup, return success */
2980Sstevel@tonic-gate out:
2990Sstevel@tonic-gate 	metafreenamelist(nlp);
3000Sstevel@tonic-gate 	return (rval);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * parse args and replace hotspares
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate static int
replace_hotspares(mdsetname_t ** spp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)3070Sstevel@tonic-gate replace_hotspares(
3080Sstevel@tonic-gate 	mdsetname_t	**spp,
3090Sstevel@tonic-gate 	int		argc,
3100Sstevel@tonic-gate 	char		*argv[],
3110Sstevel@tonic-gate 	mdcmdopts_t	options,
3120Sstevel@tonic-gate 	md_error_t	*ep
3130Sstevel@tonic-gate )
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	mdhspnamelist_t	*hspnlp = NULL;
3160Sstevel@tonic-gate 	int		cnt;
3170Sstevel@tonic-gate 	mdname_t	*oldnp;
3180Sstevel@tonic-gate 	mdname_t	*newnp;
3190Sstevel@tonic-gate 	mdhspnamelist_t	*p;
3200Sstevel@tonic-gate 	int		rval = -1;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/* get hotspare pool name(s) */
3230Sstevel@tonic-gate 	if (argc != 3)
3240Sstevel@tonic-gate 		usage(*spp, 1);
325*1623Stw21770 	if (meta_is_all(argv[0])) {
3260Sstevel@tonic-gate 		/* check for ownership */
3270Sstevel@tonic-gate 		assert(*spp != NULL);
3280Sstevel@tonic-gate 		if (meta_check_ownership(*spp, ep) != 0)
3290Sstevel@tonic-gate 			return (-1);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		if ((cnt = meta_get_hsp_names(*spp, &hspnlp, 0, ep)) < 0) {
3320Sstevel@tonic-gate 			return (-1);
3330Sstevel@tonic-gate 		} else if (cnt == 0) {
3340Sstevel@tonic-gate 			return (mderror(ep, MDE_NO_HSPS, NULL));
3350Sstevel@tonic-gate 		}
3360Sstevel@tonic-gate 	} else if ((cnt = metahspnamelist(spp, &hspnlp, 1, &argv[0],
3370Sstevel@tonic-gate 	    ep)) < 0) {
3380Sstevel@tonic-gate 		return (-1);
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 	assert(cnt > 0);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	assert(*spp != NULL);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/* grab set lock */
3450Sstevel@tonic-gate 	if (meta_lock(*spp, TRUE, ep))
3460Sstevel@tonic-gate 		return (-1);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	/* check for ownership */
3490Sstevel@tonic-gate 	if (meta_check_ownership(*spp, ep) != 0)
3500Sstevel@tonic-gate 		return (-1);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/* get old component */
353*1623Stw21770 	if ((oldnp = metaname(spp, argv[1], LOGICAL_DEVICE, ep)) == NULL)
3540Sstevel@tonic-gate 		goto out;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/* get new component */
357*1623Stw21770 	if ((newnp = metaname(spp, argv[2], LOGICAL_DEVICE, ep)) == NULL)
3580Sstevel@tonic-gate 		goto out;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	/* replace hotspares */
3610Sstevel@tonic-gate 	cnt = 0;
3620Sstevel@tonic-gate 	for (p = hspnlp; (p != NULL); p = p->next) {
3630Sstevel@tonic-gate 		mdhspname_t	*hspnp = p->hspnamep;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 		if (meta_hs_replace(*spp, hspnp, oldnp, newnp, options, ep)
3660Sstevel@tonic-gate 		    != 0) {
3670Sstevel@tonic-gate 			if (mdisdeverror(ep, MDE_INVAL_HS))
3680Sstevel@tonic-gate 				mdclrerror(ep);
3690Sstevel@tonic-gate 			else
3700Sstevel@tonic-gate 				goto out;
3710Sstevel@tonic-gate 		} else {
3720Sstevel@tonic-gate 			++cnt;
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	/* make sure we got some */
3770Sstevel@tonic-gate 	if (cnt == 0) {
3780Sstevel@tonic-gate 		(void) mddeverror(ep, MDE_INVAL_HS, oldnp->dev, oldnp->cname);
3790Sstevel@tonic-gate 		goto out;
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	/* success */
3830Sstevel@tonic-gate 	rval = 0;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	/* cleanup, return success */
3860Sstevel@tonic-gate out:
3870Sstevel@tonic-gate 	if (hspnlp != NULL)
3880Sstevel@tonic-gate 		metafreehspnamelist(hspnlp);
3890Sstevel@tonic-gate 	return (rval);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate  * print_hsp_devid will collect the information for each underlying
3940Sstevel@tonic-gate  * physical device for all the hotspare pools and print out the
3950Sstevel@tonic-gate  * device relocation information
3960Sstevel@tonic-gate  * INPUT:
3970Sstevel@tonic-gate  *	mdsetname_t *sp			set the hsp is in
3980Sstevel@tonic-gate  *	mdhspnamelist_t *hspnlp		list of hsp
3990Sstevel@tonic-gate  *	FILE	*fp			where to print to
4000Sstevel@tonic-gate  *	md_error_t	*ep		errors
4010Sstevel@tonic-gate  * RETURN:
4020Sstevel@tonic-gate  *	0 	SUCCESS
4030Sstevel@tonic-gate  *	-1	ERROR
4040Sstevel@tonic-gate  */
4050Sstevel@tonic-gate static int
print_hsp_devid(mdsetname_t * sp,mdhspnamelist_t * hspnlp,FILE * fp,md_error_t * ep)4060Sstevel@tonic-gate print_hsp_devid(
4070Sstevel@tonic-gate 	mdsetname_t	*sp,
4080Sstevel@tonic-gate 	mdhspnamelist_t *hspnlp,
4090Sstevel@tonic-gate 	FILE		*fp,
4100Sstevel@tonic-gate 	md_error_t	*ep
4110Sstevel@tonic-gate )
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	mddevid_t	*ldevidp = NULL;
4140Sstevel@tonic-gate 	int		retval = 0;
4150Sstevel@tonic-gate 	mdhspnamelist_t	*p;
4160Sstevel@tonic-gate 	mddevid_t	*nextp;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	/* for all hotspare pools */
4190Sstevel@tonic-gate 	for (p = hspnlp; (p != NULL); p = p->next) {
4200Sstevel@tonic-gate 		mdhspname_t	*hspnp = p->hspnamep;
4210Sstevel@tonic-gate 		uint_t		hsi;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 		/* for all hotspares within a pool */
4240Sstevel@tonic-gate 		for (hsi = 0;
4250Sstevel@tonic-gate 		    hsi < hspnp->unitp->hotspares.hotspares_len; hsi++) {
4260Sstevel@tonic-gate 			mdname_t	*hsname;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 			hsname =
4290Sstevel@tonic-gate 			    hspnp->unitp->hotspares.hotspares_val[hsi].hsnamep;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 			meta_create_non_dup_list(hsname, &ldevidp);
4320Sstevel@tonic-gate 		}
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	retval = meta_print_devid(sp, fp, ldevidp, ep);
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	/* cleanup */
4380Sstevel@tonic-gate 	for (nextp = ldevidp; nextp != NULL; ldevidp = nextp) {
4390Sstevel@tonic-gate 		Free(ldevidp->ctdname);
4400Sstevel@tonic-gate 		nextp = ldevidp->next;
4410Sstevel@tonic-gate 		Free(ldevidp);
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 	return (retval);
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate  * parse args and status hotspares
4480Sstevel@tonic-gate  */
4490Sstevel@tonic-gate static int
status_hotspares(mdsetname_t ** spp,int argc,char * argv[],md_error_t * ep)4500Sstevel@tonic-gate status_hotspares(
4510Sstevel@tonic-gate 	mdsetname_t	**spp,
4520Sstevel@tonic-gate 	int		argc,
4530Sstevel@tonic-gate 	char		*argv[],
4540Sstevel@tonic-gate 	md_error_t	*ep
4550Sstevel@tonic-gate )
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate 	mdhspnamelist_t	*hspnlp = NULL;
4580Sstevel@tonic-gate 	int		cnt;
4590Sstevel@tonic-gate 	mdhspnamelist_t	*p;
4600Sstevel@tonic-gate 	int		rval = -1;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	/* get hotspare pool name(s) */
4630Sstevel@tonic-gate 	if (argc == 0) {
4640Sstevel@tonic-gate 		/* check for ownership */
4650Sstevel@tonic-gate 		assert(*spp != NULL);
4660Sstevel@tonic-gate 		if (meta_check_ownership(*spp, ep) != 0)
4670Sstevel@tonic-gate 			return (-1);
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 		if ((cnt = meta_get_hsp_names(*spp, &hspnlp, 0, ep)) < 0) {
4700Sstevel@tonic-gate 			return (-1);
4710Sstevel@tonic-gate 		} else if (cnt == 0) {
4720Sstevel@tonic-gate 			return (mderror(ep, MDE_NO_HSPS, NULL));
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 	} else if ((cnt = metahspnamelist(spp, &hspnlp, argc, argv, ep)) < 0) {
4750Sstevel@tonic-gate 		return (-1);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 	assert(cnt > 0);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/* check for ownership */
4800Sstevel@tonic-gate 	assert(*spp != NULL);
4810Sstevel@tonic-gate 	if (meta_check_ownership(*spp, ep) != 0)
4820Sstevel@tonic-gate 		return (-1);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	/* status hotspare pools */
4850Sstevel@tonic-gate 	for (p = hspnlp; (p != NULL); p = p->next) {
4860Sstevel@tonic-gate 		mdhspname_t	*hspnp = p->hspnamep;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 		if (status_hsp(*spp, hspnp, ep) != 0)
4890Sstevel@tonic-gate 			goto out;
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	if (print_hsp_devid(*spp, hspnlp, stdout, ep) == 0) {
4930Sstevel@tonic-gate 		rval = 0;
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	/* cleanup, return success */
4970Sstevel@tonic-gate out:
4980Sstevel@tonic-gate 	if (hspnlp != NULL)
4990Sstevel@tonic-gate 		metafreehspnamelist(hspnlp);
5000Sstevel@tonic-gate 	return (rval);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate /*
5040Sstevel@tonic-gate  * parse args and doit
5050Sstevel@tonic-gate  */
5060Sstevel@tonic-gate int
main(int argc,char ** argv)5070Sstevel@tonic-gate main(
5080Sstevel@tonic-gate 	int		argc,
5090Sstevel@tonic-gate 	char		**argv
5100Sstevel@tonic-gate )
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
5130Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
5140Sstevel@tonic-gate 	enum metahs_op	which_op = NONE;
5150Sstevel@tonic-gate 	mdcmdopts_t	options = (MDCMD_PRINT | MDCMD_DOIT);
5160Sstevel@tonic-gate 	int		c;
5170Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
5180Sstevel@tonic-gate 	md_error_t	*ep = &status;
5190Sstevel@tonic-gate 	int		error;
5200Sstevel@tonic-gate 	bool_t		called_thru_rpc = FALSE;
5210Sstevel@tonic-gate 	char		*cp;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	/*
5240Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
5250Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
5260Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
5270Sstevel@tonic-gate 	 * something.
5280Sstevel@tonic-gate 	 */
5290Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
5300Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
5310Sstevel@tonic-gate #endif
5320Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
5330Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
5370Sstevel@tonic-gate 		if (sdssc_bind_library() == SDSSC_OKAY)
5380Sstevel@tonic-gate 			if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
5390Sstevel@tonic-gate 						&error) == SDSSC_PROXY_DONE)
5400Sstevel@tonic-gate 				exit(error);
5410Sstevel@tonic-gate 	} else {
5420Sstevel@tonic-gate 		*cp = '\0'; /* cut off ".rpc_call" */
5430Sstevel@tonic-gate 		called_thru_rpc = TRUE;
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	/* initialize */
5470Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0) {
5480Sstevel@tonic-gate 		mde_perror(ep, "");
5490Sstevel@tonic-gate 		md_exit(sp, 1);
5500Sstevel@tonic-gate 	}
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	/* parse args */
5530Sstevel@tonic-gate 	optind = 1;
5540Sstevel@tonic-gate 	opterr = 1;
5550Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "hs:aderin?")) != -1) {
5560Sstevel@tonic-gate 		switch (c) {
5570Sstevel@tonic-gate 		case 'h':
5580Sstevel@tonic-gate 			usage(sp, 0);
5590Sstevel@tonic-gate 			break;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		case 's':
5620Sstevel@tonic-gate 			sname = optarg;
5630Sstevel@tonic-gate 			break;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		case 'a':
5660Sstevel@tonic-gate 			if (which_op != NONE)
5670Sstevel@tonic-gate 				usage(sp, 1);
5680Sstevel@tonic-gate 			which_op = ADD_A_HS;
5690Sstevel@tonic-gate 			break;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 		case 'd':
5720Sstevel@tonic-gate 			if (which_op != NONE)
5730Sstevel@tonic-gate 				usage(sp, 1);
5740Sstevel@tonic-gate 			which_op = DELETE_A_HS;
5750Sstevel@tonic-gate 			break;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 		case 'e':
5780Sstevel@tonic-gate 			if (which_op != NONE)
5790Sstevel@tonic-gate 				usage(sp, 1);
5800Sstevel@tonic-gate 			which_op = ENABLE_A_HS;
5810Sstevel@tonic-gate 			break;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 		case 'r':
5840Sstevel@tonic-gate 			if (which_op != NONE)
5850Sstevel@tonic-gate 				usage(sp, 1);
5860Sstevel@tonic-gate 			which_op = REPLACE_A_HS;
5870Sstevel@tonic-gate 			break;
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 		case 'i':
5900Sstevel@tonic-gate 			if (which_op != NONE)
5910Sstevel@tonic-gate 				usage(sp, 1);
5920Sstevel@tonic-gate 			which_op = STATUS_A_HSP;
5930Sstevel@tonic-gate 			break;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 		case 'n':
5960Sstevel@tonic-gate 			if (called_thru_rpc == TRUE) {
5970Sstevel@tonic-gate 				options &= ~MDCMD_DOIT;
5980Sstevel@tonic-gate 			} else {
5990Sstevel@tonic-gate 				usage(sp, 1);
6000Sstevel@tonic-gate 			}
6010Sstevel@tonic-gate 			break;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 		case '?':
6050Sstevel@tonic-gate 			if (optopt == '?')
6060Sstevel@tonic-gate 				usage(sp, 0);
6070Sstevel@tonic-gate 			/*FALLTHROUGH*/
6080Sstevel@tonic-gate 		default:
6090Sstevel@tonic-gate 			usage(sp, 1);
6100Sstevel@tonic-gate 			break;
6110Sstevel@tonic-gate 		}
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	/* get set context */
6150Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
6160Sstevel@tonic-gate 		mde_perror(ep, "");
6170Sstevel@tonic-gate 		md_exit(sp, 1);
6180Sstevel@tonic-gate 	}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * Send the command to all nodes if the -s argument refers to a MN
6220Sstevel@tonic-gate 	 * set or the next argument refers to MN set hotspare name ( argc
6230Sstevel@tonic-gate 	 * greater than optind if there is a next argument)
6240Sstevel@tonic-gate 	 */
6250Sstevel@tonic-gate 	if ((called_thru_rpc == FALSE) &&
6260Sstevel@tonic-gate 	    (meta_is_mn_set(sp, ep) || ((argc > optind) &&
6270Sstevel@tonic-gate 	    meta_is_mn_name(&sp, argv[optind], ep)))) {
6280Sstevel@tonic-gate 		int	i;
6290Sstevel@tonic-gate 		int	newargc;
6300Sstevel@tonic-gate 		int	result;
6310Sstevel@tonic-gate 		char	**newargv;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 		/*
6340Sstevel@tonic-gate 		 * If we are dealing with a MN set and we were not
6350Sstevel@tonic-gate 		 * called thru an rpc call, we are just to send this
6360Sstevel@tonic-gate 		 * command string to the master of the set and let it
6370Sstevel@tonic-gate 		 * deal with it.
6380Sstevel@tonic-gate 		 * First we send out a dryrun version of this command.
6390Sstevel@tonic-gate 		 * If that returns success, we know it succeeded on all
6400Sstevel@tonic-gate 		 * nodes and it is safe to do the real command now.
6410Sstevel@tonic-gate 		 */
6420Sstevel@tonic-gate 		newargv = calloc(argc+1, sizeof (char *));
6430Sstevel@tonic-gate 		newargv[0] = "metahs";
6440Sstevel@tonic-gate 		newargv[1] = "-n"; /* always do "-n" first */
6450Sstevel@tonic-gate 		newargc = 2;
6460Sstevel@tonic-gate 		for (i = 1; i < argc; i++, newargc++)
6470Sstevel@tonic-gate 			newargv[newargc] = argv[i];
6480Sstevel@tonic-gate 		result = meta_mn_send_command(sp, newargc, newargv,
6490Sstevel@tonic-gate 		    MD_DISP_STDERR | MD_DRYRUN, NO_CONTEXT_STRING, ep);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		/* If we found a problem don't do it for real */
6520Sstevel@tonic-gate 		if (result != 0) {
6530Sstevel@tonic-gate 			md_exit(sp, result);
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 		/*
6570Sstevel@tonic-gate 		 * Do it for real now. Remove "-n" from the arguments and
6580Sstevel@tonic-gate 		 * MD_DRYRUN from the flags. If this fails the master must panic
6590Sstevel@tonic-gate 		 * as the mddbs may be inconsistent.
6600Sstevel@tonic-gate 		 */
6610Sstevel@tonic-gate 		newargv[1] = ""; /* this was "-n" before */
6620Sstevel@tonic-gate 		result = meta_mn_send_command(sp, newargc, newargv,
6630Sstevel@tonic-gate 		    MD_DISP_STDERR | MD_RETRY_BUSY | MD_PANIC_WHEN_INCONSISTENT,
6640Sstevel@tonic-gate 		    NO_CONTEXT_STRING, ep);
6650Sstevel@tonic-gate 		free(newargv);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 		/* No further action required */
6680Sstevel@tonic-gate 		md_exit(sp, result);
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	argc -= optind;
6720Sstevel@tonic-gate 	argv += optind;
6730Sstevel@tonic-gate 	if (which_op == NONE)
6740Sstevel@tonic-gate 		usage(sp, 1);
6750Sstevel@tonic-gate 
676*1623Stw21770 	/*
677*1623Stw21770 	 * if a hot spare pool was specified by name then
678*1623Stw21770 	 * get the canonical form of the name and set up
679*1623Stw21770 	 * sp if the name was specified in the form 'set/hsp'
680*1623Stw21770 	 * unless 'all' is specified or the request is made to
681*1623Stw21770 	 * enable a hs which means that argv[0] will be a component
682*1623Stw21770 	 */
683*1623Stw21770 	if (argc > 0 && !meta_is_all(argv[0]) && which_op != ENABLE_A_HS) {
684*1623Stw21770 		char *cname = NULL;
685*1623Stw21770 
686*1623Stw21770 		cname = meta_name_getname(&sp, argv[0], HSP_DEVICE, ep);
687*1623Stw21770 		if (cname == NULL) {
688*1623Stw21770 			mde_perror(ep, "");
689*1623Stw21770 			md_exit(sp, 1);
690*1623Stw21770 		}
691*1623Stw21770 		Free(cname);
692*1623Stw21770 	}
693*1623Stw21770 
6940Sstevel@tonic-gate 	if (which_op == STATUS_A_HSP) {
6950Sstevel@tonic-gate 		if (status_hotspares(&sp, argc, argv, ep) != 0) {
6960Sstevel@tonic-gate 			mde_perror(ep, "");
6970Sstevel@tonic-gate 			md_exit(sp, 1);
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 		md_exit(sp, 0);
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	if (meta_check_root(ep) != 0) {
7030Sstevel@tonic-gate 		mde_perror(ep, "");
7040Sstevel@tonic-gate 		md_exit(sp, 1);
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	/* dispatch */
7090Sstevel@tonic-gate 	switch (which_op) {
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	case ADD_A_HS:
7120Sstevel@tonic-gate 		if (add_hotspares(&sp, argc, argv, options, ep) != 0) {
7130Sstevel@tonic-gate 			mde_perror(ep, "");
7140Sstevel@tonic-gate 			md_exit(sp, 1);
7150Sstevel@tonic-gate 		}
7160Sstevel@tonic-gate 		break;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	case DELETE_A_HS:
7190Sstevel@tonic-gate 		if (delete_hotspares(&sp, argc, argv, options, ep) != 0) {
7200Sstevel@tonic-gate 			mde_perror(ep, "");
7210Sstevel@tonic-gate 			md_exit(sp, 1);
7220Sstevel@tonic-gate 		}
7230Sstevel@tonic-gate 		break;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	case ENABLE_A_HS:
7260Sstevel@tonic-gate 		if (enable_hotspares(&sp, argc, argv, options, ep) != 0) {
7270Sstevel@tonic-gate 			mde_perror(ep, "");
7280Sstevel@tonic-gate 			md_exit(sp, 1);
7290Sstevel@tonic-gate 		}
7300Sstevel@tonic-gate 		break;
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	case REPLACE_A_HS:
7330Sstevel@tonic-gate 		if (replace_hotspares(&sp, argc, argv, options, ep) != 0) {
7340Sstevel@tonic-gate 			mde_perror(ep, "");
7350Sstevel@tonic-gate 			md_exit(sp, 1);
7360Sstevel@tonic-gate 		}
7370Sstevel@tonic-gate 		break;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	default:
7400Sstevel@tonic-gate 		assert(0);
7410Sstevel@tonic-gate 		break;
7420Sstevel@tonic-gate 	}
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/* update md.cf */
7450Sstevel@tonic-gate out:
7460Sstevel@tonic-gate 	if (meta_update_md_cf(sp, ep) != 0) {
7470Sstevel@tonic-gate 		mde_perror(ep, "");
7480Sstevel@tonic-gate 		md_exit(sp, 1);
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 	md_exit(sp, 0);
7510Sstevel@tonic-gate 	/*NOTREACHED*/
7520Sstevel@tonic-gate 	return (0);
7530Sstevel@tonic-gate }
754