2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/utsname.h>
2N/A#include <sys/mdb_modapi.h>
2N/A#include <stdio.h>
2N/A#include <errno.h>
2N/A#include <strings.h>
2N/A#include <stdlib.h>
2N/A#include <unistd.h>
2N/A#include <netdb.h>
2N/A#include <libintl.h>
2N/A#include <sys/stream.h>
2N/A#include <sys/socket.h>
2N/A#include <sys/stat.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A#include <ctype.h>
2N/A#include <thread.h>
2N/A#include <pthread.h>
2N/A
2N/A#include <sys/unistat/spcs_s.h>
2N/A#include <sys/unistat/spcs_s_u.h>
2N/A#include <sys/unistat/spcs_s_impl.h>
2N/A#include <sys/unistat/spcs_errors.h>
2N/A
2N/A#include <sys/nsctl/rdc_io.h>
2N/A#include <sys/nsctl/rdc_ioctl.h>
2N/A#include <sys/nsctl/rdc_prot.h>
2N/A#include <sys/nsctl/librdc.h>
2N/A#include <sys/nsctl/rdcerr.h>
2N/A#include <sys/nsctl/cfg.h>
2N/A
2N/A#include <sys/unistat/spcs_dtrinkets.h>
2N/A#include <sys/unistat/spcs_etrinkets.h>
2N/A
2N/A#include <sys/socket.h>
2N/A#include <sys/mnttab.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A#include <netinet/tcp.h>
2N/A#include <rpc/rpc_com.h>
2N/A#include <rpc/rpc.h>
2N/A
2N/A#define RDC_LOCAL_TAG "local"
2N/A
2N/A/*
2N/A * bitmap_in_use
2N/A * return 1 if in use
2N/A * return 0 if not in use
2N/A * return -1 on error
2N/A */
2N/A
2N/Aint
2N/Abitmap_in_use(int cmd, char *hostp, char *bmp)
2N/A{
2N/A int i, setnumber;
2N/A CFGFILE *cfg;
2N/A char host[CFG_MAX_BUF];
2N/A char shost[CFG_MAX_BUF];
2N/A char pri[CFG_MAX_BUF]; /* rdc primary vol */
2N/A char sec[CFG_MAX_BUF]; /* rdc secondary vol */
2N/A char sbm[CFG_MAX_BUF]; /* rdc secondary bitmap */
2N/A char bit[CFG_MAX_BUF]; /* a bitmap */
2N/A char mas[CFG_MAX_BUF]; /* II master */
2N/A char sha[CFG_MAX_BUF]; /* II shadow */
2N/A char mod[CFG_MAX_BUF]; /* II mode */
2N/A char ovr[CFG_MAX_BUF]; /* II overflow */
2N/A char buf[CFG_MAX_BUF];
2N/A char key[CFG_MAX_KEY];
2N/A int rc;
2N/A int ret = 0;
2N/A
2N/A
2N/A if ((cfg = cfg_open(NULL)) == NULL) {
2N/A rdc_set_error(NULL, RDC_DSCFG, 0, NULL);
2N/A return (-1);
2N/A }
2N/A if (!cfg_lock(cfg, CFG_RDLOCK)) {
2N/A rdc_set_error(NULL, RDC_DSCFG, 0, NULL);
2N/A cfg_close(cfg);
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * look into II config to see if this is being used elsewhere
2N/A */
2N/A /*CSTYLED*/
2N/A for (i = 0; ; i++) {
2N/A setnumber = i + 1;
2N/A snprintf(key, sizeof (key), "ii.set%d", setnumber);
2N/A if (cfg_get_cstring(cfg, key, buf, CFG_MAX_BUF) < 0)
2N/A break;
2N/A
2N/A rc = sscanf(buf, "%s %s %s %s %s", mas, sha, bit, mod, ovr);
2N/A if (rc != 5) {
2N/A rdc_set_error(NULL, RDC_OS, 0, NULL);
2N/A ret = -1;
2N/A goto done;
2N/A }
2N/A
2N/A /*
2N/A * got master shadow bitmap, now compare
2N/A */
2N/A if ((strcmp(bmp, mas) == 0) ||
2N/A (strcmp(bmp, sha) == 0) ||
2N/A (strcmp(bmp, bit) == 0) ||
2N/A (strcmp(bmp, ovr) == 0)) {
2N/A rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
2N/A "bitmap %s is in use by"
2N/A "Point-in-Time Copy", bmp);
2N/A ret = 1;
2N/A goto done;
2N/A }
2N/A }
2N/A /*
2N/A * and last but not least, make sure sndr is not using vol for anything
2N/A */
2N/A /*CSTYLED*/
2N/A for (i = 0; ; i++) {
2N/A setnumber = i + 1;
2N/A snprintf(key, sizeof (key), "sndr.set%d", setnumber);
2N/A if (cfg_get_cstring(cfg, key, buf, CFG_MAX_BUF) < 0)
2N/A break;
2N/A /*
2N/A * I think this is quicker than
2N/A * having to double dip into the config
2N/A */
2N/A (void) sscanf(buf, "%s %s %s %s %s %s", host, pri, bit,
2N/A shost, sec, sbm);
2N/A if (cmd == RDC_CMD_ENABLE) {
2N/A if (self_check(host)) {
2N/A if ((strcmp(bmp, pri) == 0) ||
2N/A (strcmp(bmp, bit) == 0)) {
2N/A rdc_set_error(NULL, RDC_INTERNAL,
2N/A RDC_NONFATAL, dgettext("librdc",
2N/A "bitmap %s is in use by %s"),
2N/A bmp, RDC_NAME_DU_JOUR);
2N/A
2N/A
2N/A ret = 1;
2N/A goto done;
2N/A }
2N/A } else {
2N/A if ((strcmp(bmp, sec) == 0) ||
2N/A (strcmp(bmp, sbm) == 0)) {
2N/A rdc_set_error(NULL, RDC_INTERNAL,
2N/A RDC_NONFATAL, dgettext("librdc",
2N/A "bitmap %s is in use by %s"),
2N/A bmp, RDC_NAME_DU_JOUR);
2N/A ret = 1;
2N/A goto done;
2N/A }
2N/A }
2N/A } else if (cmd == RDC_CMD_RECONFIG) {
2N/A
2N/A /*
2N/A * read this logic 1000 times and consider
2N/A * multi homed, one to many, many to one (marketing)
2N/A * etc, etc, before changing
2N/A */
2N/A if (self_check(hostp)) {
2N/A if (self_check(host)) {
2N/A if ((strcmp(bmp, pri) == 0) ||
2N/A (strcmp(bmp, bit) == 0)) {
2N/A rdc_set_error(NULL,
2N/A RDC_INTERNAL, RDC_NONFATAL,
2N/A dgettext("librdc", "bitmap"
2N/A " %s is in use by %s"),
2N/A bmp, RDC_NAME_DU_JOUR);
2N/A ret = 1;
2N/A goto done;
2N/A }
2N/A } else {
2N/A if ((strcmp(hostp, shost) == 0) &&
2N/A (strcmp(bmp, sec) == 0) ||
2N/A (strcmp(bmp, sbm) == 0)) {
2N/A rdc_set_error(NULL,
2N/A RDC_INTERNAL, RDC_NONFATAL,
2N/A dgettext("librdc", "bitmap"
2N/A " %s is in use by %s"),
2N/A bmp, RDC_NAME_DU_JOUR);
2N/A ret = 1;
2N/A goto done;
2N/A }
2N/A }
2N/A } else { /* self_check(hostp) failed */
2N/A if (self_check(host)) {
2N/A if ((strcmp(shost, hostp) == 0) &&
2N/A (strcmp(bmp, sec) == 0) ||
2N/A (strcmp(bmp, sbm) == 0)) {
2N/A rdc_set_error(NULL,
2N/A RDC_INTERNAL, RDC_NONFATAL,
2N/A dgettext("librdc", "bitmap"
2N/A " %s is in use by %s"),
2N/A bmp, RDC_NAME_DU_JOUR);
2N/A ret = 1;
2N/A goto done;
2N/A }
2N/A } else {
2N/A if ((strcmp(host, hostp) == 0) &&
2N/A (strcmp(bmp, pri) == 0) ||
2N/A (strcmp(bmp, bit) == 0)) {
2N/A rdc_set_error(NULL,
2N/A RDC_INTERNAL, RDC_NONFATAL,
2N/A dgettext("librdc", "bitmap"
2N/A " %s is in use by %s"),
2N/A bmp, RDC_NAME_DU_JOUR);
2N/A ret = 1;
2N/A goto done;
2N/A }
2N/A }
2N/A }
2N/A
2N/A }
2N/A
2N/A }
2N/Adone:
2N/A cfg_close(cfg);
2N/A return (ret);
2N/A
2N/A}
2N/A
2N/Aint
2N/Acheck_dgislocal(char *dgname)
2N/A{
2N/A char *othernode;
2N/A int rc;
2N/A
2N/A /*
2N/A * check where this disk service is mastered
2N/A */
2N/A
2N/A rc = cfg_dgname_islocal(dgname, &othernode);
2N/A if (rc < 0) {
2N/A rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
2N/A gettext("unable to find "
2N/A "disk service, %s: %s"), dgname, strerror(errno));
2N/A return (-1);
2N/A }
2N/A
2N/A if (rc == 0) {
2N/A rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
2N/A gettext("disk service, %s, is "
2N/A "active on node \"%s\"\nPlease re-issue "
2N/A "the command on that node"), dgname, othernode);
2N/A return (-1);
2N/A }
2N/A return (DCMD_OK);
2N/A}
2N/A
2N/Aint
2N/Actag_check(rdcconfig_t *rdc)
2N/A{
2N/A char *file_dgname;
2N/A char *bmp_dgname;
2N/A char *fromhost, *tohost;
2N/A char *fromfile, *tofile;
2N/A char *frombitmap, *tobitmap;
2N/A char *localfile;
2N/A char *ctag;
2N/A char file_buf[MAX_RDC_HOST_SIZE];
2N/A char bmp_buf[MAX_RDC_HOST_SIZE];
2N/A int is_primary;
2N/A int islocal = 0;
2N/A struct hostent *hp;
2N/A char fromname[MAXHOSTNAMELEN], toname[MAXHOSTNAMELEN];
2N/A
2N/A fromhost = rdc->phost;
2N/A fromfile = rdc->pfile;
2N/A frombitmap = rdc->pbmp;
2N/A tohost = rdc->shost;
2N/A tofile = rdc->sfile;
2N/A tobitmap = rdc->sbmp;
2N/A ctag = rdc->ctag;
2N/A
2N/A /*
2N/A * Check for the special (local) cluster tag
2N/A */
2N/A if (!cfg_iscluster())
2N/A return (0);
2N/A
2N/A if (ctag != NULL && strcmp(rdc->ctag, RDC_LOCAL_TAG) == 0) {
2N/A strcpy(rdc->ctag, "-");
2N/A islocal = TRUE;
2N/A } else {
2N/A islocal = FALSE;
2N/A }
2N/A
2N/A hp = gethost_byname(fromhost);
2N/A strncpy(fromname, hp->h_name, MAXHOSTNAMELEN);
2N/A hp = gethost_byname(tohost);
2N/A strncpy(toname, hp->h_name, MAXHOSTNAMELEN);
2N/A if (!self_check(fromname) && !self_check(toname)) {
2N/A /*
2N/A * If we could get a list of logical hosts on this cluster
2N/A * then we could print something intelligent about where
2N/A * the volume is mastered. For now, just print some babble
2N/A * about the fact that we have no idea.
2N/A */
2N/A rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
2N/A gettext("either %s:%s or %s:%s is not local"),
2N/A fromhost, fromfile, tohost, tofile);
2N/A return (-1);
2N/A }
2N/A
2N/A is_primary = self_check(fromname);
2N/A
2N/A /*
2N/A * If implicit disk group name and no ctag specified by user,
2N/A * we set the ctag to it.
2N/A * If implicit disk group name, it must match any supplied ctag.
2N/A */
2N/A if (is_primary)
2N/A localfile = fromfile;
2N/A else
2N/A localfile = tofile;
2N/A file_dgname = cfg_dgname(localfile, file_buf, sizeof (file_buf));
2N/A if (file_dgname != NULL && file_dgname[0] != '\0')
2N/A if (check_dgislocal(file_dgname) < 0) {
2N/A /* errors already set */
2N/A return (-1);
2N/A }
2N/A
2N/A if (strlen(ctag) == 0 && file_dgname && strlen(file_dgname))
2N/A strncpy(ctag, file_dgname, MAX_RDC_HOST_SIZE);
2N/A
2N/A /*
2N/A * making an exception here for users giving the "local"tag
2N/A * this overrides this error message. (rdc_islocal ! = 1)
2N/A */
2N/A if (strlen(ctag) != 0 && file_dgname && islocal != 1 &&
2N/A strlen(file_dgname) != 0 &&
2N/A strncmp(ctag, file_dgname, MAX_RDC_HOST_SIZE) != 0) {
2N/A rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
2N/A gettext("ctag \"%s\" does not "
2N/A "match disk group name \"%s\" of volume %s"), ctag,
2N/A file_dgname, localfile);
2N/A return (-1);
2N/A }
2N/A if ((file_dgname == NULL) || ((strlen(ctag) == 0) &&
2N/A (strlen(file_dgname) == 0))) {
2N/A /*
2N/A * we must have a non-volume managed disk here
2N/A * so ask for a tag and get out
2N/A */
2N/A rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
2N/A gettext("volume \"%s\" is not part"
2N/A " of a disk group,\nplease specify resource ctag\n"),
2N/A localfile);
2N/A
2N/A }
2N/A
2N/A /*
2N/A * Local bitmap must also have same ctag.
2N/A */
2N/A if (is_primary)
2N/A localfile = frombitmap;
2N/A else
2N/A localfile = tobitmap;
2N/A bmp_dgname = cfg_dgname(localfile, bmp_buf, sizeof (bmp_buf));
2N/A if (bmp_dgname != NULL && bmp_dgname[0] != '\0')
2N/A if (check_dgislocal(bmp_dgname) < 0) {
2N/A /* error already set */
2N/A return (-1);
2N/A }
2N/A
2N/A if (file_dgname && strlen(file_dgname) != 0) {
2N/A /* File is in a real disk group */
2N/A if ((bmp_dgname == NULL) || (strlen(bmp_dgname) == 0)) {
2N/A /* Bitmap is not in a real disk group */
2N/A rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
2N/A gettext("bitmap %s is not in disk group \"%s\""),
2N/A localfile, islocal < 1?file_dgname:ctag);
2N/A return (-1);
2N/A }
2N/A }
2N/A if (strlen(ctag) != 0 && bmp_dgname && islocal != 1 &&
2N/A strlen(bmp_dgname) != 0 &&
2N/A strncmp(ctag, bmp_dgname, MAX_RDC_HOST_SIZE) != 0) {
2N/A rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
2N/A gettext("ctag \"%s\" does not "
2N/A "match disk group name \"%s\" of bitmap %s"),
2N/A ctag, bmp_dgname, localfile);
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/Aint
2N/Amounted(char *device)
2N/A{
2N/A char target[NSC_MAXPATH];
2N/A struct mnttab mntref;
2N/A struct mnttab mntent;
2N/A FILE *mntfp;
2N/A int rdsk;
2N/A char *s;
2N/A int rc;
2N/A int i;
2N/A
2N/A rdsk = i = 0;
2N/A for (s = target; i < NSC_MAXPATH && (*s = *device++); i++) {
2N/A if (*s == 'r' && rdsk == 0 && strncmp(device, "dsk/", 4) == 0)
2N/A rdsk = 1;
2N/A else
2N/A s++;
2N/A }
2N/A *s = '\0';
2N/A
2N/A mntref.mnt_special = target;
2N/A mntref.mnt_mountp = NULL;
2N/A mntref.mnt_fstype = NULL;
2N/A mntref.mnt_mntopts = NULL;
2N/A mntref.mnt_time = NULL;
2N/A
2N/A mntfp = fopen(MNTTAB, "r");
2N/A
2N/A if (mntfp == NULL) {
2N/A /* Assume the worst, that it is mounted */
2N/A return (1);
2N/A }
2N/A
2N/A if ((rc = getmntany(mntfp, &mntent, &mntref)) != -1) {
2N/A /* found something before EOF */
2N/A fclose(mntfp);
2N/A return (1);
2N/A }
2N/A
2N/A fclose(mntfp);
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Acan_enable(rdcconfig_t *rdc)
2N/A{
2N/A struct stat stb;
2N/A
2N/A if ((strcmp(rdc->pfile, rdc->pbmp) == 0) ||
2N/A (strcmp(rdc->sfile, rdc->sbmp) == 0)) {
2N/A rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
2N/A dgettext("librdc", "volumes and bitmaps must not match"));
2N/A return (0);
2N/A }
2N/A if (ctag_check(rdc) < 0) {
2N/A /* rdc_error should already be set */
2N/A return (0);
2N/A }
2N/A
2N/A if (self_check(rdc->phost)) {
2N/A if (stat(rdc->pfile, &stb) != 0) {
2N/A rdc_set_error(NULL, RDC_OS, RDC_FATAL, NULL);
2N/A return (0);
2N/A }
2N/A if (!S_ISCHR(stb.st_mode)) {
2N/A rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
2N/A dgettext("librdc", "%s is not a character device"),
2N/A rdc->pfile);
2N/A return (0);
2N/A }
2N/A return (rdc->persist ?
2N/A !bitmap_in_use(RDC_CMD_ENABLE, rdc->phost, rdc->pbmp) : 1);
2N/A } else { /* on the secondary */
2N/A if (stat(rdc->sfile, &stb) != 0) {
2N/A rdc_set_error(NULL, RDC_OS, 0,
2N/A dgettext("librdc", "unable to access %s: %s"),
2N/A rdc->sfile, strerror(errno));
2N/A }
2N/A if (!S_ISCHR(stb.st_mode)) {
2N/A rdc_set_error(NULL, RDC_INTERNAL, RDC_NONFATAL,
2N/A dgettext("librdc",
2N/A "%s is not a character device"), rdc->sfile);
2N/A }
2N/A return (rdc->persist ?
2N/A !bitmap_in_use(RDC_CMD_ENABLE, rdc->shost, rdc->sbmp) : 1);
2N/A }
2N/A}
2N/A
2N/Aint
2N/Acan_reconfig_pbmp(rdcconfig_t *rdc, char *bmp)
2N/A{
2N/A if (!rdc->persist)
2N/A return (0);
2N/A
2N/A return (!bitmap_in_use(RDC_CMD_RECONFIG, rdc->phost, bmp));
2N/A}
2N/A
2N/Aint
2N/Acan_reconfig_sbmp(rdcconfig_t *rdc, char *bmp)
2N/A{
2N/A if (!rdc->persist)
2N/A return (0);
2N/A
2N/A return (!bitmap_in_use(RDC_CMD_RECONFIG, rdc->shost, bmp));
2N/A}
2N/A
2N/Ardc_rc_t *
2N/Acant_rsync(rdcconfig_t *rdc)
2N/A{
2N/A rdc_rc_t *rc;
2N/A
2N/A if (mounted(rdc->pfile)) {
2N/A rc = new_rc();
2N/A if (rc == NULL)
2N/A return (NULL);
2N/A strncpy(rc->set.phost, rdc->phost, MAX_RDC_HOST_SIZE);
2N/A strncpy(rc->set.pfile, rdc->pfile, NSC_MAXPATH);
2N/A strncpy(rc->set.pbmp, rdc->pbmp, NSC_MAXPATH);
2N/A strncpy(rc->set.shost, rdc->shost, MAX_RDC_HOST_SIZE);
2N/A strncpy(rc->set.sfile, rdc->sfile, NSC_MAXPATH);
2N/A strncpy(rc->set.sbmp, rdc->sbmp, NSC_MAXPATH);
2N/A
2N/A rc->rc = -1;
2N/A
2N/A rdc_set_error(NULL, RDC_INTERNAL, 0, "unable to sync %s volume"
2N/A " is currently mounted", rdc->pfile);
2N/A strncpy(rc->msg, rdc_error(NULL), RDC_ERR_SIZE);
2N/A
2N/A return (rc);
2N/A }
2N/A return (NULL);
2N/A}