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 /*
30*0Sstevel@tonic-gate  * offline sub-mirror
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <meta.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <sdssc.h>
36*0Sstevel@tonic-gate /*
37*0Sstevel@tonic-gate  * print usage message
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate static void
40*0Sstevel@tonic-gate usage(
41*0Sstevel@tonic-gate 	mdsetname_t	*sp,
42*0Sstevel@tonic-gate 	int		eval
43*0Sstevel@tonic-gate )
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\
46*0Sstevel@tonic-gate usage:	%s [-s setname] [-f] mirror submirror\n"),
47*0Sstevel@tonic-gate 	    myname);
48*0Sstevel@tonic-gate 	md_exit(sp, eval);
49*0Sstevel@tonic-gate }
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /*
52*0Sstevel@tonic-gate  * Metaoffline: to offline a metadevice
53*0Sstevel@tonic-gate  */
54*0Sstevel@tonic-gate int
55*0Sstevel@tonic-gate main(
56*0Sstevel@tonic-gate 	int		argc,
57*0Sstevel@tonic-gate 	char		*argv[]
58*0Sstevel@tonic-gate )
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	char		*sname = NULL;
61*0Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
62*0Sstevel@tonic-gate 	mdcmdopts_t	options = (MDCMD_PRINT);
63*0Sstevel@tonic-gate 	mdname_t	*mirnp;
64*0Sstevel@tonic-gate 	mdname_t	*submirnp;
65*0Sstevel@tonic-gate 	int		c;
66*0Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
67*0Sstevel@tonic-gate 	md_error_t	*ep = &status;
68*0Sstevel@tonic-gate 	int		error;
69*0Sstevel@tonic-gate 	bool_t		called_thru_rpc = FALSE;
70*0Sstevel@tonic-gate 	char		*cp;
71*0Sstevel@tonic-gate 	int		origargc = argc;
72*0Sstevel@tonic-gate 	char		**origargv = argv;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	/*
75*0Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
76*0Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
77*0Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
78*0Sstevel@tonic-gate 	 * something.
79*0Sstevel@tonic-gate 	 */
80*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
81*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
82*0Sstevel@tonic-gate #endif
83*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
84*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
87*0Sstevel@tonic-gate 		if (sdssc_bind_library() == SDSSC_OKAY)
88*0Sstevel@tonic-gate 			if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
89*0Sstevel@tonic-gate 						&error) == SDSSC_PROXY_DONE)
90*0Sstevel@tonic-gate 				exit(error);
91*0Sstevel@tonic-gate 	} else {
92*0Sstevel@tonic-gate 		*cp = '\0'; /* cut off ".rpc_call" */
93*0Sstevel@tonic-gate 		called_thru_rpc = TRUE;
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	/* initialize */
97*0Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
98*0Sstevel@tonic-gate 			meta_check_root(ep) != 0) {
99*0Sstevel@tonic-gate 		mde_perror(ep, "");
100*0Sstevel@tonic-gate 		md_exit(sp, 1);
101*0Sstevel@tonic-gate 	}
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	/* parse args */
104*0Sstevel@tonic-gate 	optind = 1;
105*0Sstevel@tonic-gate 	opterr = 1;
106*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "hs:f?")) != -1) {
107*0Sstevel@tonic-gate 		switch (c) {
108*0Sstevel@tonic-gate 		case 'h':
109*0Sstevel@tonic-gate 			usage(sp, 0);
110*0Sstevel@tonic-gate 			break;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 		case 's':
113*0Sstevel@tonic-gate 			sname = optarg;
114*0Sstevel@tonic-gate 			break;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 		case 'f':
117*0Sstevel@tonic-gate 			options |= MDCMD_FORCE;
118*0Sstevel@tonic-gate 			break;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 		case '?':
121*0Sstevel@tonic-gate 			if (optopt == '?')
122*0Sstevel@tonic-gate 				usage(sp, 0);
123*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
124*0Sstevel@tonic-gate 		default:
125*0Sstevel@tonic-gate 			usage(sp, 1);
126*0Sstevel@tonic-gate 			break;
127*0Sstevel@tonic-gate 		}
128*0Sstevel@tonic-gate 	}
129*0Sstevel@tonic-gate 	argc -= optind;
130*0Sstevel@tonic-gate 	argv += optind;
131*0Sstevel@tonic-gate 	if (argc != 2)
132*0Sstevel@tonic-gate 		usage(sp, 1);
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	if (sname != NULL) {
135*0Sstevel@tonic-gate 		if ((sp = metasetname(sname, ep)) == NULL) {
136*0Sstevel@tonic-gate 			mde_perror(ep, "");
137*0Sstevel@tonic-gate 			md_exit(sp, 1);
138*0Sstevel@tonic-gate 		}
139*0Sstevel@tonic-gate 	}
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	/* get names */
142*0Sstevel@tonic-gate 	if (((mirnp = metaname(&sp, argv[0], ep)) == NULL) ||
143*0Sstevel@tonic-gate 	    ((submirnp = metaname(&sp, argv[1], ep)) == NULL)) {
144*0Sstevel@tonic-gate 		mde_perror(ep, "");
145*0Sstevel@tonic-gate 		md_exit(sp, 1);
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	assert(sp != NULL);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	if ((called_thru_rpc == FALSE) &&
151*0Sstevel@tonic-gate 	    meta_is_mn_name(&sp, argv[0], ep)) {
152*0Sstevel@tonic-gate 		/*
153*0Sstevel@tonic-gate 		 * If we are dealing with a MN set and we were not
154*0Sstevel@tonic-gate 		 * called thru an rpc call, we are just to send this
155*0Sstevel@tonic-gate 		 * command string to the master of the set and let it
156*0Sstevel@tonic-gate 		 * deal with it.
157*0Sstevel@tonic-gate 		 * Note that if sp is NULL, meta_is_mn_name() derives sp
158*0Sstevel@tonic-gate 		 * from argv[0] which is the metadevice arg
159*0Sstevel@tonic-gate 		 * If this fails, the master must panic as the mddb may be
160*0Sstevel@tonic-gate 		 * inconsistent.
161*0Sstevel@tonic-gate 		 */
162*0Sstevel@tonic-gate 		int  result;
163*0Sstevel@tonic-gate 		result = meta_mn_send_command(sp, origargc, origargv,
164*0Sstevel@tonic-gate 		    MD_DISP_STDERR | MD_PANIC_WHEN_INCONSISTENT,
165*0Sstevel@tonic-gate 		    NO_CONTEXT_STRING, ep);
166*0Sstevel@tonic-gate 		md_exit(sp, result);
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	/* grab set lock */
170*0Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, ep)) {
171*0Sstevel@tonic-gate 		mde_perror(ep, "");
172*0Sstevel@tonic-gate 		md_exit(sp, 1);
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	/* check for ownership */
176*0Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
177*0Sstevel@tonic-gate 		mde_perror(ep, "");
178*0Sstevel@tonic-gate 		md_exit(sp, 1);
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	/* offline submirror */
182*0Sstevel@tonic-gate 	if (meta_mirror_offline(sp, mirnp, submirnp, options, ep) != 0) {
183*0Sstevel@tonic-gate 		mde_perror(ep, "");
184*0Sstevel@tonic-gate 		md_exit(sp, 1);
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	/* return success */
188*0Sstevel@tonic-gate 	md_exit(sp, 0);
189*0Sstevel@tonic-gate 	/*NOTREACHED*/
190*0Sstevel@tonic-gate 	return (0);
191*0Sstevel@tonic-gate }
192