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*11053SSurya.Prakki@Sun.COM * Common Development and Distribution License (the "License").
6*11053SSurya.Prakki@Sun.COM * 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*11053SSurya.Prakki@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <stdarg.h>
280Sstevel@tonic-gate #include <ctype.h>
290Sstevel@tonic-gate #include <sys/fcntl.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <devid.h>
320Sstevel@tonic-gate #include <ftw.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <mdiox.h>
350Sstevel@tonic-gate #include <sys/lvm/mdio.h>
360Sstevel@tonic-gate #include <meta.h>
370Sstevel@tonic-gate #include <syslog.h>
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(char * myname)440Sstevel@tonic-gate usage(char *myname)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate (void) fprintf(stderr, gettext(
47*11053SSurya.Prakki@Sun.COM "usage: %s -h\n"
48*11053SSurya.Prakki@Sun.COM " %s [-s setname] -r [-lnv]\n"
49*11053SSurya.Prakki@Sun.COM " %s [-s setname] -u cxtxdx [-lnv]\n"),
50*11053SSurya.Prakki@Sun.COM myname, myname, myname);
510Sstevel@tonic-gate }
520Sstevel@tonic-gate
5362Sjeanm int
main(int argc,char ** argv)540Sstevel@tonic-gate main(int argc, char **argv)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate char c;
570Sstevel@tonic-gate char *sname = MD_LOCAL_NAME;
580Sstevel@tonic-gate mddevopts_t options = 0;
590Sstevel@tonic-gate md_error_t status = mdnullerror;
600Sstevel@tonic-gate md_error_t *ep = &status;
610Sstevel@tonic-gate mdsetname_t *sp = NULL;
620Sstevel@tonic-gate mdsetname_t *local_sp = NULL;
630Sstevel@tonic-gate char *argname;
640Sstevel@tonic-gate int todo = 0;
650Sstevel@tonic-gate int ret = 0;
660Sstevel@tonic-gate int md_upgd_stat = 0;
670Sstevel@tonic-gate int error;
680Sstevel@tonic-gate md_set_desc *sd;
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * Get the locale set up before calling any other routines
720Sstevel@tonic-gate * with messages to ouput. Just in case we're not in a build
730Sstevel@tonic-gate * environment, make sure that TEXT_DOMAIN gets set to
740Sstevel@tonic-gate * something.
750Sstevel@tonic-gate */
760Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
770Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
800Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
810Sstevel@tonic-gate
820Sstevel@tonic-gate if ((sdssc_bind_library() == SDSSC_OKAY) &&
83*11053SSurya.Prakki@Sun.COM (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
84*11053SSurya.Prakki@Sun.COM &error) == SDSSC_PROXY_DONE))
85*11053SSurya.Prakki@Sun.COM exit(error);
860Sstevel@tonic-gate
870Sstevel@tonic-gate openlog("metadevadm", LOG_ODELAY, LOG_USER);
880Sstevel@tonic-gate
890Sstevel@tonic-gate /* initialize */
900Sstevel@tonic-gate if (md_init(argc, argv, 0, 1, ep) != 0 ||
91*11053SSurya.Prakki@Sun.COM meta_check_root(ep) != 0) {
920Sstevel@tonic-gate closelog();
930Sstevel@tonic-gate mde_perror(ep, "");
940Sstevel@tonic-gate md_exit(sp, 1);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate
970Sstevel@tonic-gate /* parse args */
980Sstevel@tonic-gate optind = 1;
990Sstevel@tonic-gate opterr = 1;
1000Sstevel@tonic-gate while ((c = getopt(argc, argv, "vlhnrs:u:")) != -1) {
1010Sstevel@tonic-gate switch (c) {
1020Sstevel@tonic-gate case 'v':
1030Sstevel@tonic-gate options |= DEV_VERBOSE;
1040Sstevel@tonic-gate break;
1050Sstevel@tonic-gate case 'n':
1060Sstevel@tonic-gate options |= DEV_NOACTION;
1070Sstevel@tonic-gate break;
1080Sstevel@tonic-gate case 'r':
1090Sstevel@tonic-gate options |= DEV_RELOAD;
1100Sstevel@tonic-gate todo = 1;
1110Sstevel@tonic-gate break;
1120Sstevel@tonic-gate case 's':
1130Sstevel@tonic-gate sname = optarg;
1140Sstevel@tonic-gate break;
1150Sstevel@tonic-gate case 'u':
1160Sstevel@tonic-gate todo = 1;
1170Sstevel@tonic-gate options |= DEV_UPDATE;
1180Sstevel@tonic-gate argname = optarg;
1190Sstevel@tonic-gate if (argname == NULL) {
1200Sstevel@tonic-gate usage("metadevadm");
1210Sstevel@tonic-gate closelog();
1220Sstevel@tonic-gate md_exit(sp, 0);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate break;
1250Sstevel@tonic-gate case 'l':
1260Sstevel@tonic-gate options |= DEV_LOG;
1270Sstevel@tonic-gate break;
1280Sstevel@tonic-gate case 'h':
1290Sstevel@tonic-gate default:
1300Sstevel@tonic-gate usage("metadevadm");
1310Sstevel@tonic-gate closelog();
1320Sstevel@tonic-gate md_exit(sp, 0);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate if ((sp = metasetname(sname, ep)) == NULL) {
1370Sstevel@tonic-gate mde_perror(ep, "");
1380Sstevel@tonic-gate closelog();
1390Sstevel@tonic-gate md_exit(sp, 1);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate if (!metaislocalset(sp)) {
1430Sstevel@tonic-gate if ((sd = metaget_setdesc(sp, ep)) == NULL) {
1440Sstevel@tonic-gate mde_perror(ep, "");
1450Sstevel@tonic-gate closelog();
1460Sstevel@tonic-gate md_exit(sp, 1);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate if (MD_MNSET_DESC(sd)) {
149*11053SSurya.Prakki@Sun.COM (void) printf("%s\n", gettext("metadevadm cannot be "
1500Sstevel@tonic-gate "run on multi-owner disksets\n"));
1510Sstevel@tonic-gate closelog();
1520Sstevel@tonic-gate md_exit(sp, 0);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate if ((options & DEV_VERBOSE) && (todo != 1)) {
1570Sstevel@tonic-gate usage("metadevadm");
1580Sstevel@tonic-gate closelog();
1590Sstevel@tonic-gate md_exit(sp, 0);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if ((options & DEV_NOACTION) && (todo != 1)) {
1630Sstevel@tonic-gate usage("metadevadm");
1640Sstevel@tonic-gate closelog();
1650Sstevel@tonic-gate md_exit(sp, 0);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if (todo == 0) {
1690Sstevel@tonic-gate usage("metadevadm");
1700Sstevel@tonic-gate closelog();
1710Sstevel@tonic-gate md_exit(sp, 0);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate if ((local_sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) {
1750Sstevel@tonic-gate mde_perror(ep, "");
1760Sstevel@tonic-gate closelog();
1770Sstevel@tonic-gate md_exit(local_sp, 1);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate /* lock the local set */
1810Sstevel@tonic-gate if (meta_lock(local_sp, TRUE, ep) != 0) {
1820Sstevel@tonic-gate mde_perror(ep, "");
1830Sstevel@tonic-gate closelog();
1840Sstevel@tonic-gate md_exit(local_sp, 1);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate /* grab set lock */
1880Sstevel@tonic-gate if (meta_lock(sp, TRUE, ep)) {
1890Sstevel@tonic-gate mde_perror(ep, "");
1900Sstevel@tonic-gate closelog();
1910Sstevel@tonic-gate md_exit(local_sp, 1);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate /* check for ownership */
1950Sstevel@tonic-gate if (meta_check_ownership(sp, ep) != 0) {
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate * If the set is not owned by this node then only update the
1980Sstevel@tonic-gate * local set's replica.
1990Sstevel@tonic-gate */
2000Sstevel@tonic-gate options |= DEV_LOCAL_SET;
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate * check for upgrade. If upgrade in progress then just exit.
2050Sstevel@tonic-gate */
2060Sstevel@tonic-gate if (metaioctl(MD_UPGRADE_STAT, &md_upgd_stat, ep, NULL) != 0) {
2070Sstevel@tonic-gate mde_perror(ep, "");
2080Sstevel@tonic-gate closelog();
2090Sstevel@tonic-gate (void) meta_unlock(sp, ep);
2100Sstevel@tonic-gate md_exit(local_sp, 1);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate if (md_upgd_stat == 0) {
2130Sstevel@tonic-gate ret = meta_fixdevid(sp, options, argname, ep);
2140Sstevel@tonic-gate if (ret == METADEVADM_ERR) {
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate * If the call failed, for a DEV_RELOAD still need to
2170Sstevel@tonic-gate * update the .conf file to provide the latest devid
2180Sstevel@tonic-gate * information so exit later.
2190Sstevel@tonic-gate */
2200Sstevel@tonic-gate if (options & DEV_UPDATE) {
2210Sstevel@tonic-gate closelog();
2220Sstevel@tonic-gate (void) meta_unlock(sp, ep);
2230Sstevel@tonic-gate md_exit(local_sp, 1);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate * Sync replica list in kernel to replica list in conf files.
2300Sstevel@tonic-gate * This will update driver name and minor number in conf file
2310Sstevel@tonic-gate * if reload was run. Will update device id in conf file if
2320Sstevel@tonic-gate * update was run.
2330Sstevel@tonic-gate */
2340Sstevel@tonic-gate meta_sync_db_locations(sp, ep);
2350Sstevel@tonic-gate closelog();
2360Sstevel@tonic-gate (void) meta_unlock(sp, ep);
2370Sstevel@tonic-gate md_exit(local_sp, ret);
23862Sjeanm return (0);
2390Sstevel@tonic-gate }
240