1fcf3ce44SJohn Forte /* 2fcf3ce44SJohn Forte * CDDL HEADER START 3fcf3ce44SJohn Forte * 4fcf3ce44SJohn Forte * The contents of this file are subject to the terms of the 5fcf3ce44SJohn Forte * Common Development and Distribution License (the "License"). 6fcf3ce44SJohn Forte * You may not use this file except in compliance with the License. 7fcf3ce44SJohn Forte * 8fcf3ce44SJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fcf3ce44SJohn Forte * or http://www.opensolaris.org/os/licensing. 10fcf3ce44SJohn Forte * See the License for the specific language governing permissions 11fcf3ce44SJohn Forte * and limitations under the License. 12fcf3ce44SJohn Forte * 13fcf3ce44SJohn Forte * When distributing Covered Code, include this CDDL HEADER in each 14fcf3ce44SJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fcf3ce44SJohn Forte * If applicable, add the following below this CDDL HEADER, with the 16fcf3ce44SJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying 17fcf3ce44SJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner] 18fcf3ce44SJohn Forte * 19fcf3ce44SJohn Forte * CDDL HEADER END 20fcf3ce44SJohn Forte */ 21fcf3ce44SJohn Forte /* 22*570de38fSSurya Prakki * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23fcf3ce44SJohn Forte * Use is subject to license terms. 24fcf3ce44SJohn Forte */ 25fcf3ce44SJohn Forte 26fcf3ce44SJohn Forte #include <sys/types.h> 27fcf3ce44SJohn Forte #include <sys/time.h> 28fcf3ce44SJohn Forte #include <errno.h> 29fcf3ce44SJohn Forte #include <signal.h> 30fcf3ce44SJohn Forte #include <stdio.h> 31fcf3ce44SJohn Forte #include <string.h> 32fcf3ce44SJohn Forte #include <fcntl.h> 33fcf3ce44SJohn Forte #include <stdlib.h> 34fcf3ce44SJohn Forte #include <unistd.h> 35fcf3ce44SJohn Forte #include <values.h> 36fcf3ce44SJohn Forte #include <locale.h> 37fcf3ce44SJohn Forte #include <sys/stat.h> 38fcf3ce44SJohn Forte #include <strings.h> 39fcf3ce44SJohn Forte #include <stdarg.h> 40fcf3ce44SJohn Forte #include <sys/param.h> 41fcf3ce44SJohn Forte #include <nsctl.h> 42fcf3ce44SJohn Forte 43fcf3ce44SJohn Forte #include <sys/nsctl/cfg.h> 44fcf3ce44SJohn Forte #include <sys/unistat/spcs_s.h> 45fcf3ce44SJohn Forte #include <sys/unistat/spcs_s_u.h> 46fcf3ce44SJohn Forte #include <sys/unistat/spcs_errors.h> 47fcf3ce44SJohn Forte #include <sys/nsctl/dsw.h> 48fcf3ce44SJohn Forte #include <sys/nsctl/dsw_dev.h> 49fcf3ce44SJohn Forte 50fcf3ce44SJohn Forte #define DSW_TEXT_DOMAIN "II" 51fcf3ce44SJohn Forte 52fcf3ce44SJohn Forte void iicpbmp_usage(); 53fcf3ce44SJohn Forte void copybmp(char *, char *); 54fcf3ce44SJohn Forte int find_bitmap_cfg(char *); 55fcf3ce44SJohn Forte 56fcf3ce44SJohn Forte extern int optind; 57fcf3ce44SJohn Forte 58fcf3ce44SJohn Forte char *cmdnam; 59fcf3ce44SJohn Forte 60fcf3ce44SJohn Forte extern char *optarg; 61fcf3ce44SJohn Forte extern int optind, opterr, optopt; 62fcf3ce44SJohn Forte int update_cfg = 1; 63fcf3ce44SJohn Forte CFGFILE *cfg; 64fcf3ce44SJohn Forte char shadow[DSW_NAMELEN]; 65fcf3ce44SJohn Forte char buf[CFG_MAX_BUF]; 66fcf3ce44SJohn Forte char key[CFG_MAX_KEY]; 67fcf3ce44SJohn Forte int setnumber; 68fcf3ce44SJohn Forte 69fcf3ce44SJohn Forte #ifdef lint 70fcf3ce44SJohn Forte int 71fcf3ce44SJohn Forte iicpbmp_lintmain(int argc, char *argv[]) 72fcf3ce44SJohn Forte #else 73fcf3ce44SJohn Forte int 74fcf3ce44SJohn Forte main(int argc, char *argv[]) 75fcf3ce44SJohn Forte #endif 76fcf3ce44SJohn Forte { 77fcf3ce44SJohn Forte cmdnam = argv[0]; 78fcf3ce44SJohn Forte 79fcf3ce44SJohn Forte if (argc > 1) { 80fcf3ce44SJohn Forte if (strcmp(argv[1], "-c") == 0) { 81fcf3ce44SJohn Forte /* don't update cfg information */ 82fcf3ce44SJohn Forte update_cfg = 0; 83fcf3ce44SJohn Forte argc--; 84fcf3ce44SJohn Forte argv++; 85fcf3ce44SJohn Forte } 86fcf3ce44SJohn Forte } 87fcf3ce44SJohn Forte 88fcf3ce44SJohn Forte if (argc == 1 || (argc%2) == 0) /* must have pairs of filenames */ 89fcf3ce44SJohn Forte iicpbmp_usage(); 90fcf3ce44SJohn Forte 91fcf3ce44SJohn Forte if (update_cfg) { 92fcf3ce44SJohn Forte if ((cfg = cfg_open(NULL)) == NULL) { 93*570de38fSSurya Prakki (void) fprintf(stderr, 94*570de38fSSurya Prakki gettext("Error opening config\n")); 95fcf3ce44SJohn Forte exit(1); 96fcf3ce44SJohn Forte } 97fcf3ce44SJohn Forte 98fcf3ce44SJohn Forte if (!cfg_lock(cfg, CFG_WRLOCK)) { 99fcf3ce44SJohn Forte spcs_log("ii", NULL, 100fcf3ce44SJohn Forte "iicpbmp CFG_WRLOCK failed, errno %d", errno); 101*570de38fSSurya Prakki (void) fprintf(stderr, 102*570de38fSSurya Prakki gettext("Error locking config\n")); 103fcf3ce44SJohn Forte exit(1); 104fcf3ce44SJohn Forte } 105fcf3ce44SJohn Forte } 106fcf3ce44SJohn Forte 107fcf3ce44SJohn Forte for (argv++; *argv != NULL; argv += 2) 108fcf3ce44SJohn Forte copybmp(argv[0], argv[1]); 109fcf3ce44SJohn Forte if (update_cfg) 110fcf3ce44SJohn Forte cfg_close(cfg); 111fcf3ce44SJohn Forte exit(0); 112fcf3ce44SJohn Forte return (0); 113fcf3ce44SJohn Forte } 114fcf3ce44SJohn Forte 115fcf3ce44SJohn Forte void 116fcf3ce44SJohn Forte iicpbmp_usage() 117fcf3ce44SJohn Forte { 118*570de38fSSurya Prakki (void) fprintf(stderr, gettext("Usage:\n")); 119*570de38fSSurya Prakki (void) fprintf(stderr, 120*570de38fSSurya Prakki gettext("\tiicpbmp [-c] old_bitmap new_bitmap\n")); 121fcf3ce44SJohn Forte exit(1); 122fcf3ce44SJohn Forte } 123fcf3ce44SJohn Forte 124fcf3ce44SJohn Forte void 125fcf3ce44SJohn Forte copybmp(char *old_bitmap, char *new_bitmap) 126fcf3ce44SJohn Forte { 127fcf3ce44SJohn Forte int i; 128fcf3ce44SJohn Forte int dsw_fd; 129fcf3ce44SJohn Forte FILE *ifp, *ofp; 130fcf3ce44SJohn Forte ii_header_t header; 131fcf3ce44SJohn Forte char cp_buffer[256]; 132fcf3ce44SJohn Forte dsw_stat_t args; 133fcf3ce44SJohn Forte 134fcf3ce44SJohn Forte dsw_fd = open(DSWDEV, O_RDONLY); 135fcf3ce44SJohn Forte if (dsw_fd < 0) { 136fcf3ce44SJohn Forte perror(DSWDEV); 137fcf3ce44SJohn Forte exit(1); 138fcf3ce44SJohn Forte } 139fcf3ce44SJohn Forte if (*old_bitmap != '/' || *new_bitmap != '/') { 140*570de38fSSurya Prakki (void) fprintf(stderr, gettext("Both old and new bitmap " 141*570de38fSSurya Prakki "file names must begin with a /.\n")); 142fcf3ce44SJohn Forte exit(1); 143fcf3ce44SJohn Forte } 144fcf3ce44SJohn Forte 145fcf3ce44SJohn Forte if (strlen(new_bitmap) > DSW_NAMELEN) { 146*570de38fSSurya Prakki (void) fprintf(stderr, 147*570de38fSSurya Prakki gettext("New bitmap name is too long.\n")); 148fcf3ce44SJohn Forte exit(1); 149fcf3ce44SJohn Forte } 150fcf3ce44SJohn Forte 151fcf3ce44SJohn Forte if (update_cfg && find_bitmap_cfg(old_bitmap) == 0) { 152fcf3ce44SJohn Forte perror(old_bitmap); 153*570de38fSSurya Prakki (void) fprintf(stderr, 154*570de38fSSurya Prakki gettext("Old bitmap not in existing cfg\n")); 155fcf3ce44SJohn Forte exit(1); 156fcf3ce44SJohn Forte } 157fcf3ce44SJohn Forte 158*570de38fSSurya Prakki (void) strncpy(args.shadow_vol, shadow, DSW_NAMELEN); 159fcf3ce44SJohn Forte args.shadow_vol[DSW_NAMELEN-1] = '\0'; 160fcf3ce44SJohn Forte 161fcf3ce44SJohn Forte args.status = spcs_s_ucreate(); 162fcf3ce44SJohn Forte if (ioctl(dsw_fd, DSWIOC_STAT, &args) != -1) { 163*570de38fSSurya Prakki (void) fprintf(stderr, gettext("Suspend the Point-in-Time Copy " 164fcf3ce44SJohn Forte "set first\n")); 165*570de38fSSurya Prakki (void) close(dsw_fd); 166fcf3ce44SJohn Forte exit(1); 167fcf3ce44SJohn Forte } 168fcf3ce44SJohn Forte 169fcf3ce44SJohn Forte if ((ifp = fopen(old_bitmap, "r")) == NULL) { 170fcf3ce44SJohn Forte perror(old_bitmap); 171*570de38fSSurya Prakki (void) fprintf(stderr, gettext("Can't open old bitmap file\n")); 172fcf3ce44SJohn Forte exit(1); 173fcf3ce44SJohn Forte } 174fcf3ce44SJohn Forte 175fcf3ce44SJohn Forte /* Check old header looks like an Point-in-Time Copy bitmap header */ 176fcf3ce44SJohn Forte 177fcf3ce44SJohn Forte if (fread(&header, sizeof (header), 1, ifp) != 1) { 178*570de38fSSurya Prakki (void) fprintf(stderr, gettext("Can't read old bitmap file\n")); 179fcf3ce44SJohn Forte exit(1); 180fcf3ce44SJohn Forte } 181fcf3ce44SJohn Forte 182fcf3ce44SJohn Forte if (header.ii_magic != DSW_CLEAN && header.ii_magic != DSW_DIRTY) { 183*570de38fSSurya Prakki (void) fprintf(stderr, gettext("%s is not a Point-in-Time Copy " 184fcf3ce44SJohn Forte "bitmap.\n"), old_bitmap); 185fcf3ce44SJohn Forte exit(1); 186fcf3ce44SJohn Forte } 187fcf3ce44SJohn Forte 188fcf3ce44SJohn Forte if (strncmp(header.bitmap_vol, old_bitmap, DSW_NAMELEN) != 0) { 189*570de38fSSurya Prakki (void) fprintf(stderr, gettext( 190fcf3ce44SJohn Forte "%s has Point-in-Time Copy bitmap magic number,\n" 191fcf3ce44SJohn Forte "but does not contain correct data.\n"), old_bitmap); 192fcf3ce44SJohn Forte exit(1); 193fcf3ce44SJohn Forte } 194fcf3ce44SJohn Forte 195fcf3ce44SJohn Forte if ((ofp = fopen(new_bitmap, "w")) == NULL) { 196fcf3ce44SJohn Forte perror(new_bitmap); 197*570de38fSSurya Prakki (void) fprintf(stderr, gettext("Can't open new bitmap file\n")); 198fcf3ce44SJohn Forte exit(1); 199fcf3ce44SJohn Forte } 200fcf3ce44SJohn Forte 201fcf3ce44SJohn Forte /* Set up new header */ 202fcf3ce44SJohn Forte 203*570de38fSSurya Prakki (void) memset(header.bitmap_vol, 0, DSW_NAMELEN); 204*570de38fSSurya Prakki (void) strncpy(header.bitmap_vol, new_bitmap, DSW_NAMELEN); 205fcf3ce44SJohn Forte 206fcf3ce44SJohn Forte if (fwrite(&header, sizeof (header), 1, ofp) != 1) { 207fcf3ce44SJohn Forte perror(new_bitmap); 208*570de38fSSurya Prakki (void) fprintf(stderr, 209*570de38fSSurya Prakki gettext("Can't write new bitmap header\n")); 210fcf3ce44SJohn Forte exit(1); 211fcf3ce44SJohn Forte } 212fcf3ce44SJohn Forte 213fcf3ce44SJohn Forte /* Copy the bitmap itself */ 214fcf3ce44SJohn Forte 215fcf3ce44SJohn Forte while ((i = fread(cp_buffer, sizeof (char), sizeof (cp_buffer), ifp)) 216fcf3ce44SJohn Forte > 0) { 217fcf3ce44SJohn Forte if (fwrite(cp_buffer, sizeof (char), i, ofp) != i) { 218fcf3ce44SJohn Forte perror(gettext("Write new bitmap failed")); 219fcf3ce44SJohn Forte break; 220fcf3ce44SJohn Forte } 221fcf3ce44SJohn Forte } 222*570de38fSSurya Prakki (void) fclose(ofp); 223*570de38fSSurya Prakki (void) fclose(ifp); 224*570de38fSSurya Prakki (void) close(dsw_fd); 225fcf3ce44SJohn Forte if (update_cfg) { 226*570de38fSSurya Prakki (void) sprintf(key, "ii.set%d.bitmap", setnumber); 227fcf3ce44SJohn Forte if (cfg_put_cstring(cfg, key, new_bitmap, strlen(new_bitmap)) 228fcf3ce44SJohn Forte < 0) { 229fcf3ce44SJohn Forte perror("cfg_put_cstring"); 230fcf3ce44SJohn Forte } 231*570de38fSSurya Prakki (void) cfg_commit(cfg); 232fcf3ce44SJohn Forte spcs_log("ii", NULL, 233fcf3ce44SJohn Forte "iicpbmp copy bit map for %s from %s to %s", 234fcf3ce44SJohn Forte shadow, old_bitmap, new_bitmap); 235fcf3ce44SJohn Forte } 236fcf3ce44SJohn Forte } 237fcf3ce44SJohn Forte 238fcf3ce44SJohn Forte /* 239fcf3ce44SJohn Forte * find_bitmap_cfg() 240fcf3ce44SJohn Forte * 241fcf3ce44SJohn Forte */ 242fcf3ce44SJohn Forte 243fcf3ce44SJohn Forte int 244fcf3ce44SJohn Forte find_bitmap_cfg(char *bitmap) 245fcf3ce44SJohn Forte { 246fcf3ce44SJohn Forte for (setnumber = 1; ; setnumber++) { 247fcf3ce44SJohn Forte bzero(buf, CFG_MAX_BUF); 248*570de38fSSurya Prakki (void) snprintf(key, sizeof (key), "ii.set%d.bitmap", 249*570de38fSSurya Prakki setnumber); 250*570de38fSSurya Prakki 251fcf3ce44SJohn Forte if (cfg_get_cstring(cfg, key, buf, DSW_NAMELEN) < 0) 252fcf3ce44SJohn Forte return (0); 253fcf3ce44SJohn Forte if (strcmp(buf, bitmap) == 0) { 254*570de38fSSurya Prakki (void) snprintf(key, sizeof (key), "ii.set%d.shadow", 255fcf3ce44SJohn Forte setnumber); 256*570de38fSSurya Prakki (void) cfg_get_cstring(cfg, key, shadow, DSW_NAMELEN); 257fcf3ce44SJohn Forte return (setnumber); 258fcf3ce44SJohn Forte } 259fcf3ce44SJohn Forte } 260fcf3ce44SJohn Forte } 261