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  * detach submirrors
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 /*
38*0Sstevel@tonic-gate  * print usage message
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate static void
41*0Sstevel@tonic-gate usage(
42*0Sstevel@tonic-gate 	mdsetname_t	*sp,
43*0Sstevel@tonic-gate 	int		eval
44*0Sstevel@tonic-gate )
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\
47*0Sstevel@tonic-gate usage:	%s [-s setname] [-f] mirror submirror\n\
48*0Sstevel@tonic-gate 	%s [-s setname] [-f] trans\n"),
49*0Sstevel@tonic-gate 	    myname, myname);
50*0Sstevel@tonic-gate 	md_exit(sp, eval);
51*0Sstevel@tonic-gate }
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /*
54*0Sstevel@tonic-gate  * detach submirror from mirror
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate static int
57*0Sstevel@tonic-gate mirror_detach(
58*0Sstevel@tonic-gate 	mdsetname_t	**spp,
59*0Sstevel@tonic-gate 	mdname_t	*mirnp,
60*0Sstevel@tonic-gate 	int		argc,
61*0Sstevel@tonic-gate 	char		*argv[],
62*0Sstevel@tonic-gate 	mdcmdopts_t	options,
63*0Sstevel@tonic-gate 	md_error_t	*ep
64*0Sstevel@tonic-gate )
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate 	mdname_t	*submirnp;
67*0Sstevel@tonic-gate 	int		c;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	/* reset and parse args */
70*0Sstevel@tonic-gate 	optind = 1;
71*0Sstevel@tonic-gate 	opterr = 1;
72*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:f")) != -1) {
73*0Sstevel@tonic-gate 		switch (c) {
74*0Sstevel@tonic-gate 		case 's':
75*0Sstevel@tonic-gate 			break;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 		case 'f':
78*0Sstevel@tonic-gate 			options |= MDCMD_FORCE;
79*0Sstevel@tonic-gate 			break;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 		default:
82*0Sstevel@tonic-gate 			usage(*spp, 1);
83*0Sstevel@tonic-gate 			/*NOTREACHED*/
84*0Sstevel@tonic-gate 			break;
85*0Sstevel@tonic-gate 		}
86*0Sstevel@tonic-gate 	}
87*0Sstevel@tonic-gate 	argc -= optind;
88*0Sstevel@tonic-gate 	argv += optind;
89*0Sstevel@tonic-gate 	if (argc != 2)
90*0Sstevel@tonic-gate 		usage(*spp, 1);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	/* get submirror */
93*0Sstevel@tonic-gate 	if ((submirnp = metaname(spp, argv[1], ep)) == NULL)
94*0Sstevel@tonic-gate 		return (-1);
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	/* detach submirror */
97*0Sstevel@tonic-gate 	if (meta_mirror_detach(*spp, mirnp, submirnp, options, ep) != 0)
98*0Sstevel@tonic-gate 		return (-1);
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	/* update md.cf */
101*0Sstevel@tonic-gate 	if (meta_update_md_cf(*spp, ep) != 0)
102*0Sstevel@tonic-gate 		return (-1);
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	/* return success */
105*0Sstevel@tonic-gate 	return (0);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate /*
109*0Sstevel@tonic-gate  * detach log from trans
110*0Sstevel@tonic-gate  */
111*0Sstevel@tonic-gate static int
112*0Sstevel@tonic-gate trans_detach(
113*0Sstevel@tonic-gate 	mdsetname_t	*sp,
114*0Sstevel@tonic-gate 	mdname_t	*transnp,
115*0Sstevel@tonic-gate 	int		argc,
116*0Sstevel@tonic-gate 	char		*argv[],
117*0Sstevel@tonic-gate 	mdcmdopts_t	options,
118*0Sstevel@tonic-gate 	md_error_t	*ep
119*0Sstevel@tonic-gate )
120*0Sstevel@tonic-gate {
121*0Sstevel@tonic-gate 	int		delayed;
122*0Sstevel@tonic-gate 	int		c;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	/* reset and parse args */
125*0Sstevel@tonic-gate 	optind = 1;
126*0Sstevel@tonic-gate 	opterr = 1;
127*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:f")) != -1) {
128*0Sstevel@tonic-gate 		switch (c) {
129*0Sstevel@tonic-gate 		case 's':
130*0Sstevel@tonic-gate 			break;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 		case 'f':
133*0Sstevel@tonic-gate 			options |= MDCMD_FORCE;
134*0Sstevel@tonic-gate 			break;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 		default:
137*0Sstevel@tonic-gate 			usage(sp, 1);
138*0Sstevel@tonic-gate 			/*NOTREACHED*/
139*0Sstevel@tonic-gate 			break;
140*0Sstevel@tonic-gate 		}
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 	argc -= optind;
143*0Sstevel@tonic-gate 	argv += optind;
144*0Sstevel@tonic-gate 	if (argc != 1)
145*0Sstevel@tonic-gate 		usage(sp, 1);
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	/* detach log */
148*0Sstevel@tonic-gate 	if (meta_trans_detach(sp, transnp, options, &delayed, ep) != 0)
149*0Sstevel@tonic-gate 		return (-1);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	/* update md.cf */
152*0Sstevel@tonic-gate 	if (meta_update_md_cf(sp, ep) != 0)
153*0Sstevel@tonic-gate 		return (-1);
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	/* return success */
156*0Sstevel@tonic-gate 	return (0);
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate /*
160*0Sstevel@tonic-gate  * parse args and doit
161*0Sstevel@tonic-gate  */
162*0Sstevel@tonic-gate int
163*0Sstevel@tonic-gate main(
164*0Sstevel@tonic-gate 	int 		argc,
165*0Sstevel@tonic-gate 	char		*argv[]
166*0Sstevel@tonic-gate )
167*0Sstevel@tonic-gate {
168*0Sstevel@tonic-gate 	char		*sname = NULL;
169*0Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
170*0Sstevel@tonic-gate 	mdcmdopts_t	options = (MDCMD_PRINT);
171*0Sstevel@tonic-gate 	mdname_t	*np;
172*0Sstevel@tonic-gate 	char		*miscname;
173*0Sstevel@tonic-gate 	int		c;
174*0Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
175*0Sstevel@tonic-gate 	md_error_t	*ep = &status;
176*0Sstevel@tonic-gate 	int		error;
177*0Sstevel@tonic-gate 	bool_t		called_thru_rpc = FALSE;
178*0Sstevel@tonic-gate 	char		*cp;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	/*
182*0Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
183*0Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
184*0Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
185*0Sstevel@tonic-gate 	 * something.
186*0Sstevel@tonic-gate 	 */
187*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
188*0Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
189*0Sstevel@tonic-gate #endif
190*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
191*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
194*0Sstevel@tonic-gate 		if (sdssc_bind_library() == SDSSC_OKAY)
195*0Sstevel@tonic-gate 			if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
196*0Sstevel@tonic-gate 						&error) == SDSSC_PROXY_DONE)
197*0Sstevel@tonic-gate 				exit(error);
198*0Sstevel@tonic-gate 	} else {
199*0Sstevel@tonic-gate 		*cp = '\0'; /* cut off ".rpc_call" */
200*0Sstevel@tonic-gate 		called_thru_rpc = TRUE;
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	/* initialize */
205*0Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
206*0Sstevel@tonic-gate 			meta_check_root(ep) != 0) {
207*0Sstevel@tonic-gate 		mde_perror(ep, "");
208*0Sstevel@tonic-gate 		md_exit(sp, 1);
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	/* find set and metadevice first */
212*0Sstevel@tonic-gate 	optind = 1;
213*0Sstevel@tonic-gate 	opterr = 1;
214*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "hs:f?")) != -1) {
215*0Sstevel@tonic-gate 		switch (c) {
216*0Sstevel@tonic-gate 		case 'h':
217*0Sstevel@tonic-gate 			usage(sp, 0);
218*0Sstevel@tonic-gate 			break;
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 		case 's':
221*0Sstevel@tonic-gate 			sname = optarg;
222*0Sstevel@tonic-gate 			break;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 		case '?':
225*0Sstevel@tonic-gate 			if (optopt == '?')
226*0Sstevel@tonic-gate 				usage(sp, 0);
227*0Sstevel@tonic-gate 			break;
228*0Sstevel@tonic-gate 		}
229*0Sstevel@tonic-gate 	}
230*0Sstevel@tonic-gate 	if ((argc - optind) <= 0)
231*0Sstevel@tonic-gate 		usage(sp, 1);
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	if (sname != NULL) {
234*0Sstevel@tonic-gate 		if ((sp = metasetname(sname, ep)) == NULL) {
235*0Sstevel@tonic-gate 			mde_perror(ep, "");
236*0Sstevel@tonic-gate 			md_exit(sp, 1);
237*0Sstevel@tonic-gate 		}
238*0Sstevel@tonic-gate 	}
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	/* Get metadevice name */
241*0Sstevel@tonic-gate 	if (((np = metaname(&sp, argv[optind], ep)) == NULL) ||
242*0Sstevel@tonic-gate 	    (metachkmeta(np, ep) != 0)) {
243*0Sstevel@tonic-gate 		mde_perror(ep, "");
244*0Sstevel@tonic-gate 		md_exit(sp, 1);
245*0Sstevel@tonic-gate 	}
246*0Sstevel@tonic-gate 	assert(sp != NULL);
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	if ((called_thru_rpc == FALSE) &&
249*0Sstevel@tonic-gate 	    meta_is_mn_name(&sp, argv[optind], ep)) {
250*0Sstevel@tonic-gate 		/*
251*0Sstevel@tonic-gate 		 * If we are dealing with a MN set and we were not
252*0Sstevel@tonic-gate 		 * called thru an rpc call, we are just to send this
253*0Sstevel@tonic-gate 		 * command string to the master of the set and let it
254*0Sstevel@tonic-gate 		 * deal with it.
255*0Sstevel@tonic-gate 		 * Note that if sp is NULL, meta_is_mn_name() derives sp
256*0Sstevel@tonic-gate 		 * from argv[optind] which is the metadevice arg
257*0Sstevel@tonic-gate 		 * If this fails, the master must panic as the mddb may be
258*0Sstevel@tonic-gate 		 * inconsistent
259*0Sstevel@tonic-gate 		 */
260*0Sstevel@tonic-gate 		int  result;
261*0Sstevel@tonic-gate 		result = meta_mn_send_command(sp, argc, argv, MD_DISP_STDERR |
262*0Sstevel@tonic-gate 		    MD_PANIC_WHEN_INCONSISTENT, NO_CONTEXT_STRING, ep);
263*0Sstevel@tonic-gate 		/*
264*0Sstevel@tonic-gate 		 * The error message has been already been displayed
265*0Sstevel@tonic-gate 		 * just exit
266*0Sstevel@tonic-gate 		 */
267*0Sstevel@tonic-gate 		md_exit(sp, result);
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	/* grab set lock */
271*0Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, ep)) {
272*0Sstevel@tonic-gate 		mde_perror(ep, "");
273*0Sstevel@tonic-gate 		md_exit(sp, 1);
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	/* check ownership */
277*0Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
278*0Sstevel@tonic-gate 		mde_perror(ep, "");
279*0Sstevel@tonic-gate 		md_exit(sp, 1);
280*0Sstevel@tonic-gate 	}
281*0Sstevel@tonic-gate 	if ((miscname = metagetmiscname(np, ep)) == NULL) {
282*0Sstevel@tonic-gate 		mde_perror(ep, "");
283*0Sstevel@tonic-gate 		md_exit(sp, 1);
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	/* dispatch based on device type */
287*0Sstevel@tonic-gate 	if (strcmp(miscname, MD_MIRROR) == 0) {
288*0Sstevel@tonic-gate 		if (mirror_detach(&sp, np, argc, argv, options, ep) != 0) {
289*0Sstevel@tonic-gate 			mde_perror(ep, "");
290*0Sstevel@tonic-gate 			md_exit(sp, 1);
291*0Sstevel@tonic-gate 		}
292*0Sstevel@tonic-gate 	} else if (strcmp(miscname, MD_TRANS) == 0) {
293*0Sstevel@tonic-gate 		if (trans_detach(sp, np, argc, argv, options, ep) != 0) {
294*0Sstevel@tonic-gate 			mde_perror(ep, "");
295*0Sstevel@tonic-gate 			md_exit(sp, 1);
296*0Sstevel@tonic-gate 		}
297*0Sstevel@tonic-gate 	} else {
298*0Sstevel@tonic-gate 		md_eprintf(gettext(
299*0Sstevel@tonic-gate 		    "%s: invalid metadevice type %s\n"),
300*0Sstevel@tonic-gate 		    np->cname, miscname);
301*0Sstevel@tonic-gate 		md_exit(sp, 1);
302*0Sstevel@tonic-gate 	}
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	/* return success */
305*0Sstevel@tonic-gate 	md_exit(sp, 0);
306*0Sstevel@tonic-gate 	/*NOTREACHED*/
307*0Sstevel@tonic-gate 	return (0);
308*0Sstevel@tonic-gate }
309