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 * rename or exchange metadevice identity
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] [-x] metadevice1 metadevice2\n\
470Sstevel@tonic-gate %s -h\n\
480Sstevel@tonic-gate options:\n\
490Sstevel@tonic-gate -s operations are done on the set setname, rather than the local set\n\
500Sstevel@tonic-gate -f force exchange or rename\n\
510Sstevel@tonic-gate -x exchange the identities of metadevice1 and metadevice2\n\
520Sstevel@tonic-gate -h help: print this message\n"), myname, myname);
530Sstevel@tonic-gate md_exit(sp, eval);
540Sstevel@tonic-gate }
550Sstevel@tonic-gate
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * mainline. crack command line arguments.
580Sstevel@tonic-gate */
590Sstevel@tonic-gate int
main(int argc,char * argv[])600Sstevel@tonic-gate main(
610Sstevel@tonic-gate int argc,
620Sstevel@tonic-gate char *argv[]
630Sstevel@tonic-gate )
640Sstevel@tonic-gate {
650Sstevel@tonic-gate char *sname = NULL;
660Sstevel@tonic-gate mdsetname_t *sp = NULL;
670Sstevel@tonic-gate int xflag = 0;
680Sstevel@tonic-gate mdcmdopts_t options = (MDCMD_PRINT | MDCMD_DOIT);
690Sstevel@tonic-gate md_error_t status = mdnullerror;
700Sstevel@tonic-gate md_error_t *ep = &status;
710Sstevel@tonic-gate int rc = 0;
720Sstevel@tonic-gate mdname_t *mdnms[2];
730Sstevel@tonic-gate int c, i;
740Sstevel@tonic-gate int error;
750Sstevel@tonic-gate bool_t called_thru_rpc = FALSE;
760Sstevel@tonic-gate char *cp;
770Sstevel@tonic-gate int origargc = argc;
780Sstevel@tonic-gate char **origargv = argv;
790Sstevel@tonic-gate char *miscname;
800Sstevel@tonic-gate
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate * Get the locale set up before calling any other routines
830Sstevel@tonic-gate * with messages to ouput. Just in case we're not in a build
840Sstevel@tonic-gate * environment, make sure that TEXT_DOMAIN gets set to
850Sstevel@tonic-gate * something.
860Sstevel@tonic-gate */
870Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
880Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
890Sstevel@tonic-gate #endif
900Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
910Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
920Sstevel@tonic-gate
930Sstevel@tonic-gate
940Sstevel@tonic-gate if ((cp = strstr(argv[0], ".rpc_call")) == NULL) {
950Sstevel@tonic-gate if (sdssc_bind_library() == SDSSC_OKAY)
960Sstevel@tonic-gate if (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
970Sstevel@tonic-gate &error) == SDSSC_PROXY_DONE)
980Sstevel@tonic-gate exit(error);
990Sstevel@tonic-gate } else {
1000Sstevel@tonic-gate *cp = '\0'; /* cut off ".rpc_call" */
1010Sstevel@tonic-gate called_thru_rpc = TRUE;
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /* initialize */
1050Sstevel@tonic-gate if (md_init(argc, argv, 0, 1, ep) != 0 ||
1060Sstevel@tonic-gate meta_check_root(ep) != 0) {
1070Sstevel@tonic-gate mde_perror(ep, "");
1080Sstevel@tonic-gate md_exit(sp, 1);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate /* parse args */
1120Sstevel@tonic-gate optind = 1;
1130Sstevel@tonic-gate opterr = 1;
1140Sstevel@tonic-gate while ((c = getopt(argc, argv, "fns:xh?")) != -1) {
1150Sstevel@tonic-gate switch (c) {
1160Sstevel@tonic-gate case 'h':
1170Sstevel@tonic-gate usage(sp, 0);
1180Sstevel@tonic-gate break;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate case 's':
1210Sstevel@tonic-gate sname = optarg;
1220Sstevel@tonic-gate break;
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate case 'x':
1250Sstevel@tonic-gate ++xflag;
1260Sstevel@tonic-gate break;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate case 'f':
1290Sstevel@tonic-gate options |= MDCMD_FORCE;
1300Sstevel@tonic-gate break;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate case 'n':
1330Sstevel@tonic-gate if (called_thru_rpc == TRUE) {
1340Sstevel@tonic-gate options &= ~MDCMD_DOIT;
1350Sstevel@tonic-gate } else {
1360Sstevel@tonic-gate usage(sp, 1);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate break;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate case '?':
1410Sstevel@tonic-gate if (optopt == '?')
1420Sstevel@tonic-gate usage(sp, 0);
1430Sstevel@tonic-gate /*FALLTHROUGH*/
1440Sstevel@tonic-gate default:
1450Sstevel@tonic-gate usage(sp, 1);
1460Sstevel@tonic-gate break;
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate argc -= optind;
1500Sstevel@tonic-gate argv += optind;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate if (sname != NULL) {
1530Sstevel@tonic-gate if ((sp = metasetname(sname, ep)) == NULL) {
1540Sstevel@tonic-gate mde_perror(ep, "");
1550Sstevel@tonic-gate md_exit(sp, 1);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate if (argc != 2) {
1600Sstevel@tonic-gate usage(sp, 1);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate if ((called_thru_rpc == FALSE) &&
1640Sstevel@tonic-gate meta_is_mn_name(&sp, argv[0], ep)) {
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate * If we are dealing with a MN set and we were not
1670Sstevel@tonic-gate * called thru an rpc call, we are just to send this
1680Sstevel@tonic-gate * command string to the master of the set and let it
1690Sstevel@tonic-gate * deal with it.
1700Sstevel@tonic-gate * Note that if sp is NULL, meta_is_mn_name() derives sp
1710Sstevel@tonic-gate * from argv[0] which is the metadevice arg
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate int result;
1740Sstevel@tonic-gate int i;
1750Sstevel@tonic-gate int newargc;
1760Sstevel@tonic-gate char **newargv;
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate * For MN sets we start a dryrun version of this command
1800Sstevel@tonic-gate * before sending out the real version.
1810Sstevel@tonic-gate * Thus we need a new array for the arguments as the first
1820Sstevel@tonic-gate * one will be -n to indicate the dryrun
1830Sstevel@tonic-gate */
1840Sstevel@tonic-gate newargv = calloc(origargc+1, sizeof (char *));
1850Sstevel@tonic-gate newargv[0] = "metarename";
1860Sstevel@tonic-gate newargv[1] = "-n"; /* always do "-n" first */
1870Sstevel@tonic-gate newargc = 2;
1880Sstevel@tonic-gate for (i = 1; i < origargc; i++, newargc++)
1890Sstevel@tonic-gate newargv[newargc] = origargv[i];
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate result = meta_mn_send_command(sp, newargc, newargv,
1920Sstevel@tonic-gate MD_DISP_STDERR | MD_DRYRUN, NO_CONTEXT_STRING, ep);
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate /* If we found a problem don't do it for real */
1950Sstevel@tonic-gate if (result != 0) {
1960Sstevel@tonic-gate md_exit(sp, result);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate * Do it for real now. Remove "-n" from the arguments and
2010Sstevel@tonic-gate * MD_DRYRUN from the flags. If this fails, the master must
2020Sstevel@tonic-gate * panic as the mddb may be inconsistent.
2030Sstevel@tonic-gate */
2040Sstevel@tonic-gate newargv[1] = ""; /* this was "-n" before */
2050Sstevel@tonic-gate result = meta_mn_send_command(sp, newargc, newargv,
2060Sstevel@tonic-gate MD_DISP_STDERR | MD_RETRY_BUSY | MD_PANIC_WHEN_INCONSISTENT,
2070Sstevel@tonic-gate NO_CONTEXT_STRING, ep);
2080Sstevel@tonic-gate free(newargv);
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate md_exit(sp, result);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate for (i = 0; i < 2; i++) {
2140Sstevel@tonic-gate if (!is_metaname(argv[i])) {
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate * one of the input devices is not a valid
2170Sstevel@tonic-gate * metadevice name
2180Sstevel@tonic-gate */
2190Sstevel@tonic-gate usage(sp, 1);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate if (i == 1 && !xflag) {
2220Sstevel@tonic-gate /* rename, create dest metadevice name */
223*1623Stw21770 if (meta_init_make_device(&sp, argv[i], ep) <= 0) {
2240Sstevel@tonic-gate mde_perror(ep, argv[i]);
2250Sstevel@tonic-gate md_exit(sp, 1);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate
229*1623Stw21770 if ((mdnms[i] = metaname(&sp, argv[i],
230*1623Stw21770 META_DEVICE, ep)) == NULL) {
2310Sstevel@tonic-gate mde_perror(ep, argv[i]);
2320Sstevel@tonic-gate md_exit(sp, 1);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate * The FORCE option is only valid for a trans metadevice, clear it if
2380Sstevel@tonic-gate * it is not trans
2390Sstevel@tonic-gate */
2400Sstevel@tonic-gate if ((miscname = metagetmiscname(mdnms[0], ep)) == NULL) {
2410Sstevel@tonic-gate mde_perror(ep, "");
2420Sstevel@tonic-gate md_exit(sp, 1);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate if (strcmp(miscname, MD_TRANS) != 0) {
2460Sstevel@tonic-gate options &= ~MDCMD_FORCE;
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate if (meta_lock(sp, TRUE, ep)) {
2500Sstevel@tonic-gate mde_perror(ep, "");
2510Sstevel@tonic-gate md_exit(sp, 1);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if (meta_check_ownership(sp, ep) != 0) {
2550Sstevel@tonic-gate mde_perror(ep, "");
2560Sstevel@tonic-gate md_exit(sp, 1);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate if (xflag) {
2600Sstevel@tonic-gate rc = meta_exchange(sp, mdnms[0], mdnms[1], options, ep);
2610Sstevel@tonic-gate } else {
2620Sstevel@tonic-gate rc = meta_rename(sp, mdnms[0], mdnms[1], options, ep);
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate out:
2650Sstevel@tonic-gate if (rc != 0 || !mdisok(ep)) {
2660Sstevel@tonic-gate mde_perror(ep, "");
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate md_exit(sp, rc);
2690Sstevel@tonic-gate /*NOTREACHED*/
2700Sstevel@tonic-gate return (rc);
2710Sstevel@tonic-gate }
272