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
51623Stw21770 * Common Development and Distribution License (the "License").
61623Stw21770 * 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*6192Sps216548 * Copyright 2008 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 * sync metadevices
300Sstevel@tonic-gate */
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <meta.h>
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include <sys/lvm/md_mirror.h>
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include <sdssc.h>
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate * print usage message
420Sstevel@tonic-gate */
430Sstevel@tonic-gate static void
usage(mdsetname_t * sp,int eval)440Sstevel@tonic-gate usage(
450Sstevel@tonic-gate mdsetname_t *sp,
460Sstevel@tonic-gate int eval
470Sstevel@tonic-gate )
480Sstevel@tonic-gate {
490Sstevel@tonic-gate (void) fprintf(stderr, gettext("\
500Sstevel@tonic-gate usage: %s [-s setname] -r [buffer_size]\n\
510Sstevel@tonic-gate %s [-s setname] [buffer_size] metadevices...\n\
520Sstevel@tonic-gate %s [-s setname] -c metadevices...\n"),
530Sstevel@tonic-gate myname, myname, myname);
540Sstevel@tonic-gate md_exit(sp, eval);
550Sstevel@tonic-gate }
560Sstevel@tonic-gate
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate * crack command line arguments.
590Sstevel@tonic-gate */
600Sstevel@tonic-gate int
main(int argc,char * argv[])610Sstevel@tonic-gate main(
620Sstevel@tonic-gate int argc,
630Sstevel@tonic-gate char *argv[]
640Sstevel@tonic-gate )
650Sstevel@tonic-gate {
660Sstevel@tonic-gate char *sname = NULL;
670Sstevel@tonic-gate mdsetname_t *sp = NULL;
680Sstevel@tonic-gate int rflag = 0;
690Sstevel@tonic-gate int pflag = 0;
700Sstevel@tonic-gate daddr_t size = 0;
710Sstevel@tonic-gate int c;
720Sstevel@tonic-gate md_error_t status = mdnullerror;
730Sstevel@tonic-gate md_error_t *ep = &status;
740Sstevel@tonic-gate int rval = 0;
750Sstevel@tonic-gate int error;
760Sstevel@tonic-gate md_resync_cmd_t resync_cmd = MD_RESYNC_START;
770Sstevel@tonic-gate bool_t called_thru_rpc = FALSE;
780Sstevel@tonic-gate char *cp;
790Sstevel@tonic-gate int mn_set = FALSE;
800Sstevel@tonic-gate int cflag = 0;
810Sstevel@tonic-gate
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate * Get the locale set up before calling any other routines
840Sstevel@tonic-gate * with messages to ouput. Just in case we're not in a build
850Sstevel@tonic-gate * environment, make sure that TEXT_DOMAIN gets set to
860Sstevel@tonic-gate * something.
870Sstevel@tonic-gate */
880Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
890Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
900Sstevel@tonic-gate #endif
910Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
920Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
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
1050Sstevel@tonic-gate /* initialize */
1060Sstevel@tonic-gate if (md_init(argc, argv, 0, 1, ep) != 0 ||
107*6192Sps216548 meta_check_root(ep) != 0) {
1080Sstevel@tonic-gate mde_perror(ep, "");
1090Sstevel@tonic-gate md_exit(sp, 1);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /* parse args */
1130Sstevel@tonic-gate optind = 1;
1140Sstevel@tonic-gate opterr = 1;
1150Sstevel@tonic-gate while ((c = getopt(argc, argv, "phs:rc?")) != -1) {
1160Sstevel@tonic-gate switch (c) {
1170Sstevel@tonic-gate case 'h':
1180Sstevel@tonic-gate usage(sp, 0);
1190Sstevel@tonic-gate break;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate case 's':
1220Sstevel@tonic-gate sname = optarg;
1230Sstevel@tonic-gate break;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate case 'r':
1260Sstevel@tonic-gate ++rflag;
1270Sstevel@tonic-gate break;
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate case 'p':
1300Sstevel@tonic-gate ++pflag;
1310Sstevel@tonic-gate break;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate case 'c':
1340Sstevel@tonic-gate ++cflag;
1350Sstevel@tonic-gate resync_cmd = MD_RESYNC_KILL;
1360Sstevel@tonic-gate break;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate case '?':
1390Sstevel@tonic-gate if (optopt == '?')
1400Sstevel@tonic-gate usage(sp, 0);
1410Sstevel@tonic-gate /*FALLTHROUGH*/
1420Sstevel@tonic-gate default:
1430Sstevel@tonic-gate usage(sp, 1);
1440Sstevel@tonic-gate break;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate if ((pflag + rflag) > 1) {
1480Sstevel@tonic-gate usage(sp, 1);
1490Sstevel@tonic-gate mde_perror(ep, "");
1500Sstevel@tonic-gate md_exit(sp, 1);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate argc -= optind;
1530Sstevel@tonic-gate argv += optind;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate if (sname != NULL) {
1560Sstevel@tonic-gate if ((sp = metasetname(sname, ep)) == NULL) {
1570Sstevel@tonic-gate mde_perror(ep, "");
1580Sstevel@tonic-gate md_exit(sp, 1);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate * look for buffer size. If one is not specified we pass '0' to
1640Sstevel@tonic-gate * the meta_resync_all() call. This uses whatever size has been
1650Sstevel@tonic-gate * configured via md_mirror:md_resync_bufsz
1660Sstevel@tonic-gate * The default value (if not overridden in /etc/system) is
1670Sstevel@tonic-gate * MD_DEF_RESYNC_BUF_SIZE
1680Sstevel@tonic-gate */
1690Sstevel@tonic-gate if ((argc > 0) && (isdigit(argv[0][0]))) {
170*6192Sps216548 if ((size = atoi(argv[0])) < 0) {
1710Sstevel@tonic-gate md_eprintf(gettext(
1720Sstevel@tonic-gate "illegal buffer size %s\n"),
1730Sstevel@tonic-gate argv[0]);
1740Sstevel@tonic-gate md_exit(sp, 1);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate --argc;
1770Sstevel@tonic-gate ++argv;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate /* sync all devices in set */
1810Sstevel@tonic-gate if (rflag) {
1820Sstevel@tonic-gate /* get set */
1830Sstevel@tonic-gate if (argc != 0)
1840Sstevel@tonic-gate usage(sp, 1);
1850Sstevel@tonic-gate if ((sp == NULL) &&
1860Sstevel@tonic-gate ((sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) &&
1870Sstevel@tonic-gate (metaget_setdesc(sp, ep) == NULL)) {
1880Sstevel@tonic-gate mde_perror(ep, "");
1890Sstevel@tonic-gate md_exit(sp, 1);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate assert(sp != NULL);
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate * For a MN set "metasync -r" can only be called by the
1950Sstevel@tonic-gate * initiator. We must not take the set lock for a MN set as
1960Sstevel@tonic-gate * it will only generate individual metasync commands which
1970Sstevel@tonic-gate * will individually take the lock when executing the
1980Sstevel@tonic-gate * individual metasync commands.
1990Sstevel@tonic-gate * Therefore only take the set lock for non MN sets.
2000Sstevel@tonic-gate */
2010Sstevel@tonic-gate if (meta_is_mn_set(sp, ep) == 0) {
2020Sstevel@tonic-gate /* grab set lock */
2030Sstevel@tonic-gate if (meta_lock(sp, TRUE, ep)) {
2040Sstevel@tonic-gate mde_perror(ep, "");
2050Sstevel@tonic-gate md_exit(sp, 1);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate /* check for ownership */
2090Sstevel@tonic-gate if (meta_check_ownership(sp, ep) != 0) {
2100Sstevel@tonic-gate mde_perror(ep, "");
2110Sstevel@tonic-gate md_exit(sp, 1);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate /* resync all metadevices in set */
2150Sstevel@tonic-gate if (meta_resync_all(sp, size, ep) != 0) {
2160Sstevel@tonic-gate mde_perror(ep, "");
2170Sstevel@tonic-gate md_exit(sp, 1);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate md_exit(sp, 0);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate /* sync specified metadevices */
2230Sstevel@tonic-gate if (argc <= 0)
2240Sstevel@tonic-gate usage(sp, 1);
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate * Note that if sp is NULL, meta_is_mn_name() derives sp
2280Sstevel@tonic-gate * from argv[0] which is the metadevice arg
2290Sstevel@tonic-gate */
2300Sstevel@tonic-gate if (meta_is_mn_name(&sp, argv[0], ep))
2310Sstevel@tonic-gate mn_set = TRUE;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate for (; (argc > 0); --argc, ++argv) {
2340Sstevel@tonic-gate mdname_t *np;
2350Sstevel@tonic-gate int result;
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate /* get device */
2381623Stw21770 if ((np = metaname(&sp, argv[0], META_DEVICE, ep)) == NULL) {
2390Sstevel@tonic-gate mde_perror(ep, "");
2400Sstevel@tonic-gate rval = -1;
2410Sstevel@tonic-gate continue;
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate assert(sp != NULL);
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate * If we are not called through an rpc call and the
2470Sstevel@tonic-gate * set associated with the command is an MN set, send
2480Sstevel@tonic-gate * a setsync message to the master of the set and let it
2490Sstevel@tonic-gate * deal with it.
2500Sstevel@tonic-gate */
2510Sstevel@tonic-gate if (!called_thru_rpc && mn_set) {
2520Sstevel@tonic-gate if ((result = meta_mn_send_setsync(sp, np, size,
2530Sstevel@tonic-gate ep)) != 0) {
2540Sstevel@tonic-gate mde_perror(ep, "Unable to start resync");
2550Sstevel@tonic-gate md_exit(sp, result);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate continue;
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate /* grab set lock */
2610Sstevel@tonic-gate if (meta_lock(sp, TRUE, ep)) {
2620Sstevel@tonic-gate mde_perror(ep, "");
2630Sstevel@tonic-gate md_exit(sp, 1);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate /* check for ownership */
2670Sstevel@tonic-gate if (meta_check_ownership(sp, ep) != 0) {
2680Sstevel@tonic-gate mde_perror(ep, "");
2690Sstevel@tonic-gate md_exit(sp, 1); /* no point in continuing */
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate /* resync or regen (raid only) metadevice */
2730Sstevel@tonic-gate if (pflag) {
2740Sstevel@tonic-gate /* regen */
2750Sstevel@tonic-gate if (meta_raid_regen_byname(sp, np, size, ep) != 0) {
2760Sstevel@tonic-gate mde_perror(ep, "");
2770Sstevel@tonic-gate rval = -1;
2780Sstevel@tonic-gate continue;
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate } else {
2810Sstevel@tonic-gate if (meta_resync_byname(sp, np, size, ep, resync_cmd)
2820Sstevel@tonic-gate != 0) {
2830Sstevel@tonic-gate mde_perror(ep, "");
2840Sstevel@tonic-gate rval = -1;
2850Sstevel@tonic-gate continue;
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate /* return success */
2910Sstevel@tonic-gate md_exit(sp, rval);
2920Sstevel@tonic-gate /*NOTREACHED*/
2930Sstevel@tonic-gate return (rval);
2940Sstevel@tonic-gate }
295