xref: /onnv-gate/usr/src/cmd/lvm/util/metattach.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  * attach submirrors
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <meta.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sdssc.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * print usage message
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate static void
usage(mdsetname_t * sp,int eval)400Sstevel@tonic-gate usage(
410Sstevel@tonic-gate 	mdsetname_t	*sp,
420Sstevel@tonic-gate 	int		eval
430Sstevel@tonic-gate )
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\
460Sstevel@tonic-gate usage:	%s [-s setname] mirror [metadevice]\n\
470Sstevel@tonic-gate 	%s [-s setname] [-i interlace] concat/stripe component...\n\
480Sstevel@tonic-gate 	%s [-s setname] RAID component...\n\
490Sstevel@tonic-gate 	%s [-s setname] [-A alignment] softpart size|all\n"),
500Sstevel@tonic-gate 	    myname, myname, myname, myname);
510Sstevel@tonic-gate 	md_exit(sp, eval);
520Sstevel@tonic-gate }
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * attach more space to a soft partition
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate static int
sp_attach(mdsetname_t ** spp,mdname_t * spnp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)580Sstevel@tonic-gate sp_attach(
590Sstevel@tonic-gate 	mdsetname_t	**spp,
600Sstevel@tonic-gate 	mdname_t	*spnp,
610Sstevel@tonic-gate 	int		argc,
620Sstevel@tonic-gate 	char		*argv[],
630Sstevel@tonic-gate 	mdcmdopts_t	options,
640Sstevel@tonic-gate 	md_error_t	*ep
650Sstevel@tonic-gate )
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	int		c;
680Sstevel@tonic-gate 	sp_ext_offset_t	alignment = 0;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	/* reset and parse args */
710Sstevel@tonic-gate 	optind = 1;
720Sstevel@tonic-gate 	opterr = 1;
730Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ns:A:")) != -1) {
740Sstevel@tonic-gate 		switch (c) {
750Sstevel@tonic-gate 		case 'n':
760Sstevel@tonic-gate 		case 's':
770Sstevel@tonic-gate 			break;
780Sstevel@tonic-gate 		case 'A':
790Sstevel@tonic-gate 			if (meta_sp_parsesize(optarg, &alignment) == -1) {
800Sstevel@tonic-gate 			    usage(*spp, 1);
810Sstevel@tonic-gate 			    /* NOTREACHED */
820Sstevel@tonic-gate 			}
830Sstevel@tonic-gate 			break;
840Sstevel@tonic-gate 		default:
850Sstevel@tonic-gate 			usage(*spp, 1);
860Sstevel@tonic-gate 			/* NOTREACHED */
870Sstevel@tonic-gate 			break;
880Sstevel@tonic-gate 		}
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 	argc -= optind + 1;
910Sstevel@tonic-gate 	argv += optind + 1;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	if (argc != 1)
940Sstevel@tonic-gate 		usage(*spp, 1);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (meta_sp_attach(*spp, spnp, argv[0], options, alignment, ep) != 0) {
970Sstevel@tonic-gate 		return (-1);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	/* update md.cf file */
1010Sstevel@tonic-gate 	if (meta_update_md_cf(*spp, ep) != 0)
1020Sstevel@tonic-gate 		return (-1);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	return (0);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * attach components to stripe
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate static int
stripe_attach(mdsetname_t ** spp,mdname_t * stripenp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)1100Sstevel@tonic-gate stripe_attach(
1110Sstevel@tonic-gate 	mdsetname_t	**spp,
1120Sstevel@tonic-gate 	mdname_t	*stripenp,
1130Sstevel@tonic-gate 	int		argc,
1140Sstevel@tonic-gate 	char		*argv[],
1150Sstevel@tonic-gate 	mdcmdopts_t	options,
1160Sstevel@tonic-gate 	md_error_t	*ep
1170Sstevel@tonic-gate )
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	diskaddr_t	interlace = 0;
1200Sstevel@tonic-gate 	int		c;
1210Sstevel@tonic-gate 	mdnamelist_t	*compnlp = NULL;
1220Sstevel@tonic-gate 	mdnamelist_t	*p;
1230Sstevel@tonic-gate 	mdname_t	*currootnp;
1240Sstevel@tonic-gate 	md_stripe_t	*stripep;
1250Sstevel@tonic-gate 	md_row_t	*rp;
1260Sstevel@tonic-gate 	md_comp_t	*cp;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	/* reset and parse args */
1300Sstevel@tonic-gate 	optind = 1;
1310Sstevel@tonic-gate 	opterr = 1;
1320Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:ani:")) != -1) {
1330Sstevel@tonic-gate 		switch (c) {
1340Sstevel@tonic-gate 		case 'n':
1350Sstevel@tonic-gate 		case 's':
1360Sstevel@tonic-gate 			break;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 		case 'a':
1390Sstevel@tonic-gate 			break;	/* obsolete */
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 		case 'i':
1420Sstevel@tonic-gate 			if (parse_interlace(stripenp->cname, optarg,
1430Sstevel@tonic-gate 			    &interlace, ep) != 0) {
1440Sstevel@tonic-gate 				return (-1);
1450Sstevel@tonic-gate 			}
1460Sstevel@tonic-gate 			if (meta_stripe_check_interlace(interlace,
1470Sstevel@tonic-gate 			    stripenp->cname, ep))
1480Sstevel@tonic-gate 				return (-1);
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 		default:
1520Sstevel@tonic-gate 			usage(*spp, 1);
1530Sstevel@tonic-gate 			/*NOTREACHED*/
1540Sstevel@tonic-gate 			break;
1550Sstevel@tonic-gate 		}
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	argc -= optind + 1;
1590Sstevel@tonic-gate 	argv += optind + 1;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (argc <= 0)
1620Sstevel@tonic-gate 		usage(*spp, 1);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	/* get list of components */
165*1623Stw21770 	if (metanamelist(spp, &compnlp, argc, argv,
166*1623Stw21770 	    UNKNOWN, ep) < 0)
1670Sstevel@tonic-gate 		return (-1);
1680Sstevel@tonic-gate 	assert(compnlp != NULL);
1690Sstevel@tonic-gate 	for (p = compnlp; (p != NULL); p = p->next) {
1700Sstevel@tonic-gate 		mdname_t	*compnp = p->namep;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 		/* see if we are a soft partition */
1730Sstevel@tonic-gate 		if (meta_sp_issp(*spp, compnp, ep) != 0) {
1740Sstevel@tonic-gate 			/* nope, check component */
1750Sstevel@tonic-gate 			if (metachkcomp(compnp, ep) != 0)
1760Sstevel@tonic-gate 				return (-1);
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	/* get root device */
1810Sstevel@tonic-gate 	if ((currootnp = meta_get_current_root_dev(*spp, ep)) != NULL) {
1820Sstevel@tonic-gate 		/*
1830Sstevel@tonic-gate 		 * Root is either a stripe or a slice
1840Sstevel@tonic-gate 		 * If root device is the 1st component of the stripe
1850Sstevel@tonic-gate 		 * Then fail as root cannot be expanded
1860Sstevel@tonic-gate 		 */
1870Sstevel@tonic-gate 		if ((stripep = meta_get_stripe(*spp, stripenp, ep)) == NULL)
1880Sstevel@tonic-gate 			return (-1);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 		rp = &stripep->rows.rows_val[0];
1910Sstevel@tonic-gate 		cp = &rp->comps.comps_val[0];
1920Sstevel@tonic-gate 		if (metachkcomp(cp->compnamep, ep) == 0) {
1930Sstevel@tonic-gate 			/* Component is a disk */
1940Sstevel@tonic-gate 			if (strcmp(currootnp->cname,
1950Sstevel@tonic-gate 			    cp->compnamep->cname) == 0) {
1960Sstevel@tonic-gate 				md_eprintf(gettext(
1970Sstevel@tonic-gate 				"%s: volume mounted as root cannot be "
1980Sstevel@tonic-gate 				"expanded\n"), stripenp->cname);
1990Sstevel@tonic-gate 				md_exit(*spp, 1);
2000Sstevel@tonic-gate 			}
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/* attach components */
2050Sstevel@tonic-gate 	if (meta_stripe_attach(*spp, stripenp, compnlp, interlace, options,
2060Sstevel@tonic-gate 	    ep) != 0) {
2070Sstevel@tonic-gate 		return (-1);
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* update md.cf file */
2110Sstevel@tonic-gate 	if (meta_update_md_cf(*spp, ep) != 0)
2120Sstevel@tonic-gate 		return (-1);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	/* return success */
2150Sstevel@tonic-gate 	return (0);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate  * attach components to raid
2200Sstevel@tonic-gate  */
2210Sstevel@tonic-gate static int
raid_attach(mdsetname_t ** spp,mdname_t * raidnp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)2220Sstevel@tonic-gate raid_attach(
2230Sstevel@tonic-gate 	mdsetname_t	**spp,
2240Sstevel@tonic-gate 	mdname_t	*raidnp,
2250Sstevel@tonic-gate 	int		argc,
2260Sstevel@tonic-gate 	char		*argv[],
2270Sstevel@tonic-gate 	mdcmdopts_t	options,
2280Sstevel@tonic-gate 	md_error_t	*ep
2290Sstevel@tonic-gate )
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 	int		c;
2320Sstevel@tonic-gate 	mdnamelist_t	*compnlp = NULL;
2330Sstevel@tonic-gate 	mdnamelist_t	*p;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/* reset and parse args */
2360Sstevel@tonic-gate 	optind = 1;
2370Sstevel@tonic-gate 	opterr = 1;
2380Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:ai:")) != -1) {
2390Sstevel@tonic-gate 		switch (c) {
2400Sstevel@tonic-gate 		case 'n':
2410Sstevel@tonic-gate 		case 's':
2420Sstevel@tonic-gate 			break;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		case 'a':
2450Sstevel@tonic-gate 			break;	/* obsolete */
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 		default:
2480Sstevel@tonic-gate 			usage(*spp, 1);
2490Sstevel@tonic-gate 			/*NOTREACHED*/
2500Sstevel@tonic-gate 			break;
2510Sstevel@tonic-gate 		}
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 	argc -= optind + 1;
2540Sstevel@tonic-gate 	argv += optind + 1;
2550Sstevel@tonic-gate 	if (argc <= 0)
2560Sstevel@tonic-gate 		usage(*spp, 1);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	/* get list of components */
259*1623Stw21770 	if (metanamelist(spp, &compnlp, argc, argv,
260*1623Stw21770 	    UNKNOWN, ep) < 0)
2610Sstevel@tonic-gate 		return (-1);
2620Sstevel@tonic-gate 	assert(compnlp != NULL);
2630Sstevel@tonic-gate 	for (p = compnlp; (p != NULL); p = p->next) {
2640Sstevel@tonic-gate 		mdname_t	*compnp = p->namep;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		/* check for soft partitions */
2670Sstevel@tonic-gate 		if (meta_sp_issp(*spp, compnp, ep) != 0) {
2680Sstevel@tonic-gate 			/* check disk */
2690Sstevel@tonic-gate 			if (metachkcomp(compnp, ep) != 0)
2700Sstevel@tonic-gate 				return (-1);
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/* attach components */
2750Sstevel@tonic-gate 	if (meta_raid_attach(*spp, raidnp, compnlp, options, ep) != 0)
2760Sstevel@tonic-gate 		return (-1);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/* update md.cf file */
2790Sstevel@tonic-gate 	if (meta_update_md_cf(*spp, ep) != 0)
2800Sstevel@tonic-gate 		return (-1);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/* return success */
2830Sstevel@tonic-gate 	return (0);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*
2870Sstevel@tonic-gate  * attach submirror to mirror
2880Sstevel@tonic-gate  */
2890Sstevel@tonic-gate static int
mirror_attach(mdsetname_t ** spp,mdname_t * mirnp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)2900Sstevel@tonic-gate mirror_attach(
2910Sstevel@tonic-gate 	mdsetname_t	**spp,
2920Sstevel@tonic-gate 	mdname_t	*mirnp,
2930Sstevel@tonic-gate 	int		argc,
2940Sstevel@tonic-gate 	char		*argv[],
2950Sstevel@tonic-gate 	mdcmdopts_t	options,
2960Sstevel@tonic-gate 	md_error_t	*ep
2970Sstevel@tonic-gate )
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	int		c;
3000Sstevel@tonic-gate 	mdname_t	*submirnp;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	/* reset and parse args */
3030Sstevel@tonic-gate 	optind = 1;
3040Sstevel@tonic-gate 	opterr = 1;
3050Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ns:")) != -1) {
3060Sstevel@tonic-gate 		switch (c) {
3070Sstevel@tonic-gate 		case 'n':
3080Sstevel@tonic-gate 		case 's':
3090Sstevel@tonic-gate 			break;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 		default:
3120Sstevel@tonic-gate 			usage(*spp, 1);
3130Sstevel@tonic-gate 			/*NOTREACHED*/
3140Sstevel@tonic-gate 			break;
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 	argc -= optind + 1;
3180Sstevel@tonic-gate 	argv += optind + 1;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/* get submirror */
3210Sstevel@tonic-gate 	if (argc == 1) {
322*1623Stw21770 		if (((submirnp = metaname(spp, argv[0], META_DEVICE,
323*1623Stw21770 		    ep)) == NULL) ||
3240Sstevel@tonic-gate 		    (metachkmeta(submirnp, ep) != 0)) {
3250Sstevel@tonic-gate 			return (-1);
3260Sstevel@tonic-gate 		}
3270Sstevel@tonic-gate 	} else if (argc == 0) {
3280Sstevel@tonic-gate 		submirnp = NULL;
3290Sstevel@tonic-gate 	} else {
3300Sstevel@tonic-gate 		usage(*spp, 1);
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	/* attach submirror */
3340Sstevel@tonic-gate 	if (meta_mirror_attach(*spp, mirnp, submirnp, options, ep) != 0)
3350Sstevel@tonic-gate 		return (-1);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	/* update md.cf file */
3380Sstevel@tonic-gate 	if (meta_update_md_cf(*spp, ep) != 0)
3390Sstevel@tonic-gate 		return (-1);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/* return success */
3420Sstevel@tonic-gate 	return (0);
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate  * attach devices
3470Sstevel@tonic-gate  */
3480Sstevel@tonic-gate int
main(int argc,char * argv[])3490Sstevel@tonic-gate main(
3500Sstevel@tonic-gate 	int		argc,
3510Sstevel@tonic-gate 	char		*argv[]
3520Sstevel@tonic-gate )
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate 	char		*sname = NULL;
3550Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
3560Sstevel@tonic-gate 	mdcmdopts_t	options = (MDCMD_PRINT|MDCMD_DOIT);
3570Sstevel@tonic-gate 	mdname_t	*np;
3580Sstevel@tonic-gate 	char		*miscname;
3590Sstevel@tonic-gate 	int		c;
3600Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
3610Sstevel@tonic-gate 	md_error_t	*ep = &status;
3620Sstevel@tonic-gate 	int		error;
3630Sstevel@tonic-gate 	bool_t		called_thru_rpc = FALSE;
3640Sstevel@tonic-gate 	char		*cp;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/*
3670Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
3680Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
3690Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
3700Sstevel@tonic-gate 	 * something.
3710Sstevel@tonic-gate 	 */
3720Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
3730Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
3740Sstevel@tonic-gate #endif
3750Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3760Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	/* initialize */
3790Sstevel@tonic-gate 	if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
3800Sstevel@tonic-gate 		if (sdssc_bind_library() == SDSSC_OKAY)
3810Sstevel@tonic-gate 			if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
3820Sstevel@tonic-gate 						&error) == SDSSC_PROXY_DONE)
3830Sstevel@tonic-gate 				exit(error);
3840Sstevel@tonic-gate 	} else {
3850Sstevel@tonic-gate 		*cp = '\0'; /* cut off ".rpc_call" */
3860Sstevel@tonic-gate 		called_thru_rpc = TRUE;
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
3900Sstevel@tonic-gate 			meta_check_root(ep) != 0) {
3910Sstevel@tonic-gate 		mde_perror(ep, "");
3920Sstevel@tonic-gate 		md_exit(sp, 1);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/* find set and metadevice first */
3960Sstevel@tonic-gate 	optind = 1;
3970Sstevel@tonic-gate 	opterr = 1;
3980Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "hns:A:ai:?")) != -1) {
3990Sstevel@tonic-gate 		switch (c) {
4000Sstevel@tonic-gate 		case 'h':
4010Sstevel@tonic-gate 			usage(sp, 0);
4020Sstevel@tonic-gate 			break;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 		case 'n':
4050Sstevel@tonic-gate 			if (called_thru_rpc == TRUE) {
4060Sstevel@tonic-gate 				options &= ~MDCMD_DOIT;
4070Sstevel@tonic-gate 			} else {
4080Sstevel@tonic-gate 				usage(sp, 1);
4090Sstevel@tonic-gate 			}
4100Sstevel@tonic-gate 			break;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 		case 's':
4130Sstevel@tonic-gate 			sname = optarg;
4140Sstevel@tonic-gate 			break;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 		case '?':
4170Sstevel@tonic-gate 			if (optopt == '?')
4180Sstevel@tonic-gate 				usage(sp, 0);
4190Sstevel@tonic-gate 			break;
4200Sstevel@tonic-gate 		}
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 	if ((argc - optind) <= 0)
4230Sstevel@tonic-gate 		usage(sp, 1);
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	if (sname != NULL) {
4260Sstevel@tonic-gate 		if ((sp = metasetname(sname, ep)) == NULL) {
4270Sstevel@tonic-gate 			mde_perror(ep, "");
4280Sstevel@tonic-gate 			md_exit(sp, 1);
4290Sstevel@tonic-gate 		}
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
432*1623Stw21770 	if (((np = metaname(&sp, argv[optind], META_DEVICE, ep)) == NULL) ||
4330Sstevel@tonic-gate 	    (metachkmeta(np, ep) != 0)) {
4340Sstevel@tonic-gate 		mde_perror(ep, "");
4350Sstevel@tonic-gate 		md_exit(sp, 1);
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 	assert(sp != NULL);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	if ((called_thru_rpc == FALSE) &&
4400Sstevel@tonic-gate 	    meta_is_mn_name(&sp, argv[optind], ep)) {
4410Sstevel@tonic-gate 		/*
4420Sstevel@tonic-gate 		 * If we are dealing with a MN set and we were not
4430Sstevel@tonic-gate 		 * called thru an rpc call, we are just to send this
4440Sstevel@tonic-gate 		 * command string to the master of the set and let it
4450Sstevel@tonic-gate 		 * deal with it.
4460Sstevel@tonic-gate 		 * Note that if sp is NULL, meta_is_mn_name() derives sp
4470Sstevel@tonic-gate 		 * from argv[optind] which is the metadevice arg
4480Sstevel@tonic-gate 		 */
4490Sstevel@tonic-gate 		int	i;
4500Sstevel@tonic-gate 		int	newargc;
4510Sstevel@tonic-gate 		int	result;
4520Sstevel@tonic-gate 		char	**newargv;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		if ((miscname = metagetmiscname(np, ep)) == NULL) {
4550Sstevel@tonic-gate 			mde_perror(ep, "");
4560Sstevel@tonic-gate 			md_exit(sp, 1);
4570Sstevel@tonic-gate 		}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 		newargv = calloc(argc+1, sizeof (char *));
4600Sstevel@tonic-gate 		newargv[0] = "metattach";
4610Sstevel@tonic-gate 		newargv[1] = "-n"; /* always do "-n" first */
4620Sstevel@tonic-gate 		newargc = 2;
4630Sstevel@tonic-gate 		for (i = 1; i < argc; i++, newargc++)
4640Sstevel@tonic-gate 			newargv[newargc] = argv[i];
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 		result = meta_mn_send_command(sp, newargc, newargv,
4670Sstevel@tonic-gate 		    MD_DISP_STDERR | MD_DRYRUN, NO_CONTEXT_STRING, ep);
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 		/* If we found a problem don't do it for real */
4700Sstevel@tonic-gate 		if (result != 0) {
4710Sstevel@tonic-gate 			md_exit(sp, result);
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		/*
4750Sstevel@tonic-gate 		 * Do it for real now. Remove "-n" from the arguments and
4760Sstevel@tonic-gate 		 * MD_DRYRUN from the flags. If we fail now, the master must
4770Sstevel@tonic-gate 		 * panic as the mddbs may be inconsistent.
4780Sstevel@tonic-gate 		 */
4790Sstevel@tonic-gate 		newargv[1] = ""; /* this was "-n" before */
4800Sstevel@tonic-gate 		result = meta_mn_send_command(sp, newargc, newargv,
4810Sstevel@tonic-gate 		    MD_DISP_STDERR | MD_RETRY_BUSY | MD_PANIC_WHEN_INCONSISTENT,
4820Sstevel@tonic-gate 		    NO_CONTEXT_STRING, ep);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		free(newargv);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 		/*
4870Sstevel@tonic-gate 		 * If the metattach command succeeds, for a mirror, send a
4880Sstevel@tonic-gate 		 * resync starting message for the metadevice
4890Sstevel@tonic-gate 		 */
4900Sstevel@tonic-gate 		if ((result == 0) && (strcmp(miscname, MD_MIRROR) == 0))
4910Sstevel@tonic-gate 			if ((result = meta_mn_send_resync_starting(np, ep))
4920Sstevel@tonic-gate 			    != 0)
4930Sstevel@tonic-gate 				mde_perror(ep, "Unable to start resync");
4940Sstevel@tonic-gate 		md_exit(sp, result);
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, ep)) {
4980Sstevel@tonic-gate 		mde_perror(ep, "");
4990Sstevel@tonic-gate 		md_exit(sp, 1);
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
5030Sstevel@tonic-gate 		mde_perror(ep, "");
5040Sstevel@tonic-gate 		md_exit(sp, 1);
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 	if ((miscname = metagetmiscname(np, ep)) == NULL) {
5070Sstevel@tonic-gate 		mde_perror(ep, "");
5080Sstevel@tonic-gate 		md_exit(sp, 1);
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	/* dispatch based on device type */
5120Sstevel@tonic-gate 	if (strcmp(miscname, MD_STRIPE) == 0) {
5130Sstevel@tonic-gate 		if (stripe_attach(&sp, np, argc, argv, options, ep) != 0) {
5140Sstevel@tonic-gate 			mde_perror(ep, "");
5150Sstevel@tonic-gate 			md_exit(sp, 1);
5160Sstevel@tonic-gate 		}
5170Sstevel@tonic-gate 	} else if (strcmp(miscname, MD_RAID) == 0) {
5180Sstevel@tonic-gate 		if (raid_attach(&sp, np, argc, argv, options, ep) != 0) {
5190Sstevel@tonic-gate 			mde_perror(ep, "");
5200Sstevel@tonic-gate 			md_exit(sp, 1);
5210Sstevel@tonic-gate 		}
5220Sstevel@tonic-gate 	} else if (strcmp(miscname, MD_MIRROR) == 0) {
5230Sstevel@tonic-gate 		if (mirror_attach(&sp, np, argc, argv, options, ep) != 0) {
5240Sstevel@tonic-gate 			mde_perror(ep, "");
5250Sstevel@tonic-gate 			md_exit(sp, 1);
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 	} else if (strcmp(miscname, MD_TRANS) == 0) {
5280Sstevel@tonic-gate 		md_eprintf(gettext(MD_EOF_TRANS_MSG));
5290Sstevel@tonic-gate 		md_exit(sp, 1);
5300Sstevel@tonic-gate 	} else if (strcmp(miscname, MD_SP) == 0) {
5310Sstevel@tonic-gate 		if (sp_attach(&sp, np, argc, argv, options, ep) != 0) {
5320Sstevel@tonic-gate 			mde_perror(ep, "");
5330Sstevel@tonic-gate 			md_exit(sp, 1);
5340Sstevel@tonic-gate 		}
5350Sstevel@tonic-gate 	} else {
5360Sstevel@tonic-gate 		md_eprintf(gettext(
5370Sstevel@tonic-gate 		    "%s: invalid metadevice type %s\n"),
5380Sstevel@tonic-gate 		    np->cname, miscname);
5390Sstevel@tonic-gate 		md_exit(sp, 1);
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/* return success */
5430Sstevel@tonic-gate 	md_exit(sp, 0);
5440Sstevel@tonic-gate 	/*NOTREACHED*/
5450Sstevel@tonic-gate 	return (0);
5460Sstevel@tonic-gate }
547