xref: /titanic_41/usr/src/cmd/lvm/util/metattach.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  * attach submirrors
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <meta.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sdssc.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * print usage message
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate static void
usage(mdsetname_t * sp,int eval)407c478bd9Sstevel@tonic-gate usage(
417c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp,
427c478bd9Sstevel@tonic-gate 	int		eval
437c478bd9Sstevel@tonic-gate )
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\
467c478bd9Sstevel@tonic-gate usage:	%s [-s setname] mirror [metadevice]\n\
477c478bd9Sstevel@tonic-gate 	%s [-s setname] [-i interlace] concat/stripe component...\n\
487c478bd9Sstevel@tonic-gate 	%s [-s setname] RAID component...\n\
497c478bd9Sstevel@tonic-gate 	%s [-s setname] [-A alignment] softpart size|all\n"),
507c478bd9Sstevel@tonic-gate 	    myname, myname, myname, myname);
517c478bd9Sstevel@tonic-gate 	md_exit(sp, eval);
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * attach more space to a soft partition
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate static int
sp_attach(mdsetname_t ** spp,mdname_t * spnp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)587c478bd9Sstevel@tonic-gate sp_attach(
597c478bd9Sstevel@tonic-gate 	mdsetname_t	**spp,
607c478bd9Sstevel@tonic-gate 	mdname_t	*spnp,
617c478bd9Sstevel@tonic-gate 	int		argc,
627c478bd9Sstevel@tonic-gate 	char		*argv[],
637c478bd9Sstevel@tonic-gate 	mdcmdopts_t	options,
647c478bd9Sstevel@tonic-gate 	md_error_t	*ep
657c478bd9Sstevel@tonic-gate )
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	int		c;
687c478bd9Sstevel@tonic-gate 	sp_ext_offset_t	alignment = 0;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	/* reset and parse args */
717c478bd9Sstevel@tonic-gate 	optind = 1;
727c478bd9Sstevel@tonic-gate 	opterr = 1;
737c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ns:A:")) != -1) {
747c478bd9Sstevel@tonic-gate 		switch (c) {
757c478bd9Sstevel@tonic-gate 		case 'n':
767c478bd9Sstevel@tonic-gate 		case 's':
777c478bd9Sstevel@tonic-gate 			break;
787c478bd9Sstevel@tonic-gate 		case 'A':
797c478bd9Sstevel@tonic-gate 			if (meta_sp_parsesize(optarg, &alignment) == -1) {
807c478bd9Sstevel@tonic-gate 			    usage(*spp, 1);
817c478bd9Sstevel@tonic-gate 			    /* NOTREACHED */
827c478bd9Sstevel@tonic-gate 			}
837c478bd9Sstevel@tonic-gate 			break;
847c478bd9Sstevel@tonic-gate 		default:
857c478bd9Sstevel@tonic-gate 			usage(*spp, 1);
867c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
877c478bd9Sstevel@tonic-gate 			break;
887c478bd9Sstevel@tonic-gate 		}
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 	argc -= optind + 1;
917c478bd9Sstevel@tonic-gate 	argv += optind + 1;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (argc != 1)
947c478bd9Sstevel@tonic-gate 		usage(*spp, 1);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (meta_sp_attach(*spp, spnp, argv[0], options, alignment, ep) != 0) {
977c478bd9Sstevel@tonic-gate 		return (-1);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/* update md.cf file */
1017c478bd9Sstevel@tonic-gate 	if (meta_update_md_cf(*spp, ep) != 0)
1027c478bd9Sstevel@tonic-gate 		return (-1);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	return (0);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * attach components to stripe
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate static int
stripe_attach(mdsetname_t ** spp,mdname_t * stripenp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)1107c478bd9Sstevel@tonic-gate stripe_attach(
1117c478bd9Sstevel@tonic-gate 	mdsetname_t	**spp,
1127c478bd9Sstevel@tonic-gate 	mdname_t	*stripenp,
1137c478bd9Sstevel@tonic-gate 	int		argc,
1147c478bd9Sstevel@tonic-gate 	char		*argv[],
1157c478bd9Sstevel@tonic-gate 	mdcmdopts_t	options,
1167c478bd9Sstevel@tonic-gate 	md_error_t	*ep
1177c478bd9Sstevel@tonic-gate )
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	diskaddr_t	interlace = 0;
1207c478bd9Sstevel@tonic-gate 	int		c;
1217c478bd9Sstevel@tonic-gate 	mdnamelist_t	*compnlp = NULL;
1227c478bd9Sstevel@tonic-gate 	mdnamelist_t	*p;
1237c478bd9Sstevel@tonic-gate 	mdname_t	*currootnp;
1247c478bd9Sstevel@tonic-gate 	md_stripe_t	*stripep;
1257c478bd9Sstevel@tonic-gate 	md_row_t	*rp;
1267c478bd9Sstevel@tonic-gate 	md_comp_t	*cp;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/* reset and parse args */
1307c478bd9Sstevel@tonic-gate 	optind = 1;
1317c478bd9Sstevel@tonic-gate 	opterr = 1;
1327c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:ani:")) != -1) {
1337c478bd9Sstevel@tonic-gate 		switch (c) {
1347c478bd9Sstevel@tonic-gate 		case 'n':
1357c478bd9Sstevel@tonic-gate 		case 's':
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		case 'a':
1397c478bd9Sstevel@tonic-gate 			break;	/* obsolete */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		case 'i':
1427c478bd9Sstevel@tonic-gate 			if (parse_interlace(stripenp->cname, optarg,
1437c478bd9Sstevel@tonic-gate 			    &interlace, ep) != 0) {
1447c478bd9Sstevel@tonic-gate 				return (-1);
1457c478bd9Sstevel@tonic-gate 			}
1467c478bd9Sstevel@tonic-gate 			if (meta_stripe_check_interlace(interlace,
1477c478bd9Sstevel@tonic-gate 			    stripenp->cname, ep))
1487c478bd9Sstevel@tonic-gate 				return (-1);
1497c478bd9Sstevel@tonic-gate 			break;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		default:
1527c478bd9Sstevel@tonic-gate 			usage(*spp, 1);
1537c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	argc -= optind + 1;
1597c478bd9Sstevel@tonic-gate 	argv += optind + 1;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (argc <= 0)
1627c478bd9Sstevel@tonic-gate 		usage(*spp, 1);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/* get list of components */
165*d7cd8252Stw21770 	if (metanamelist(spp, &compnlp, argc, argv,
166*d7cd8252Stw21770 	    UNKNOWN, ep) < 0)
1677c478bd9Sstevel@tonic-gate 		return (-1);
1687c478bd9Sstevel@tonic-gate 	assert(compnlp != NULL);
1697c478bd9Sstevel@tonic-gate 	for (p = compnlp; (p != NULL); p = p->next) {
1707c478bd9Sstevel@tonic-gate 		mdname_t	*compnp = p->namep;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		/* see if we are a soft partition */
1737c478bd9Sstevel@tonic-gate 		if (meta_sp_issp(*spp, compnp, ep) != 0) {
1747c478bd9Sstevel@tonic-gate 			/* nope, check component */
1757c478bd9Sstevel@tonic-gate 			if (metachkcomp(compnp, ep) != 0)
1767c478bd9Sstevel@tonic-gate 				return (-1);
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/* get root device */
1817c478bd9Sstevel@tonic-gate 	if ((currootnp = meta_get_current_root_dev(*spp, ep)) != NULL) {
1827c478bd9Sstevel@tonic-gate 		/*
1837c478bd9Sstevel@tonic-gate 		 * Root is either a stripe or a slice
1847c478bd9Sstevel@tonic-gate 		 * If root device is the 1st component of the stripe
1857c478bd9Sstevel@tonic-gate 		 * Then fail as root cannot be expanded
1867c478bd9Sstevel@tonic-gate 		 */
1877c478bd9Sstevel@tonic-gate 		if ((stripep = meta_get_stripe(*spp, stripenp, ep)) == NULL)
1887c478bd9Sstevel@tonic-gate 			return (-1);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		rp = &stripep->rows.rows_val[0];
1917c478bd9Sstevel@tonic-gate 		cp = &rp->comps.comps_val[0];
1927c478bd9Sstevel@tonic-gate 		if (metachkcomp(cp->compnamep, ep) == 0) {
1937c478bd9Sstevel@tonic-gate 			/* Component is a disk */
1947c478bd9Sstevel@tonic-gate 			if (strcmp(currootnp->cname,
1957c478bd9Sstevel@tonic-gate 			    cp->compnamep->cname) == 0) {
1967c478bd9Sstevel@tonic-gate 				md_eprintf(gettext(
1977c478bd9Sstevel@tonic-gate 				"%s: volume mounted as root cannot be "
1987c478bd9Sstevel@tonic-gate 				"expanded\n"), stripenp->cname);
1997c478bd9Sstevel@tonic-gate 				md_exit(*spp, 1);
2007c478bd9Sstevel@tonic-gate 			}
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/* attach components */
2057c478bd9Sstevel@tonic-gate 	if (meta_stripe_attach(*spp, stripenp, compnlp, interlace, options,
2067c478bd9Sstevel@tonic-gate 	    ep) != 0) {
2077c478bd9Sstevel@tonic-gate 		return (-1);
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/* update md.cf file */
2117c478bd9Sstevel@tonic-gate 	if (meta_update_md_cf(*spp, ep) != 0)
2127c478bd9Sstevel@tonic-gate 		return (-1);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/* return success */
2157c478bd9Sstevel@tonic-gate 	return (0);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * attach components to raid
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate static int
raid_attach(mdsetname_t ** spp,mdname_t * raidnp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)2227c478bd9Sstevel@tonic-gate raid_attach(
2237c478bd9Sstevel@tonic-gate 	mdsetname_t	**spp,
2247c478bd9Sstevel@tonic-gate 	mdname_t	*raidnp,
2257c478bd9Sstevel@tonic-gate 	int		argc,
2267c478bd9Sstevel@tonic-gate 	char		*argv[],
2277c478bd9Sstevel@tonic-gate 	mdcmdopts_t	options,
2287c478bd9Sstevel@tonic-gate 	md_error_t	*ep
2297c478bd9Sstevel@tonic-gate )
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	int		c;
2327c478bd9Sstevel@tonic-gate 	mdnamelist_t	*compnlp = NULL;
2337c478bd9Sstevel@tonic-gate 	mdnamelist_t	*p;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/* reset and parse args */
2367c478bd9Sstevel@tonic-gate 	optind = 1;
2377c478bd9Sstevel@tonic-gate 	opterr = 1;
2387c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:ai:")) != -1) {
2397c478bd9Sstevel@tonic-gate 		switch (c) {
2407c478bd9Sstevel@tonic-gate 		case 'n':
2417c478bd9Sstevel@tonic-gate 		case 's':
2427c478bd9Sstevel@tonic-gate 			break;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		case 'a':
2457c478bd9Sstevel@tonic-gate 			break;	/* obsolete */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		default:
2487c478bd9Sstevel@tonic-gate 			usage(*spp, 1);
2497c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
2507c478bd9Sstevel@tonic-gate 			break;
2517c478bd9Sstevel@tonic-gate 		}
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 	argc -= optind + 1;
2547c478bd9Sstevel@tonic-gate 	argv += optind + 1;
2557c478bd9Sstevel@tonic-gate 	if (argc <= 0)
2567c478bd9Sstevel@tonic-gate 		usage(*spp, 1);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/* get list of components */
259*d7cd8252Stw21770 	if (metanamelist(spp, &compnlp, argc, argv,
260*d7cd8252Stw21770 	    UNKNOWN, ep) < 0)
2617c478bd9Sstevel@tonic-gate 		return (-1);
2627c478bd9Sstevel@tonic-gate 	assert(compnlp != NULL);
2637c478bd9Sstevel@tonic-gate 	for (p = compnlp; (p != NULL); p = p->next) {
2647c478bd9Sstevel@tonic-gate 		mdname_t	*compnp = p->namep;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		/* check for soft partitions */
2677c478bd9Sstevel@tonic-gate 		if (meta_sp_issp(*spp, compnp, ep) != 0) {
2687c478bd9Sstevel@tonic-gate 			/* check disk */
2697c478bd9Sstevel@tonic-gate 			if (metachkcomp(compnp, ep) != 0)
2707c478bd9Sstevel@tonic-gate 				return (-1);
2717c478bd9Sstevel@tonic-gate 		}
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/* attach components */
2757c478bd9Sstevel@tonic-gate 	if (meta_raid_attach(*spp, raidnp, compnlp, options, ep) != 0)
2767c478bd9Sstevel@tonic-gate 		return (-1);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/* update md.cf file */
2797c478bd9Sstevel@tonic-gate 	if (meta_update_md_cf(*spp, ep) != 0)
2807c478bd9Sstevel@tonic-gate 		return (-1);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/* return success */
2837c478bd9Sstevel@tonic-gate 	return (0);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate  * attach submirror to mirror
2887c478bd9Sstevel@tonic-gate  */
2897c478bd9Sstevel@tonic-gate static int
mirror_attach(mdsetname_t ** spp,mdname_t * mirnp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)2907c478bd9Sstevel@tonic-gate mirror_attach(
2917c478bd9Sstevel@tonic-gate 	mdsetname_t	**spp,
2927c478bd9Sstevel@tonic-gate 	mdname_t	*mirnp,
2937c478bd9Sstevel@tonic-gate 	int		argc,
2947c478bd9Sstevel@tonic-gate 	char		*argv[],
2957c478bd9Sstevel@tonic-gate 	mdcmdopts_t	options,
2967c478bd9Sstevel@tonic-gate 	md_error_t	*ep
2977c478bd9Sstevel@tonic-gate )
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	int		c;
3007c478bd9Sstevel@tonic-gate 	mdname_t	*submirnp;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/* reset and parse args */
3037c478bd9Sstevel@tonic-gate 	optind = 1;
3047c478bd9Sstevel@tonic-gate 	opterr = 1;
3057c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ns:")) != -1) {
3067c478bd9Sstevel@tonic-gate 		switch (c) {
3077c478bd9Sstevel@tonic-gate 		case 'n':
3087c478bd9Sstevel@tonic-gate 		case 's':
3097c478bd9Sstevel@tonic-gate 			break;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		default:
3127c478bd9Sstevel@tonic-gate 			usage(*spp, 1);
3137c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
3147c478bd9Sstevel@tonic-gate 			break;
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 	argc -= optind + 1;
3187c478bd9Sstevel@tonic-gate 	argv += optind + 1;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/* get submirror */
3217c478bd9Sstevel@tonic-gate 	if (argc == 1) {
322*d7cd8252Stw21770 		if (((submirnp = metaname(spp, argv[0], META_DEVICE,
323*d7cd8252Stw21770 		    ep)) == NULL) ||
3247c478bd9Sstevel@tonic-gate 		    (metachkmeta(submirnp, ep) != 0)) {
3257c478bd9Sstevel@tonic-gate 			return (-1);
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 	} else if (argc == 0) {
3287c478bd9Sstevel@tonic-gate 		submirnp = NULL;
3297c478bd9Sstevel@tonic-gate 	} else {
3307c478bd9Sstevel@tonic-gate 		usage(*spp, 1);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/* attach submirror */
3347c478bd9Sstevel@tonic-gate 	if (meta_mirror_attach(*spp, mirnp, submirnp, options, ep) != 0)
3357c478bd9Sstevel@tonic-gate 		return (-1);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/* update md.cf file */
3387c478bd9Sstevel@tonic-gate 	if (meta_update_md_cf(*spp, ep) != 0)
3397c478bd9Sstevel@tonic-gate 		return (-1);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* return success */
3427c478bd9Sstevel@tonic-gate 	return (0);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * attach devices
3477c478bd9Sstevel@tonic-gate  */
3487c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])3497c478bd9Sstevel@tonic-gate main(
3507c478bd9Sstevel@tonic-gate 	int		argc,
3517c478bd9Sstevel@tonic-gate 	char		*argv[]
3527c478bd9Sstevel@tonic-gate )
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	char		*sname = NULL;
3557c478bd9Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
3567c478bd9Sstevel@tonic-gate 	mdcmdopts_t	options = (MDCMD_PRINT|MDCMD_DOIT);
3577c478bd9Sstevel@tonic-gate 	mdname_t	*np;
3587c478bd9Sstevel@tonic-gate 	char		*miscname;
3597c478bd9Sstevel@tonic-gate 	int		c;
3607c478bd9Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
3617c478bd9Sstevel@tonic-gate 	md_error_t	*ep = &status;
3627c478bd9Sstevel@tonic-gate 	int		error;
3637c478bd9Sstevel@tonic-gate 	bool_t		called_thru_rpc = FALSE;
3647c478bd9Sstevel@tonic-gate 	char		*cp;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
3687c478bd9Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
3697c478bd9Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
3707c478bd9Sstevel@tonic-gate 	 * something.
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
3737c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
3747c478bd9Sstevel@tonic-gate #endif
3757c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3767c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/* initialize */
3797c478bd9Sstevel@tonic-gate 	if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
3807c478bd9Sstevel@tonic-gate 		if (sdssc_bind_library() == SDSSC_OKAY)
3817c478bd9Sstevel@tonic-gate 			if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
3827c478bd9Sstevel@tonic-gate 						&error) == SDSSC_PROXY_DONE)
3837c478bd9Sstevel@tonic-gate 				exit(error);
3847c478bd9Sstevel@tonic-gate 	} else {
3857c478bd9Sstevel@tonic-gate 		*cp = '\0'; /* cut off ".rpc_call" */
3867c478bd9Sstevel@tonic-gate 		called_thru_rpc = TRUE;
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
3907c478bd9Sstevel@tonic-gate 			meta_check_root(ep) != 0) {
3917c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
3927c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	/* find set and metadevice first */
3967c478bd9Sstevel@tonic-gate 	optind = 1;
3977c478bd9Sstevel@tonic-gate 	opterr = 1;
3987c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "hns:A:ai:?")) != -1) {
3997c478bd9Sstevel@tonic-gate 		switch (c) {
4007c478bd9Sstevel@tonic-gate 		case 'h':
4017c478bd9Sstevel@tonic-gate 			usage(sp, 0);
4027c478bd9Sstevel@tonic-gate 			break;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 		case 'n':
4057c478bd9Sstevel@tonic-gate 			if (called_thru_rpc == TRUE) {
4067c478bd9Sstevel@tonic-gate 				options &= ~MDCMD_DOIT;
4077c478bd9Sstevel@tonic-gate 			} else {
4087c478bd9Sstevel@tonic-gate 				usage(sp, 1);
4097c478bd9Sstevel@tonic-gate 			}
4107c478bd9Sstevel@tonic-gate 			break;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		case 's':
4137c478bd9Sstevel@tonic-gate 			sname = optarg;
4147c478bd9Sstevel@tonic-gate 			break;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 		case '?':
4177c478bd9Sstevel@tonic-gate 			if (optopt == '?')
4187c478bd9Sstevel@tonic-gate 				usage(sp, 0);
4197c478bd9Sstevel@tonic-gate 			break;
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 	if ((argc - optind) <= 0)
4237c478bd9Sstevel@tonic-gate 		usage(sp, 1);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if (sname != NULL) {
4267c478bd9Sstevel@tonic-gate 		if ((sp = metasetname(sname, ep)) == NULL) {
4277c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
4287c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
432*d7cd8252Stw21770 	if (((np = metaname(&sp, argv[optind], META_DEVICE, ep)) == NULL) ||
4337c478bd9Sstevel@tonic-gate 	    (metachkmeta(np, ep) != 0)) {
4347c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
4357c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 	assert(sp != NULL);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if ((called_thru_rpc == FALSE) &&
4407c478bd9Sstevel@tonic-gate 	    meta_is_mn_name(&sp, argv[optind], ep)) {
4417c478bd9Sstevel@tonic-gate 		/*
4427c478bd9Sstevel@tonic-gate 		 * If we are dealing with a MN set and we were not
4437c478bd9Sstevel@tonic-gate 		 * called thru an rpc call, we are just to send this
4447c478bd9Sstevel@tonic-gate 		 * command string to the master of the set and let it
4457c478bd9Sstevel@tonic-gate 		 * deal with it.
4467c478bd9Sstevel@tonic-gate 		 * Note that if sp is NULL, meta_is_mn_name() derives sp
4477c478bd9Sstevel@tonic-gate 		 * from argv[optind] which is the metadevice arg
4487c478bd9Sstevel@tonic-gate 		 */
4497c478bd9Sstevel@tonic-gate 		int	i;
4507c478bd9Sstevel@tonic-gate 		int	newargc;
4517c478bd9Sstevel@tonic-gate 		int	result;
4527c478bd9Sstevel@tonic-gate 		char	**newargv;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		if ((miscname = metagetmiscname(np, ep)) == NULL) {
4557c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
4567c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		newargv = calloc(argc+1, sizeof (char *));
4607c478bd9Sstevel@tonic-gate 		newargv[0] = "metattach";
4617c478bd9Sstevel@tonic-gate 		newargv[1] = "-n"; /* always do "-n" first */
4627c478bd9Sstevel@tonic-gate 		newargc = 2;
4637c478bd9Sstevel@tonic-gate 		for (i = 1; i < argc; i++, newargc++)
4647c478bd9Sstevel@tonic-gate 			newargv[newargc] = argv[i];
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		result = meta_mn_send_command(sp, newargc, newargv,
4677c478bd9Sstevel@tonic-gate 		    MD_DISP_STDERR | MD_DRYRUN, NO_CONTEXT_STRING, ep);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		/* If we found a problem don't do it for real */
4707c478bd9Sstevel@tonic-gate 		if (result != 0) {
4717c478bd9Sstevel@tonic-gate 			md_exit(sp, result);
4727c478bd9Sstevel@tonic-gate 		}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 		/*
4757c478bd9Sstevel@tonic-gate 		 * Do it for real now. Remove "-n" from the arguments and
4767c478bd9Sstevel@tonic-gate 		 * MD_DRYRUN from the flags. If we fail now, the master must
4777c478bd9Sstevel@tonic-gate 		 * panic as the mddbs may be inconsistent.
4787c478bd9Sstevel@tonic-gate 		 */
4797c478bd9Sstevel@tonic-gate 		newargv[1] = ""; /* this was "-n" before */
4807c478bd9Sstevel@tonic-gate 		result = meta_mn_send_command(sp, newargc, newargv,
4817c478bd9Sstevel@tonic-gate 		    MD_DISP_STDERR | MD_RETRY_BUSY | MD_PANIC_WHEN_INCONSISTENT,
4827c478bd9Sstevel@tonic-gate 		    NO_CONTEXT_STRING, ep);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		free(newargv);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		/*
4877c478bd9Sstevel@tonic-gate 		 * If the metattach command succeeds, for a mirror, send a
4887c478bd9Sstevel@tonic-gate 		 * resync starting message for the metadevice
4897c478bd9Sstevel@tonic-gate 		 */
4907c478bd9Sstevel@tonic-gate 		if ((result == 0) && (strcmp(miscname, MD_MIRROR) == 0))
4917c478bd9Sstevel@tonic-gate 			if ((result = meta_mn_send_resync_starting(np, ep))
4927c478bd9Sstevel@tonic-gate 			    != 0)
4937c478bd9Sstevel@tonic-gate 				mde_perror(ep, "Unable to start resync");
4947c478bd9Sstevel@tonic-gate 		md_exit(sp, result);
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, ep)) {
4987c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
4997c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
5037c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
5047c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 	if ((miscname = metagetmiscname(np, ep)) == NULL) {
5077c478bd9Sstevel@tonic-gate 		mde_perror(ep, "");
5087c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	/* dispatch based on device type */
5127c478bd9Sstevel@tonic-gate 	if (strcmp(miscname, MD_STRIPE) == 0) {
5137c478bd9Sstevel@tonic-gate 		if (stripe_attach(&sp, np, argc, argv, options, ep) != 0) {
5147c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
5157c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
5167c478bd9Sstevel@tonic-gate 		}
5177c478bd9Sstevel@tonic-gate 	} else if (strcmp(miscname, MD_RAID) == 0) {
5187c478bd9Sstevel@tonic-gate 		if (raid_attach(&sp, np, argc, argv, options, ep) != 0) {
5197c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
5207c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 	} else if (strcmp(miscname, MD_MIRROR) == 0) {
5237c478bd9Sstevel@tonic-gate 		if (mirror_attach(&sp, np, argc, argv, options, ep) != 0) {
5247c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
5257c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 	} else if (strcmp(miscname, MD_TRANS) == 0) {
5287c478bd9Sstevel@tonic-gate 		md_eprintf(gettext(MD_EOF_TRANS_MSG));
5297c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
5307c478bd9Sstevel@tonic-gate 	} else if (strcmp(miscname, MD_SP) == 0) {
5317c478bd9Sstevel@tonic-gate 		if (sp_attach(&sp, np, argc, argv, options, ep) != 0) {
5327c478bd9Sstevel@tonic-gate 			mde_perror(ep, "");
5337c478bd9Sstevel@tonic-gate 			md_exit(sp, 1);
5347c478bd9Sstevel@tonic-gate 		}
5357c478bd9Sstevel@tonic-gate 	} else {
5367c478bd9Sstevel@tonic-gate 		md_eprintf(gettext(
5377c478bd9Sstevel@tonic-gate 		    "%s: invalid metadevice type %s\n"),
5387c478bd9Sstevel@tonic-gate 		    np->cname, miscname);
5397c478bd9Sstevel@tonic-gate 		md_exit(sp, 1);
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/* return success */
5437c478bd9Sstevel@tonic-gate 	md_exit(sp, 0);
5447c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
5457c478bd9Sstevel@tonic-gate 	return (0);
5467c478bd9Sstevel@tonic-gate }
547