1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include	<stdio.h>
30*0Sstevel@tonic-gate #include	<stdarg.h>
31*0Sstevel@tonic-gate #include	<ctype.h>
32*0Sstevel@tonic-gate #include	<sys/fcntl.h>
33*0Sstevel@tonic-gate #include	<sys/types.h>
34*0Sstevel@tonic-gate #include	<devid.h>
35*0Sstevel@tonic-gate #include	<ftw.h>
36*0Sstevel@tonic-gate #include	<string.h>
37*0Sstevel@tonic-gate #include	<mdiox.h>
38*0Sstevel@tonic-gate #include	<sys/lvm/mdio.h>
39*0Sstevel@tonic-gate #include 	<meta.h>
40*0Sstevel@tonic-gate #include 	<syslog.h>
41*0Sstevel@tonic-gate #include	<sdssc.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate /*
44*0Sstevel@tonic-gate  * print usage message
45*0Sstevel@tonic-gate  */
46*0Sstevel@tonic-gate static void
47*0Sstevel@tonic-gate usage(char *myname)
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
50*0Sstevel@tonic-gate "usage:  %s -h\n"
51*0Sstevel@tonic-gate "	%s [-s setname] -r [-lnv]\n"
52*0Sstevel@tonic-gate "	%s [-s setname] -u cxtxdx [-lnv]\n"),
53*0Sstevel@tonic-gate 	myname, myname, myname);
54*0Sstevel@tonic-gate }
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate void
57*0Sstevel@tonic-gate main(int argc, char **argv)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate 	char		c;
60*0Sstevel@tonic-gate 	char		*sname = MD_LOCAL_NAME;
61*0Sstevel@tonic-gate 	mddevopts_t	options = 0;
62*0Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
63*0Sstevel@tonic-gate 	md_error_t	*ep = &status;
64*0Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
65*0Sstevel@tonic-gate 	mdsetname_t	*local_sp = NULL;
66*0Sstevel@tonic-gate 	char		*argname;
67*0Sstevel@tonic-gate 	int		todo = 0;
68*0Sstevel@tonic-gate 	int		ret = 0;
69*0Sstevel@tonic-gate 	int		md_upgd_stat = 0;
70*0Sstevel@tonic-gate 	int		error;
71*0Sstevel@tonic-gate 	md_set_desc	*sd;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	/*
74*0Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
75*0Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
76*0Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
77*0Sstevel@tonic-gate 	 * something.
78*0Sstevel@tonic-gate 	 */
79*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
80*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
81*0Sstevel@tonic-gate #endif
82*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
83*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	if ((sdssc_bind_library() == SDSSC_OKAY) &&
86*0Sstevel@tonic-gate 		(sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
87*0Sstevel@tonic-gate 		    &error) == SDSSC_PROXY_DONE))
88*0Sstevel@tonic-gate 			exit(error);
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	openlog("metadevadm", LOG_ODELAY, LOG_USER);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	/* initialize */
93*0Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
94*0Sstevel@tonic-gate 			meta_check_root(ep) != 0) {
95*0Sstevel@tonic-gate 		closelog();
96*0Sstevel@tonic-gate 		mde_perror(ep, "");
97*0Sstevel@tonic-gate 		md_exit(sp, 1);
98*0Sstevel@tonic-gate 	}
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	/* parse args */
101*0Sstevel@tonic-gate 	optind = 1;
102*0Sstevel@tonic-gate 	opterr = 1;
103*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "vlhnrs:u:")) != -1) {
104*0Sstevel@tonic-gate 		switch (c) {
105*0Sstevel@tonic-gate 		case 'v':
106*0Sstevel@tonic-gate 			options |= DEV_VERBOSE;
107*0Sstevel@tonic-gate 			break;
108*0Sstevel@tonic-gate 		case 'n':
109*0Sstevel@tonic-gate 			options |= DEV_NOACTION;
110*0Sstevel@tonic-gate 			break;
111*0Sstevel@tonic-gate 		case 'r':
112*0Sstevel@tonic-gate 			options |= DEV_RELOAD;
113*0Sstevel@tonic-gate 			todo = 1;
114*0Sstevel@tonic-gate 			break;
115*0Sstevel@tonic-gate 		case 's':
116*0Sstevel@tonic-gate 			sname = optarg;
117*0Sstevel@tonic-gate 			break;
118*0Sstevel@tonic-gate 		case 'u':
119*0Sstevel@tonic-gate 			todo = 1;
120*0Sstevel@tonic-gate 			options |= DEV_UPDATE;
121*0Sstevel@tonic-gate 			argname = optarg;
122*0Sstevel@tonic-gate 			if (argname == NULL) {
123*0Sstevel@tonic-gate 				usage("metadevadm");
124*0Sstevel@tonic-gate 				closelog();
125*0Sstevel@tonic-gate 				md_exit(sp, 0);
126*0Sstevel@tonic-gate 			}
127*0Sstevel@tonic-gate 			break;
128*0Sstevel@tonic-gate 		case 'l':
129*0Sstevel@tonic-gate 			options |= DEV_LOG;
130*0Sstevel@tonic-gate 			break;
131*0Sstevel@tonic-gate 		case 'h':
132*0Sstevel@tonic-gate 		default:
133*0Sstevel@tonic-gate 			usage("metadevadm");
134*0Sstevel@tonic-gate 			closelog();
135*0Sstevel@tonic-gate 			md_exit(sp, 0);
136*0Sstevel@tonic-gate 		}
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	if ((sp = metasetname(sname, ep)) == NULL) {
140*0Sstevel@tonic-gate 		mde_perror(ep, "");
141*0Sstevel@tonic-gate 		closelog();
142*0Sstevel@tonic-gate 		md_exit(sp, 1);
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	if (!metaislocalset(sp)) {
146*0Sstevel@tonic-gate 		if ((sd = metaget_setdesc(sp, ep)) == NULL) {
147*0Sstevel@tonic-gate 			mde_perror(ep, "");
148*0Sstevel@tonic-gate 			closelog();
149*0Sstevel@tonic-gate 			md_exit(sp, 1);
150*0Sstevel@tonic-gate 		}
151*0Sstevel@tonic-gate 		if (MD_MNSET_DESC(sd)) {
152*0Sstevel@tonic-gate 			printf("%s\n", gettext("metadevadm cannot be "
153*0Sstevel@tonic-gate 			    "run on multi-owner disksets\n"));
154*0Sstevel@tonic-gate 			closelog();
155*0Sstevel@tonic-gate 			md_exit(sp, 0);
156*0Sstevel@tonic-gate 		}
157*0Sstevel@tonic-gate 	}
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	if ((options & DEV_VERBOSE) && (todo != 1)) {
160*0Sstevel@tonic-gate 		usage("metadevadm");
161*0Sstevel@tonic-gate 		closelog();
162*0Sstevel@tonic-gate 		md_exit(sp, 0);
163*0Sstevel@tonic-gate 	}
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	if ((options & DEV_NOACTION) && (todo != 1)) {
166*0Sstevel@tonic-gate 		usage("metadevadm");
167*0Sstevel@tonic-gate 		closelog();
168*0Sstevel@tonic-gate 		md_exit(sp, 0);
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	if (todo == 0) {
172*0Sstevel@tonic-gate 		usage("metadevadm");
173*0Sstevel@tonic-gate 		closelog();
174*0Sstevel@tonic-gate 		md_exit(sp, 0);
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	if ((local_sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) {
178*0Sstevel@tonic-gate 		mde_perror(ep, "");
179*0Sstevel@tonic-gate 		closelog();
180*0Sstevel@tonic-gate 		md_exit(local_sp, 1);
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	/* lock the local set */
184*0Sstevel@tonic-gate 	if (meta_lock(local_sp, TRUE, ep) != 0) {
185*0Sstevel@tonic-gate 		mde_perror(ep, "");
186*0Sstevel@tonic-gate 		closelog();
187*0Sstevel@tonic-gate 		md_exit(local_sp, 1);
188*0Sstevel@tonic-gate 	}
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	/* grab set lock */
191*0Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, ep)) {
192*0Sstevel@tonic-gate 		mde_perror(ep, "");
193*0Sstevel@tonic-gate 		closelog();
194*0Sstevel@tonic-gate 		md_exit(local_sp, 1);
195*0Sstevel@tonic-gate 	}
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	/* check for ownership */
198*0Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
199*0Sstevel@tonic-gate 		/*
200*0Sstevel@tonic-gate 		 * If the set is not owned by this node then only update the
201*0Sstevel@tonic-gate 		 * local set's replica.
202*0Sstevel@tonic-gate 		 */
203*0Sstevel@tonic-gate 		options |= DEV_LOCAL_SET;
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	/*
207*0Sstevel@tonic-gate 	 * check for upgrade. If upgrade in progress then just exit.
208*0Sstevel@tonic-gate 	 */
209*0Sstevel@tonic-gate 	if (metaioctl(MD_UPGRADE_STAT, &md_upgd_stat, ep, NULL) != 0) {
210*0Sstevel@tonic-gate 			mde_perror(ep, "");
211*0Sstevel@tonic-gate 			closelog();
212*0Sstevel@tonic-gate 			(void) meta_unlock(sp, ep);
213*0Sstevel@tonic-gate 			md_exit(local_sp, 1);
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate 	if (md_upgd_stat == 0) {
216*0Sstevel@tonic-gate 		ret = meta_fixdevid(sp, options, argname, ep);
217*0Sstevel@tonic-gate 		if (ret == METADEVADM_ERR) {
218*0Sstevel@tonic-gate 			/*
219*0Sstevel@tonic-gate 			 * If the call failed, for a DEV_RELOAD still need to
220*0Sstevel@tonic-gate 			 * update the .conf file to provide the latest devid
221*0Sstevel@tonic-gate 			 * information so exit later.
222*0Sstevel@tonic-gate 			 */
223*0Sstevel@tonic-gate 			if (options & DEV_UPDATE) {
224*0Sstevel@tonic-gate 				closelog();
225*0Sstevel@tonic-gate 				(void) meta_unlock(sp, ep);
226*0Sstevel@tonic-gate 				md_exit(local_sp, 1);
227*0Sstevel@tonic-gate 			}
228*0Sstevel@tonic-gate 		}
229*0Sstevel@tonic-gate 	}
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	/*
232*0Sstevel@tonic-gate 	 * Sync replica list in kernel to replica list in conf files.
233*0Sstevel@tonic-gate 	 * This will update driver name and minor number in conf file
234*0Sstevel@tonic-gate 	 * if reload was run.  Will update device id in conf file if
235*0Sstevel@tonic-gate 	 * update was run.
236*0Sstevel@tonic-gate 	 */
237*0Sstevel@tonic-gate 	meta_sync_db_locations(sp, ep);
238*0Sstevel@tonic-gate 	closelog();
239*0Sstevel@tonic-gate 	(void) meta_unlock(sp, ep);
240*0Sstevel@tonic-gate 	md_exit(local_sp, ret);
241*0Sstevel@tonic-gate }
242