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 2005 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 /*
30*0Sstevel@tonic-gate  * sync metadevices
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <meta.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <sys/lvm/md_mirror.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <ctype.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include <sdssc.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate /*
42*0Sstevel@tonic-gate  * print usage message
43*0Sstevel@tonic-gate  */
44*0Sstevel@tonic-gate static void
45*0Sstevel@tonic-gate usage(
46*0Sstevel@tonic-gate 	mdsetname_t	*sp,
47*0Sstevel@tonic-gate 	int		eval
48*0Sstevel@tonic-gate )
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\
51*0Sstevel@tonic-gate usage:	%s [-s setname] -r [buffer_size]\n\
52*0Sstevel@tonic-gate 	%s [-s setname] [buffer_size] metadevices...\n\
53*0Sstevel@tonic-gate 	%s [-s setname] -c metadevices...\n"),
54*0Sstevel@tonic-gate 	    myname, myname, myname);
55*0Sstevel@tonic-gate 	md_exit(sp, eval);
56*0Sstevel@tonic-gate }
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /*
59*0Sstevel@tonic-gate  * crack command line arguments.
60*0Sstevel@tonic-gate  */
61*0Sstevel@tonic-gate int
62*0Sstevel@tonic-gate main(
63*0Sstevel@tonic-gate 	int		argc,
64*0Sstevel@tonic-gate 	char		*argv[]
65*0Sstevel@tonic-gate )
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate 	char		*sname = NULL;
68*0Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
69*0Sstevel@tonic-gate 	int		rflag = 0;
70*0Sstevel@tonic-gate 	int		pflag = 0;
71*0Sstevel@tonic-gate 	daddr_t		size = 0;
72*0Sstevel@tonic-gate 	int		c;
73*0Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
74*0Sstevel@tonic-gate 	md_error_t	*ep = &status;
75*0Sstevel@tonic-gate 	int		rval = 0;
76*0Sstevel@tonic-gate 	int		error;
77*0Sstevel@tonic-gate 	md_resync_cmd_t	resync_cmd = MD_RESYNC_START;
78*0Sstevel@tonic-gate 	bool_t		called_thru_rpc = FALSE;
79*0Sstevel@tonic-gate 	char		*cp;
80*0Sstevel@tonic-gate 	int		mn_set = FALSE;
81*0Sstevel@tonic-gate 	int		cflag = 0;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	/*
84*0Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
85*0Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
86*0Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
87*0Sstevel@tonic-gate 	 * something.
88*0Sstevel@tonic-gate 	 */
89*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
90*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
91*0Sstevel@tonic-gate #endif
92*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
93*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
96*0Sstevel@tonic-gate 		if (sdssc_bind_library() == SDSSC_OKAY)
97*0Sstevel@tonic-gate 			if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
98*0Sstevel@tonic-gate 			    &error) == SDSSC_PROXY_DONE)
99*0Sstevel@tonic-gate 				exit(error);
100*0Sstevel@tonic-gate 	} else {
101*0Sstevel@tonic-gate 		*cp = '\0'; /* cut off ".rpc_call" */
102*0Sstevel@tonic-gate 		called_thru_rpc = TRUE;
103*0Sstevel@tonic-gate 	}
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	/* initialize */
107*0Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
108*0Sstevel@tonic-gate 			meta_check_root(ep) != 0) {
109*0Sstevel@tonic-gate 		mde_perror(ep, "");
110*0Sstevel@tonic-gate 		md_exit(sp, 1);
111*0Sstevel@tonic-gate 	}
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	/* parse args */
114*0Sstevel@tonic-gate 	optind = 1;
115*0Sstevel@tonic-gate 	opterr = 1;
116*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "phs:rc?")) != -1) {
117*0Sstevel@tonic-gate 		switch (c) {
118*0Sstevel@tonic-gate 		case 'h':
119*0Sstevel@tonic-gate 			usage(sp, 0);
120*0Sstevel@tonic-gate 			break;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 		case 's':
123*0Sstevel@tonic-gate 			sname = optarg;
124*0Sstevel@tonic-gate 			break;
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 		case 'r':
127*0Sstevel@tonic-gate 			++rflag;
128*0Sstevel@tonic-gate 			break;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 		case 'p':
131*0Sstevel@tonic-gate 			++pflag;
132*0Sstevel@tonic-gate 			break;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 		case 'c':
135*0Sstevel@tonic-gate 			++cflag;
136*0Sstevel@tonic-gate 			resync_cmd = MD_RESYNC_KILL;
137*0Sstevel@tonic-gate 			break;
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 		case '?':
140*0Sstevel@tonic-gate 			if (optopt == '?')
141*0Sstevel@tonic-gate 				usage(sp, 0);
142*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
143*0Sstevel@tonic-gate 		default:
144*0Sstevel@tonic-gate 			usage(sp, 1);
145*0Sstevel@tonic-gate 			break;
146*0Sstevel@tonic-gate 		}
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 	if ((pflag + rflag) > 1) {
149*0Sstevel@tonic-gate 		usage(sp, 1);
150*0Sstevel@tonic-gate 		mde_perror(ep, "");
151*0Sstevel@tonic-gate 		md_exit(sp, 1);
152*0Sstevel@tonic-gate 	}
153*0Sstevel@tonic-gate 	argc -= optind;
154*0Sstevel@tonic-gate 	argv += optind;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	if (sname != NULL) {
157*0Sstevel@tonic-gate 		if ((sp = metasetname(sname, ep)) == NULL) {
158*0Sstevel@tonic-gate 			mde_perror(ep, "");
159*0Sstevel@tonic-gate 			md_exit(sp, 1);
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	/*
164*0Sstevel@tonic-gate 	 * look for buffer size. If one is not specified we pass '0' to
165*0Sstevel@tonic-gate 	 * the meta_resync_all() call. This uses whatever size has been
166*0Sstevel@tonic-gate 	 * configured via md_mirror:md_resync_bufsz
167*0Sstevel@tonic-gate 	 * The default value (if not overridden in /etc/system) is
168*0Sstevel@tonic-gate 	 * MD_DEF_RESYNC_BUF_SIZE
169*0Sstevel@tonic-gate 	 */
170*0Sstevel@tonic-gate 	if ((argc > 0) && (isdigit(argv[0][0]))) {
171*0Sstevel@tonic-gate 		if ((size = atoi(argv[optind++])) < 0) {
172*0Sstevel@tonic-gate 			md_eprintf(gettext(
173*0Sstevel@tonic-gate 			    "illegal buffer size %s\n"),
174*0Sstevel@tonic-gate 			    argv[0]);
175*0Sstevel@tonic-gate 			md_exit(sp, 1);
176*0Sstevel@tonic-gate 		}
177*0Sstevel@tonic-gate 		--argc;
178*0Sstevel@tonic-gate 		++argv;
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	/* sync all devices in set */
182*0Sstevel@tonic-gate 	if (rflag) {
183*0Sstevel@tonic-gate 		/* get set */
184*0Sstevel@tonic-gate 		if (argc != 0)
185*0Sstevel@tonic-gate 			usage(sp, 1);
186*0Sstevel@tonic-gate 		if ((sp == NULL) &&
187*0Sstevel@tonic-gate 		    ((sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) &&
188*0Sstevel@tonic-gate 		    (metaget_setdesc(sp, ep) == NULL)) {
189*0Sstevel@tonic-gate 			mde_perror(ep, "");
190*0Sstevel@tonic-gate 			md_exit(sp, 1);
191*0Sstevel@tonic-gate 		}
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 		assert(sp != NULL);
194*0Sstevel@tonic-gate 		/*
195*0Sstevel@tonic-gate 		 * For a MN set "metasync -r" can only be called by the
196*0Sstevel@tonic-gate 		 * initiator. We must not take the set lock for a MN set as
197*0Sstevel@tonic-gate 		 * it will only generate individual metasync commands which
198*0Sstevel@tonic-gate 		 * will individually take the lock when executing the
199*0Sstevel@tonic-gate 		 * individual metasync commands.
200*0Sstevel@tonic-gate 		 * Therefore only take the set lock for non MN sets.
201*0Sstevel@tonic-gate 		 */
202*0Sstevel@tonic-gate 		if (meta_is_mn_set(sp, ep) == 0) {
203*0Sstevel@tonic-gate 			/* grab set lock */
204*0Sstevel@tonic-gate 			if (meta_lock(sp, TRUE, ep)) {
205*0Sstevel@tonic-gate 				mde_perror(ep, "");
206*0Sstevel@tonic-gate 				md_exit(sp, 1);
207*0Sstevel@tonic-gate 			}
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 			/* check for ownership */
210*0Sstevel@tonic-gate 			if (meta_check_ownership(sp, ep) != 0) {
211*0Sstevel@tonic-gate 				mde_perror(ep, "");
212*0Sstevel@tonic-gate 				md_exit(sp, 1);
213*0Sstevel@tonic-gate 			}
214*0Sstevel@tonic-gate 		}
215*0Sstevel@tonic-gate 		/* resync all metadevices in set */
216*0Sstevel@tonic-gate 		if (meta_resync_all(sp, size, ep) != 0) {
217*0Sstevel@tonic-gate 			mde_perror(ep, "");
218*0Sstevel@tonic-gate 			md_exit(sp, 1);
219*0Sstevel@tonic-gate 		}
220*0Sstevel@tonic-gate 		md_exit(sp, 0);
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	/* sync specified metadevices */
224*0Sstevel@tonic-gate 	if (argc <= 0)
225*0Sstevel@tonic-gate 		usage(sp, 1);
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	/*
228*0Sstevel@tonic-gate 	 * Note that if sp is NULL, meta_is_mn_name() derives sp
229*0Sstevel@tonic-gate 	 * from argv[0] which is the metadevice arg
230*0Sstevel@tonic-gate 	 */
231*0Sstevel@tonic-gate 	if (meta_is_mn_name(&sp, argv[0], ep))
232*0Sstevel@tonic-gate 		mn_set = TRUE;
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	for (; (argc > 0); --argc, ++argv) {
235*0Sstevel@tonic-gate 		mdname_t	*np;
236*0Sstevel@tonic-gate 		int		result;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 		/* get device */
239*0Sstevel@tonic-gate 		if ((np = metaname(&sp, argv[0], ep)) == NULL) {
240*0Sstevel@tonic-gate 			mde_perror(ep, "");
241*0Sstevel@tonic-gate 			rval = -1;
242*0Sstevel@tonic-gate 			continue;
243*0Sstevel@tonic-gate 		}
244*0Sstevel@tonic-gate 		assert(sp != NULL);
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 		/*
247*0Sstevel@tonic-gate 		 * If we are not called through an rpc call and the
248*0Sstevel@tonic-gate 		 * set associated with the command is an MN set, send
249*0Sstevel@tonic-gate 		 * a setsync message to the master of the set and let it
250*0Sstevel@tonic-gate 		 * deal with it.
251*0Sstevel@tonic-gate 		 */
252*0Sstevel@tonic-gate 		if (!called_thru_rpc && mn_set) {
253*0Sstevel@tonic-gate 			if ((result = meta_mn_send_setsync(sp, np, size,
254*0Sstevel@tonic-gate 			    ep)) != 0) {
255*0Sstevel@tonic-gate 				mde_perror(ep, "Unable to start resync");
256*0Sstevel@tonic-gate 				md_exit(sp, result);
257*0Sstevel@tonic-gate 			}
258*0Sstevel@tonic-gate 			continue;
259*0Sstevel@tonic-gate 		}
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 		/* grab set lock */
262*0Sstevel@tonic-gate 		if (meta_lock(sp, TRUE, ep)) {
263*0Sstevel@tonic-gate 			mde_perror(ep, "");
264*0Sstevel@tonic-gate 			md_exit(sp, 1);
265*0Sstevel@tonic-gate 		}
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 		/* check for ownership */
268*0Sstevel@tonic-gate 		if (meta_check_ownership(sp, ep) != 0) {
269*0Sstevel@tonic-gate 			mde_perror(ep, "");
270*0Sstevel@tonic-gate 			md_exit(sp, 1);	/* no point in continuing */
271*0Sstevel@tonic-gate 		}
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 		/* resync or regen (raid only) metadevice */
274*0Sstevel@tonic-gate 		if (pflag) {
275*0Sstevel@tonic-gate 			/* regen */
276*0Sstevel@tonic-gate 			if (meta_raid_regen_byname(sp, np, size, ep) != 0) {
277*0Sstevel@tonic-gate 				mde_perror(ep, "");
278*0Sstevel@tonic-gate 				rval = -1;
279*0Sstevel@tonic-gate 				continue;
280*0Sstevel@tonic-gate 			}
281*0Sstevel@tonic-gate 		} else {
282*0Sstevel@tonic-gate 			if (meta_resync_byname(sp, np, size, ep, resync_cmd)
283*0Sstevel@tonic-gate 			    != 0) {
284*0Sstevel@tonic-gate 				mde_perror(ep, "");
285*0Sstevel@tonic-gate 				rval = -1;
286*0Sstevel@tonic-gate 				continue;
287*0Sstevel@tonic-gate 			}
288*0Sstevel@tonic-gate 		}
289*0Sstevel@tonic-gate 	}
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	/* return success */
292*0Sstevel@tonic-gate 	md_exit(sp, rval);
293*0Sstevel@tonic-gate 	/*NOTREACHED*/
294*0Sstevel@tonic-gate 	return (rval);
295*0Sstevel@tonic-gate }
296