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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*62Sjeanm  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include	<stdio.h>
300Sstevel@tonic-gate #include	<stdarg.h>
310Sstevel@tonic-gate #include	<ctype.h>
320Sstevel@tonic-gate #include	<sys/fcntl.h>
330Sstevel@tonic-gate #include	<sys/types.h>
340Sstevel@tonic-gate #include	<devid.h>
350Sstevel@tonic-gate #include	<ftw.h>
360Sstevel@tonic-gate #include	<string.h>
370Sstevel@tonic-gate #include	<mdiox.h>
380Sstevel@tonic-gate #include	<sys/lvm/mdio.h>
390Sstevel@tonic-gate #include 	<meta.h>
400Sstevel@tonic-gate #include 	<syslog.h>
410Sstevel@tonic-gate #include	<sdssc.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * print usage message
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate static void
470Sstevel@tonic-gate usage(char *myname)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
500Sstevel@tonic-gate "usage:  %s -h\n"
510Sstevel@tonic-gate "	%s [-s setname] -r [-lnv]\n"
520Sstevel@tonic-gate "	%s [-s setname] -u cxtxdx [-lnv]\n"),
530Sstevel@tonic-gate 	myname, myname, myname);
540Sstevel@tonic-gate }
550Sstevel@tonic-gate 
56*62Sjeanm int
570Sstevel@tonic-gate main(int argc, char **argv)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	char		c;
600Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
610Sstevel@tonic-gate 	mddevopts_t	options = 0;
620Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
630Sstevel@tonic-gate 	md_error_t	*ep = &status;
640Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
650Sstevel@tonic-gate 	mdsetname_t	*local_sp = NULL;
660Sstevel@tonic-gate 	char		*argname;
670Sstevel@tonic-gate 	int		todo = 0;
680Sstevel@tonic-gate 	int		ret = 0;
690Sstevel@tonic-gate 	int		md_upgd_stat = 0;
700Sstevel@tonic-gate 	int		error;
710Sstevel@tonic-gate 	md_set_desc	*sd;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	/*
740Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
750Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
760Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
770Sstevel@tonic-gate 	 * something.
780Sstevel@tonic-gate 	 */
790Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
800Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
830Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if ((sdssc_bind_library() == SDSSC_OKAY) &&
860Sstevel@tonic-gate 		(sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
870Sstevel@tonic-gate 		    &error) == SDSSC_PROXY_DONE))
880Sstevel@tonic-gate 			exit(error);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	openlog("metadevadm", LOG_ODELAY, LOG_USER);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	/* initialize */
930Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
940Sstevel@tonic-gate 			meta_check_root(ep) != 0) {
950Sstevel@tonic-gate 		closelog();
960Sstevel@tonic-gate 		mde_perror(ep, "");
970Sstevel@tonic-gate 		md_exit(sp, 1);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	/* parse args */
1010Sstevel@tonic-gate 	optind = 1;
1020Sstevel@tonic-gate 	opterr = 1;
1030Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "vlhnrs:u:")) != -1) {
1040Sstevel@tonic-gate 		switch (c) {
1050Sstevel@tonic-gate 		case 'v':
1060Sstevel@tonic-gate 			options |= DEV_VERBOSE;
1070Sstevel@tonic-gate 			break;
1080Sstevel@tonic-gate 		case 'n':
1090Sstevel@tonic-gate 			options |= DEV_NOACTION;
1100Sstevel@tonic-gate 			break;
1110Sstevel@tonic-gate 		case 'r':
1120Sstevel@tonic-gate 			options |= DEV_RELOAD;
1130Sstevel@tonic-gate 			todo = 1;
1140Sstevel@tonic-gate 			break;
1150Sstevel@tonic-gate 		case 's':
1160Sstevel@tonic-gate 			sname = optarg;
1170Sstevel@tonic-gate 			break;
1180Sstevel@tonic-gate 		case 'u':
1190Sstevel@tonic-gate 			todo = 1;
1200Sstevel@tonic-gate 			options |= DEV_UPDATE;
1210Sstevel@tonic-gate 			argname = optarg;
1220Sstevel@tonic-gate 			if (argname == NULL) {
1230Sstevel@tonic-gate 				usage("metadevadm");
1240Sstevel@tonic-gate 				closelog();
1250Sstevel@tonic-gate 				md_exit(sp, 0);
1260Sstevel@tonic-gate 			}
1270Sstevel@tonic-gate 			break;
1280Sstevel@tonic-gate 		case 'l':
1290Sstevel@tonic-gate 			options |= DEV_LOG;
1300Sstevel@tonic-gate 			break;
1310Sstevel@tonic-gate 		case 'h':
1320Sstevel@tonic-gate 		default:
1330Sstevel@tonic-gate 			usage("metadevadm");
1340Sstevel@tonic-gate 			closelog();
1350Sstevel@tonic-gate 			md_exit(sp, 0);
1360Sstevel@tonic-gate 		}
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
1400Sstevel@tonic-gate 		mde_perror(ep, "");
1410Sstevel@tonic-gate 		closelog();
1420Sstevel@tonic-gate 		md_exit(sp, 1);
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if (!metaislocalset(sp)) {
1460Sstevel@tonic-gate 		if ((sd = metaget_setdesc(sp, ep)) == NULL) {
1470Sstevel@tonic-gate 			mde_perror(ep, "");
1480Sstevel@tonic-gate 			closelog();
1490Sstevel@tonic-gate 			md_exit(sp, 1);
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 		if (MD_MNSET_DESC(sd)) {
1520Sstevel@tonic-gate 			printf("%s\n", gettext("metadevadm cannot be "
1530Sstevel@tonic-gate 			    "run on multi-owner disksets\n"));
1540Sstevel@tonic-gate 			closelog();
1550Sstevel@tonic-gate 			md_exit(sp, 0);
1560Sstevel@tonic-gate 		}
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if ((options & DEV_VERBOSE) && (todo != 1)) {
1600Sstevel@tonic-gate 		usage("metadevadm");
1610Sstevel@tonic-gate 		closelog();
1620Sstevel@tonic-gate 		md_exit(sp, 0);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if ((options & DEV_NOACTION) && (todo != 1)) {
1660Sstevel@tonic-gate 		usage("metadevadm");
1670Sstevel@tonic-gate 		closelog();
1680Sstevel@tonic-gate 		md_exit(sp, 0);
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (todo == 0) {
1720Sstevel@tonic-gate 		usage("metadevadm");
1730Sstevel@tonic-gate 		closelog();
1740Sstevel@tonic-gate 		md_exit(sp, 0);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if ((local_sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) {
1780Sstevel@tonic-gate 		mde_perror(ep, "");
1790Sstevel@tonic-gate 		closelog();
1800Sstevel@tonic-gate 		md_exit(local_sp, 1);
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	/* lock the local set */
1840Sstevel@tonic-gate 	if (meta_lock(local_sp, TRUE, ep) != 0) {
1850Sstevel@tonic-gate 		mde_perror(ep, "");
1860Sstevel@tonic-gate 		closelog();
1870Sstevel@tonic-gate 		md_exit(local_sp, 1);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	/* grab set lock */
1910Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, ep)) {
1920Sstevel@tonic-gate 		mde_perror(ep, "");
1930Sstevel@tonic-gate 		closelog();
1940Sstevel@tonic-gate 		md_exit(local_sp, 1);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/* check for ownership */
1980Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
1990Sstevel@tonic-gate 		/*
2000Sstevel@tonic-gate 		 * If the set is not owned by this node then only update the
2010Sstevel@tonic-gate 		 * local set's replica.
2020Sstevel@tonic-gate 		 */
2030Sstevel@tonic-gate 		options |= DEV_LOCAL_SET;
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	/*
2070Sstevel@tonic-gate 	 * check for upgrade. If upgrade in progress then just exit.
2080Sstevel@tonic-gate 	 */
2090Sstevel@tonic-gate 	if (metaioctl(MD_UPGRADE_STAT, &md_upgd_stat, ep, NULL) != 0) {
2100Sstevel@tonic-gate 			mde_perror(ep, "");
2110Sstevel@tonic-gate 			closelog();
2120Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
2130Sstevel@tonic-gate 			md_exit(local_sp, 1);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 	if (md_upgd_stat == 0) {
2160Sstevel@tonic-gate 		ret = meta_fixdevid(sp, options, argname, ep);
2170Sstevel@tonic-gate 		if (ret == METADEVADM_ERR) {
2180Sstevel@tonic-gate 			/*
2190Sstevel@tonic-gate 			 * If the call failed, for a DEV_RELOAD still need to
2200Sstevel@tonic-gate 			 * update the .conf file to provide the latest devid
2210Sstevel@tonic-gate 			 * information so exit later.
2220Sstevel@tonic-gate 			 */
2230Sstevel@tonic-gate 			if (options & DEV_UPDATE) {
2240Sstevel@tonic-gate 				closelog();
2250Sstevel@tonic-gate 				(void) meta_unlock(sp, ep);
2260Sstevel@tonic-gate 				md_exit(local_sp, 1);
2270Sstevel@tonic-gate 			}
2280Sstevel@tonic-gate 		}
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	/*
2320Sstevel@tonic-gate 	 * Sync replica list in kernel to replica list in conf files.
2330Sstevel@tonic-gate 	 * This will update driver name and minor number in conf file
2340Sstevel@tonic-gate 	 * if reload was run.  Will update device id in conf file if
2350Sstevel@tonic-gate 	 * update was run.
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	meta_sync_db_locations(sp, ep);
2380Sstevel@tonic-gate 	closelog();
2390Sstevel@tonic-gate 	(void) meta_unlock(sp, ep);
2400Sstevel@tonic-gate 	md_exit(local_sp, ret);
241*62Sjeanm 	return (0);
2420Sstevel@tonic-gate }
243