xref: /onnv-gate/usr/src/cmd/lvm/util/metaclear.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  * clear metadevices
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <meta.h>
330Sstevel@tonic-gate #include <sdssc.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * clear metadevice or hotspare pool
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate static int
clear_name(mdsetname_t ** spp,char * uname,mdcmdopts_t options,md_error_t * ep)400Sstevel@tonic-gate clear_name(
410Sstevel@tonic-gate 	mdsetname_t	**spp,
420Sstevel@tonic-gate 	char		*uname,
430Sstevel@tonic-gate 	mdcmdopts_t	options,
440Sstevel@tonic-gate 	md_error_t	*ep
450Sstevel@tonic-gate )
460Sstevel@tonic-gate {
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	/* clear hotspare pool */
49*1623Stw21770 	if (is_existing_hsp(*spp, uname)) {
500Sstevel@tonic-gate 		mdhspname_t	*hspnp;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 		/* get hotspare pool name */
530Sstevel@tonic-gate 		if ((hspnp = metahspname(spp, uname, ep)) == NULL)
540Sstevel@tonic-gate 			return (-1);
550Sstevel@tonic-gate 		assert(*spp != NULL);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 		/* grab set lock */
580Sstevel@tonic-gate 		if (meta_lock(*spp, TRUE, ep))
590Sstevel@tonic-gate 			return (-1);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 		/* check for ownership */
620Sstevel@tonic-gate 		if (meta_check_ownership(*spp, ep) != 0)
630Sstevel@tonic-gate 			return (-1);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 		/* clear hotspare pool */
660Sstevel@tonic-gate 		return (meta_hsp_reset(*spp, hspnp, options, ep));
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	/* clear metadevice */
700Sstevel@tonic-gate 	else {
710Sstevel@tonic-gate 		mdname_t	*np;
720Sstevel@tonic-gate 
73*1623Stw21770 		/* check for ownership */
74*1623Stw21770 		if (meta_check_ownership(*spp, ep) != 0)
75*1623Stw21770 			return (-1);
76*1623Stw21770 
770Sstevel@tonic-gate 		/* get metadevice name */
78*1623Stw21770 		if (((np = metaname(spp, uname, META_DEVICE, ep)) == NULL) ||
790Sstevel@tonic-gate 		    (metachkmeta(np, ep) != 0)) {
800Sstevel@tonic-gate 			return (-1);
810Sstevel@tonic-gate 		}
820Sstevel@tonic-gate 		assert(*spp != NULL);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 		/* grab set lock */
850Sstevel@tonic-gate 		if (meta_lock(*spp, TRUE, ep))
860Sstevel@tonic-gate 			return (-1);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 		/* clear metadevice */
890Sstevel@tonic-gate 		return (meta_reset_by_name(*spp, np, options, ep));
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * print usage message
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate static void
usage(mdsetname_t * sp,int eval)970Sstevel@tonic-gate usage(
980Sstevel@tonic-gate 	mdsetname_t	*sp,
990Sstevel@tonic-gate 	int		eval
1000Sstevel@tonic-gate )
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\
1030Sstevel@tonic-gate usage:	%s [-s setname] -a\n\
1040Sstevel@tonic-gate 	%s [-s setname] [options] metadevice...\n\
1050Sstevel@tonic-gate options:\n\
1060Sstevel@tonic-gate -f	force clear\n\
1070Sstevel@tonic-gate -r	recursive clear\n\
1080Sstevel@tonic-gate -p	clear all soft partitions on metadevice/component\n"), myname, myname);
1090Sstevel@tonic-gate 	md_exit(sp, eval);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * mainline.   crack command line arguments.
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate int
main(int argc,char * argv[])1160Sstevel@tonic-gate main(
1170Sstevel@tonic-gate 	int		argc,
1180Sstevel@tonic-gate 	char		*argv[]
1190Sstevel@tonic-gate )
1200Sstevel@tonic-gate {
121*1623Stw21770 	char		*sname = MD_LOCAL_NAME;
1220Sstevel@tonic-gate 	mdsetname_t	*sp = NULL;
1230Sstevel@tonic-gate 	int		aflag = 0;
1240Sstevel@tonic-gate 	int		pflag = 0;
125*1623Stw21770 	int		set_flag = 0;
1260Sstevel@tonic-gate 	mdcmdopts_t	options = (MDCMD_PRINT|MDCMD_DOIT);
1270Sstevel@tonic-gate 	int		c;
1280Sstevel@tonic-gate 	md_error_t	status = mdnullerror;
1290Sstevel@tonic-gate 	md_error_t	*ep = &status;
1300Sstevel@tonic-gate 	int		eval = 1;
1310Sstevel@tonic-gate 	int		error;
1320Sstevel@tonic-gate 	bool_t		called_thru_rpc = FALSE;
1330Sstevel@tonic-gate 	char		*cp;
1340Sstevel@tonic-gate 	int		mnset = FALSE;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/*
1370Sstevel@tonic-gate 	 * Get the locale set up before calling any other routines
1380Sstevel@tonic-gate 	 * with messages to ouput.  Just in case we're not in a build
1390Sstevel@tonic-gate 	 * environment, make sure that TEXT_DOMAIN gets set to
1400Sstevel@tonic-gate 	 * something.
1410Sstevel@tonic-gate 	 */
1420Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1430Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1440Sstevel@tonic-gate #endif
1450Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1460Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
1490Sstevel@tonic-gate 		if (sdssc_bind_library() == SDSSC_OKAY)
1500Sstevel@tonic-gate 			if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
1510Sstevel@tonic-gate 						&error) == SDSSC_PROXY_DONE)
1520Sstevel@tonic-gate 				exit(error);
1530Sstevel@tonic-gate 	} else {
1540Sstevel@tonic-gate 		*cp = '\0'; /* cut off ".rpc_call" */
1550Sstevel@tonic-gate 		called_thru_rpc = TRUE;
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/* initialize */
1600Sstevel@tonic-gate 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
1610Sstevel@tonic-gate 			meta_check_root(ep) != 0)
1620Sstevel@tonic-gate 		goto errout;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	/* parse args */
1650Sstevel@tonic-gate 	optind = 1;
1660Sstevel@tonic-gate 	opterr = 1;
1670Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "hs:afrp?")) != -1) {
1680Sstevel@tonic-gate 		switch (c) {
1690Sstevel@tonic-gate 		case 'h':
1700Sstevel@tonic-gate 			usage(sp, 0);
1710Sstevel@tonic-gate 			break;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 		case 's':
1740Sstevel@tonic-gate 			sname = optarg;
175*1623Stw21770 			set_flag++;
1760Sstevel@tonic-gate 			break;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 		case 'a':
1790Sstevel@tonic-gate 			++aflag;
1800Sstevel@tonic-gate 			options |= MDCMD_FORCE;
1810Sstevel@tonic-gate 			break;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 		case 'f':
1840Sstevel@tonic-gate 			options |= MDCMD_FORCE;
1850Sstevel@tonic-gate 			break;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		case 'r':
1880Sstevel@tonic-gate 			options |= MDCMD_RECURSE | MDCMD_FORCE;
1890Sstevel@tonic-gate 			break;
1900Sstevel@tonic-gate 		case 'p':
1910Sstevel@tonic-gate 			++pflag;
1920Sstevel@tonic-gate 			break;
1930Sstevel@tonic-gate 		case '?':
1940Sstevel@tonic-gate 			if (optopt == '?')
1950Sstevel@tonic-gate 				usage(sp, 0);
1960Sstevel@tonic-gate 			/*FALLTHROUGH*/
1970Sstevel@tonic-gate 		default:
1980Sstevel@tonic-gate 			usage(sp, 1);
1990Sstevel@tonic-gate 			break;
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 	argc -= optind;
2030Sstevel@tonic-gate 	argv += optind;
2040Sstevel@tonic-gate 
205*1623Stw21770 	/* with mn sets if -a, set name must have been specified by -s */
206*1623Stw21770 	if (called_thru_rpc && aflag && !set_flag) {
207*1623Stw21770 		md_eprintf(gettext(
208*1623Stw21770 		    "-a parameter requires the use of -s in multi-node sets"));
209*1623Stw21770 		md_exit(sp, 1);
210*1623Stw21770 	}
211*1623Stw21770 
212*1623Stw21770 	/* get set context */
213*1623Stw21770 	if ((sp = metasetname(sname, ep)) == NULL) {
214*1623Stw21770 		mde_perror(ep, "");
215*1623Stw21770 		md_exit(sp, 1);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (called_thru_rpc) {
2190Sstevel@tonic-gate 		/* Check if the device is open  on all nodes */
2200Sstevel@tonic-gate 		options |= MDCMD_MN_OPEN_CHECK;
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (aflag) {	/* clear all devices */
2240Sstevel@tonic-gate 		if (argc != 0)
2250Sstevel@tonic-gate 			usage(sp, 1);
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 		/*
2280Sstevel@tonic-gate 		 * If a MN set, we will generate a series of individual
2290Sstevel@tonic-gate 		 * metaclear commands which will each grab the set lock.
2300Sstevel@tonic-gate 		 * Therefore do not grab the set lock now.
2310Sstevel@tonic-gate 		 */
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 		if (!meta_is_mn_set(sp, ep)) {
2340Sstevel@tonic-gate 			/* grab set lock */
2350Sstevel@tonic-gate 			if (meta_lock(sp, TRUE, ep))
2360Sstevel@tonic-gate 				goto errout;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 			/* check for ownership */
2390Sstevel@tonic-gate 			if (meta_check_ownership(sp, ep) != 0)
2400Sstevel@tonic-gate 				goto errout;
2410Sstevel@tonic-gate 		} else {
2420Sstevel@tonic-gate 			mnset = TRUE;
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		/* reset all devices in set */
2460Sstevel@tonic-gate 		if (meta_reset_all(sp, options, ep) != 0) {
2470Sstevel@tonic-gate 			if (!mnset)
2480Sstevel@tonic-gate 				mde_perror(ep, "");
2490Sstevel@tonic-gate 		} else
2500Sstevel@tonic-gate 			eval = 0;
2510Sstevel@tonic-gate 	} else {
2520Sstevel@tonic-gate 		/*
2530Sstevel@tonic-gate 		 * We are dealing with either a single or multiple names.
2540Sstevel@tonic-gate 		 * The set for the command is either denoted by the -s option
2550Sstevel@tonic-gate 		 * or the set of the first name.
2560Sstevel@tonic-gate 		 */
2570Sstevel@tonic-gate 		if (argc <= 0)
2580Sstevel@tonic-gate 			usage(sp, 1);
259*1623Stw21770 
2600Sstevel@tonic-gate 		if (meta_is_mn_name(&sp, argv[0], ep))
2610Sstevel@tonic-gate 			mnset = TRUE;
2620Sstevel@tonic-gate 		eval = 0;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		for (; (argc > 0); --argc, ++argv) {
2650Sstevel@tonic-gate 			char		*cname;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 			/*
2680Sstevel@tonic-gate 			 * If we are dealing with a MN set and we were not
2690Sstevel@tonic-gate 			 * called thru an rpc call, we are just to send this
2700Sstevel@tonic-gate 			 * command string to the master of the set and let it
2710Sstevel@tonic-gate 			 * deal with it.
2720Sstevel@tonic-gate 			 */
2730Sstevel@tonic-gate 			if (!called_thru_rpc && mnset) {
274*1623Stw21770 				/* get the canonical name */
2750Sstevel@tonic-gate 				if (pflag) {
2760Sstevel@tonic-gate 					/*
2770Sstevel@tonic-gate 					 * If -p, set cname to the device
2780Sstevel@tonic-gate 					 * argument.
2790Sstevel@tonic-gate 					 */
280*1623Stw21770 					cname = Strdup(argv[0]);
2810Sstevel@tonic-gate 				} else {
2820Sstevel@tonic-gate 					/*
2830Sstevel@tonic-gate 					 * For hotspares and metadevices, set
2840Sstevel@tonic-gate 					 * cname to the full name,
2850Sstevel@tonic-gate 					 * setname/hspxxx or setname/dxxx
2860Sstevel@tonic-gate 					 */
287*1623Stw21770 					cname = meta_name_getname(&sp,
288*1623Stw21770 					    argv[0], META_DEVICE, ep);
289*1623Stw21770 					if (cname == NULL) {
290*1623Stw21770 						mde_perror(ep, "");
291*1623Stw21770 						eval = 1;
292*1623Stw21770 						continue;
2930Sstevel@tonic-gate 					}
2940Sstevel@tonic-gate 				}
2950Sstevel@tonic-gate 				if (meta_mn_send_metaclear_command(sp,
2960Sstevel@tonic-gate 				    cname, options, pflag, ep) != 0) {
2970Sstevel@tonic-gate 					eval = 1;
2980Sstevel@tonic-gate 				}
299*1623Stw21770 				Free(cname);
3000Sstevel@tonic-gate 			} else {
3010Sstevel@tonic-gate 				if (pflag) {
3020Sstevel@tonic-gate 					/*
3030Sstevel@tonic-gate 					 * clear all soft partitions on named
3040Sstevel@tonic-gate 					 * devices
3050Sstevel@tonic-gate 					 */
3060Sstevel@tonic-gate 					if (meta_sp_reset_component(sp, argv[0],
3070Sstevel@tonic-gate 					    options, ep) != 0) {
3080Sstevel@tonic-gate 						mde_perror(ep, "");
3090Sstevel@tonic-gate 						eval = 1;
3100Sstevel@tonic-gate 						continue;
3110Sstevel@tonic-gate 					}
3120Sstevel@tonic-gate 				} else {
313*1623Stw21770 					/*
314*1623Stw21770 					 * get the canonical name and
315*1623Stw21770 					 * setup sp if it has been
316*1623Stw21770 					 * specified as part of the
317*1623Stw21770 					 * metadevice/hsp name param
318*1623Stw21770 					 */
319*1623Stw21770 					cname = meta_name_getname(&sp,
320*1623Stw21770 					    argv[0], META_DEVICE, ep);
321*1623Stw21770 					if (cname == NULL) {
3220Sstevel@tonic-gate 						mde_perror(ep, "");
3230Sstevel@tonic-gate 						eval = 1;
3240Sstevel@tonic-gate 						continue;
3250Sstevel@tonic-gate 					}
326*1623Stw21770 
327*1623Stw21770 					/* clear named devices */
328*1623Stw21770 					if (clear_name(&sp, cname,
329*1623Stw21770 					    options, ep) != 0) {
330*1623Stw21770 						mde_perror(ep, "");
331*1623Stw21770 						eval = 1;
332*1623Stw21770 						Free(cname);
333*1623Stw21770 						continue;
334*1623Stw21770 					}
335*1623Stw21770 					Free(cname);
3360Sstevel@tonic-gate 				}
3370Sstevel@tonic-gate 			}
3380Sstevel@tonic-gate 		}
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 	/* update md.cf */
3410Sstevel@tonic-gate 	if (meta_update_md_cf(sp, ep) != 0) {
3420Sstevel@tonic-gate 		mde_perror(ep, "");
3430Sstevel@tonic-gate 		eval = 1;
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 	md_exit(sp, eval);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate errout:
3480Sstevel@tonic-gate 	mde_perror(ep, "");
3490Sstevel@tonic-gate 	md_exit(sp, eval);
3500Sstevel@tonic-gate 	/*NOTREACHED*/
3510Sstevel@tonic-gate 	return (eval);
3520Sstevel@tonic-gate }
353