xref: /onnv-gate/usr/src/cmd/lvm/util/metaset.c (revision 11053)
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
51320Srd117015  * Common Development and Distribution License (the "License").
61320Srd117015  * 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*11053SSurya.Prakki@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Metadevice diskset utility.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <meta.h>
310Sstevel@tonic-gate #include <sys/lvm/md_mddb.h>
320Sstevel@tonic-gate #include <sdssc.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate enum metaset_cmd {
350Sstevel@tonic-gate 	notspecified,
360Sstevel@tonic-gate 	add,
370Sstevel@tonic-gate 	balance,
380Sstevel@tonic-gate 	delete,
390Sstevel@tonic-gate 	cluster,
400Sstevel@tonic-gate 	isowner,
410Sstevel@tonic-gate 	purge,
420Sstevel@tonic-gate 	query,
430Sstevel@tonic-gate 	release,
440Sstevel@tonic-gate 	take,
450Sstevel@tonic-gate 	join,			/* Join a multinode diskset */
460Sstevel@tonic-gate 	withdraw		/* Withdraw from a multinode diskset */
470Sstevel@tonic-gate };
480Sstevel@tonic-gate 
490Sstevel@tonic-gate enum cluster_cmd {
500Sstevel@tonic-gate 	ccnotspecified,
510Sstevel@tonic-gate 	clusterversion,		/* Return the version of the cluster I/F */
520Sstevel@tonic-gate 	clusterdisksin,		/* List disks in a given diskset */
530Sstevel@tonic-gate 	clustertake,		/* back door for Cluster take */
540Sstevel@tonic-gate 	clusterrelease,		/* ditto */
550Sstevel@tonic-gate 	clusterpurge,		/* back door for Cluster purge */
560Sstevel@tonic-gate 	clusterproxy		/* proxy the args after '--' to primary */
570Sstevel@tonic-gate };
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static void
600Sstevel@tonic-gate usage(
610Sstevel@tonic-gate 	mdsetname_t	*sp,
620Sstevel@tonic-gate 	char		*string)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	if ((string != NULL) && (*string != '\0'))
650Sstevel@tonic-gate 		md_eprintf("%s\n", string);
660Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
674932Spetede 	    "usage:\t%s -s setname -a [-A enable | disable] -h hostname ...\n"
684932Spetede 	    "	%s -s setname -a [-M] -h hostname ...\n"
694932Spetede 	    "	%s -s setname -a [-M] [-l length] [-L] drivename ...\n"
704932Spetede 	    "	%s -s setname -d [-M] -h hostname ...\n"
714932Spetede 	    "	%s -s setname -d [-M] -f -h all-hostnames\n"
724932Spetede 	    "	%s -s setname -d [-M] [-f] drivename ...\n"
734932Spetede 	    "	%s -s setname -d [-M] [-f] hostname ...\n"
744932Spetede 	    "	%s -s setname -A enable | disable\n"
754932Spetede 	    "	%s -s setname -t [-f]\n"
764932Spetede 	    "	%s -s setname -r\n"
774932Spetede 	    "	%s [-s setname] -j [-M]\n"
784932Spetede 	    "	%s [-s setname] -w [-M]\n"
794932Spetede 	    "	%s -s setname -P [-M]\n"
804932Spetede 	    "	%s -s setname -b [-M]\n"
814932Spetede 	    "	%s -s setname -o [-M] [-h hostname]\n"
824932Spetede 	    "	%s [-s setname]\n"
834932Spetede 	    "\n"
844932Spetede 	    "		hostname = contents of /etc/nodename\n"
854932Spetede 	    "		drivename = cNtNdN no slice\n"
864932Spetede 	    "		[-M] for multi-owner set is optional except"
874932Spetede 	    " on set creation\n"),
884932Spetede 	    myname, myname, myname, myname, myname, myname, myname, myname,
894932Spetede 	    myname, myname, myname, myname, myname, myname, myname, myname);
900Sstevel@tonic-gate 	md_exit(sp, (string == NULL) ? 0 : 1);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * The svm.sync rc script relies heavily on the metaset output.
950Sstevel@tonic-gate  * Any changes to the metaset output MUST verify that the rc script
960Sstevel@tonic-gate  * does not break. Not doing so may potentially leave the system
970Sstevel@tonic-gate  * unusable. You have been WARNED.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate static int
1000Sstevel@tonic-gate printset(mdsetname_t *sp, md_error_t *ep)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	int			i, j;
1030Sstevel@tonic-gate 	md_set_desc		*sd;
1040Sstevel@tonic-gate 	md_drive_desc		*dd, *p;
1050Sstevel@tonic-gate 	int			max_meds;
1060Sstevel@tonic-gate 	md_mnnode_desc		*nd;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if ((sd = metaget_setdesc(sp, ep)) == NULL)
1090Sstevel@tonic-gate 		return (-1);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	/*
1120Sstevel@tonic-gate 	 * Only get set owner information for traditional diskset.
1130Sstevel@tonic-gate 	 * This set owner information is stored in the node records
1140Sstevel@tonic-gate 	 * for a MN diskset.
1150Sstevel@tonic-gate 	 */
1160Sstevel@tonic-gate 	if (!(MD_MNSET_DESC(sd))) {
1170Sstevel@tonic-gate 		if (metaget_setownership(sp, ep) == -1)
1180Sstevel@tonic-gate 			return (-1);
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (((dd = metaget_drivedesc(sp, (MD_BASICNAME_OK | PRINT_FAST),
1220Sstevel@tonic-gate 	    ep)) == NULL) && !mdisok(ep))
1230Sstevel@tonic-gate 		return (-1);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if (MD_MNSET_DESC(sd)) {
1260Sstevel@tonic-gate 		(void) printf(gettext(
1270Sstevel@tonic-gate 		"\nMulti-owner Set name = %s, Set number = %d, Master = %s\n"),
1280Sstevel@tonic-gate 		    sp->setname, sp->setno, sd->sd_mn_master_nodenm);
1290Sstevel@tonic-gate 		if ((sd->sd_mn_master_nodeid == MD_MN_INVALID_NID) &&
1300Sstevel@tonic-gate 		    (dd != NULL)) {
1310Sstevel@tonic-gate 			(void) printf(gettext(
1324932Spetede 			    "Master and owner information unavailable "
1334932Spetede 			    "until joined (metaset -j)\n"));
1340Sstevel@tonic-gate 		}
1350Sstevel@tonic-gate 	} else {
1360Sstevel@tonic-gate 		(void) printf(gettext(
1370Sstevel@tonic-gate 		    "\nSet name = %s, Set number = %d\n"),
1380Sstevel@tonic-gate 		    sp->setname, sp->setno);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if (MD_MNSET_DESC(sd)) {
1420Sstevel@tonic-gate 		(void) printf(gettext("\n%-19.19s %-14.14s %-6.6s\n"),
1434932Spetede 		    gettext("Host"), gettext("Owner"), gettext("Member"));
1440Sstevel@tonic-gate 		nd = sd->sd_nodelist;
1450Sstevel@tonic-gate 		while (nd) {
1460Sstevel@tonic-gate 			/*
1470Sstevel@tonic-gate 			 * Don't print nodes that aren't ok since they may be
1480Sstevel@tonic-gate 			 * removed from config during a reconfig cycle.  If a
1490Sstevel@tonic-gate 			 * node was being added to a diskset and the entire
1500Sstevel@tonic-gate 			 * cluster went down but the node being added was unable
1510Sstevel@tonic-gate 			 * to reboot, there's no way to know if that node had
1520Sstevel@tonic-gate 			 * its own node record set to OK or not.  So, node
1530Sstevel@tonic-gate 			 * record is left in ADD state during reconfig cycle.
1540Sstevel@tonic-gate 			 * When that node reboots and returns to the cluster,
1550Sstevel@tonic-gate 			 * the reconfig cycle will either remove the node
1560Sstevel@tonic-gate 			 * record (if not marked OK on that node) or will mark
1570Sstevel@tonic-gate 			 * it OK on all nodes.
1580Sstevel@tonic-gate 			 * It is very important to only remove a node record
1590Sstevel@tonic-gate 			 * from the other nodes when that node record is not
1600Sstevel@tonic-gate 			 * marked OK on its own node - otherwise, different
1610Sstevel@tonic-gate 			 * nodes would have different nodelists possibly
1620Sstevel@tonic-gate 			 * causing different nodes to to choose different
1630Sstevel@tonic-gate 			 * masters.
1640Sstevel@tonic-gate 			 */
1650Sstevel@tonic-gate 			if (!(nd->nd_flags & MD_MN_NODE_OK)) {
1660Sstevel@tonic-gate 				nd = nd->nd_next;
1670Sstevel@tonic-gate 				continue;
1680Sstevel@tonic-gate 			}
1690Sstevel@tonic-gate 			if ((nd->nd_flags & MD_MN_NODE_ALIVE) &&
1700Sstevel@tonic-gate 			    (nd->nd_flags & MD_MN_NODE_OWN)) {
1710Sstevel@tonic-gate 				(void) printf(
1720Sstevel@tonic-gate 				    gettext("  %-17.17s  %-12.12s  %-4.4s\n"),
1730Sstevel@tonic-gate 				    nd->nd_nodename, gettext("multi-owner"),
1740Sstevel@tonic-gate 				    gettext("Yes"));
1754932Spetede 			} else if ((!(nd->nd_flags & MD_MN_NODE_ALIVE)) &&
1760Sstevel@tonic-gate 			    (nd->nd_flags & MD_MN_NODE_OWN)) {
1774932Spetede 				/* Should never be able to happen */
1780Sstevel@tonic-gate 				(void) printf(
1790Sstevel@tonic-gate 				    gettext("  %-17.17s  %-12.12s  %-4.4s\n"),
1800Sstevel@tonic-gate 				    nd->nd_nodename, gettext("multi-owner"),
1810Sstevel@tonic-gate 				    gettext("No"));
1820Sstevel@tonic-gate 			} else if ((nd->nd_flags & MD_MN_NODE_ALIVE) &&
1830Sstevel@tonic-gate 			    (!(nd->nd_flags & MD_MN_NODE_OWN))) {
1840Sstevel@tonic-gate 				(void) printf(
1850Sstevel@tonic-gate 				    gettext("  %-17.17s  %-12.12s  %-4.4s\n"),
1860Sstevel@tonic-gate 				    nd->nd_nodename, gettext(""),
1870Sstevel@tonic-gate 				    gettext("Yes"));
1880Sstevel@tonic-gate 			} else if ((!(nd->nd_flags & MD_MN_NODE_ALIVE)) &&
1890Sstevel@tonic-gate 			    (!(nd->nd_flags & MD_MN_NODE_OWN))) {
1900Sstevel@tonic-gate 				(void) printf(
1910Sstevel@tonic-gate 				    gettext("  %-17.17s  %-12.12s  %-4.4s\n"),
1920Sstevel@tonic-gate 				    nd->nd_nodename, gettext(""),
1930Sstevel@tonic-gate 				    gettext("No"));
1940Sstevel@tonic-gate 			}
1950Sstevel@tonic-gate 			nd = nd->nd_next;
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 	} else {
1980Sstevel@tonic-gate 		(void) printf("\n%-19.19s %-5.5s\n",
1994932Spetede 		    gettext("Host"), gettext("Owner"));
2000Sstevel@tonic-gate 		for (i = 0; i < MD_MAXSIDES; i++) {
2010Sstevel@tonic-gate 			/* Skip empty slots */
2020Sstevel@tonic-gate 			if (sd->sd_nodes[i][0] == '\0')
2030Sstevel@tonic-gate 				continue;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 			/*
2060Sstevel@tonic-gate 			 * Standard hostname field is 17 bytes but metaset will
2070Sstevel@tonic-gate 			 * display up to MD_MAX_NODENAME, def in meta_basic.h
2080Sstevel@tonic-gate 			 */
2090Sstevel@tonic-gate 			(void) printf("  %-17.*s  %s\n", MD_MAX_NODENAME,
2100Sstevel@tonic-gate 			    sd->sd_nodes[i], (sd->sd_flags & MD_SR_AUTO_TAKE ?
2114932Spetede 			    (sd->sd_isown[i] ? gettext("Yes (auto)") :
2124932Spetede 			    gettext("No (auto)"))
2134932Spetede 			    : (sd->sd_isown[i] ? gettext("Yes") : "")));
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	if (sd->sd_med.n_cnt > 0)
2180Sstevel@tonic-gate 		(void) printf("\n%-19.19s %-7.7s\n",
2190Sstevel@tonic-gate 		    gettext("Mediator Host(s)"), gettext("Aliases"));
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	if ((max_meds = get_max_meds(ep)) == 0)
2220Sstevel@tonic-gate 		return (-1);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	for (i = 0; i < max_meds; i++) {
2250Sstevel@tonic-gate 		if (sd->sd_med.n_lst[i].a_cnt == 0)
2260Sstevel@tonic-gate 			continue;
2272063Shshaw 		/*
2282063Shshaw 		 * Standard hostname field is 17 bytes but metaset will
2292063Shshaw 		 * display up to MD_MAX_NODENAME, def in meta_basic.h
2302063Shshaw 		 */
2312063Shshaw 		(void) printf("  %-17.*s   ", MD_MAX_NODENAME,
2324932Spetede 		    sd->sd_med.n_lst[i].a_nm[0]);
2330Sstevel@tonic-gate 		for (j = 1; j < sd->sd_med.n_lst[i].a_cnt; j++) {
2340Sstevel@tonic-gate 			(void) printf("%s", sd->sd_med.n_lst[i].a_nm[j]);
2350Sstevel@tonic-gate 			if (sd->sd_med.n_lst[i].a_cnt - j > 1)
2360Sstevel@tonic-gate 				(void) printf(gettext(", "));
2370Sstevel@tonic-gate 		}
2380Sstevel@tonic-gate 		(void) printf("\n");
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (dd) {
2420Sstevel@tonic-gate 		int	len = 0;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		/*
2460Sstevel@tonic-gate 		 * Building a format string on the fly that will
2470Sstevel@tonic-gate 		 * be used in (f)printf. This allows the length
2480Sstevel@tonic-gate 		 * of the ctd to vary from small to large without
2490Sstevel@tonic-gate 		 * looking horrible.
2500Sstevel@tonic-gate 		 */
2510Sstevel@tonic-gate 		for (p = dd; p != NULL; p = p->dd_next)
2520Sstevel@tonic-gate 			len = max(len, strlen(p->dd_dnp->cname));
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 		len += 2;
2550Sstevel@tonic-gate 		(void) printf("\n%-*.*s %-5.5s\n", len, len,
2560Sstevel@tonic-gate 		    gettext("Drive"),
2570Sstevel@tonic-gate 		    gettext("Dbase"));
2580Sstevel@tonic-gate 		for (p = dd; p != NULL; p = p->dd_next) {
2590Sstevel@tonic-gate 			(void) printf("\n%-*.*s %-5.5s\n", len, len,
2600Sstevel@tonic-gate 			    p->dd_dnp->cname,
2610Sstevel@tonic-gate 			    (p->dd_dbcnt ? gettext("Yes") :
2620Sstevel@tonic-gate 			    gettext("No")));
2630Sstevel@tonic-gate 		}
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	return (0);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate static int
2700Sstevel@tonic-gate printsets(mdsetname_t *sp, md_error_t *ep)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate 	int			i;
2730Sstevel@tonic-gate 	mdsetname_t		*sp1;
2740Sstevel@tonic-gate 	set_t			max_sets;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/*
2770Sstevel@tonic-gate 	 * print setname given.
2780Sstevel@tonic-gate 	 */
2790Sstevel@tonic-gate 	if (! metaislocalset(sp)) {
2800Sstevel@tonic-gate 		if (printset(sp, ep))
2810Sstevel@tonic-gate 			return (-1);
2820Sstevel@tonic-gate 		return (0);
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	if ((max_sets = get_max_sets(ep)) == 0)
2860Sstevel@tonic-gate 		return (-1);
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	/*
2890Sstevel@tonic-gate 	 * Print all known sets
2900Sstevel@tonic-gate 	 */
2910Sstevel@tonic-gate 	for (i = 1; i < max_sets; i++) {
2920Sstevel@tonic-gate 		if ((sp1 = metasetnosetname(i, ep)) == NULL) {
2930Sstevel@tonic-gate 			if (! mdiserror(ep, MDE_NO_SET))
2940Sstevel@tonic-gate 				break;
2950Sstevel@tonic-gate 			mdclrerror(ep);
2960Sstevel@tonic-gate 			continue;
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		if (printset(sp1, ep))
3000Sstevel@tonic-gate 			break;
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 	if (! mdisok(ep))
3030Sstevel@tonic-gate 		return (-1);
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	return (0);
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate  * Print the current versionn of the cluster contract private interface.
3100Sstevel@tonic-gate  */
3110Sstevel@tonic-gate static void
3120Sstevel@tonic-gate printclusterversion()
3130Sstevel@tonic-gate {
314*11053SSurya.Prakki@Sun.COM 	(void) printf("%s\n", METASETIFVERSION);
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate /*
3180Sstevel@tonic-gate  * Print the disks that make up the given disk set. This is used
3190Sstevel@tonic-gate  * exclusively by Sun Cluster and is contract private.
3200Sstevel@tonic-gate  * Should never be called with sname of a Multinode diskset.
3210Sstevel@tonic-gate  */
3220Sstevel@tonic-gate static int
3230Sstevel@tonic-gate printdisksin(char *sname, md_error_t *ep)
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate 	mdsetname_t	*sp;
3260Sstevel@tonic-gate 	md_drive_desc	*dd, *p;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 		/*
3310Sstevel@tonic-gate 		 * During a deletion of a set the associated service is
3320Sstevel@tonic-gate 		 * put offline. The SC3.0 reservation code calls disksuite
3330Sstevel@tonic-gate 		 * to find a list of disks associated with the set so that
3340Sstevel@tonic-gate 		 * it can release the reservation on those disks. In this
3350Sstevel@tonic-gate 		 * case there won't be any disks or even a set left. So just
3360Sstevel@tonic-gate 		 * return.
3370Sstevel@tonic-gate 		 */
3380Sstevel@tonic-gate 		return (0);
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	if (metaget_setownership(sp, ep) == -1)
3420Sstevel@tonic-gate 		return (-1);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	if (((dd = metaget_drivedesc(sp, (MD_BASICNAME_OK | PRINT_FAST),
3450Sstevel@tonic-gate 	    ep)) == NULL) && !mdisok(ep))
3460Sstevel@tonic-gate 		return (-1);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	for (p = dd; p != NULL; p = p->dd_next)
3490Sstevel@tonic-gate 		(void) printf("%s\n", p->dd_dnp->rname);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	return (0);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate static void
3550Sstevel@tonic-gate parse_printset(int argc, char **argv)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	int		c;
3580Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
3590Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
3600Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
3610Sstevel@tonic-gate 	md_error_t	*ep = &status;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	/* reset and parse args */
3640Sstevel@tonic-gate 	optind = 1;
3650Sstevel@tonic-gate 	opterr = 1;
3660Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:")) != -1) {
3670Sstevel@tonic-gate 		switch (c) {
3680Sstevel@tonic-gate 		case 's':
3690Sstevel@tonic-gate 			sname = optarg;
3700Sstevel@tonic-gate 			break;
3710Sstevel@tonic-gate 		default:
3720Sstevel@tonic-gate 			usage(sp, gettext("unknown options"));
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	argc -= optind;
3770Sstevel@tonic-gate 	argv += optind;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if (argc != 0)
3800Sstevel@tonic-gate 		usage(sp, gettext("too many args"));
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
3830Sstevel@tonic-gate 		mde_perror(ep, "");
3840Sstevel@tonic-gate 		md_exit(sp, 1);
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if (printsets(sp, ep) && !mdiserror(ep, MDE_SMF_NO_SERVICE)) {
3880Sstevel@tonic-gate 		mde_perror(ep, "");
3890Sstevel@tonic-gate 		md_exit(sp, 1);
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (meta_smf_isonline(meta_smf_getmask(), ep) == 0) {
3930Sstevel@tonic-gate 		mde_perror(ep, "");
3940Sstevel@tonic-gate 		md_exit(sp, 1);
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	md_exit(sp, 0);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate static void
4010Sstevel@tonic-gate parse_add(int argc, char **argv)
4020Sstevel@tonic-gate {
4034932Spetede 	int			c, created_set;
4044932Spetede 	int			hosts = FALSE;
4054932Spetede 	int			meds = FALSE;
4064932Spetede 	int			auto_take = FALSE;
4074932Spetede 	int			force_label = FALSE;
4084932Spetede 	int			default_size = TRUE;
4090Sstevel@tonic-gate 	mdsetname_t		*sp = NULL;
4100Sstevel@tonic-gate 	char			*sname = MD_LOCAL_NAME;
4114932Spetede 	md_error_t		status = mdnullerror;
4124932Spetede 	md_error_t		 *ep = &status;
4130Sstevel@tonic-gate 	mddrivenamelist_t	*dnlp = NULL;
4140Sstevel@tonic-gate 	mddrivenamelist_t	*p;
4154932Spetede 	daddr_t			dbsize, nblks;
4160Sstevel@tonic-gate 	mdsetname_t		*local_sp = NULL;
4170Sstevel@tonic-gate 	int			multi_node = 0;
4180Sstevel@tonic-gate 	md_set_desc		*sd;
4190Sstevel@tonic-gate 	rval_e			sdssc_rval;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	/* reset and parse args */
4220Sstevel@tonic-gate 	optind = 1;
4230Sstevel@tonic-gate 	opterr = 1;
4240Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "MaA:hl:Lms:")) != -1) {
4250Sstevel@tonic-gate 		switch (c) {
4260Sstevel@tonic-gate 		case 'M':
4270Sstevel@tonic-gate 			multi_node = 1;
4280Sstevel@tonic-gate 			break;
4290Sstevel@tonic-gate 		case 'A':
4300Sstevel@tonic-gate 			/* verified sub-option in main */
4310Sstevel@tonic-gate 			if (strcmp(optarg, "enable") == 0)
4320Sstevel@tonic-gate 				auto_take = TRUE;
4330Sstevel@tonic-gate 			break;
4340Sstevel@tonic-gate 		case 'a':
4350Sstevel@tonic-gate 			break;
4360Sstevel@tonic-gate 		case 'h':
4370Sstevel@tonic-gate 		case 'm':
4380Sstevel@tonic-gate 			if (meds == TRUE || hosts == TRUE)
4390Sstevel@tonic-gate 				usage(sp, gettext(
4400Sstevel@tonic-gate 				    "only one -m or -h option allowed"));
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 			if (default_size == FALSE || force_label == TRUE)
4430Sstevel@tonic-gate 				usage(sp, gettext(
4440Sstevel@tonic-gate 				    "conflicting options"));
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 			if (c == 'h')
4470Sstevel@tonic-gate 				hosts = TRUE;
4480Sstevel@tonic-gate 			else
4490Sstevel@tonic-gate 				meds = TRUE;
4500Sstevel@tonic-gate 			break;
4510Sstevel@tonic-gate 		case 'l':
4520Sstevel@tonic-gate 			if (hosts == TRUE || meds == TRUE)
4530Sstevel@tonic-gate 				usage(sp, gettext(
4540Sstevel@tonic-gate 				    "conflicting options"));
4550Sstevel@tonic-gate 			if (sscanf(optarg, "%ld", &dbsize) != 1) {
4560Sstevel@tonic-gate 				md_eprintf(gettext(
4570Sstevel@tonic-gate 				    "%s: bad format\n"), optarg);
4580Sstevel@tonic-gate 				usage(sp, "");
4590Sstevel@tonic-gate 			}
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 			default_size = FALSE;
4620Sstevel@tonic-gate 			break;
4630Sstevel@tonic-gate 		case 'L':
4640Sstevel@tonic-gate 			/* Same criteria as -l */
4650Sstevel@tonic-gate 			if (hosts == TRUE || meds == TRUE)
4660Sstevel@tonic-gate 				usage(sp, gettext(
4670Sstevel@tonic-gate 				    "conflicting options"));
4680Sstevel@tonic-gate 			force_label = TRUE;
4690Sstevel@tonic-gate 			break;
4700Sstevel@tonic-gate 		case 's':
4710Sstevel@tonic-gate 			sname = optarg;
4720Sstevel@tonic-gate 			break;
4730Sstevel@tonic-gate 		default:
4740Sstevel@tonic-gate 			usage(sp, gettext(
4750Sstevel@tonic-gate 			    "unknown options"));
4760Sstevel@tonic-gate 		}
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/* Can only use -A enable when creating the single-node set */
4800Sstevel@tonic-gate 	if (auto_take && hosts != TRUE)
4810Sstevel@tonic-gate 		usage(sp, gettext("conflicting options"));
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	argc -= optind;
4840Sstevel@tonic-gate 	argv += optind;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	/*
4870Sstevel@tonic-gate 	 * Add hosts
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 	if (hosts == TRUE) {
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 		if ((local_sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) {
4920Sstevel@tonic-gate 			mde_perror(ep, "");
4930Sstevel@tonic-gate 			md_exit(local_sp, 1);
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		if (meta_lock(local_sp, TRUE, ep) != 0) {
4970Sstevel@tonic-gate 			mde_perror(ep, "");
4980Sstevel@tonic-gate 			md_exit(local_sp, 1);
4990Sstevel@tonic-gate 		}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 		/*
5020Sstevel@tonic-gate 		 * Keep track of Cluster set creation. Need to complete
5030Sstevel@tonic-gate 		 * the transaction no matter if the set was created or not.
5040Sstevel@tonic-gate 		 */
5050Sstevel@tonic-gate 		created_set = 0;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 		/*
5080Sstevel@tonic-gate 		 * Have no set, cannot take the lock, so only take the
5090Sstevel@tonic-gate 		 * local lock.
5100Sstevel@tonic-gate 		 */
5110Sstevel@tonic-gate 		if ((sp = metasetname(sname, ep)) == NULL) {
5120Sstevel@tonic-gate 			sdssc_rval = 0;
5130Sstevel@tonic-gate 			if (multi_node) {
5140Sstevel@tonic-gate 				/*
5150Sstevel@tonic-gate 				 * When running on a cluster system that
5160Sstevel@tonic-gate 				 * does not support MN disksets, the routine
5170Sstevel@tonic-gate 				 * sdssc_mo_create_begin will be bound
5180Sstevel@tonic-gate 				 * to the SVM routine not_bound_error
5190Sstevel@tonic-gate 				 * which returns SDSSC_NOT_BOUND_ERROR.
5200Sstevel@tonic-gate 				 *
5210Sstevel@tonic-gate 				 * When running on a cluster system that
5220Sstevel@tonic-gate 				 * does support MN disksets, the routine
5230Sstevel@tonic-gate 				 * sdssc_mo_create_begin will be bound to
5240Sstevel@tonic-gate 				 * the sdssc_mo_create_begin routine in
5250Sstevel@tonic-gate 				 * library libsdssc_so.  A call to
5260Sstevel@tonic-gate 				 * sdssc_mo_create_begin will return with
5270Sstevel@tonic-gate 				 * either SDSSC_ERROR or SDSSC_OKAY. If
5280Sstevel@tonic-gate 				 * an SDSSC_OKAY is returned, then the
5290Sstevel@tonic-gate 				 * cluster framework has allocated a
5300Sstevel@tonic-gate 				 * set number for this new set that is unique
5310Sstevel@tonic-gate 				 * across traditional and MN disksets.
5320Sstevel@tonic-gate 				 * Libmeta will get this unique set number
5330Sstevel@tonic-gate 				 * by calling sdssc_get_index.
5340Sstevel@tonic-gate 				 *
5350Sstevel@tonic-gate 				 * When running on a non-cluster system,
5360Sstevel@tonic-gate 				 * the routine sdssc_mo_create_begin
5370Sstevel@tonic-gate 				 * will be bound to the SVM routine
5380Sstevel@tonic-gate 				 * not_bound which returns SDSSC_NOT_BOUND.
5390Sstevel@tonic-gate 				 * In this case, all sdssc routines will
5400Sstevel@tonic-gate 				 * return SDSSC_NOT_BOUND.  No need to check
5410Sstevel@tonic-gate 				 * for return value of SDSSC_NOT_BOUND since
5420Sstevel@tonic-gate 				 * the libmeta call to get the set number
5430Sstevel@tonic-gate 				 * (sdssc_get_index) will also fail with
5440Sstevel@tonic-gate 				 * SDSSC_NOT_BOUND causing libmeta to
5450Sstevel@tonic-gate 				 * determine its own set number.
5460Sstevel@tonic-gate 				 */
5470Sstevel@tonic-gate 				sdssc_rval = sdssc_mo_create_begin(sname, argc,
5484932Spetede 				    argv, SDSSC_PICK_SETNO);
5490Sstevel@tonic-gate 				if (sdssc_rval == SDSSC_NOT_BOUND_ERROR) {
550*11053SSurya.Prakki@Sun.COM 					(void) mderror(ep, MDE_NOT_MN, NULL);
5510Sstevel@tonic-gate 					mde_perror(ep,
5520Sstevel@tonic-gate 					"Cluster node does not support "
5530Sstevel@tonic-gate 					"multi-owner diskset operations");
5540Sstevel@tonic-gate 					md_exit(local_sp, 1);
5550Sstevel@tonic-gate 				} else if (sdssc_rval == SDSSC_ERROR) {
5560Sstevel@tonic-gate 					mde_perror(ep, "");
5570Sstevel@tonic-gate 					md_exit(local_sp, 1);
5580Sstevel@tonic-gate 				}
5590Sstevel@tonic-gate 			} else {
5600Sstevel@tonic-gate 				sdssc_rval = sdssc_create_begin(sname, argc,
5614932Spetede 				    argv, SDSSC_PICK_SETNO);
5620Sstevel@tonic-gate 				if (sdssc_rval == SDSSC_ERROR) {
5630Sstevel@tonic-gate 					mde_perror(ep, "");
5640Sstevel@tonic-gate 					md_exit(local_sp, 1);
5650Sstevel@tonic-gate 				}
5660Sstevel@tonic-gate 			}
5670Sstevel@tonic-gate 			/*
5680Sstevel@tonic-gate 			 * Created diskset (as opposed to adding a
5690Sstevel@tonic-gate 			 * host to an existing diskset).
5700Sstevel@tonic-gate 			 */
5710Sstevel@tonic-gate 			created_set = 1;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 			sp = Zalloc(sizeof (*sp));
5740Sstevel@tonic-gate 			sp->setname = Strdup(sname);
5750Sstevel@tonic-gate 			sp->lockfd = MD_NO_LOCK;
5760Sstevel@tonic-gate 			mdclrerror(ep);
5770Sstevel@tonic-gate 		} else {
5780Sstevel@tonic-gate 			if ((sd = metaget_setdesc(sp, ep)) == NULL) {
5790Sstevel@tonic-gate 				mde_perror(ep, "");
5800Sstevel@tonic-gate 				md_exit(local_sp, 1);
5810Sstevel@tonic-gate 			}
5820Sstevel@tonic-gate 			if (MD_MNSET_DESC(sd)) {
5830Sstevel@tonic-gate 				multi_node = 1;
5840Sstevel@tonic-gate 			}
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 			/*
5870Sstevel@tonic-gate 			 * can't add hosts to an existing set & enable
5880Sstevel@tonic-gate 			 * auto-take
5890Sstevel@tonic-gate 			 */
5900Sstevel@tonic-gate 			if (auto_take)
5910Sstevel@tonic-gate 				usage(sp, gettext("conflicting options"));
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 			/*
5940Sstevel@tonic-gate 			 * Have a valid set, take the set lock also.
5950Sstevel@tonic-gate 			 *
5960Sstevel@tonic-gate 			 * A MN diskset does not use the set meta_lock but
5970Sstevel@tonic-gate 			 * instead uses the clnt_lock of rpc.metad and the
5980Sstevel@tonic-gate 			 * suspend/resume feature of the rpc.mdcommd.  Can't
5990Sstevel@tonic-gate 			 * use set meta_lock since class 1 messages are
6000Sstevel@tonic-gate 			 * grabbing this lock and if this thread is holding
6010Sstevel@tonic-gate 			 * the set meta_lock then no rpc.mdcommd suspend
6020Sstevel@tonic-gate 			 * can occur.
6030Sstevel@tonic-gate 			 */
6040Sstevel@tonic-gate 			if (!multi_node) {
6050Sstevel@tonic-gate 				if (meta_lock(sp, TRUE, ep) != 0) {
6060Sstevel@tonic-gate 					mde_perror(ep, "");
6070Sstevel@tonic-gate 					md_exit(local_sp, 1);
6080Sstevel@tonic-gate 				}
6090Sstevel@tonic-gate 			}
6100Sstevel@tonic-gate 		}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 		if (meta_set_addhosts(sp, multi_node, argc, argv, auto_take,
6130Sstevel@tonic-gate 		    ep)) {
6140Sstevel@tonic-gate 			if (created_set)
6150Sstevel@tonic-gate 				sdssc_create_end(sname, SDSSC_CLEANUP);
6160Sstevel@tonic-gate 			mde_perror(&status, "");
6170Sstevel@tonic-gate 			if (!multi_node)
6180Sstevel@tonic-gate 				(void) meta_unlock(sp, ep);
6190Sstevel@tonic-gate 			md_exit(local_sp, 1);
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		if (created_set)
6230Sstevel@tonic-gate 			sdssc_create_end(sname, SDSSC_COMMIT);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		else {
6260Sstevel@tonic-gate 			/*
6270Sstevel@tonic-gate 			 * If adding hosts to existing diskset,
6280Sstevel@tonic-gate 			 * call DCS svcs
6290Sstevel@tonic-gate 			 */
6300Sstevel@tonic-gate 			sdssc_add_hosts(sname, argc, argv);
6310Sstevel@tonic-gate 		}
6320Sstevel@tonic-gate 		if (!multi_node)
6330Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
6340Sstevel@tonic-gate 		md_exit(local_sp, 0);
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	/*
6380Sstevel@tonic-gate 	 * Add mediators
6390Sstevel@tonic-gate 	 */
6400Sstevel@tonic-gate 	if (meds == TRUE) {
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		if ((sp = metasetname(sname, ep)) == NULL) {
6430Sstevel@tonic-gate 			mde_perror(ep, "");
6440Sstevel@tonic-gate 			md_exit(local_sp, 1);
6450Sstevel@tonic-gate 		}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 		if ((local_sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) {
6480Sstevel@tonic-gate 			mde_perror(ep, "");
6490Sstevel@tonic-gate 			md_exit(local_sp, 1);
6500Sstevel@tonic-gate 		}
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 		if ((sd = metaget_setdesc(sp, ep)) == NULL) {
6530Sstevel@tonic-gate 			mde_perror(ep, "");
6540Sstevel@tonic-gate 			md_exit(local_sp, 1);
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 		if (MD_MNSET_DESC(sd)) {
6570Sstevel@tonic-gate 			multi_node = 1;
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 		if (meta_lock(local_sp, TRUE, ep) != 0) {
6610Sstevel@tonic-gate 			mde_perror(ep, "");
6620Sstevel@tonic-gate 			md_exit(local_sp, 1);
6630Sstevel@tonic-gate 		}
6640Sstevel@tonic-gate 		/*
6650Sstevel@tonic-gate 		 * A MN diskset does not use the set meta_lock but
6660Sstevel@tonic-gate 		 * instead uses the clnt_lock of rpc.metad and the
6670Sstevel@tonic-gate 		 * suspend/resume feature of the rpc.mdcommd.  Can't
6680Sstevel@tonic-gate 		 * use set meta_lock since class 1 messages are
6690Sstevel@tonic-gate 		 * grabbing this lock and if this thread is holding
6700Sstevel@tonic-gate 		 * the set meta_lock then no rpc.mdcommd suspend
6710Sstevel@tonic-gate 		 * can occur.
6720Sstevel@tonic-gate 		 */
6730Sstevel@tonic-gate 		if (!multi_node) {
6740Sstevel@tonic-gate 			if (meta_lock(sp, TRUE, ep) != 0) {
6750Sstevel@tonic-gate 				mde_perror(ep, "");
6760Sstevel@tonic-gate 				md_exit(local_sp, 1);
6770Sstevel@tonic-gate 			}
6780Sstevel@tonic-gate 		}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		if (meta_set_addmeds(sp, argc, argv, ep)) {
6810Sstevel@tonic-gate 			mde_perror(&status, "");
6820Sstevel@tonic-gate 			if (!multi_node)
6830Sstevel@tonic-gate 				(void) meta_unlock(sp, ep);
6840Sstevel@tonic-gate 			md_exit(local_sp, 1);
6850Sstevel@tonic-gate 		}
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 		if (!multi_node)
6880Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
6890Sstevel@tonic-gate 		md_exit(local_sp, 0);
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * Add drives
6940Sstevel@tonic-gate 	 */
6950Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
6960Sstevel@tonic-gate 		mde_perror(ep, "");
6970Sstevel@tonic-gate 		md_exit(local_sp, 1);
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	if ((local_sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) {
7010Sstevel@tonic-gate 		mde_perror(ep, "");
7020Sstevel@tonic-gate 		md_exit(local_sp, 1);
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	/* Determine if diskset is a MN diskset or not */
7060Sstevel@tonic-gate 	if ((sd = metaget_setdesc(sp, ep)) == NULL) {
7070Sstevel@tonic-gate 		mde_perror(ep, "");
7080Sstevel@tonic-gate 		md_exit(local_sp, 1);
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 	if (MD_MNSET_DESC(sd)) {
7110Sstevel@tonic-gate 		multi_node = 1;
7120Sstevel@tonic-gate 	}
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	if (meta_lock(local_sp, TRUE, ep) != 0) {
7150Sstevel@tonic-gate 		mde_perror(ep, "");
7160Sstevel@tonic-gate 		md_exit(local_sp, 1);
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	/* Make sure database size is within limits */
7200Sstevel@tonic-gate 	if (default_size == FALSE) {
7210Sstevel@tonic-gate 		if ((multi_node && dbsize < MDDB_MN_MINBLKS) ||
7220Sstevel@tonic-gate 		    (!multi_node && dbsize < MDDB_MINBLKS))
7230Sstevel@tonic-gate 			usage(sp, gettext(
7240Sstevel@tonic-gate 			    "size (-l) is too small"));
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 		if ((multi_node && dbsize > MDDB_MN_MAXBLKS) ||
7270Sstevel@tonic-gate 		    (!multi_node && dbsize > MDDB_MAXBLKS))
7280Sstevel@tonic-gate 			usage(sp, gettext(
7290Sstevel@tonic-gate 			    "size (-l) is too big"));
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	/*
7330Sstevel@tonic-gate 	 * Have a valid set, take the set lock also.
7340Sstevel@tonic-gate 	 *
7350Sstevel@tonic-gate 	 * A MN diskset does not use the set meta_lock but
7360Sstevel@tonic-gate 	 * instead uses the clnt_lock of rpc.metad and the
7370Sstevel@tonic-gate 	 * suspend/resume feature of the rpc.mdcommd.  Can't
7380Sstevel@tonic-gate 	 * use set meta_lock since class 1 messages are
7390Sstevel@tonic-gate 	 * grabbing this lock and if this thread is holding
7400Sstevel@tonic-gate 	 * the set meta_lock then no rpc.mdcommd suspend
7410Sstevel@tonic-gate 	 * can occur.
7420Sstevel@tonic-gate 	 */
7430Sstevel@tonic-gate 	if (!multi_node) {
7440Sstevel@tonic-gate 		if (meta_lock(sp, TRUE, ep) != 0) {
7450Sstevel@tonic-gate 			mde_perror(ep, "");
7460Sstevel@tonic-gate 			md_exit(local_sp, 1);
7470Sstevel@tonic-gate 		}
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/*
7520Sstevel@tonic-gate 	 * If using the default size,
7530Sstevel@tonic-gate 	 *   then let's adjust the default to the minimum
7540Sstevel@tonic-gate 	 *   size currently in use.
7550Sstevel@tonic-gate 	 */
7560Sstevel@tonic-gate 	if (default_size) {
7570Sstevel@tonic-gate 		dbsize = multi_node ? MD_MN_DBSIZE : MD_DBSIZE;
7580Sstevel@tonic-gate 		if ((nblks = meta_db_minreplica(sp, ep)) < 0)
7590Sstevel@tonic-gate 			mdclrerror(ep);
7600Sstevel@tonic-gate 		else
7610Sstevel@tonic-gate 			dbsize = nblks;	/* adjust replica size */
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	if ((c = metadrivenamelist(&sp, &dnlp, argc, argv, ep)) < 0) {
7650Sstevel@tonic-gate 		mde_perror(ep, "");
7660Sstevel@tonic-gate 		if (!multi_node)
7670Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
7680Sstevel@tonic-gate 		md_exit(local_sp, 1);
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	if (c == 0) {
7720Sstevel@tonic-gate 		md_perror(gettext(
7730Sstevel@tonic-gate 		    "No drives specified to add.\n"));
7740Sstevel@tonic-gate 		if (!multi_node)
7750Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
7760Sstevel@tonic-gate 		md_exit(local_sp, 1);
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	if (meta_set_adddrives(sp, dnlp, dbsize, force_label, ep)) {
7800Sstevel@tonic-gate 		metafreedrivenamelist(dnlp);
7810Sstevel@tonic-gate 		mde_perror(ep, "");
7820Sstevel@tonic-gate 		if (!multi_node)
7830Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
7840Sstevel@tonic-gate 		md_exit(local_sp, 1);
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	/*
7880Sstevel@tonic-gate 	 * MN disksets don't have a device id in the master block
7890Sstevel@tonic-gate 	 * For traditional disksets, check for the drive device
7900Sstevel@tonic-gate 	 * id not fitting in the master block
7910Sstevel@tonic-gate 	 */
7920Sstevel@tonic-gate 	if (!multi_node) {
7930Sstevel@tonic-gate 		for (p = dnlp; p != NULL; p = p->next) {
7940Sstevel@tonic-gate 			int 		fd;
7950Sstevel@tonic-gate 			ddi_devid_t	devid;
7960Sstevel@tonic-gate 			mdname_t	*np;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 			np = metaslicename(p->drivenamep, 0, ep);
7990Sstevel@tonic-gate 			if (np == NULL)
8000Sstevel@tonic-gate 				continue;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 			if ((fd = open(np->rname, O_RDONLY | O_NDELAY)) < 0)
8030Sstevel@tonic-gate 				continue;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 			if (devid_get(fd, &devid) == 0) {
8060Sstevel@tonic-gate 				size_t len;
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 				len = devid_sizeof(devid);
8090Sstevel@tonic-gate 				if (len > (DEV_BSIZE - sizeof (mddb_mb_t)))
8100Sstevel@tonic-gate 					(void) mddserror(ep,
8110Sstevel@tonic-gate 					    MDE_DS_NOTSELFIDENTIFY, NULL, NULL,
8120Sstevel@tonic-gate 					    np->rname, NULL);
8130Sstevel@tonic-gate 				devid_free(devid);
8140Sstevel@tonic-gate 			} else {
8150Sstevel@tonic-gate 				(void) mddserror(ep, MDE_DS_NOTSELFIDENTIFY,
8160Sstevel@tonic-gate 				    NULL, NULL, np->rname, NULL);
8170Sstevel@tonic-gate 			}
8180Sstevel@tonic-gate 			(void) close(fd);
8190Sstevel@tonic-gate 		}
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	/*
8230Sstevel@tonic-gate 	 * MN disksets don't use DCS clustering services.
8240Sstevel@tonic-gate 	 * For traditional disksets:
8250Sstevel@tonic-gate 	 * There's not really much we can do here if this call fails.
8260Sstevel@tonic-gate 	 * The drives have been added to the set and DiskSuite believes
8270Sstevel@tonic-gate 	 * it owns the drives.
8280Sstevel@tonic-gate 	 * Relase the set and hope for the best.
8290Sstevel@tonic-gate 	 */
8300Sstevel@tonic-gate 	if ((!multi_node) &&
8310Sstevel@tonic-gate 	    (sdssc_notify_service(sname, Make_Primary) == SDSSC_ERROR)) {
832*11053SSurya.Prakki@Sun.COM 		(void) meta_set_release(sp, ep);
833*11053SSurya.Prakki@Sun.COM 		(void) printf(gettext(
8340Sstevel@tonic-gate 		    "Sun Clustering failed to make set primary\n"));
8350Sstevel@tonic-gate 	}
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	metafreedrivenamelist(dnlp);
8380Sstevel@tonic-gate 	if (!multi_node)
8390Sstevel@tonic-gate 		(void) meta_unlock(sp, ep);
8400Sstevel@tonic-gate 	md_exit(local_sp, 0);
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate static void
8440Sstevel@tonic-gate parse_balance(int argc, char **argv)
8450Sstevel@tonic-gate {
8460Sstevel@tonic-gate 	int		c;
8470Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
8480Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
8490Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
8500Sstevel@tonic-gate 	md_set_desc	*sd;
8510Sstevel@tonic-gate 	int		multi_node = 0;
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	/* reset and parse args */
8540Sstevel@tonic-gate 	optind = 1;
8550Sstevel@tonic-gate 	opterr = 1;
8560Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Mbs:")) != -1) {
8570Sstevel@tonic-gate 		switch (c) {
8580Sstevel@tonic-gate 		case 'M':
8590Sstevel@tonic-gate 			break;
8600Sstevel@tonic-gate 		case 'b':
8610Sstevel@tonic-gate 			break;
8620Sstevel@tonic-gate 		case 's':
8630Sstevel@tonic-gate 			sname = optarg;
8640Sstevel@tonic-gate 			break;
8650Sstevel@tonic-gate 		default:
8660Sstevel@tonic-gate 			usage(sp, gettext("unknown options"));
8670Sstevel@tonic-gate 		}
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	argc -= optind;
8710Sstevel@tonic-gate 	argv += optind;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	if (argc != 0)
8740Sstevel@tonic-gate 		usage(sp, gettext("too many args"));
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	if ((sp = metasetname(sname, &status)) == NULL) {
8770Sstevel@tonic-gate 		mde_perror(&status, "");
8780Sstevel@tonic-gate 		md_exit(sp, 1);
8790Sstevel@tonic-gate 	}
8800Sstevel@tonic-gate 	if ((sd = metaget_setdesc(sp, &status)) == NULL) {
8810Sstevel@tonic-gate 		mde_perror(&status, "");
8820Sstevel@tonic-gate 		md_exit(sp, 1);
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 	if (MD_MNSET_DESC(sd)) {
8850Sstevel@tonic-gate 		multi_node = 1;
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 	/*
8880Sstevel@tonic-gate 	 * Have a valid set, take the set lock also.
8890Sstevel@tonic-gate 	 *
8900Sstevel@tonic-gate 	 * A MN diskset does not use the set meta_lock but
8910Sstevel@tonic-gate 	 * instead uses the clnt_lock of rpc.metad and the
8920Sstevel@tonic-gate 	 * suspend/resume feature of the rpc.mdcommd.  Can't
8930Sstevel@tonic-gate 	 * use set meta_lock since class 1 messages are
8940Sstevel@tonic-gate 	 * grabbing this lock and if this thread is holding
8950Sstevel@tonic-gate 	 * the set meta_lock then no rpc.mdcommd suspend
8960Sstevel@tonic-gate 	 * can occur.
8970Sstevel@tonic-gate 	 */
8980Sstevel@tonic-gate 	if (!multi_node) {
8990Sstevel@tonic-gate 		if (meta_lock(sp, TRUE, &status) != 0) {
9000Sstevel@tonic-gate 			mde_perror(&status, "");
9010Sstevel@tonic-gate 			md_exit(sp, 1);
9020Sstevel@tonic-gate 		}
9030Sstevel@tonic-gate 	}
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	if (meta_set_balance(sp, &status) != 0) {
9060Sstevel@tonic-gate 		mde_perror(&status, "");
9070Sstevel@tonic-gate 		md_exit(sp, 1);
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 	md_exit(sp, 0);
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate static void
9130Sstevel@tonic-gate parse_autotake(int argc, char **argv)
9140Sstevel@tonic-gate {
9150Sstevel@tonic-gate 	int			c;
9160Sstevel@tonic-gate 	int			enable = 0;
9170Sstevel@tonic-gate 	mdsetname_t		*sp = NULL;
9180Sstevel@tonic-gate 	char			*sname = MD_LOCAL_NAME;
9190Sstevel@tonic-gate 	md_error_t		status = mdnullerror;
9200Sstevel@tonic-gate 	md_error_t		*ep = &status;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	/* reset and parse args */
9230Sstevel@tonic-gate 	optind = 1;
9240Sstevel@tonic-gate 	opterr = 1;
9250Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "A:s:")) != -1) {
9260Sstevel@tonic-gate 		switch (c) {
9270Sstevel@tonic-gate 		case 'A':
9280Sstevel@tonic-gate 			/* verified sub-option in main */
9290Sstevel@tonic-gate 			if (strcmp(optarg, "enable") == 0)
9300Sstevel@tonic-gate 				enable = 1;
9310Sstevel@tonic-gate 			break;
9320Sstevel@tonic-gate 		case 's':
9330Sstevel@tonic-gate 			/* verified presence of setname in main */
9340Sstevel@tonic-gate 			sname = optarg;
9350Sstevel@tonic-gate 			break;
9360Sstevel@tonic-gate 		default:
9370Sstevel@tonic-gate 			usage(sp, gettext("unknown options"));
9380Sstevel@tonic-gate 		}
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
9420Sstevel@tonic-gate 		mde_perror(ep, "");
9430Sstevel@tonic-gate 		md_exit(sp, 1);
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, ep) != 0) {
9470Sstevel@tonic-gate 		mde_perror(ep, "");
9480Sstevel@tonic-gate 		md_exit(sp, 1);
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
9520Sstevel@tonic-gate 		mde_perror(ep, "");
9530Sstevel@tonic-gate 		md_exit(sp, 1);
9540Sstevel@tonic-gate 	}
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	if (meta_set_auto_take(sp, enable, ep) != 0) {
9570Sstevel@tonic-gate 		mde_perror(ep, "");
9580Sstevel@tonic-gate 		md_exit(sp, 1);
9590Sstevel@tonic-gate 	}
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	md_exit(sp, 0);
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate static void
9650Sstevel@tonic-gate parse_del(int argc, char **argv)
9660Sstevel@tonic-gate {
9670Sstevel@tonic-gate 	int			c;
9680Sstevel@tonic-gate 	mdsetname_t		*sp = NULL;
9690Sstevel@tonic-gate 	char			*sname = MD_LOCAL_NAME;
9700Sstevel@tonic-gate 	int			hosts = FALSE;
9710Sstevel@tonic-gate 	int			meds = FALSE;
9720Sstevel@tonic-gate 	int			forceflg = FALSE;
9730Sstevel@tonic-gate 	md_error_t		status = mdnullerror;
9740Sstevel@tonic-gate 	md_error_t		*ep = &status;
9750Sstevel@tonic-gate 	mddrivenamelist_t	*dnlp = NULL;
9760Sstevel@tonic-gate 	mdsetname_t		*local_sp = NULL;
9770Sstevel@tonic-gate 	md_set_desc		*sd;
9780Sstevel@tonic-gate 	int			multi_node = 0;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	/* reset and parse args */
9810Sstevel@tonic-gate 	optind = 1;
9820Sstevel@tonic-gate 	opterr = 1;
9830Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Mdfhms:")) != -1) {
9840Sstevel@tonic-gate 		switch (c) {
9850Sstevel@tonic-gate 		case 'M':
9860Sstevel@tonic-gate 			break;
9870Sstevel@tonic-gate 		case 'd':
9880Sstevel@tonic-gate 			break;
9890Sstevel@tonic-gate 		case 'f':
9900Sstevel@tonic-gate 			forceflg = TRUE;
9910Sstevel@tonic-gate 			break;
9920Sstevel@tonic-gate 		case 'h':
9930Sstevel@tonic-gate 		case 'm':
9940Sstevel@tonic-gate 			if (meds == TRUE || hosts == TRUE)
9950Sstevel@tonic-gate 				usage(sp, gettext(
9960Sstevel@tonic-gate 				    "only one -m or -h option allowed"));
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 			if (c == 'h')
9990Sstevel@tonic-gate 				hosts = TRUE;
10000Sstevel@tonic-gate 			else
10010Sstevel@tonic-gate 				meds = TRUE;
10020Sstevel@tonic-gate 			break;
10030Sstevel@tonic-gate 		case 's':
10040Sstevel@tonic-gate 			sname = optarg;
10050Sstevel@tonic-gate 			break;
10060Sstevel@tonic-gate 		default:
10070Sstevel@tonic-gate 			usage(sp, gettext("unknown options"));
10080Sstevel@tonic-gate 		}
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	argc -= optind;
10120Sstevel@tonic-gate 	argv += optind;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
10150Sstevel@tonic-gate 		mde_perror(ep, "");
10160Sstevel@tonic-gate 		md_exit(local_sp, 1);
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	if ((local_sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) {
10200Sstevel@tonic-gate 		mde_perror(ep, "");
10210Sstevel@tonic-gate 		md_exit(local_sp, 1);
10220Sstevel@tonic-gate 	}
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	if ((sd = metaget_setdesc(sp, ep)) == NULL) {
10250Sstevel@tonic-gate 		mde_perror(ep, "");
10260Sstevel@tonic-gate 		md_exit(local_sp, 1);
10270Sstevel@tonic-gate 	}
10280Sstevel@tonic-gate 	if (MD_MNSET_DESC(sd))
10290Sstevel@tonic-gate 		multi_node = 1;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	if (meta_lock(local_sp, TRUE, ep) != 0) {
10320Sstevel@tonic-gate 		mde_perror(ep, "");
10330Sstevel@tonic-gate 		md_exit(local_sp, 1);
10340Sstevel@tonic-gate 	}
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	/*
10370Sstevel@tonic-gate 	 * Have a valid set, take the set lock also.
10380Sstevel@tonic-gate 	 *
10390Sstevel@tonic-gate 	 * A MN diskset does not use the set meta_lock but
10400Sstevel@tonic-gate 	 * instead uses the clnt_lock of rpc.metad and the
10410Sstevel@tonic-gate 	 * suspend/resume feature of the rpc.mdcommd.  Can't
10420Sstevel@tonic-gate 	 * use set meta_lock since class 1 messages are
10430Sstevel@tonic-gate 	 * grabbing this lock and if this thread is holding
10440Sstevel@tonic-gate 	 * the set meta_lock then no rpc.mdcommd suspend
10450Sstevel@tonic-gate 	 * can occur.
10460Sstevel@tonic-gate 	 */
10470Sstevel@tonic-gate 	if (!multi_node) {
10480Sstevel@tonic-gate 		if (meta_lock(sp, TRUE, ep) != 0) {
10490Sstevel@tonic-gate 			mde_perror(ep, "");
10500Sstevel@tonic-gate 			md_exit(local_sp, 1);
10510Sstevel@tonic-gate 		}
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	/*
10550Sstevel@tonic-gate 	 * Delete hosts
10560Sstevel@tonic-gate 	 */
10570Sstevel@tonic-gate 	if (hosts == TRUE) {
10580Sstevel@tonic-gate 		if (meta_check_ownership(sp, ep) != 0) {
10590Sstevel@tonic-gate 			/*
10600Sstevel@tonic-gate 			 * If we don't own the set bail out here otherwise
10610Sstevel@tonic-gate 			 * we could delete the node from the DCS service
10620Sstevel@tonic-gate 			 * yet not delete the host from the set.
10630Sstevel@tonic-gate 			 */
10640Sstevel@tonic-gate 			mde_perror(ep, "");
10650Sstevel@tonic-gate 			if (!multi_node)
10660Sstevel@tonic-gate 				(void) meta_unlock(sp, ep);
10670Sstevel@tonic-gate 			md_exit(local_sp, 1);
10680Sstevel@tonic-gate 		}
10690Sstevel@tonic-gate 		if (sdssc_delete_hosts(sname, argc, argv) == SDSSC_ERROR) {
10704932Spetede 			if (!metad_isautotakebyname(sname)) {
10714932Spetede 				/*
10724932Spetede 				 * SC could have been installed after the set
10734932Spetede 				 * was created. We still want to be able to
10744932Spetede 				 * delete these sets.
10754932Spetede 				 */
10764932Spetede 				md_perror(gettext(
10774932Spetede 				    "Failed to delete hosts from DCS service"));
10784932Spetede 				if (!multi_node)
10794932Spetede 					(void) meta_unlock(sp, ep);
10804932Spetede 				md_exit(local_sp, 1);
10814932Spetede 			}
10820Sstevel@tonic-gate 		}
10830Sstevel@tonic-gate 		if (meta_set_deletehosts(sp, argc, argv, forceflg, ep)) {
10840Sstevel@tonic-gate 			if (sdssc_add_hosts(sname, argc, argv) == SDSSC_ERROR) {
10850Sstevel@tonic-gate 				(void) printf(gettext(
10860Sstevel@tonic-gate 				    "Failed to restore host(s) in DCS "
10870Sstevel@tonic-gate 				    "database\n"));
10880Sstevel@tonic-gate 			}
10890Sstevel@tonic-gate 			mde_perror(ep, "");
10900Sstevel@tonic-gate 			if (!multi_node)
10910Sstevel@tonic-gate 				(void) meta_unlock(sp, ep);
10920Sstevel@tonic-gate 			md_exit(local_sp, 1);
10930Sstevel@tonic-gate 		}
10940Sstevel@tonic-gate 		if (!multi_node)
10950Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
10960Sstevel@tonic-gate 		md_exit(local_sp, 0);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	/*
11000Sstevel@tonic-gate 	 * Delete mediators
11010Sstevel@tonic-gate 	 */
11020Sstevel@tonic-gate 	if (meds == TRUE) {
11030Sstevel@tonic-gate 		if (meta_set_deletemeds(sp, argc, argv, forceflg, ep)) {
11040Sstevel@tonic-gate 			mde_perror(ep, "");
11050Sstevel@tonic-gate 			if (!multi_node)
11060Sstevel@tonic-gate 				(void) meta_unlock(sp, ep);
11070Sstevel@tonic-gate 			md_exit(local_sp, 1);
11080Sstevel@tonic-gate 		}
11090Sstevel@tonic-gate 		if (!multi_node)
11100Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
11110Sstevel@tonic-gate 		md_exit(local_sp, 0);
11120Sstevel@tonic-gate 	}
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	/*
11150Sstevel@tonic-gate 	 * Delete drives
11160Sstevel@tonic-gate 	 */
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	if ((c = metadrivenamelist(&sp, &dnlp, argc, argv, ep)) < 0) {
11190Sstevel@tonic-gate 		mde_perror(ep, "");
11200Sstevel@tonic-gate 		if (!multi_node)
11210Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
11220Sstevel@tonic-gate 		md_exit(local_sp, 1);
11230Sstevel@tonic-gate 	}
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	if (c == 0) {
11260Sstevel@tonic-gate 		md_perror(gettext(
11270Sstevel@tonic-gate 		    "No drives specified to delete.\n"));
11280Sstevel@tonic-gate 		if (!multi_node)
11290Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
11300Sstevel@tonic-gate 		md_exit(local_sp, 1);
11310Sstevel@tonic-gate 	}
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	if (meta_set_deletedrives(sp, dnlp, forceflg, ep)) {
11340Sstevel@tonic-gate 		metafreedrivenamelist(dnlp);
11350Sstevel@tonic-gate 		mde_perror(ep, "");
11360Sstevel@tonic-gate 		if (!multi_node)
11370Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
11380Sstevel@tonic-gate 		md_exit(local_sp, 1);
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	metafreedrivenamelist(dnlp);
11420Sstevel@tonic-gate 	if (!multi_node)
11430Sstevel@tonic-gate 		(void) meta_unlock(sp, ep);
11440Sstevel@tonic-gate 	md_exit(local_sp, 0);
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate static void
11480Sstevel@tonic-gate parse_isowner(int argc, char **argv)
11490Sstevel@tonic-gate {
11500Sstevel@tonic-gate 	int		c;
11510Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
11520Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
11530Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
11540Sstevel@tonic-gate 	md_error_t	*ep = &status;
11550Sstevel@tonic-gate 	char		*host = NULL;
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	/* reset and parse args */
11580Sstevel@tonic-gate 	optind = 1;
11590Sstevel@tonic-gate 	opterr = 1;
11600Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Moh:s:")) != -1) {
11610Sstevel@tonic-gate 		switch (c) {
11620Sstevel@tonic-gate 		case 'M':
11630Sstevel@tonic-gate 			break;
11640Sstevel@tonic-gate 		case 'o':
11650Sstevel@tonic-gate 			break;
11660Sstevel@tonic-gate 		case 'h':
11670Sstevel@tonic-gate 			if (host != NULL) {
11680Sstevel@tonic-gate 				usage(sp, gettext(
11690Sstevel@tonic-gate 				    "only one -h option allowed"));
11700Sstevel@tonic-gate 			}
11710Sstevel@tonic-gate 			host = optarg;
11720Sstevel@tonic-gate 			break;
11730Sstevel@tonic-gate 		case 's':
11740Sstevel@tonic-gate 			sname = optarg;
11750Sstevel@tonic-gate 			break;
11760Sstevel@tonic-gate 		default:
11770Sstevel@tonic-gate 			usage(sp, gettext("unknown options"));
11780Sstevel@tonic-gate 		}
11790Sstevel@tonic-gate 	}
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	argc -= optind;
11820Sstevel@tonic-gate 	argv += optind;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	if (argc != 0)
11850Sstevel@tonic-gate 		usage(sp, gettext("too many args"));
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
11880Sstevel@tonic-gate 		mde_perror(ep, "");
11890Sstevel@tonic-gate 		md_exit(sp, 1);
11900Sstevel@tonic-gate 	}
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	if (host == NULL) {
11930Sstevel@tonic-gate 		if (meta_check_ownership(sp, ep) != 0) {
11940Sstevel@tonic-gate 			mde_perror(ep, "");
11950Sstevel@tonic-gate 			md_exit(sp, 1);
11960Sstevel@tonic-gate 		}
11970Sstevel@tonic-gate 	} else {
11980Sstevel@tonic-gate 		if (meta_check_ownership_on_host(sp, host, ep) != 0) {
11990Sstevel@tonic-gate 			mde_perror(ep, "");
12000Sstevel@tonic-gate 			md_exit(sp, 1);
12010Sstevel@tonic-gate 		}
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate 	md_exit(sp, 0);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate static void
12070Sstevel@tonic-gate parse_purge(int argc, char **argv)
12080Sstevel@tonic-gate {
12090Sstevel@tonic-gate 	int		c;
12100Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
12110Sstevel@tonic-gate 	mdsetname_t	*local_sp = NULL;
12120Sstevel@tonic-gate 	md_drive_desc	*dd;
12130Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
12140Sstevel@tonic-gate 	char		*thishost = mynode();
12150Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
12160Sstevel@tonic-gate 	md_error_t	*ep = &status;
12170Sstevel@tonic-gate 	int		bypass_cluster_purge = 0;
12180Sstevel@tonic-gate 	int		forceflg = FALSE;
12190Sstevel@tonic-gate 	int		ret = 0;
12200Sstevel@tonic-gate 	int		multi_node = 0;
12210Sstevel@tonic-gate 	md_set_desc		*sd;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	optind = 1;
12240Sstevel@tonic-gate 	opterr = 1;
12250Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "C:fPs:")) != -1) {
12260Sstevel@tonic-gate 		switch (c) {
12270Sstevel@tonic-gate 		case 'M':
12280Sstevel@tonic-gate 			break;
12290Sstevel@tonic-gate 		case 'C':
12300Sstevel@tonic-gate 			bypass_cluster_purge = 1;
12310Sstevel@tonic-gate 			break;
12320Sstevel@tonic-gate 		case 'f':
12330Sstevel@tonic-gate 			forceflg = TRUE;
12340Sstevel@tonic-gate 			break;
12350Sstevel@tonic-gate 		case 'P':
12360Sstevel@tonic-gate 			break;
12370Sstevel@tonic-gate 		case 's':
12380Sstevel@tonic-gate 			sname = optarg;
12390Sstevel@tonic-gate 			break;
12400Sstevel@tonic-gate 		default:
12410Sstevel@tonic-gate 			usage(sp, gettext("unknown options"));
12420Sstevel@tonic-gate 		}
12430Sstevel@tonic-gate 	}
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	argc -= optind;
12460Sstevel@tonic-gate 	argv += optind;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	if (argc != 0)
12490Sstevel@tonic-gate 		usage(sp, gettext("too many arguments"));
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	if ((local_sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) {
12520Sstevel@tonic-gate 		mde_perror(ep, "");
12530Sstevel@tonic-gate 		md_exit(local_sp, 1);
12540Sstevel@tonic-gate 	}
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	if (meta_lock(local_sp, TRUE, ep) != 0) {
12570Sstevel@tonic-gate 		mde_perror(ep, "");
12580Sstevel@tonic-gate 		md_exit(local_sp, 1);
12590Sstevel@tonic-gate 	}
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
12620Sstevel@tonic-gate 		mde_perror(ep, "");
12630Sstevel@tonic-gate 		md_exit(sp, 1);
12640Sstevel@tonic-gate 	}
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	if ((sd = metaget_setdesc(sp, ep)) == NULL) {
12670Sstevel@tonic-gate 		mde_perror(ep, "");
12680Sstevel@tonic-gate 		md_exit(local_sp, 1);
12690Sstevel@tonic-gate 	}
12700Sstevel@tonic-gate 	if (MD_MNSET_DESC(sd))
12710Sstevel@tonic-gate 		multi_node = 1;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	if (!multi_node) {
12740Sstevel@tonic-gate 		if (meta_lock(sp, TRUE, ep) != 0) {
12750Sstevel@tonic-gate 			mde_perror(ep, "");
12760Sstevel@tonic-gate 			md_exit(local_sp, 1);
12770Sstevel@tonic-gate 		}
12780Sstevel@tonic-gate 	}
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	/* Must not own the set if purging it from this host */
12810Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) == 0) {
12820Sstevel@tonic-gate 		/*
12830Sstevel@tonic-gate 		 * Need to see if there are disks in the set, if not then
12840Sstevel@tonic-gate 		 * there is no ownership but meta_check_ownership returns 0
12850Sstevel@tonic-gate 		 */
12860Sstevel@tonic-gate 		dd = metaget_drivedesc(sp, (MD_BASICNAME_OK | PRINT_FAST), ep);
12870Sstevel@tonic-gate 		if (!mdisok(ep)) {
12880Sstevel@tonic-gate 			mde_perror(ep, "");
12890Sstevel@tonic-gate 			if (!multi_node)
12900Sstevel@tonic-gate 				(void) meta_unlock(sp, ep);
12910Sstevel@tonic-gate 			md_exit(local_sp, 1);
12920Sstevel@tonic-gate 		}
12930Sstevel@tonic-gate 		if (dd != NULL) {
12940Sstevel@tonic-gate 			(void) printf(gettext
12950Sstevel@tonic-gate 			    ("Must not be owner of the set when purging it\n"));
12960Sstevel@tonic-gate 			if (!multi_node)
12970Sstevel@tonic-gate 				(void) meta_unlock(sp, ep);
12980Sstevel@tonic-gate 			md_exit(local_sp, 1);
12990Sstevel@tonic-gate 		}
13000Sstevel@tonic-gate 	}
13010Sstevel@tonic-gate 	/*
13020Sstevel@tonic-gate 	 * Remove the node from the DCS service
13030Sstevel@tonic-gate 	 */
13040Sstevel@tonic-gate 	if (!bypass_cluster_purge) {
13050Sstevel@tonic-gate 		if (sdssc_delete_hosts(sname, 1, &thishost) == SDSSC_ERROR) {
13060Sstevel@tonic-gate 			md_perror(gettext
13070Sstevel@tonic-gate 			    ("Failed to purge hosts from DCS service"));
13080Sstevel@tonic-gate 			if (!multi_node)
13090Sstevel@tonic-gate 				(void) meta_unlock(sp, ep);
13100Sstevel@tonic-gate 			md_exit(local_sp, 1);
13110Sstevel@tonic-gate 		}
13120Sstevel@tonic-gate 	}
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	if ((ret = meta_set_purge(sp, bypass_cluster_purge, forceflg,
13150Sstevel@tonic-gate 	    ep)) != 0) {
13160Sstevel@tonic-gate 		if (!bypass_cluster_purge) {
13170Sstevel@tonic-gate 			if (sdssc_add_hosts(sname, 1, &thishost) ==
13180Sstevel@tonic-gate 			    SDSSC_ERROR) {
13190Sstevel@tonic-gate 				(void) printf(gettext(
13200Sstevel@tonic-gate 				    "Failed to restore host in DCS "
13210Sstevel@tonic-gate 				    "database\n"));
13220Sstevel@tonic-gate 			}
13230Sstevel@tonic-gate 		}
13240Sstevel@tonic-gate 		mde_perror(ep, "");
13250Sstevel@tonic-gate 		if (!multi_node)
13260Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
13270Sstevel@tonic-gate 		md_exit(local_sp, ret);
13280Sstevel@tonic-gate 	}
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	if (!multi_node)
13310Sstevel@tonic-gate 		(void) meta_unlock(sp, ep);
13320Sstevel@tonic-gate 	md_exit(local_sp, 0);
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate static void
13360Sstevel@tonic-gate parse_query(int argc, char **argv)
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate 	int		c;
13390Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
13400Sstevel@tonic-gate 	mddb_dtag_lst_t	*dtlp = NULL;
13410Sstevel@tonic-gate 	mddb_dtag_lst_t	*tdtlp;
13420Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
13430Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	/* reset and parse args */
13460Sstevel@tonic-gate 	optind = 1;
13470Sstevel@tonic-gate 	opterr = 1;
13480Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Mqs:")) != -1) {
13490Sstevel@tonic-gate 		switch (c) {
13500Sstevel@tonic-gate 		case 'M':
13510Sstevel@tonic-gate 			break;
13520Sstevel@tonic-gate 		case 'q':
13530Sstevel@tonic-gate 			break;
13540Sstevel@tonic-gate 		case 's':
13550Sstevel@tonic-gate 			sname = optarg;
13560Sstevel@tonic-gate 			break;
13570Sstevel@tonic-gate 		default:
13580Sstevel@tonic-gate 			usage(sp, gettext("unknown options"));
13590Sstevel@tonic-gate 		}
13600Sstevel@tonic-gate 	}
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 	argc -= optind;
13630Sstevel@tonic-gate 	argv += optind;
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	if (argc != 0)
13660Sstevel@tonic-gate 		usage(sp, gettext("too many args"));
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	if ((sp = metasetname(sname, &status)) == NULL) {
13690Sstevel@tonic-gate 		mde_perror(&status, "");
13700Sstevel@tonic-gate 		md_exit(sp, 1);
13710Sstevel@tonic-gate 	}
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, &status) != 0) {
13740Sstevel@tonic-gate 		mde_perror(&status, "");
13750Sstevel@tonic-gate 		md_exit(sp, 1);
13760Sstevel@tonic-gate 	}
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	if (meta_set_query(sp, &dtlp, &status) != 0) {
13790Sstevel@tonic-gate 		mde_perror(&status, "");
13800Sstevel@tonic-gate 		md_exit(sp, 1);
13810Sstevel@tonic-gate 	}
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 	if (dtlp != NULL)
13840Sstevel@tonic-gate 		(void) printf("The following tag(s) were found:\n");
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	for (tdtlp = dtlp; tdtlp != NULL; tdtlp = dtlp) {
13870Sstevel@tonic-gate 		dtlp = tdtlp->dtl_nx;
13880Sstevel@tonic-gate 		(void) printf("%2d - %s - %s", tdtlp->dtl_dt.dt_id,
13890Sstevel@tonic-gate 		    tdtlp->dtl_dt.dt_hn,
13900Sstevel@tonic-gate 		    ctime((long *)&tdtlp->dtl_dt.dt_tv.tv_sec));
13910Sstevel@tonic-gate 		Free(tdtlp);
13920Sstevel@tonic-gate 	}
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	md_exit(sp, 0);
13950Sstevel@tonic-gate }
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate /* Should never be called with sname of a Multinode diskset. */
13980Sstevel@tonic-gate static void
13990Sstevel@tonic-gate parse_releaseset(int argc, char **argv)
14000Sstevel@tonic-gate {
14010Sstevel@tonic-gate 	int		c;
14020Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
14030Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
14040Sstevel@tonic-gate 	md_error_t	*ep = &status;
14050Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
14060Sstevel@tonic-gate 	sdssc_boolean_e	cluster_release = SDSSC_False;
14070Sstevel@tonic-gate 	sdssc_version_t	vers;
14080Sstevel@tonic-gate 	rval_e		rval;
14090Sstevel@tonic-gate 	md_set_desc	*sd;
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	/* reset and parse args */
14120Sstevel@tonic-gate 	optind = 1;
14130Sstevel@tonic-gate 	opterr = 1;
14141320Srd117015 	while ((c = getopt(argc, argv, "C:s:r")) != -1) {
14150Sstevel@tonic-gate 		switch (c) {
14160Sstevel@tonic-gate 		case 'C':
14170Sstevel@tonic-gate 			cluster_release = SDSSC_True;
14180Sstevel@tonic-gate 			break;
14190Sstevel@tonic-gate 		case 's':
14200Sstevel@tonic-gate 			sname = optarg;
14210Sstevel@tonic-gate 			break;
14220Sstevel@tonic-gate 		case 'r':
14230Sstevel@tonic-gate 			break;
14240Sstevel@tonic-gate 		default:
14250Sstevel@tonic-gate 			usage(sp, gettext("unknown options"));
14260Sstevel@tonic-gate 		}
14270Sstevel@tonic-gate 	}
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	argc -= optind;
14300Sstevel@tonic-gate 	argv += optind;
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	if (argc > 0)
14330Sstevel@tonic-gate 		usage(sp, gettext("too many args"));
14340Sstevel@tonic-gate 
1435*11053SSurya.Prakki@Sun.COM 	(void) memset(&vers, 0, sizeof (vers));
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	if ((sdssc_version(&vers) == SDSSC_OKAY) &&
14380Sstevel@tonic-gate 	    (vers.major == 3) &&
14390Sstevel@tonic-gate 	    (cluster_release == SDSSC_False)) {
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 		/*
14420Sstevel@tonic-gate 		 * If the release is being done by the user via the CLI
14430Sstevel@tonic-gate 		 * we need to notify the DCS to release this node as being
14440Sstevel@tonic-gate 		 * the primary. The reason nothing else needs to be done
14450Sstevel@tonic-gate 		 * is due to the fact that the reservation code will exec
14460Sstevel@tonic-gate 		 * metaset -C release to complete the operation.
14470Sstevel@tonic-gate 		 */
14480Sstevel@tonic-gate 		rval = sdssc_notify_service(sname, Release_Primary);
14490Sstevel@tonic-gate 		if (rval == SDSSC_ERROR) {
1450*11053SSurya.Prakki@Sun.COM 			(void) printf(gettext(
14510Sstevel@tonic-gate 			    "metaset: failed to notify DCS of release\n"));
14520Sstevel@tonic-gate 		}
14530Sstevel@tonic-gate 		md_exit(NULL, rval == SDSSC_ERROR);
14540Sstevel@tonic-gate 	}
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 		/*
14590Sstevel@tonic-gate 		 * It's entirely possible for the SC3.0 reservation code
14600Sstevel@tonic-gate 		 * to call for DiskSet to release a diskset and have that
14610Sstevel@tonic-gate 		 * diskset not exist. During a diskset removal DiskSuite
14620Sstevel@tonic-gate 		 * maybe able to remove all traces of the diskset before
14630Sstevel@tonic-gate 		 * the reservation code execs metaset -C release in which
14640Sstevel@tonic-gate 		 * case the metasetname will fail, but the overall command
14650Sstevel@tonic-gate 		 * shouldn't.
14660Sstevel@tonic-gate 		 */
14670Sstevel@tonic-gate 		if (vers.major == 3)
14680Sstevel@tonic-gate 			md_exit(sp, 0);
14690Sstevel@tonic-gate 		else {
14700Sstevel@tonic-gate 			mde_perror(ep, "");
14710Sstevel@tonic-gate 			md_exit(sp, 1);
14720Sstevel@tonic-gate 		}
14730Sstevel@tonic-gate 	}
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	if ((sd = metaget_setdesc(sp, ep)) == NULL) {
14760Sstevel@tonic-gate 		mde_perror(ep, "");
14770Sstevel@tonic-gate 		md_exit(sp, 1);
14780Sstevel@tonic-gate 	}
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	if (sd->sd_flags & MD_SR_AUTO_TAKE) {
14810Sstevel@tonic-gate 		md_eprintf(gettext("cannot release auto-take diskset\n"));
14820Sstevel@tonic-gate 		md_exit(sp, 1);
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	if (meta_lock_nowait(sp, ep) != 0) {
14861320Srd117015 		mde_perror(ep, "");
14871320Srd117015 		md_exit(sp, 10);	/* special errcode */
14880Sstevel@tonic-gate 	}
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	if (meta_set_release(sp, ep)) {
14910Sstevel@tonic-gate 		mde_perror(ep, "");
14920Sstevel@tonic-gate 		md_exit(sp, 1);
14930Sstevel@tonic-gate 	}
14940Sstevel@tonic-gate 	md_exit(sp, 0);
14950Sstevel@tonic-gate }
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate /* Should never be called with sname of a Multinode diskset. */
14980Sstevel@tonic-gate static void
14990Sstevel@tonic-gate parse_takeset(int argc, char **argv)
15000Sstevel@tonic-gate {
15010Sstevel@tonic-gate 	int		c;
15020Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
15030Sstevel@tonic-gate 	int		flags = 0;
15040Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
15050Sstevel@tonic-gate 	mhd_mhiargs_t	mhiargs;
15060Sstevel@tonic-gate 	char 		*cp = NULL;
15070Sstevel@tonic-gate 	int		pos = -1;	/* position of timeout value */
15080Sstevel@tonic-gate 	int		usetag = 0;
15090Sstevel@tonic-gate 	static char	*nullopts[] = { NULL };
15100Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
15110Sstevel@tonic-gate 	md_error_t	*ep = &status;
15120Sstevel@tonic-gate 	sdssc_boolean_e	cluster_take = SDSSC_False;
15130Sstevel@tonic-gate 	sdssc_version_t	vers;
15140Sstevel@tonic-gate 	rval_e		rval;
15151945Sjeanm 	int		set_take_rval;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	/* reset and parse args */
15180Sstevel@tonic-gate 	optind = 1;
15190Sstevel@tonic-gate 	opterr = 1;
15201320Srd117015 	while ((c = getopt(argc, argv, "C:fs:tu:y")) != -1) {
15210Sstevel@tonic-gate 		switch (c) {
15220Sstevel@tonic-gate 		case 'C':
15230Sstevel@tonic-gate 			cluster_take = SDSSC_True;
15240Sstevel@tonic-gate 			break;
15250Sstevel@tonic-gate 		case 'f':
15260Sstevel@tonic-gate 			flags |= TAKE_FORCE;
15270Sstevel@tonic-gate 			break;
15280Sstevel@tonic-gate 		case 's':
15290Sstevel@tonic-gate 			sname = optarg;
15300Sstevel@tonic-gate 			break;
15310Sstevel@tonic-gate 		case 't':
15320Sstevel@tonic-gate 			break;
15330Sstevel@tonic-gate 		case 'u':
15340Sstevel@tonic-gate 			usetag = atoi(optarg);
15350Sstevel@tonic-gate 			flags |= TAKE_USETAG;
15360Sstevel@tonic-gate 			break;
15370Sstevel@tonic-gate 		case 'y':
15380Sstevel@tonic-gate 			flags |= TAKE_USEIT;
15390Sstevel@tonic-gate 			break;
15400Sstevel@tonic-gate 		default:
15410Sstevel@tonic-gate 			usage(sp, gettext("unknown options"));
15420Sstevel@tonic-gate 		}
15430Sstevel@tonic-gate 	}
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 	mhiargs = defmhiargs;
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	argc -= optind;
15480Sstevel@tonic-gate 	argv += optind;
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 	if (argc > 1)
15510Sstevel@tonic-gate 		usage(sp, gettext("too many args"));
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	/*
15540Sstevel@tonic-gate 	 * If we have a list of timeout value overrides, handle it here
15550Sstevel@tonic-gate 	 */
15560Sstevel@tonic-gate 	while (argv[0] != NULL && *argv[0] != '\0') {
15570Sstevel@tonic-gate 		/*
15580Sstevel@tonic-gate 		 * The use of the nullopts[] "token list" here is to make
15590Sstevel@tonic-gate 		 * getsubopts() simply parse a comma separated list
15600Sstevel@tonic-gate 		 * returning either "" or the contents of the field, the
15610Sstevel@tonic-gate 		 * end condition is exaustion of the initial string, which
15620Sstevel@tonic-gate 		 * is modified in the process.
15630Sstevel@tonic-gate 		 */
15640Sstevel@tonic-gate 		(void) getsubopt(&argv[0], nullopts, &cp);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 		c = 0;			/* re-use c as temp value of timeout */
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 		if (*cp != '-')		/* '-' uses default */
15690Sstevel@tonic-gate 			c = atoi(cp);
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 		if (c < 0) {
15720Sstevel@tonic-gate 			usage(sp, gettext(
15730Sstevel@tonic-gate 			    "time out values must be > 0"));
15740Sstevel@tonic-gate 		}
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 		if (++pos > 3) {
15770Sstevel@tonic-gate 			usage(sp, gettext(
15780Sstevel@tonic-gate 			    "too many timeout values specified."));
15790Sstevel@tonic-gate 		}
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 		if (c == 0)		/* 0 or "" field uses default */
15820Sstevel@tonic-gate 			continue;
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 		/*
15850Sstevel@tonic-gate 		 * Assign temp value to appropriate structure member based on
15860Sstevel@tonic-gate 		 * its position in the comma separated list.
15870Sstevel@tonic-gate 		 */
15880Sstevel@tonic-gate 		switch (pos) {
15894932Spetede 			case 0:
15904932Spetede 				mhiargs.mh_ff = c;
15914932Spetede 				break;
15920Sstevel@tonic-gate 
15934932Spetede 			case 1:
15944932Spetede 				mhiargs.mh_tk.reinstate_resv_delay = c;
15954932Spetede 				break;
15960Sstevel@tonic-gate 
15974932Spetede 			case 2:
15984932Spetede 				mhiargs.mh_tk.min_ownership_delay = c;
15994932Spetede 				break;
16000Sstevel@tonic-gate 
16014932Spetede 			case 3:
16024932Spetede 				mhiargs.mh_tk.max_ownership_delay = c;
16034932Spetede 				break;
16040Sstevel@tonic-gate 		}
16050Sstevel@tonic-gate 	}
16060Sstevel@tonic-gate 
1607*11053SSurya.Prakki@Sun.COM 	(void) memset(&vers, 0, sizeof (vers));
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	if ((sdssc_version(&vers) == SDSSC_OKAY) &&
16100Sstevel@tonic-gate 	    (vers.major == 3) &&
16110Sstevel@tonic-gate 	    (cluster_take == SDSSC_False)) {
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 		/*
16140Sstevel@tonic-gate 		 * If the take is beging done by the user via the CLI we need
16150Sstevel@tonic-gate 		 * to notify the DCS to make this current node the primary.
16160Sstevel@tonic-gate 		 * The SC3.0 reservation code will in turn exec metaset with
16170Sstevel@tonic-gate 		 * the -C take arg to complete this operation.
16180Sstevel@tonic-gate 		 */
16190Sstevel@tonic-gate 		if ((rval = sdssc_notify_service(sname, Make_Primary)) ==
16200Sstevel@tonic-gate 		    SDSSC_ERROR) {
1621*11053SSurya.Prakki@Sun.COM 			(void) printf(gettext(
16220Sstevel@tonic-gate 			    "metaset: failed to notify DCS of take\n"));
16230Sstevel@tonic-gate 		}
16240Sstevel@tonic-gate 		md_exit(NULL, rval == SDSSC_ERROR);
16250Sstevel@tonic-gate 	}
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
16280Sstevel@tonic-gate 		mde_perror(ep, "");
16290Sstevel@tonic-gate 		md_exit(sp, 1);
16300Sstevel@tonic-gate 	}
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	if ((vers.major == 3) && (meta_check_ownership(sp, ep) == 0)) {
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 		/*
16350Sstevel@tonic-gate 		 * If we're running in a cluster environment and this
16360Sstevel@tonic-gate 		 * node already owns the set. Don't bother trying to
16370Sstevel@tonic-gate 		 * take the set again. There's one case where an adminstrator
16380Sstevel@tonic-gate 		 * is adding disks to a set for the first time. metaset
16390Sstevel@tonic-gate 		 * will take the ownership of the set at that point. During
16400Sstevel@tonic-gate 		 * that add operation SC3.0 notices activity on the device
16410Sstevel@tonic-gate 		 * and also tries to perform a take operation. The SC3.0 take
16420Sstevel@tonic-gate 		 * will fail because the adminstrative add has the set locked
16430Sstevel@tonic-gate 		 */
16440Sstevel@tonic-gate 		md_exit(sp, 0);
16450Sstevel@tonic-gate 	}
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	if (meta_lock_nowait(sp, ep) != 0) {
16481320Srd117015 		mde_perror(ep, "");
16491320Srd117015 		md_exit(sp, 10);	/* special errcode */
16500Sstevel@tonic-gate 	}
16510Sstevel@tonic-gate 
16521945Sjeanm 	/*
16531945Sjeanm 	 * If a 2 is returned from meta_set_take, this take was able to resolve
16541945Sjeanm 	 * an unresolved replicated disk (i.e. a disk is now available that
16551945Sjeanm 	 * had been missing during the import of the replicated diskset).
16561945Sjeanm 	 * Need to release the diskset and re-take in order to have
16571945Sjeanm 	 * the subdrivers re-snarf using the newly resolved (or newly mapped)
16581945Sjeanm 	 * devids.  This also allows the namespace to be updated with the
16591945Sjeanm 	 * correct major names in the case where the disk being replicated
16601945Sjeanm 	 * was handled by a different driver than the replicated disk.
16611945Sjeanm 	 */
16621945Sjeanm 	set_take_rval = meta_set_take(sp, &mhiargs, flags, usetag, &status);
16631945Sjeanm 	if (set_take_rval == 2) {
16641945Sjeanm 		if (meta_set_release(sp, &status)) {
16651945Sjeanm 			mde_perror(&status,
16661945Sjeanm 			    "Need to release and take set to resolve names.");
16671945Sjeanm 			md_exit(sp, 1);
16681945Sjeanm 		}
16691945Sjeanm 		metaflushdrivenames();
16701945Sjeanm 		metaflushsetname(sp);
16711945Sjeanm 		set_take_rval = meta_set_take(sp, &mhiargs,
16721945Sjeanm 		    (flags | TAKE_RETAKE), usetag, &status);
16731945Sjeanm 	}
16741945Sjeanm 
16751945Sjeanm 	if (set_take_rval == -1) {
16760Sstevel@tonic-gate 		mde_perror(&status, "");
16770Sstevel@tonic-gate 		if (mdismddberror(&status, MDE_DB_TAGDATA))
16780Sstevel@tonic-gate 			md_exit(sp, 2);
16790Sstevel@tonic-gate 		if (mdismddberror(&status, MDE_DB_ACCOK))
16800Sstevel@tonic-gate 			md_exit(sp, 3);
16810Sstevel@tonic-gate 		if (mdismddberror(&status, MDE_DB_STALE))
16820Sstevel@tonic-gate 			md_exit(sp, 66);
16830Sstevel@tonic-gate 		md_exit(sp, 1);
16840Sstevel@tonic-gate 	}
16850Sstevel@tonic-gate 	md_exit(sp, 0);
16860Sstevel@tonic-gate }
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate /*
16890Sstevel@tonic-gate  * Joins a node to a specific set or to all multinode disksets known
16900Sstevel@tonic-gate  * by this node.  If set is specified then caller should have verified
16910Sstevel@tonic-gate  * that the set is a multinode diskset.
16920Sstevel@tonic-gate  *
16930Sstevel@tonic-gate  * If an error occurs, metaset exits with a 1.
16940Sstevel@tonic-gate  * If there is no error, metaset exits with a 0.
16950Sstevel@tonic-gate  */
16960Sstevel@tonic-gate static void
16970Sstevel@tonic-gate parse_joinset(int argc, char **argv)
16980Sstevel@tonic-gate {
16990Sstevel@tonic-gate 	int		c;
17000Sstevel@tonic-gate 	mdsetname_t	*sp = NULL, *local_sp = NULL;
17010Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
17020Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
17030Sstevel@tonic-gate 	md_error_t	*ep = &status;
17040Sstevel@tonic-gate 	md_set_desc	*sd;
17050Sstevel@tonic-gate 	char		buf[BUFSIZ];
17060Sstevel@tonic-gate 	char		*p = buf;
17070Sstevel@tonic-gate 	set_t		max_sets, setno;
17080Sstevel@tonic-gate 	int		err, cumm_err = 0;
17090Sstevel@tonic-gate 	size_t		bufsz;
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	bufsz = sizeof (buf);
17120Sstevel@tonic-gate 	/* reset and parse args */
17130Sstevel@tonic-gate 	optind = 1;
17140Sstevel@tonic-gate 	opterr = 1;
17150Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Ms:j")) != -1) {
17160Sstevel@tonic-gate 		switch (c) {
17170Sstevel@tonic-gate 		case 'M':
17180Sstevel@tonic-gate 			break;
17190Sstevel@tonic-gate 		case 'j':
17200Sstevel@tonic-gate 			break;
17210Sstevel@tonic-gate 		case 's':
17220Sstevel@tonic-gate 			sname = optarg;
17230Sstevel@tonic-gate 			break;
17240Sstevel@tonic-gate 		default:
17250Sstevel@tonic-gate 			usage(sp, gettext("unknown options"));
17260Sstevel@tonic-gate 		}
17270Sstevel@tonic-gate 	}
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	argc -= optind;
17300Sstevel@tonic-gate 	argv += optind;
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 	if (argc > 1)
17330Sstevel@tonic-gate 		usage(sp, gettext("too many args"));
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	/*
17360Sstevel@tonic-gate 	 * If no setname option was used, then join all disksets
17370Sstevel@tonic-gate 	 * that this node knows about.   Attempt to join all
17380Sstevel@tonic-gate 	 * disksets that this node knows about.
17390Sstevel@tonic-gate 	 *
17400Sstevel@tonic-gate 	 * Additional text is added to the error messages during
17410Sstevel@tonic-gate 	 * this section of code in order to help the user understand
17420Sstevel@tonic-gate 	 * why the 'join of all sets' failed and which set caused
17430Sstevel@tonic-gate 	 * the failure.
17440Sstevel@tonic-gate 	 */
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 	/*
17470Sstevel@tonic-gate 	 * Hold local set lock throughout this call to keep
17480Sstevel@tonic-gate 	 * other actions from interfering (such as creating a new
17490Sstevel@tonic-gate 	 * set, etc.).
17500Sstevel@tonic-gate 	 */
17510Sstevel@tonic-gate 	if ((local_sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) {
17520Sstevel@tonic-gate 		mde_perror(ep, "");
17530Sstevel@tonic-gate 		md_exit(sp, 1);
17540Sstevel@tonic-gate 	}
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	if (meta_lock(local_sp, TRUE, ep) != 0) {
17570Sstevel@tonic-gate 		mde_perror(ep, "");
17580Sstevel@tonic-gate 		md_exit(local_sp, 1);
17590Sstevel@tonic-gate 	}
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 	if (strcmp(sname, MD_LOCAL_NAME) == 0) {
17620Sstevel@tonic-gate 		/*
17630Sstevel@tonic-gate 		 * If no set name is given, then walk through all sets
17640Sstevel@tonic-gate 		 * on this node which could include:
17650Sstevel@tonic-gate 		 * 	- MN disksets
17660Sstevel@tonic-gate 		 *	- traditional disksets
17670Sstevel@tonic-gate 		 *	- non-existent disksets
17680Sstevel@tonic-gate 		 * Attempt to join the MN disksets.
17690Sstevel@tonic-gate 		 * If the join of one set fails, print out an error message
17700Sstevel@tonic-gate 		 * about that set and continue the walk.
17710Sstevel@tonic-gate 		 */
17720Sstevel@tonic-gate 		if ((max_sets = get_max_sets(ep)) == 0) {
17730Sstevel@tonic-gate 			mde_perror(ep, "");
17740Sstevel@tonic-gate 			md_exit(local_sp, 1);
17750Sstevel@tonic-gate 		}
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 		/* Start walking through all possible disksets */
17780Sstevel@tonic-gate 		for (setno = 1; setno < max_sets; setno++) {
17790Sstevel@tonic-gate 			if ((sp = metasetnosetname(setno, ep)) == NULL) {
17800Sstevel@tonic-gate 				if (mdiserror(ep, MDE_NO_SET)) {
17810Sstevel@tonic-gate 					/* No set for this setno - continue */
17820Sstevel@tonic-gate 					mdclrerror(ep);
17830Sstevel@tonic-gate 					continue;
17840Sstevel@tonic-gate 				} else {
17850Sstevel@tonic-gate 					(void) sprintf(p, gettext(
17860Sstevel@tonic-gate 					"Unable to get set %d information"),
17870Sstevel@tonic-gate 					    setno);
17880Sstevel@tonic-gate 					mde_perror(ep, p);
17890Sstevel@tonic-gate 					cumm_err = 1;
17900Sstevel@tonic-gate 					mdclrerror(ep);
17910Sstevel@tonic-gate 					continue;
17920Sstevel@tonic-gate 				}
17930Sstevel@tonic-gate 			}
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 			/* If setname is there, set desc should exist. */
17960Sstevel@tonic-gate 			if ((sd = metaget_setdesc(sp, ep)) == NULL) {
17970Sstevel@tonic-gate 				(void) snprintf(p, bufsz, gettext(
17980Sstevel@tonic-gate 				    "Unable to get set %s desc information"),
17990Sstevel@tonic-gate 				    sp->setname);
18000Sstevel@tonic-gate 				mde_perror(ep, p);
18010Sstevel@tonic-gate 				cumm_err = 1;
18020Sstevel@tonic-gate 				mdclrerror(ep);
18030Sstevel@tonic-gate 				continue;
18040Sstevel@tonic-gate 			}
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 			/* Only check MN disksets */
18070Sstevel@tonic-gate 			if (!MD_MNSET_DESC(sd)) {
18080Sstevel@tonic-gate 				continue;
18090Sstevel@tonic-gate 			}
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 			/*
18120Sstevel@tonic-gate 			 * Return value of 0 is success.
18130Sstevel@tonic-gate 			 * Return value of -1 means a failure.
18140Sstevel@tonic-gate 			 * Return value of -2 means set could not be
18150Sstevel@tonic-gate 			 * joined, but shouldn't cause an error.
18160Sstevel@tonic-gate 			 * Reasons would be:
18170Sstevel@tonic-gate 			 * 	- no drives in set
18180Sstevel@tonic-gate 			 * 	- node already joined to set
18190Sstevel@tonic-gate 			 * Return value of -3 means joined stale set.
18200Sstevel@tonic-gate 			 * Can't check for all reasons here
18210Sstevel@tonic-gate 			 * since set isn't locked yet across all
18220Sstevel@tonic-gate 			 * nodes in the cluster.  The call
18230Sstevel@tonic-gate 			 * to libmeta routine, meta_set_join, will
18240Sstevel@tonic-gate 			 * lock across the cluster and perform
18250Sstevel@tonic-gate 			 * the checks.
18260Sstevel@tonic-gate 			 */
18270Sstevel@tonic-gate 			if ((err = meta_set_join(sp, ep)) == -1) {
18280Sstevel@tonic-gate 				/* Print error of diskset join failure */
18290Sstevel@tonic-gate 				(void) snprintf(p, bufsz,
18300Sstevel@tonic-gate 				    gettext("Join to diskset %s failed"),
18310Sstevel@tonic-gate 				    sp->setname);
18320Sstevel@tonic-gate 				mde_perror(ep, p);
18330Sstevel@tonic-gate 				cumm_err = 1;
18340Sstevel@tonic-gate 				mdclrerror(ep);
18350Sstevel@tonic-gate 				continue;
18360Sstevel@tonic-gate 			}
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 			if (err == -3) {
18390Sstevel@tonic-gate 				/* Print error of diskset join failure */
18400Sstevel@tonic-gate 				(void) snprintf(p, bufsz,
18410Sstevel@tonic-gate 				    gettext("Joined to stale diskset %s"),
18420Sstevel@tonic-gate 				    sp->setname);
18430Sstevel@tonic-gate 				mde_perror(ep, p);
18440Sstevel@tonic-gate 				mdclrerror(ep);
18450Sstevel@tonic-gate 			}
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 			mdclrerror(ep);
18480Sstevel@tonic-gate 		}
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 		md_exit(local_sp, cumm_err);
18510Sstevel@tonic-gate 	}
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	/*
18540Sstevel@tonic-gate 	 * Code for a specific set is much simpler.
18550Sstevel@tonic-gate 	 * Error messages don't need extra text since specific setname
18560Sstevel@tonic-gate 	 * was used.
18570Sstevel@tonic-gate 	 * Don't need to lock the local set, just the specific set given.
18580Sstevel@tonic-gate 	 */
18590Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
18600Sstevel@tonic-gate 		mde_perror(ep, "");
18610Sstevel@tonic-gate 		md_exit(local_sp, 1);
18620Sstevel@tonic-gate 	}
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 	/*
18650Sstevel@tonic-gate 	 * Fail command if meta_set_join returns -1.
18660Sstevel@tonic-gate 	 *
18670Sstevel@tonic-gate 	 * Return of 0 means that node joined set.
18680Sstevel@tonic-gate 	 *
18690Sstevel@tonic-gate 	 * Return of -2 means that node was unable to
18700Sstevel@tonic-gate 	 * join a set since that set had no drives
18710Sstevel@tonic-gate 	 * or that had already joined the set.  No
18720Sstevel@tonic-gate 	 * need to fail the command for these reasons.
18730Sstevel@tonic-gate 	 *
18740Sstevel@tonic-gate 	 * Return of -3 means that set is stale.
18750Sstevel@tonic-gate 	 * Return a value of 66 to historically match traditional disksets.
18760Sstevel@tonic-gate 	 */
18770Sstevel@tonic-gate 	if ((err = meta_set_join(sp, ep)) == -1) {
18780Sstevel@tonic-gate 		mde_perror(&status, "");
18790Sstevel@tonic-gate 		md_exit(local_sp, 1);
18800Sstevel@tonic-gate 	}
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	if (err == -3) {
18830Sstevel@tonic-gate 		/* Print error of diskset join failure */
18840Sstevel@tonic-gate 		(void) snprintf(p, bufsz,
18850Sstevel@tonic-gate 		    gettext("Joined to stale diskset %s"),
18860Sstevel@tonic-gate 		    sp->setname);
18870Sstevel@tonic-gate 		mde_perror(&status, "");
18880Sstevel@tonic-gate 		md_exit(local_sp, 66);
18890Sstevel@tonic-gate 	}
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 	md_exit(local_sp, 0);
18920Sstevel@tonic-gate }
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate /*
18950Sstevel@tonic-gate  * Withdraws a node from a specific set or from all multinode disksets known
18960Sstevel@tonic-gate  * by this node.  If set is specified then caller should have verified
18970Sstevel@tonic-gate  * that the set is a multinode diskset.
18980Sstevel@tonic-gate  *
18990Sstevel@tonic-gate  * If an error occurs, metaset exits with a 1.
19000Sstevel@tonic-gate  * If there is no error, metaset exits with a 0.
19010Sstevel@tonic-gate  */
19020Sstevel@tonic-gate static void
19030Sstevel@tonic-gate parse_withdrawset(int argc, char **argv)
19040Sstevel@tonic-gate {
19050Sstevel@tonic-gate 	int		c;
19060Sstevel@tonic-gate 	mdsetname_t	*sp = NULL, *local_sp = NULL;
19070Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
19080Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
19090Sstevel@tonic-gate 	md_error_t	*ep = &status;
19100Sstevel@tonic-gate 	char		buf[BUFSIZ];
19110Sstevel@tonic-gate 	char		*p = buf;
19120Sstevel@tonic-gate 	md_set_desc	*sd;
19130Sstevel@tonic-gate 	set_t		max_sets, setno;
19140Sstevel@tonic-gate 	int		err, cumm_err = 0;
19150Sstevel@tonic-gate 	size_t		bufsz;
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	bufsz = sizeof (buf);
19180Sstevel@tonic-gate 	/* reset and parse args */
19190Sstevel@tonic-gate 	optind = 1;
19200Sstevel@tonic-gate 	opterr = 1;
19210Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Ms:w")) != -1) {
19220Sstevel@tonic-gate 		switch (c) {
19230Sstevel@tonic-gate 		case 'M':
19240Sstevel@tonic-gate 			break;
19250Sstevel@tonic-gate 		case 'w':
19260Sstevel@tonic-gate 			break;
19270Sstevel@tonic-gate 		case 's':
19280Sstevel@tonic-gate 			sname = optarg;
19290Sstevel@tonic-gate 			break;
19300Sstevel@tonic-gate 		default:
19310Sstevel@tonic-gate 			usage(sp, gettext("unknown options"));
19320Sstevel@tonic-gate 		}
19330Sstevel@tonic-gate 	}
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 	argc -= optind;
19360Sstevel@tonic-gate 	argv += optind;
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	if (argc > 1)
19390Sstevel@tonic-gate 		usage(sp, gettext("too many args"));
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	/*
19420Sstevel@tonic-gate 	 * If no setname option was used, then withdraw from all disksets
19430Sstevel@tonic-gate 	 * that this node knows about.
19440Sstevel@tonic-gate 	 *
19450Sstevel@tonic-gate 	 * Additional text is added to the error messages during
19460Sstevel@tonic-gate 	 * this section of code in order to help the user understand
19470Sstevel@tonic-gate 	 * why the 'withdraw from all sets' failed and which set caused
19480Sstevel@tonic-gate 	 * the failure.
19490Sstevel@tonic-gate 	 */
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate 	/*
19520Sstevel@tonic-gate 	 * Hold local set lock throughout this call to keep
19530Sstevel@tonic-gate 	 * other actions from interfering (such as creating a new
19540Sstevel@tonic-gate 	 * set, etc.).
19550Sstevel@tonic-gate 	 */
19560Sstevel@tonic-gate 	if ((local_sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) {
19570Sstevel@tonic-gate 		mde_perror(ep, "");
19580Sstevel@tonic-gate 		md_exit(sp, 1);
19590Sstevel@tonic-gate 	}
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 	if (meta_lock(local_sp, TRUE, ep) != 0) {
19620Sstevel@tonic-gate 		mde_perror(ep, "");
19630Sstevel@tonic-gate 		md_exit(local_sp, 1);
19640Sstevel@tonic-gate 	}
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 	if (strcmp(sname, MD_LOCAL_NAME) == 0) {
19670Sstevel@tonic-gate 		/*
19680Sstevel@tonic-gate 		 * If no set name is given, then walk through all sets
19690Sstevel@tonic-gate 		 * on this node which could include:
19700Sstevel@tonic-gate 		 * 	- MN disksets
19710Sstevel@tonic-gate 		 *	- traditional disksets
19720Sstevel@tonic-gate 		 *	- non-existent disksets
19730Sstevel@tonic-gate 		 * Attempt to withdraw from the MN disksets.
19740Sstevel@tonic-gate 		 * If the withdraw of one set fails, print out an error
19750Sstevel@tonic-gate 		 * message about that set and continue the walk.
19760Sstevel@tonic-gate 		 */
19770Sstevel@tonic-gate 		if ((max_sets = get_max_sets(ep)) == 0) {
19780Sstevel@tonic-gate 			mde_perror(ep, "");
19790Sstevel@tonic-gate 			md_exit(local_sp, 1);
19800Sstevel@tonic-gate 		}
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 		/* Start walking through all possible disksets */
19830Sstevel@tonic-gate 		for (setno = 1; setno < max_sets; setno++) {
19840Sstevel@tonic-gate 			if ((sp = metasetnosetname(setno, ep)) == NULL) {
19850Sstevel@tonic-gate 				if (mdiserror(ep, MDE_NO_SET)) {
19860Sstevel@tonic-gate 					/* No set for this setno - continue */
19870Sstevel@tonic-gate 					mdclrerror(ep);
19880Sstevel@tonic-gate 					continue;
19890Sstevel@tonic-gate 				} else {
19900Sstevel@tonic-gate 					(void) sprintf(p, gettext(
19910Sstevel@tonic-gate 					    "Unable to get set %d information"),
19920Sstevel@tonic-gate 					    setno);
19930Sstevel@tonic-gate 					mde_perror(ep, p);
19940Sstevel@tonic-gate 					cumm_err = 1;
19950Sstevel@tonic-gate 					mdclrerror(ep);
19960Sstevel@tonic-gate 					continue;
19970Sstevel@tonic-gate 				}
19980Sstevel@tonic-gate 			}
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 			/* If setname is there, set desc should exist. */
20010Sstevel@tonic-gate 			if ((sd = metaget_setdesc(sp, ep)) == NULL) {
20020Sstevel@tonic-gate 				(void) snprintf(p, bufsz, gettext(
20030Sstevel@tonic-gate 				    "Unable to get set %s desc information"),
20040Sstevel@tonic-gate 				    sp->setname);
20050Sstevel@tonic-gate 				mde_perror(ep, p);
20060Sstevel@tonic-gate 				cumm_err = 1;
20070Sstevel@tonic-gate 				mdclrerror(ep);
20080Sstevel@tonic-gate 				continue;
20090Sstevel@tonic-gate 			}
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 			/* Only check MN disksets */
20120Sstevel@tonic-gate 			if (!MD_MNSET_DESC(sd)) {
20130Sstevel@tonic-gate 				continue;
20140Sstevel@tonic-gate 			}
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 			/*
20170Sstevel@tonic-gate 			 * Return value of 0 is success.
20180Sstevel@tonic-gate 			 * Return value of -1 means a failure.
20190Sstevel@tonic-gate 			 * Return value of -2 means set could not be
20200Sstevel@tonic-gate 			 * withdrawn from, but this shouldn't cause
20210Sstevel@tonic-gate 			 * an error.  Reasons would be:
20220Sstevel@tonic-gate 			 * 	- no drives in set
20230Sstevel@tonic-gate 			 * 	- node already withdrawn from set
20240Sstevel@tonic-gate 			 * Can't check for all reasons here
20250Sstevel@tonic-gate 			 * since set isn't locked yet across all
20260Sstevel@tonic-gate 			 * nodes in the cluster.  The call
20270Sstevel@tonic-gate 			 * to libmeta routine, meta_set_withdraw, will
20280Sstevel@tonic-gate 			 * lock across the cluster and perform
20290Sstevel@tonic-gate 			 * the checks.
20300Sstevel@tonic-gate 			 */
20310Sstevel@tonic-gate 			if ((err = meta_set_withdraw(sp, ep)) == -1) {
20320Sstevel@tonic-gate 				/* Print error of diskset withdraw failure */
20330Sstevel@tonic-gate 				(void) snprintf(p, bufsz,
20340Sstevel@tonic-gate 				    gettext("Withdraw from diskset %s failed"),
20350Sstevel@tonic-gate 				    sp->setname);
20360Sstevel@tonic-gate 				mde_perror(ep, p);
20370Sstevel@tonic-gate 				mdclrerror(ep);
20380Sstevel@tonic-gate 				cumm_err = 1;
20390Sstevel@tonic-gate 				continue;
20400Sstevel@tonic-gate 			}
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 			if (err == -2) {
20430Sstevel@tonic-gate 				mdclrerror(ep);
20440Sstevel@tonic-gate 				continue;
20450Sstevel@tonic-gate 			}
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 			mdclrerror(ep);
20480Sstevel@tonic-gate 		}
20490Sstevel@tonic-gate 		md_exit(local_sp, cumm_err);
20500Sstevel@tonic-gate 	}
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	/*
20540Sstevel@tonic-gate 	 * Code for a specific set is much simpler.
20550Sstevel@tonic-gate 	 * Error messages don't need extra text since specific setname
20560Sstevel@tonic-gate 	 * was used.
20570Sstevel@tonic-gate 	 * Don't need to lock the local set, just the specific set given.
20580Sstevel@tonic-gate 	 */
20590Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
20600Sstevel@tonic-gate 		mde_perror(ep, "");
20610Sstevel@tonic-gate 		md_exit(local_sp, 1);
20620Sstevel@tonic-gate 	}
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 	/*
20650Sstevel@tonic-gate 	 * Fail command if meta_set_withdraw returns -1.
20660Sstevel@tonic-gate 	 *
20670Sstevel@tonic-gate 	 * Return of 0 means that node withdrew from set.
20680Sstevel@tonic-gate 	 *
20690Sstevel@tonic-gate 	 * Return of -2 means that node was unable to
20700Sstevel@tonic-gate 	 * withdraw from a set since that set had no drives
20710Sstevel@tonic-gate 	 * or node was not joined to set.  No
20720Sstevel@tonic-gate 	 * need to fail the command for these reasons.
20730Sstevel@tonic-gate 	 */
20740Sstevel@tonic-gate 	if (meta_set_withdraw(sp, ep) == -1) {
20750Sstevel@tonic-gate 		mde_perror(&status, "");
20760Sstevel@tonic-gate 		md_exit(local_sp, 1);
20770Sstevel@tonic-gate 	}
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 	md_exit(local_sp, 0);
20800Sstevel@tonic-gate }
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate static void
20834932Spetede parse_cluster(int argc, char **argv, int multi_node)
20840Sstevel@tonic-gate {
20854932Spetede 	int			c, error, new_argc, x;
20860Sstevel@tonic-gate 	enum cluster_cmd	cmd = ccnotspecified;
20874932Spetede 	char			*hostname = SDSSC_PROXY_PRIMARY;
20884932Spetede 	char			*argument = NULL;
20894932Spetede 	char			*sname = MD_LOCAL_NAME;
20904932Spetede 	char			primary_node[SDSSC_NODE_NAME_LEN];
20914932Spetede 	char			**new_argv = NULL;
20924932Spetede 	char			**np = NULL;
20930Sstevel@tonic-gate 	mdsetname_t		*sp = NULL;
20940Sstevel@tonic-gate 	md_error_t		status = mdnullerror;
20950Sstevel@tonic-gate 	md_error_t		*ep = &status;
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 	/* reset and parse args */
20980Sstevel@tonic-gate 	optind = 1;
20990Sstevel@tonic-gate 	opterr = 1;
21001320Srd117015 	while ((c = getopt(argc, argv, "C:s:h:ftu:yr")) != -1) {
21010Sstevel@tonic-gate 		switch (c) {
21020Sstevel@tonic-gate 		case 'C':
21030Sstevel@tonic-gate 			if (cmd != ccnotspecified) {
21040Sstevel@tonic-gate 				md_exit(sp, -1);
21050Sstevel@tonic-gate 			}
21060Sstevel@tonic-gate 			argument = optarg;
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 			if (strcmp(argument, "disksin") == 0) {
21090Sstevel@tonic-gate 				cmd = clusterdisksin;
21100Sstevel@tonic-gate 			} else if (strcmp(argument, "version") == 0) {
21110Sstevel@tonic-gate 				cmd = clusterversion;
21120Sstevel@tonic-gate 			} else if (strcmp(argument, "release") == 0) {
21130Sstevel@tonic-gate 				cmd = clusterrelease;
21140Sstevel@tonic-gate 			} else if (strcmp(argument, "take") == 0) {
21150Sstevel@tonic-gate 				cmd = clustertake;
21160Sstevel@tonic-gate 			} else if (strcmp(argument, "proxy") == 0) {
21170Sstevel@tonic-gate 				cmd = clusterproxy;
21180Sstevel@tonic-gate 			} else if (strcmp(argument, "purge") == 0) {
21190Sstevel@tonic-gate 				cmd = clusterpurge;
21200Sstevel@tonic-gate 			} else {
21210Sstevel@tonic-gate 				md_exit(sp, -1);
21220Sstevel@tonic-gate 			}
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 			break;
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 		case 'h':
21270Sstevel@tonic-gate 			hostname = optarg;
21280Sstevel@tonic-gate 			break;
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 		case 's':
21310Sstevel@tonic-gate 			sname = optarg;
21320Sstevel@tonic-gate 			break;
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 		case 'f':
21350Sstevel@tonic-gate 		case 't':
21360Sstevel@tonic-gate 		case 'u':
21370Sstevel@tonic-gate 		case 'y':
21380Sstevel@tonic-gate 		case 'r':
21390Sstevel@tonic-gate 			break;
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 		default:
21420Sstevel@tonic-gate 			md_exit(sp, -1);
21430Sstevel@tonic-gate 		}
21440Sstevel@tonic-gate 	}
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 	/* Now call the appropriate command function. */
21470Sstevel@tonic-gate 	switch (cmd) {
21480Sstevel@tonic-gate 	case clusterversion:
21494932Spetede 		printclusterversion();
21504932Spetede 		break;
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 	case clusterdisksin:
21534932Spetede 		if (printdisksin(sname, ep)) {
21544932Spetede 			md_exit(sp, -1);
21554932Spetede 		}
21564932Spetede 		break;
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	case clusterrelease:
21594932Spetede 		if (multi_node) {
21604932Spetede 			usage(sp, gettext(
21614932Spetede 			    "-C release is not allowed on multi-owner"
21624932Spetede 			    " disksets"));
21634932Spetede 		}
21644932Spetede 		parse_releaseset(argc, argv);
21654932Spetede 		break;
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	case clustertake:
21684932Spetede 		if (multi_node) {
21694932Spetede 			usage(sp, gettext(
21704932Spetede 			    "-C take is not allowed on multi-owner disksets"));
21714932Spetede 		}
21724932Spetede 		parse_takeset(argc, argv);
21734932Spetede 		break;
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate 	case clusterproxy:
21764932Spetede 		if (multi_node) {
21774932Spetede 			usage(sp, gettext(
21784932Spetede 			    "-C proxy is not allowed on multi-owner disksets"));
21794932Spetede 		}
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 		if ((new_argv = calloc(argc, sizeof (char *))) == NULL) {
2182*11053SSurya.Prakki@Sun.COM 			(void) printf(gettext("Out of memory\n"));
21830Sstevel@tonic-gate 			md_exit(sp, 1);
21840Sstevel@tonic-gate 		}
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 		np = new_argv;
21870Sstevel@tonic-gate 		new_argc = 0;
2188*11053SSurya.Prakki@Sun.COM 		(void) memset(primary_node, '\0', SDSSC_NODE_NAME_LEN);
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 		for (x = 0; x < argc; x++) {
21910Sstevel@tonic-gate 			if (strcmp(argv[x], "-C") == 0) {
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 				/*
21940Sstevel@tonic-gate 				 * Need to skip the '-C proxy' args so
21950Sstevel@tonic-gate 				 * just increase x by one and the work is
21960Sstevel@tonic-gate 				 * done.
21970Sstevel@tonic-gate 				 */
21980Sstevel@tonic-gate 				x++;
21990Sstevel@tonic-gate 			} else {
22000Sstevel@tonic-gate 				*np++ = strdup(argv[x]);
22010Sstevel@tonic-gate 				new_argc++;
22020Sstevel@tonic-gate 			}
22030Sstevel@tonic-gate 		}
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 		switch (sdssc_get_primary_host(sname, primary_node,
22060Sstevel@tonic-gate 		    SDSSC_NODE_NAME_LEN)) {
22070Sstevel@tonic-gate 		case SDSSC_ERROR:
22080Sstevel@tonic-gate 			md_exit(sp, 1);
22090Sstevel@tonic-gate 			break;
22100Sstevel@tonic-gate 
22110Sstevel@tonic-gate 		case SDSSC_NO_SERVICE:
22120Sstevel@tonic-gate 			if (hostname != SDSSC_PROXY_PRIMARY) {
22130Sstevel@tonic-gate 				(void) strlcpy(primary_node, hostname,
22140Sstevel@tonic-gate 				    SDSSC_NODE_NAME_LEN);
22150Sstevel@tonic-gate 			}
22160Sstevel@tonic-gate 			break;
22170Sstevel@tonic-gate 		}
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate 		if (sdssc_cmd_proxy(new_argc, new_argv,
22200Sstevel@tonic-gate 		    primary_node[0] == '\0' ? SDSSC_PROXY_PRIMARY :
22210Sstevel@tonic-gate 		    primary_node, &error) == SDSSC_PROXY_DONE) {
22220Sstevel@tonic-gate 			md_exit(sp, error);
22230Sstevel@tonic-gate 		} else {
2224*11053SSurya.Prakki@Sun.COM 			(void) printf(gettext(
22250Sstevel@tonic-gate 			    "Couldn't proxy command\n"));
22260Sstevel@tonic-gate 			md_exit(sp, 1);
22270Sstevel@tonic-gate 		}
22280Sstevel@tonic-gate 		break;
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 	case clusterpurge:
22310Sstevel@tonic-gate 		parse_purge(argc, argv);
22320Sstevel@tonic-gate 		break;
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate 	default:
22354932Spetede 		break;
22360Sstevel@tonic-gate 	}
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate 	md_exit(sp, 0);
22390Sstevel@tonic-gate }
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate /*
22420Sstevel@tonic-gate  * parse args and do it
22430Sstevel@tonic-gate  */
22440Sstevel@tonic-gate int
22450Sstevel@tonic-gate main(int argc, char *argv[])
22460Sstevel@tonic-gate {
22470Sstevel@tonic-gate 	enum metaset_cmd	cmd = notspecified;
22480Sstevel@tonic-gate 	md_error_t		status = mdnullerror;
22490Sstevel@tonic-gate 	md_error_t		*ep = &status;
22500Sstevel@tonic-gate 	mdsetname_t		*sp = NULL;
22514932Spetede 	char			*hostname = SDSSC_PROXY_PRIMARY;
22524932Spetede 	char			*sname = MD_LOCAL_NAME;
22534932Spetede 	char			*auto_take_option = NULL;
22544932Spetede 	char			primary_node[SDSSC_NODE_NAME_LEN];
22554932Spetede 	int			error, c, stat;
22564932Spetede 	int			auto_take = FALSE;
22570Sstevel@tonic-gate 	md_set_desc		*sd;
22580Sstevel@tonic-gate 	int			mflag = 0;
22590Sstevel@tonic-gate 	int			multi_node = 0;
22600Sstevel@tonic-gate 	rval_e			sdssc_res;
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 	/*
22630Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
22640Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
22650Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
22660Sstevel@tonic-gate 	 * something.
22670Sstevel@tonic-gate 	 */
22680Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
22690Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
22700Sstevel@tonic-gate #endif
22710Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
22720Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 	sdssc_res = sdssc_bind_library();
22750Sstevel@tonic-gate 	if (sdssc_res == SDSSC_ERROR) {
2276*11053SSurya.Prakki@Sun.COM 		(void) printf(gettext(
22770Sstevel@tonic-gate 		    "%s: Interface error with libsds_sc.so\n"), argv[0]);
22780Sstevel@tonic-gate 		exit(1);
22790Sstevel@tonic-gate 	}
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	/* initialize */
22820Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0) {
22830Sstevel@tonic-gate 		mde_perror(ep, "");
22840Sstevel@tonic-gate 		md_exit(sp, 1);
22850Sstevel@tonic-gate 	}
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 	optind = 1;
22880Sstevel@tonic-gate 	opterr = 1;
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 	/*
22910Sstevel@tonic-gate 	 * NOTE: The "C" option is strictly for cluster use. it is not
22920Sstevel@tonic-gate 	 * and should not be documented for the customer. - JST
22930Sstevel@tonic-gate 	 */
22941320Srd117015 	while ((c = getopt(argc, argv, "C:MaA:bdfh:jl:Lm:oPqrs:tu:wy?"))
22950Sstevel@tonic-gate 	    != -1) {
22960Sstevel@tonic-gate 		switch (c) {
22970Sstevel@tonic-gate 		case 'M':
22980Sstevel@tonic-gate 			mflag = 1;
22990Sstevel@tonic-gate 			break;
23000Sstevel@tonic-gate 		case 'A':
23010Sstevel@tonic-gate 			auto_take = TRUE;
23020Sstevel@tonic-gate 			if (optarg == NULL || !(strcmp(optarg, "enable") == 0 ||
23030Sstevel@tonic-gate 			    strcmp(optarg, "disable") == 0))
23040Sstevel@tonic-gate 				usage(sp, gettext(
23050Sstevel@tonic-gate 				    "-A: enable or disable must be specified"));
23060Sstevel@tonic-gate 			auto_take_option = optarg;
23070Sstevel@tonic-gate 			break;
23080Sstevel@tonic-gate 		case 'a':
23090Sstevel@tonic-gate 			if (cmd != notspecified) {
23100Sstevel@tonic-gate 				usage(sp, gettext(
23110Sstevel@tonic-gate 				    "conflicting options"));
23120Sstevel@tonic-gate 			}
23130Sstevel@tonic-gate 			cmd = add;
23140Sstevel@tonic-gate 			break;
23150Sstevel@tonic-gate 		case 'b':
23160Sstevel@tonic-gate 			if (cmd != notspecified) {
23170Sstevel@tonic-gate 				usage(sp, gettext(
23180Sstevel@tonic-gate 				    "conflicting options"));
23190Sstevel@tonic-gate 			}
23200Sstevel@tonic-gate 			cmd = balance;
23210Sstevel@tonic-gate 			break;
23220Sstevel@tonic-gate 		case 'd':
23230Sstevel@tonic-gate 			if (cmd != notspecified) {
23240Sstevel@tonic-gate 				usage(sp, gettext(
23250Sstevel@tonic-gate 				    "conflicting options"));
23260Sstevel@tonic-gate 			}
23270Sstevel@tonic-gate 			cmd = delete;
23280Sstevel@tonic-gate 			break;
23290Sstevel@tonic-gate 		case 'C':	/* cluster commands */
23300Sstevel@tonic-gate 			if (cmd != notspecified) {
23310Sstevel@tonic-gate 				md_exit(sp, -1);    /* conflicting options */
23320Sstevel@tonic-gate 			}
23330Sstevel@tonic-gate 			cmd = cluster;
23340Sstevel@tonic-gate 			break;
23350Sstevel@tonic-gate 		case 'f':
23360Sstevel@tonic-gate 			break;
23370Sstevel@tonic-gate 		case 'h':
23380Sstevel@tonic-gate 			hostname = optarg;
23390Sstevel@tonic-gate 			break;
23400Sstevel@tonic-gate 		case 'j':
23410Sstevel@tonic-gate 			if (cmd != notspecified) {
23420Sstevel@tonic-gate 				usage(sp, gettext(
23430Sstevel@tonic-gate 				    "conflicting options"));
23440Sstevel@tonic-gate 			}
23450Sstevel@tonic-gate 			cmd = join;
23460Sstevel@tonic-gate 			break;
23470Sstevel@tonic-gate 		case 'l':
23480Sstevel@tonic-gate 			break;
23490Sstevel@tonic-gate 		case 'L':
23500Sstevel@tonic-gate 			break;
23510Sstevel@tonic-gate 		case 'm':
23520Sstevel@tonic-gate 			break;
23530Sstevel@tonic-gate 		case 'o':
23540Sstevel@tonic-gate 			if (cmd != notspecified) {
23550Sstevel@tonic-gate 				usage(sp, gettext(
23560Sstevel@tonic-gate 				    "conflicting options"));
23570Sstevel@tonic-gate 			}
23580Sstevel@tonic-gate 			cmd = isowner;
23590Sstevel@tonic-gate 			break;
23600Sstevel@tonic-gate 		case 'P':
23610Sstevel@tonic-gate 			if (cmd != notspecified) {
23620Sstevel@tonic-gate 				usage(sp, gettext(
23630Sstevel@tonic-gate 				    "conflicting options"));
23640Sstevel@tonic-gate 			}
23650Sstevel@tonic-gate 			cmd = purge;
23660Sstevel@tonic-gate 			break;
23670Sstevel@tonic-gate 		case 'q':
23680Sstevel@tonic-gate 			if (cmd != notspecified) {
23690Sstevel@tonic-gate 				usage(sp, gettext(
23700Sstevel@tonic-gate 				    "conflicting options"));
23710Sstevel@tonic-gate 			}
23720Sstevel@tonic-gate 			cmd = query;
23730Sstevel@tonic-gate 			break;
23740Sstevel@tonic-gate 		case 'r':
23750Sstevel@tonic-gate 			if (cmd != notspecified) {
23760Sstevel@tonic-gate 				usage(sp, gettext(
23770Sstevel@tonic-gate 				    "conflicting options"));
23780Sstevel@tonic-gate 			}
23790Sstevel@tonic-gate 			cmd = release;
23800Sstevel@tonic-gate 			break;
23810Sstevel@tonic-gate 		case 's':
23820Sstevel@tonic-gate 			sname = optarg;
23830Sstevel@tonic-gate 			break;
23840Sstevel@tonic-gate 		case 't':
23850Sstevel@tonic-gate 			if (cmd != notspecified) {
23860Sstevel@tonic-gate 				usage(sp, gettext(
23870Sstevel@tonic-gate 				    "conflicting options"));
23880Sstevel@tonic-gate 			}
23890Sstevel@tonic-gate 			cmd = take;
23900Sstevel@tonic-gate 			break;
23910Sstevel@tonic-gate 		case 'u':
23920Sstevel@tonic-gate 			break;
23930Sstevel@tonic-gate 		case 'w':
23940Sstevel@tonic-gate 			if (cmd != notspecified) {
23950Sstevel@tonic-gate 				usage(sp, gettext(
23960Sstevel@tonic-gate 				    "conflicting options"));
23970Sstevel@tonic-gate 			}
23980Sstevel@tonic-gate 			cmd = withdraw;
23990Sstevel@tonic-gate 			break;
24000Sstevel@tonic-gate 		case 'y':
24010Sstevel@tonic-gate 			break;
24020Sstevel@tonic-gate 		case '?':
24030Sstevel@tonic-gate 			if (optopt == '?')
24040Sstevel@tonic-gate 				usage(sp, NULL);
24050Sstevel@tonic-gate 			/*FALLTHROUGH*/
24060Sstevel@tonic-gate 		default:
24070Sstevel@tonic-gate 			if (cmd == cluster) {    /* cluster is silent */
24080Sstevel@tonic-gate 				md_exit(sp, -1);
24090Sstevel@tonic-gate 			} else {
24100Sstevel@tonic-gate 				usage(sp, gettext(
24110Sstevel@tonic-gate 				    "unknown command"));
24120Sstevel@tonic-gate 			}
24130Sstevel@tonic-gate 		}
24140Sstevel@tonic-gate 	}
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate 	/* check if suncluster is installed and -A enable specified */
24170Sstevel@tonic-gate 	if (auto_take && sdssc_res != SDSSC_NOT_BOUND &&
24180Sstevel@tonic-gate 	    strcmp(auto_take_option, "enable") == 0) {
24194932Spetede 		md_eprintf(gettext(
24204932Spetede 		    "cannot enable auto-take when SunCluster is installed\n"));
24214932Spetede 		md_exit(sp, 1);
24220Sstevel@tonic-gate 	}
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	/*
24250Sstevel@tonic-gate 	 * At this point we know that if the -A enable option is specified
24260Sstevel@tonic-gate 	 * for an auto-take diskset that SC is not installed on the machine, so
24270Sstevel@tonic-gate 	 * all of the sdssc calls will just be no-ops.
24280Sstevel@tonic-gate 	 */
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 	/* list sets */
24310Sstevel@tonic-gate 	if (cmd == notspecified && auto_take == FALSE) {
24320Sstevel@tonic-gate 		parse_printset(argc, argv);
24330Sstevel@tonic-gate 		/*NOTREACHED*/
24340Sstevel@tonic-gate 	}
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 	if (meta_check_root(ep) != 0) {
24370Sstevel@tonic-gate 		mde_perror(ep, "");
24380Sstevel@tonic-gate 		md_exit(sp, 1);
24390Sstevel@tonic-gate 	}
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate 	/* snarf MDDB */
24420Sstevel@tonic-gate 	if (meta_setup_db_locations(ep) != 0) {
24430Sstevel@tonic-gate 		mde_perror(ep, "");
24440Sstevel@tonic-gate 		md_exit(sp, 1);
24450Sstevel@tonic-gate 	}
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate 	/*
24480Sstevel@tonic-gate 	 * If sname is a diskset - check for multi_node.
24490Sstevel@tonic-gate 	 * It is possible for sname to not exist.
24500Sstevel@tonic-gate 	 */
24510Sstevel@tonic-gate 	if (strcmp(sname, MD_LOCAL_NAME)) {
24520Sstevel@tonic-gate 		if ((sp = metasetname(sname, ep)) != NULL) {
24530Sstevel@tonic-gate 			/* Set exists - check for MN diskset */
24540Sstevel@tonic-gate 			if ((sd = metaget_setdesc(sp, ep)) == NULL) {
24550Sstevel@tonic-gate 				mde_perror(ep, "");
24560Sstevel@tonic-gate 				md_exit(sp, 1);
24570Sstevel@tonic-gate 			}
24580Sstevel@tonic-gate 			if (MD_MNSET_DESC(sd)) {
24590Sstevel@tonic-gate 				/*
24600Sstevel@tonic-gate 				 * If a MN diskset always set multi_node
24610Sstevel@tonic-gate 				 * regardless of whether the -M option was
24620Sstevel@tonic-gate 				 * used or not (mflag).
24630Sstevel@tonic-gate 				 */
24640Sstevel@tonic-gate 				multi_node = 1;
24650Sstevel@tonic-gate 			} else {
24660Sstevel@tonic-gate 				/*
24670Sstevel@tonic-gate 				 * If a traditional diskset, mflag must
24680Sstevel@tonic-gate 				 * not be set.
24690Sstevel@tonic-gate 				 */
24700Sstevel@tonic-gate 				if (mflag) {
24710Sstevel@tonic-gate 					usage(sp, gettext(
24720Sstevel@tonic-gate 					    "-M option only allowed "
24730Sstevel@tonic-gate 					    "on multi-owner diskset"));
24740Sstevel@tonic-gate 				}
24750Sstevel@tonic-gate 			}
24760Sstevel@tonic-gate 		} else {
24770Sstevel@tonic-gate 			/*
24780Sstevel@tonic-gate 			 * Set name does not exist, set multi_node
24790Sstevel@tonic-gate 			 * based on -M option.
24800Sstevel@tonic-gate 			 */
24810Sstevel@tonic-gate 			if (mflag) {
24820Sstevel@tonic-gate 				multi_node = 1;
24830Sstevel@tonic-gate 			}
24840Sstevel@tonic-gate 		}
24850Sstevel@tonic-gate 	}
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 	if (auto_take && multi_node) {
24880Sstevel@tonic-gate 		/* Can't mix multinode and auto-take on a diskset */
24890Sstevel@tonic-gate 		usage(sp,
24900Sstevel@tonic-gate 		    gettext("-A option not allowed on multi-owner diskset"));
24910Sstevel@tonic-gate 	}
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 	/*
24940Sstevel@tonic-gate 	 * MN disksets don't use DCS clustering services, so
24950Sstevel@tonic-gate 	 * do not get primary_node for MN diskset since no command
24960Sstevel@tonic-gate 	 * proxying is done to Primary cluster node.  Do not proxy
24970Sstevel@tonic-gate 	 * MN diskset commands of join and withdraw when issued without
24980Sstevel@tonic-gate 	 * a valid setname.
24990Sstevel@tonic-gate 	 * For traditional disksets: proxy all commands except a take
25000Sstevel@tonic-gate 	 * and release.  Use first host listed as the host to send the
25010Sstevel@tonic-gate 	 * command to if there isn't already a primary
25020Sstevel@tonic-gate 	 */
25030Sstevel@tonic-gate 	if (strcmp(sname, MD_LOCAL_NAME) && (multi_node == 0) &&
25040Sstevel@tonic-gate 	    (cmd != take) && (cmd != release) &&
25050Sstevel@tonic-gate 	    (cmd != cluster) && (cmd != join) &&
25060Sstevel@tonic-gate 	    (cmd != withdraw) && (cmd != purge)) {
25070Sstevel@tonic-gate 		stat = sdssc_get_primary_host(sname, primary_node,
25080Sstevel@tonic-gate 		    SDSSC_NODE_NAME_LEN);
25090Sstevel@tonic-gate 		switch (stat) {
25100Sstevel@tonic-gate 			case SDSSC_ERROR:
25110Sstevel@tonic-gate 				return (0);
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate 			case SDSSC_NO_SERVICE:
25140Sstevel@tonic-gate 				if (hostname != SDSSC_PROXY_PRIMARY) {
25150Sstevel@tonic-gate 					(void) strlcpy(primary_node, hostname,
25160Sstevel@tonic-gate 					    SDSSC_NODE_NAME_LEN);
25170Sstevel@tonic-gate 				} else {
2518*11053SSurya.Prakki@Sun.COM 					(void) memset(primary_node, '\0',
25190Sstevel@tonic-gate 					    SDSSC_NODE_NAME_LEN);
25200Sstevel@tonic-gate 				}
25210Sstevel@tonic-gate 				break;
25220Sstevel@tonic-gate 		}
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate 		/*
25250Sstevel@tonic-gate 		 * We've got a complicated decision here regarding
25260Sstevel@tonic-gate 		 * the hostname. If we didn't get a primary host
25270Sstevel@tonic-gate 		 * and a host name wasn't supplied on the command line
25280Sstevel@tonic-gate 		 * then we need to revert to SDSSC_PROXY_PRIMARY. Otherwise
25290Sstevel@tonic-gate 		 * use what's been found.
25300Sstevel@tonic-gate 		 */
25310Sstevel@tonic-gate 		if (sdssc_cmd_proxy(argc, argv,
25320Sstevel@tonic-gate 		    primary_node[0] == '\0' ?
25334932Spetede 		    SDSSC_PROXY_PRIMARY : primary_node,
25340Sstevel@tonic-gate 		    &error) == SDSSC_PROXY_DONE) {
25350Sstevel@tonic-gate 			exit(error);
25360Sstevel@tonic-gate 		}
25370Sstevel@tonic-gate 	}
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 	/* cluster-specific commands */
25400Sstevel@tonic-gate 	if (cmd == cluster) {
25414932Spetede 		parse_cluster(argc, argv, multi_node);
25424932Spetede 		/*NOTREACHED*/
25430Sstevel@tonic-gate 	}
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 	/* join MultiNode diskset */
25460Sstevel@tonic-gate 	if (cmd == join) {
25470Sstevel@tonic-gate 		/*
25480Sstevel@tonic-gate 		 * If diskset specified, verify that it exists
25490Sstevel@tonic-gate 		 * and is a multinode diskset.
25500Sstevel@tonic-gate 		 */
25510Sstevel@tonic-gate 		if (strcmp(sname, MD_LOCAL_NAME)) {
25520Sstevel@tonic-gate 			if ((sp = metasetname(sname, ep)) == NULL) {
25530Sstevel@tonic-gate 				mde_perror(ep, "");
25540Sstevel@tonic-gate 				md_exit(sp, 1);
25550Sstevel@tonic-gate 			}
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 			if (!multi_node) {
25580Sstevel@tonic-gate 				usage(sp, gettext(
25590Sstevel@tonic-gate 				    "-j option only allowed on "
25600Sstevel@tonic-gate 				    "multi-owner diskset"));
25610Sstevel@tonic-gate 			}
25620Sstevel@tonic-gate 		}
25630Sstevel@tonic-gate 		/*
25640Sstevel@tonic-gate 		 * Start mddoors daemon here.
25650Sstevel@tonic-gate 		 * mddoors itself takes care there will be only one
25660Sstevel@tonic-gate 		 * instance running, so starting it twice won't hurt
25670Sstevel@tonic-gate 		 */
2568*11053SSurya.Prakki@Sun.COM 		(void) pclose(popen("/usr/lib/lvm/mddoors", "w"));
25690Sstevel@tonic-gate 		parse_joinset(argc, argv);
25700Sstevel@tonic-gate 		/*NOTREACHED*/
25710Sstevel@tonic-gate 	}
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate 	/* withdraw from MultiNode diskset */
25740Sstevel@tonic-gate 	if (cmd == withdraw) {
25750Sstevel@tonic-gate 		/*
25760Sstevel@tonic-gate 		 * If diskset specified, verify that it exists
25770Sstevel@tonic-gate 		 * and is a multinode diskset.
25780Sstevel@tonic-gate 		 */
25790Sstevel@tonic-gate 		if (strcmp(sname, MD_LOCAL_NAME)) {
25800Sstevel@tonic-gate 			if ((sp = metasetname(sname, ep)) == NULL) {
25810Sstevel@tonic-gate 				mde_perror(ep, "");
25820Sstevel@tonic-gate 				md_exit(sp, 1);
25830Sstevel@tonic-gate 			}
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 			if (!multi_node) {
25860Sstevel@tonic-gate 				usage(sp, gettext(
25870Sstevel@tonic-gate 				    "-w option only allowed on "
25880Sstevel@tonic-gate 				    "multi-owner diskset"));
25890Sstevel@tonic-gate 			}
25900Sstevel@tonic-gate 		}
25910Sstevel@tonic-gate 		parse_withdrawset(argc, argv);
25920Sstevel@tonic-gate 		/*NOTREACHED*/
25930Sstevel@tonic-gate 	}
25940Sstevel@tonic-gate 
25950Sstevel@tonic-gate 	/* must have set for everything else */
25960Sstevel@tonic-gate 	if (strcmp(sname, MD_LOCAL_NAME) == 0)
25970Sstevel@tonic-gate 		usage(sp, gettext("setname must be specified"));
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 	/* add hosts or drives */
26000Sstevel@tonic-gate 	if (cmd == add) {
26010Sstevel@tonic-gate 		/*
26020Sstevel@tonic-gate 		 * In the multi node case start mddoors daemon.
26030Sstevel@tonic-gate 		 * mddoors itself takes care there will be
26040Sstevel@tonic-gate 		 * only one instance running, so starting it twice won't hurt
26050Sstevel@tonic-gate 		 */
26060Sstevel@tonic-gate 		if (multi_node) {
2607*11053SSurya.Prakki@Sun.COM 			(void) pclose(popen("/usr/lib/lvm/mddoors", "w"));
26080Sstevel@tonic-gate 		}
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate 		parse_add(argc, argv);
26110Sstevel@tonic-gate 		/*NOTREACHED*/
26120Sstevel@tonic-gate 	}
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate 	/* re-balance the replicas */
26150Sstevel@tonic-gate 	if (cmd == balance) {
26160Sstevel@tonic-gate 		parse_balance(argc, argv);
26170Sstevel@tonic-gate 		/*NOTREACHED*/
26180Sstevel@tonic-gate 	}
26190Sstevel@tonic-gate 
26200Sstevel@tonic-gate 	/* delete hosts or drives */
26210Sstevel@tonic-gate 	if (cmd == delete) {
26220Sstevel@tonic-gate 		parse_del(argc, argv);
26230Sstevel@tonic-gate 		/*NOTREACHED*/
26240Sstevel@tonic-gate 	}
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 	/* check ownership */
26270Sstevel@tonic-gate 	if (cmd == isowner) {
26280Sstevel@tonic-gate 		parse_isowner(argc, argv);
26290Sstevel@tonic-gate 		/*NOTREACHED*/
26300Sstevel@tonic-gate 	}
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate 	/* purge the diskset */
26330Sstevel@tonic-gate 	if (cmd == purge) {
26340Sstevel@tonic-gate 		parse_purge(argc, argv);
26350Sstevel@tonic-gate 		/*NOTREACHED*/
26360Sstevel@tonic-gate 	}
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 	/* query for data marks */
26390Sstevel@tonic-gate 	if (cmd == query) {
26400Sstevel@tonic-gate 		parse_query(argc, argv);
26410Sstevel@tonic-gate 		/*NOTREACHED*/
26420Sstevel@tonic-gate 	}
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 	/* release ownership */
26450Sstevel@tonic-gate 	if (cmd == release) {
26460Sstevel@tonic-gate 		if (multi_node) {
26470Sstevel@tonic-gate 			/* Can't release multinode diskset */
26480Sstevel@tonic-gate 			usage(sp, gettext(
26490Sstevel@tonic-gate 			    "-r option not allowed on multi-owner diskset"));
26500Sstevel@tonic-gate 		} else {
26510Sstevel@tonic-gate 			parse_releaseset(argc, argv);
26520Sstevel@tonic-gate 			/*NOTREACHED*/
26530Sstevel@tonic-gate 		}
26540Sstevel@tonic-gate 	}
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 	/* take ownership */
26570Sstevel@tonic-gate 	if (cmd == take) {
26580Sstevel@tonic-gate 		if (multi_node) {
26590Sstevel@tonic-gate 			/* Can't take multinode diskset */
26600Sstevel@tonic-gate 			usage(sp, gettext(
26610Sstevel@tonic-gate 			    "-t option not allowed on multi-owner diskset"));
26620Sstevel@tonic-gate 		} else {
26630Sstevel@tonic-gate 			parse_takeset(argc, argv);
26640Sstevel@tonic-gate 			/*NOTREACHED*/
26650Sstevel@tonic-gate 		}
26660Sstevel@tonic-gate 	}
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate 	/* take ownership of auto-take sets */
26690Sstevel@tonic-gate 	if (auto_take) {
26700Sstevel@tonic-gate 		parse_autotake(argc, argv);
26710Sstevel@tonic-gate 		/*NOTREACHED*/
26720Sstevel@tonic-gate 	}
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 	/*NOTREACHED*/
26750Sstevel@tonic-gate 	return (0);
26760Sstevel@tonic-gate }
2677