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