4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
40 #include <sys/param.h>
43 #include <sys/nsctl/cfg.h>
44 #include <sys/unistat/spcs_s.h>
45 #include <sys/unistat/spcs_s_u.h>
46 #include <sys/unistat/spcs_errors.h>
47 #include <sys/nsctl/dsw.h>
48 #include <sys/nsctl/dsw_dev.h>
50 #define DSW_TEXT_DOMAIN "II"
53 void copybmp(char *, char *);
54 int find_bitmap_cfg(char *);
61 extern int optind
, opterr
, optopt
;
64 char shadow
[DSW_NAMELEN
];
65 char buf
[CFG_MAX_BUF
];
66 char key
[CFG_MAX_KEY
];
71 iicpbmp_lintmain(int argc
, char *argv
[])
74 main(int argc
, char *argv
[])
80 if (strcmp(argv
[1], "-c") == 0) {
81 /* don't update cfg information */
88 if (argc
== 1 || (argc
%2) == 0) /* must have pairs of filenames */
92 if ((cfg
= cfg_open(NULL
)) == NULL
) {
93 (void) fprintf(stderr
,
94 gettext("Error opening config\n"));
98 if (!cfg_lock(cfg
, CFG_WRLOCK
)) {
100 "iicpbmp CFG_WRLOCK failed, errno %d", errno
);
101 (void) fprintf(stderr
,
102 gettext("Error locking config\n"));
107 for (argv
++; *argv
!= NULL
; argv
+= 2)
108 copybmp(argv
[0], argv
[1]);
118 (void) fprintf(stderr
, gettext("Usage:\n"));
119 (void) fprintf(stderr
,
120 gettext("\tiicpbmp [-c] old_bitmap new_bitmap\n"));
125 copybmp(char *old_bitmap
, char *new_bitmap
)
134 dsw_fd
= open(DSWDEV
, O_RDONLY
);
139 if (*old_bitmap
!= '/' || *new_bitmap
!= '/') {
140 (void) fprintf(stderr
, gettext("Both old and new bitmap "
141 "file names must begin with a /.\n"));
145 if (strlen(new_bitmap
) > DSW_NAMELEN
) {
146 (void) fprintf(stderr
,
147 gettext("New bitmap name is too long.\n"));
151 if (update_cfg
&& find_bitmap_cfg(old_bitmap
) == 0) {
153 (void) fprintf(stderr
,
154 gettext("Old bitmap not in existing cfg\n"));
158 (void) strncpy(args
.shadow_vol
, shadow
, DSW_NAMELEN
);
159 args
.shadow_vol
[DSW_NAMELEN
-1] = '\0';
161 args
.status
= spcs_s_ucreate();
162 if (ioctl(dsw_fd
, DSWIOC_STAT
, &args
) != -1) {
163 (void) fprintf(stderr
, gettext("Suspend the Point-in-Time Copy "
165 (void) close(dsw_fd
);
169 if ((ifp
= fopen(old_bitmap
, "r")) == NULL
) {
171 (void) fprintf(stderr
, gettext("Can't open old bitmap file\n"));
175 /* Check old header looks like an Point-in-Time Copy bitmap header */
177 if (fread(&header
, sizeof (header
), 1, ifp
) != 1) {
178 (void) fprintf(stderr
, gettext("Can't read old bitmap file\n"));
182 if (header
.ii_magic
!= DSW_CLEAN
&& header
.ii_magic
!= DSW_DIRTY
) {
183 (void) fprintf(stderr
, gettext("%s is not a Point-in-Time Copy "
184 "bitmap.\n"), old_bitmap
);
188 if (strncmp(header
.bitmap_vol
, old_bitmap
, DSW_NAMELEN
) != 0) {
189 (void) fprintf(stderr
, gettext(
190 "%s has Point-in-Time Copy bitmap magic number,\n"
191 "but does not contain correct data.\n"), old_bitmap
);
195 if ((ofp
= fopen(new_bitmap
, "w")) == NULL
) {
197 (void) fprintf(stderr
, gettext("Can't open new bitmap file\n"));
201 /* Set up new header */
203 (void) memset(header
.bitmap_vol
, 0, DSW_NAMELEN
);
204 (void) strncpy(header
.bitmap_vol
, new_bitmap
, DSW_NAMELEN
);
206 if (fwrite(&header
, sizeof (header
), 1, ofp
) != 1) {
208 (void) fprintf(stderr
,
209 gettext("Can't write new bitmap header\n"));
213 /* Copy the bitmap itself */
215 while ((i
= fread(cp_buffer
, sizeof (char), sizeof (cp_buffer
), ifp
))
217 if (fwrite(cp_buffer
, sizeof (char), i
, ofp
) != i
) {
218 perror(gettext("Write new bitmap failed"));
224 (void) close(dsw_fd
);
226 (void) sprintf(key
, "ii.set%d.bitmap", setnumber
);
227 if (cfg_put_cstring(cfg
, key
, new_bitmap
, strlen(new_bitmap
))
229 perror("cfg_put_cstring");
231 (void) cfg_commit(cfg
);
233 "iicpbmp copy bit map for %s from %s to %s",
234 shadow
, old_bitmap
, new_bitmap
);
244 find_bitmap_cfg(char *bitmap
)
246 for (setnumber
= 1; ; setnumber
++) {
247 bzero(buf
, CFG_MAX_BUF
);
248 (void) snprintf(key
, sizeof (key
), "ii.set%d.bitmap",
251 if (cfg_get_cstring(cfg
, key
, buf
, DSW_NAMELEN
) < 0)
253 if (strcmp(buf
, bitmap
) == 0) {
254 (void) snprintf(key
, sizeof (key
), "ii.set%d.shadow",
256 (void) cfg_get_cstring(cfg
, key
, shadow
, DSW_NAMELEN
);