xref: /onnv-gate/usr/src/cmd/lvm/util/metadetach.c (revision 1623:7bac4a816ebe)
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
5*1623Stw21770  * Common Development and Distribution License (the "License").
6*1623Stw21770  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1623Stw21770  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * detach submirrors
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <meta.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sdssc.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * print usage message
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate static void
usage(mdsetname_t * sp,int eval)400Sstevel@tonic-gate usage(
410Sstevel@tonic-gate 	mdsetname_t	*sp,
420Sstevel@tonic-gate 	int		eval
430Sstevel@tonic-gate )
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\
460Sstevel@tonic-gate usage:	%s [-s setname] [-f] mirror submirror\n\
470Sstevel@tonic-gate 	%s [-s setname] [-f] trans\n"),
480Sstevel@tonic-gate 	    myname, myname);
490Sstevel@tonic-gate 	md_exit(sp, eval);
500Sstevel@tonic-gate }
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * detach submirror from mirror
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate static int
mirror_detach(mdsetname_t ** spp,mdname_t * mirnp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)560Sstevel@tonic-gate mirror_detach(
570Sstevel@tonic-gate 	mdsetname_t	**spp,
580Sstevel@tonic-gate 	mdname_t	*mirnp,
590Sstevel@tonic-gate 	int		argc,
600Sstevel@tonic-gate 	char		*argv[],
610Sstevel@tonic-gate 	mdcmdopts_t	options,
620Sstevel@tonic-gate 	md_error_t	*ep
630Sstevel@tonic-gate )
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	mdname_t	*submirnp;
660Sstevel@tonic-gate 	int		c;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	/* reset and parse args */
690Sstevel@tonic-gate 	optind = 1;
700Sstevel@tonic-gate 	opterr = 1;
710Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:f")) != -1) {
720Sstevel@tonic-gate 		switch (c) {
730Sstevel@tonic-gate 		case 's':
740Sstevel@tonic-gate 			break;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 		case 'f':
770Sstevel@tonic-gate 			options |= MDCMD_FORCE;
780Sstevel@tonic-gate 			break;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 		default:
810Sstevel@tonic-gate 			usage(*spp, 1);
820Sstevel@tonic-gate 			/*NOTREACHED*/
830Sstevel@tonic-gate 			break;
840Sstevel@tonic-gate 		}
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 	argc -= optind;
870Sstevel@tonic-gate 	argv += optind;
880Sstevel@tonic-gate 	if (argc != 2)
890Sstevel@tonic-gate 		usage(*spp, 1);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	/* get submirror */
92*1623Stw21770 	if ((submirnp = metaname(spp, argv[1], META_DEVICE, ep)) == NULL)
930Sstevel@tonic-gate 		return (-1);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	/* detach submirror */
960Sstevel@tonic-gate 	if (meta_mirror_detach(*spp, mirnp, submirnp, options, ep) != 0)
970Sstevel@tonic-gate 		return (-1);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/* update md.cf */
1000Sstevel@tonic-gate 	if (meta_update_md_cf(*spp, ep) != 0)
1010Sstevel@tonic-gate 		return (-1);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	/* return success */
1040Sstevel@tonic-gate 	return (0);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * detach log from trans
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate static int
trans_detach(mdsetname_t * sp,mdname_t * transnp,int argc,char * argv[],mdcmdopts_t options,md_error_t * ep)1110Sstevel@tonic-gate trans_detach(
1120Sstevel@tonic-gate 	mdsetname_t	*sp,
1130Sstevel@tonic-gate 	mdname_t	*transnp,
1140Sstevel@tonic-gate 	int		argc,
1150Sstevel@tonic-gate 	char		*argv[],
1160Sstevel@tonic-gate 	mdcmdopts_t	options,
1170Sstevel@tonic-gate 	md_error_t	*ep
1180Sstevel@tonic-gate )
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	int		delayed;
1210Sstevel@tonic-gate 	int		c;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	/* reset and parse args */
1240Sstevel@tonic-gate 	optind = 1;
1250Sstevel@tonic-gate 	opterr = 1;
1260Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "s:f")) != -1) {
1270Sstevel@tonic-gate 		switch (c) {
1280Sstevel@tonic-gate 		case 's':
1290Sstevel@tonic-gate 			break;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 		case 'f':
1320Sstevel@tonic-gate 			options |= MDCMD_FORCE;
1330Sstevel@tonic-gate 			break;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 		default:
1360Sstevel@tonic-gate 			usage(sp, 1);
1370Sstevel@tonic-gate 			/*NOTREACHED*/
1380Sstevel@tonic-gate 			break;
1390Sstevel@tonic-gate 		}
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 	argc -= optind;
1420Sstevel@tonic-gate 	argv += optind;
1430Sstevel@tonic-gate 	if (argc != 1)
1440Sstevel@tonic-gate 		usage(sp, 1);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	/* detach log */
1470Sstevel@tonic-gate 	if (meta_trans_detach(sp, transnp, options, &delayed, ep) != 0)
1480Sstevel@tonic-gate 		return (-1);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	/* update md.cf */
1510Sstevel@tonic-gate 	if (meta_update_md_cf(sp, ep) != 0)
1520Sstevel@tonic-gate 		return (-1);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/* return success */
1550Sstevel@tonic-gate 	return (0);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * parse args and doit
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate int
main(int argc,char * argv[])1620Sstevel@tonic-gate main(
1630Sstevel@tonic-gate 	int 		argc,
1640Sstevel@tonic-gate 	char		*argv[]
1650Sstevel@tonic-gate )
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	char		*sname = NULL;
1680Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
1690Sstevel@tonic-gate 	mdcmdopts_t	options = (MDCMD_PRINT);
1700Sstevel@tonic-gate 	mdname_t	*np;
1710Sstevel@tonic-gate 	char		*miscname;
1720Sstevel@tonic-gate 	int		c;
1730Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
1740Sstevel@tonic-gate 	md_error_t	*ep = &status;
1750Sstevel@tonic-gate 	int		error;
1760Sstevel@tonic-gate 	bool_t		called_thru_rpc = FALSE;
1770Sstevel@tonic-gate 	char		*cp;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	/*
1810Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
1820Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
1830Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
1840Sstevel@tonic-gate 	 * something.
1850Sstevel@tonic-gate 	 */
1860Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1870Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1880Sstevel@tonic-gate #endif
1890Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1900Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
1930Sstevel@tonic-gate 		if (sdssc_bind_library() == SDSSC_OKAY)
1940Sstevel@tonic-gate 			if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
1950Sstevel@tonic-gate 						&error) == SDSSC_PROXY_DONE)
1960Sstevel@tonic-gate 				exit(error);
1970Sstevel@tonic-gate 	} else {
1980Sstevel@tonic-gate 		*cp = '\0'; /* cut off ".rpc_call" */
1990Sstevel@tonic-gate 		called_thru_rpc = TRUE;
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/* initialize */
2040Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
2050Sstevel@tonic-gate 			meta_check_root(ep) != 0) {
2060Sstevel@tonic-gate 		mde_perror(ep, "");
2070Sstevel@tonic-gate 		md_exit(sp, 1);
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* find set and metadevice first */
2110Sstevel@tonic-gate 	optind = 1;
2120Sstevel@tonic-gate 	opterr = 1;
2130Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "hs:f?")) != -1) {
2140Sstevel@tonic-gate 		switch (c) {
2150Sstevel@tonic-gate 		case 'h':
2160Sstevel@tonic-gate 			usage(sp, 0);
2170Sstevel@tonic-gate 			break;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		case 's':
2200Sstevel@tonic-gate 			sname = optarg;
2210Sstevel@tonic-gate 			break;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		case '?':
2240Sstevel@tonic-gate 			if (optopt == '?')
2250Sstevel@tonic-gate 				usage(sp, 0);
2260Sstevel@tonic-gate 			break;
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 	if ((argc - optind) <= 0)
2300Sstevel@tonic-gate 		usage(sp, 1);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (sname != NULL) {
2330Sstevel@tonic-gate 		if ((sp = metasetname(sname, ep)) == NULL) {
2340Sstevel@tonic-gate 			mde_perror(ep, "");
2350Sstevel@tonic-gate 			md_exit(sp, 1);
2360Sstevel@tonic-gate 		}
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/* Get metadevice name */
240*1623Stw21770 	if (((np = metaname(&sp, argv[optind], META_DEVICE, ep)) == NULL) ||
2410Sstevel@tonic-gate 	    (metachkmeta(np, ep) != 0)) {
2420Sstevel@tonic-gate 		mde_perror(ep, "");
2430Sstevel@tonic-gate 		md_exit(sp, 1);
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 	assert(sp != NULL);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if ((called_thru_rpc == FALSE) &&
2480Sstevel@tonic-gate 	    meta_is_mn_name(&sp, argv[optind], ep)) {
2490Sstevel@tonic-gate 		/*
2500Sstevel@tonic-gate 		 * If we are dealing with a MN set and we were not
2510Sstevel@tonic-gate 		 * called thru an rpc call, we are just to send this
2520Sstevel@tonic-gate 		 * command string to the master of the set and let it
2530Sstevel@tonic-gate 		 * deal with it.
2540Sstevel@tonic-gate 		 * Note that if sp is NULL, meta_is_mn_name() derives sp
2550Sstevel@tonic-gate 		 * from argv[optind] which is the metadevice arg
2560Sstevel@tonic-gate 		 * If this fails, the master must panic as the mddb may be
2570Sstevel@tonic-gate 		 * inconsistent
2580Sstevel@tonic-gate 		 */
2590Sstevel@tonic-gate 		int  result;
2600Sstevel@tonic-gate 		result = meta_mn_send_command(sp, argc, argv, MD_DISP_STDERR |
2610Sstevel@tonic-gate 		    MD_PANIC_WHEN_INCONSISTENT, NO_CONTEXT_STRING, ep);
2620Sstevel@tonic-gate 		/*
2630Sstevel@tonic-gate 		 * The error message has been already been displayed
2640Sstevel@tonic-gate 		 * just exit
2650Sstevel@tonic-gate 		 */
2660Sstevel@tonic-gate 		md_exit(sp, result);
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/* grab set lock */
2700Sstevel@tonic-gate 	if (meta_lock(sp, TRUE, ep)) {
2710Sstevel@tonic-gate 		mde_perror(ep, "");
2720Sstevel@tonic-gate 		md_exit(sp, 1);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	/* check ownership */
2760Sstevel@tonic-gate 	if (meta_check_ownership(sp, ep) != 0) {
2770Sstevel@tonic-gate 		mde_perror(ep, "");
2780Sstevel@tonic-gate 		md_exit(sp, 1);
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 	if ((miscname = metagetmiscname(np, ep)) == NULL) {
2810Sstevel@tonic-gate 		mde_perror(ep, "");
2820Sstevel@tonic-gate 		md_exit(sp, 1);
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	/* dispatch based on device type */
2860Sstevel@tonic-gate 	if (strcmp(miscname, MD_MIRROR) == 0) {
2870Sstevel@tonic-gate 		if (mirror_detach(&sp, np, argc, argv, options, ep) != 0) {
2880Sstevel@tonic-gate 			mde_perror(ep, "");
2890Sstevel@tonic-gate 			md_exit(sp, 1);
2900Sstevel@tonic-gate 		}
2910Sstevel@tonic-gate 	} else if (strcmp(miscname, MD_TRANS) == 0) {
2920Sstevel@tonic-gate 		if (trans_detach(sp, np, argc, argv, options, ep) != 0) {
2930Sstevel@tonic-gate 			mde_perror(ep, "");
2940Sstevel@tonic-gate 			md_exit(sp, 1);
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 	} else {
2970Sstevel@tonic-gate 		md_eprintf(gettext(
2980Sstevel@tonic-gate 		    "%s: invalid metadevice type %s\n"),
2990Sstevel@tonic-gate 		    np->cname, miscname);
3000Sstevel@tonic-gate 		md_exit(sp, 1);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/* return success */
3040Sstevel@tonic-gate 	md_exit(sp, 0);
3050Sstevel@tonic-gate 	/*NOTREACHED*/
3060Sstevel@tonic-gate 	return (0);
3070Sstevel@tonic-gate }
308