2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sbin/hammer/cmd_mirror.c,v 1.16 2008/11/09 05:22:56 dillon Exp $
39 #define SERIALBUF_SIZE (512 * 1024)
41 static int read_mrecords(int fd, char *buf, u_int size,
42 hammer_ioc_mrecord_head_t pickup);
43 static hammer_ioc_mrecord_any_t read_mrecord(int fdin, int *errorp,
44 hammer_ioc_mrecord_head_t pickup);
45 static void write_mrecord(int fdout, u_int32_t type,
46 hammer_ioc_mrecord_any_t mrec, int bytes);
47 static void generate_mrec_header(int fd, int fdout, int pfs_id,
48 hammer_tid_t *tid_begp, hammer_tid_t *tid_endp);
49 static int validate_mrec_header(int fd, int fdin, int is_target, int pfs_id,
50 struct hammer_ioc_mrecord_head *pickup,
51 hammer_tid_t *tid_begp, hammer_tid_t *tid_endp);
52 static void update_pfs_snapshot(int fd, hammer_tid_t snapshot_tid, int pfs_id);
53 static ssize_t writebw(int fd, const void *buf, size_t nbytes,
54 u_int64_t *bwcount, struct timeval *tv1);
55 static int getyn(void);
56 static void mirror_usage(int code);
59 * Generate a mirroring data stream from the specific source over the
60 * entire key range, but restricted to the specified transaction range.
62 * The HAMMER VFS does most of the work, we add a few new mrecord
63 * types to negotiate the TID ranges and verify that the entire
64 * stream made it to the destination.
67 hammer_cmd_mirror_read(char **av, int ac, int streaming)
69 struct hammer_ioc_mirror_rw mirror;
70 struct hammer_ioc_pseudofs_rw pfs;
71 union hammer_ioc_mrecord_any mrec_tmp;
72 struct hammer_ioc_mrecord_head pickup;
73 hammer_ioc_mrecord_any_t mrec;
74 hammer_tid_t sync_tid;
75 const char *filesystem;
76 char *buf = malloc(SERIALBUF_SIZE);
83 time_t base_t = time(NULL);
87 if (ac == 0 || ac > 2)
95 bzero(&mirror, sizeof(mirror));
96 hammer_key_beg_init(&mirror.key_beg);
97 hammer_key_end_init(&mirror.key_end);
99 fd = getpfs(&pfs, filesystem);
101 if (streaming && VerboseOpt) {
102 fprintf(stderr, "\nRunning");
106 gettimeofday(&bwtv, NULL);
110 * Send initial header for the purpose of determining shared-uuid.
112 generate_mrec_header(fd, 1, pfs.pfs_id, NULL, NULL);
115 * In 2-way mode the target will send us a PFS info packet
116 * first. Use the target's current snapshot TID as our default
121 n = validate_mrec_header(fd, 0, 0, pfs.pfs_id, &pickup,
122 NULL, &mirror.tid_beg);
123 if (n < 0) { /* got TERM record */
131 * Write out the PFS header, tid_beg will be updated if our PFS
132 * has a larger begin sync. tid_end is set to the latest source
133 * TID whos flush cycle has completed.
135 generate_mrec_header(fd, 1, pfs.pfs_id,
136 &mirror.tid_beg, &mirror.tid_end);
138 /* XXX streaming mode support w/ cycle or command line arg */
140 * A cycle file overrides the beginning TID
142 hammer_get_cycle(&mirror.key_beg, &mirror.tid_beg);
145 mirror.tid_beg = strtoull(av[1], NULL, 0);
147 if (streaming == 0 || VerboseOpt >= 2) {
149 "Mirror-read: Mirror from %016llx to %016llx\n",
150 mirror.tid_beg, mirror.tid_end);
152 if (mirror.key_beg.obj_id != (int64_t)HAMMER_MIN_OBJID) {
153 fprintf(stderr, "Mirror-read: Resuming at object %016llx\n",
154 mirror.key_beg.obj_id);
158 * Nothing to do if begin equals end.
160 if (mirror.tid_beg >= mirror.tid_end) {
161 if (streaming == 0 || VerboseOpt >= 2)
162 fprintf(stderr, "Mirror-read: No work to do\n");
169 * Write out bulk records
172 mirror.size = SERIALBUF_SIZE;
176 mirror.pfs_id = pfs.pfs_id;
177 mirror.shared_uuid = pfs.ondisk->shared_uuid;
178 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
179 fprintf(stderr, "Mirror-read %s failed: %s\n",
180 filesystem, strerror(errno));
183 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
185 "Mirror-read %s fatal error %d\n",
186 filesystem, mirror.head.error);
191 n = writebw(1, mirror.ubuf, mirror.count,
194 n = write(1, mirror.ubuf, mirror.count);
196 if (n != mirror.count) {
197 fprintf(stderr, "Mirror-read %s failed: "
203 total_bytes += mirror.count;
204 if (streaming && VerboseOpt) {
205 fprintf(stderr, "\r%016llx %11lld",
206 mirror.key_cur.obj_id,
210 mirror.key_beg = mirror.key_cur;
212 (unsigned)(time(NULL) - base_t) > (unsigned)TimeoutOpt) {
214 "Mirror-read %s interrupted by timer at"
217 mirror.key_cur.obj_id);
221 } while (mirror.count != 0);
225 * Write out the termination sync record - only if not interrupted
227 if (interrupted == 0) {
229 write_mrecord(1, HAMMER_MREC_TYPE_SYNC,
230 &mrec_tmp, sizeof(mrec_tmp.sync));
232 write_mrecord(1, HAMMER_MREC_TYPE_IDLE,
233 &mrec_tmp, sizeof(mrec_tmp.sync));
238 * If the -2 option was given (automatic when doing mirror-copy),
239 * a two-way pipe is assumed and we expect a response mrec from
243 mrec = read_mrecord(0, &error, &pickup);
245 mrec->head.type != HAMMER_MREC_TYPE_UPDATE ||
246 mrec->head.rec_size != sizeof(mrec->update)) {
247 fprintf(stderr, "mirror_read: Did not get final "
248 "acknowledgement packet from target\n");
253 hammer_set_cycle(&mirror.key_cur, mirror.tid_beg);
254 fprintf(stderr, "Cyclefile %s updated for "
255 "continuation\n", CyclePath);
258 sync_tid = mrec->update.tid;
260 hammer_key_beg_init(&mirror.key_beg);
261 hammer_set_cycle(&mirror.key_beg, sync_tid);
262 fprintf(stderr, "Cyclefile %s updated to 0x%016llx\n",
263 CyclePath, sync_tid);
266 } else if (CyclePath) {
267 /* NOTE! mirror.tid_beg cannot be updated */
268 fprintf(stderr, "Warning: cycle file (-c option) cannot be "
269 "fully updated unless you use mirror-copy\n");
270 hammer_set_cycle(&mirror.key_beg, mirror.tid_beg);
272 if (streaming && interrupted == 0) {
273 time_t t1 = time(NULL);
277 fprintf(stderr, " W");
280 pfs.ondisk->sync_end_tid = mirror.tid_end;
281 if (ioctl(fd, HAMMERIOC_WAI_PSEUDOFS, &pfs) < 0) {
282 fprintf(stderr, "Mirror-read %s: cannot stream: %s\n",
283 filesystem, strerror(errno));
285 t2 = time(NULL) - t1;
286 if (t2 >= 0 && t2 < DelayOpt) {
288 fprintf(stderr, "\bD");
291 sleep(DelayOpt - t2);
294 fprintf(stderr, "\b ");
301 write_mrecord(1, HAMMER_MREC_TYPE_TERM,
302 &mrec_tmp, sizeof(mrec_tmp.sync));
304 fprintf(stderr, "Mirror-read %s succeeded\n", filesystem);
308 create_pfs(const char *filesystem, uuid_t *s_uuid)
310 if (ForceYesOpt == 1)
312 fprintf(stderr, "PFS slave %s does not exist. "
313 "Auto create new slave PFS!\n", filesystem);
318 fprintf(stderr, "PFS slave %s does not exist.\n"
319 "Do you want to create a new slave PFS? (yes|no) ",
323 fprintf(stderr, "Aborting operation\n");
329 char *shared_uuid = NULL;
330 uuid_to_string(s_uuid, &shared_uuid, &status);
333 asprintf(&cmd, "/sbin/hammer pfs-slave '%s' shared-uuid=%s 1>&2",
334 filesystem, shared_uuid);
338 fprintf(stderr, "Failed to alloc memory\n");
341 if (system(cmd) != 0) {
342 fprintf(stderr, "Failed to create PFS\n");
348 * Pipe the mirroring data stream on stdin to the HAMMER VFS, adding
349 * some additional packet types to negotiate TID ranges and to verify
350 * completion. The HAMMER VFS does most of the work.
352 * It is important to note that the mirror.key_{beg,end} range must
353 * match the ranged used by the original. For now both sides use
354 * range the entire key space.
356 * It is even more important that the records in the stream conform
357 * to the TID range also supplied in the stream. The HAMMER VFS will
358 * use the REC, PASS, and SKIP record types to track the portions of
359 * the B-Tree being scanned in order to be able to proactively delete
360 * records on the target within those active areas that are not mentioned
363 * The mirror.key_cur field is used by the VFS to do this tracking. It
364 * must be initialized to key_beg but then is persistently updated by
365 * the HAMMER VFS on each successive ioctl() call. If you blow up this
366 * field you will blow up the mirror target, possibly to the point of
367 * deleting everything. As a safety measure the HAMMER VFS simply marks
368 * the records that the source has destroyed as deleted on the target,
369 * and normal pruning operations will deal with their final disposition
370 * at some later time.
373 hammer_cmd_mirror_write(char **av, int ac)
375 struct hammer_ioc_mirror_rw mirror;
376 const char *filesystem;
377 char *buf = malloc(SERIALBUF_SIZE);
378 struct hammer_ioc_pseudofs_rw pfs;
379 struct hammer_ioc_mrecord_head pickup;
380 struct hammer_ioc_synctid synctid;
381 union hammer_ioc_mrecord_any mrec_tmp;
382 hammer_ioc_mrecord_any_t mrec;
392 pickup.signature = 0;
396 bzero(&mirror, sizeof(mirror));
397 hammer_key_beg_init(&mirror.key_beg);
398 hammer_key_end_init(&mirror.key_end);
399 mirror.key_end = mirror.key_beg;
402 * Read initial packet
404 mrec = read_mrecord(0, &error, &pickup);
407 fprintf(stderr, "validate_mrec_header: short read\n");
413 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
416 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
417 fprintf(stderr, "validate_mrec_header: did not get expected "
418 "PFSD record type\n");
421 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
422 fprintf(stderr, "validate_mrec_header: unexpected payload "
428 * Create slave PFS if it doesn't yet exist
430 if (lstat(filesystem, &st) != 0) {
431 create_pfs(filesystem, &mrec->pfs.pfsd.shared_uuid);
436 fd = getpfs(&pfs, filesystem);
439 * In two-way mode the target writes out a PFS packet first.
440 * The source uses our tid_end as its tid_beg by default,
441 * picking up where it left off.
445 generate_mrec_header(fd, 1, pfs.pfs_id,
446 &mirror.tid_beg, &mirror.tid_end);
450 * Read and process the PFS header. The source informs us of
451 * the TID range the stream represents.
453 n = validate_mrec_header(fd, 0, 1, pfs.pfs_id, &pickup,
454 &mirror.tid_beg, &mirror.tid_end);
455 if (n < 0) { /* got TERM record */
461 mirror.size = SERIALBUF_SIZE;
464 * Read and process bulk records (REC, PASS, and SKIP types).
466 * On your life, do NOT mess with mirror.key_cur or your mirror
467 * target may become history.
471 mirror.pfs_id = pfs.pfs_id;
472 mirror.shared_uuid = pfs.ondisk->shared_uuid;
473 mirror.size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
474 if (mirror.size <= 0)
476 if (ioctl(fd, HAMMERIOC_MIRROR_WRITE, &mirror) < 0) {
477 fprintf(stderr, "Mirror-write %s failed: %s\n",
478 filesystem, strerror(errno));
481 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
483 "Mirror-write %s fatal error %d\n",
484 filesystem, mirror.head.error);
488 if (mirror.head.flags & HAMMER_IOC_HEAD_INTR) {
490 "Mirror-write %s interrupted by timer at"
493 mirror.key_cur.obj_id);
500 * Read and process the termination sync record.
502 mrec = read_mrecord(0, &error, &pickup);
504 if (mrec && mrec->head.type == HAMMER_MREC_TYPE_TERM) {
505 fprintf(stderr, "Mirror-write: received termination request\n");
511 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
512 mrec->head.type != HAMMER_MREC_TYPE_IDLE) ||
513 mrec->head.rec_size != sizeof(mrec->sync)) {
514 fprintf(stderr, "Mirror-write %s: Did not get termination "
515 "sync record, or rec_size is wrong rt=%d\n",
516 filesystem, mrec->head.type);
521 * Update the PFS info on the target so the user has visibility
522 * into the new snapshot, and sync the target filesystem.
524 if (mrec->head.type == HAMMER_MREC_TYPE_SYNC) {
525 update_pfs_snapshot(fd, mirror.tid_end, pfs.pfs_id);
527 bzero(&synctid, sizeof(synctid));
528 synctid.op = HAMMER_SYNCTID_SYNC2;
529 ioctl(fd, HAMMERIOC_SYNCTID, &synctid);
531 if (VerboseOpt >= 2) {
532 fprintf(stderr, "Mirror-write %s: succeeded\n",
541 * Report back to the originator.
544 mrec_tmp.update.tid = mirror.tid_end;
545 write_mrecord(1, HAMMER_MREC_TYPE_UPDATE,
546 &mrec_tmp, sizeof(mrec_tmp.update));
548 printf("Source can update synctid to 0x%016llx\n",
556 hammer_cmd_mirror_dump(void)
558 char *buf = malloc(SERIALBUF_SIZE);
559 struct hammer_ioc_mrecord_head pickup;
560 hammer_ioc_mrecord_any_t mrec;
567 * Read and process the PFS header
569 pickup.signature = 0;
572 mrec = read_mrecord(0, &error, &pickup);
575 * Read and process bulk records
578 size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
582 while (offset < size) {
583 mrec = (void *)((char *)buf + offset);
584 bytes = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
585 if (offset + bytes > size) {
586 fprintf(stderr, "Misaligned record\n");
590 switch(mrec->head.type & HAMMER_MRECF_TYPE_MASK) {
591 case HAMMER_MREC_TYPE_REC_BADCRC:
592 case HAMMER_MREC_TYPE_REC:
593 printf("Record obj=%016llx key=%016llx "
595 mrec->rec.leaf.base.obj_id,
596 mrec->rec.leaf.base.key,
597 mrec->rec.leaf.base.rec_type,
598 mrec->rec.leaf.base.obj_type);
599 if (mrec->head.type ==
600 HAMMER_MREC_TYPE_REC_BADCRC) {
601 printf(" (BAD CRC)");
604 printf(" tids %016llx:%016llx data=%d\n",
605 mrec->rec.leaf.base.create_tid,
606 mrec->rec.leaf.base.delete_tid,
607 mrec->rec.leaf.data_len);
609 case HAMMER_MREC_TYPE_PASS:
610 printf("Pass obj=%016llx key=%016llx "
612 mrec->rec.leaf.base.obj_id,
613 mrec->rec.leaf.base.key,
614 mrec->rec.leaf.base.rec_type,
615 mrec->rec.leaf.base.obj_type);
616 printf(" tids %016llx:%016llx data=%d\n",
617 mrec->rec.leaf.base.create_tid,
618 mrec->rec.leaf.base.delete_tid,
619 mrec->rec.leaf.data_len);
621 case HAMMER_MREC_TYPE_SKIP:
622 printf("Skip obj=%016llx key=%016llx rt=%02x to\n"
623 " obj=%016llx key=%016llx rt=%02x\n",
624 mrec->skip.skip_beg.obj_id,
625 mrec->skip.skip_beg.key,
626 mrec->skip.skip_beg.rec_type,
627 mrec->skip.skip_end.obj_id,
628 mrec->skip.skip_end.key,
629 mrec->skip.skip_end.rec_type);
638 * Read and process the termination sync record.
640 mrec = read_mrecord(0, &error, &pickup);
642 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
643 mrec->head.type != HAMMER_MREC_TYPE_IDLE)
645 fprintf(stderr, "Mirror-dump: Did not get termination "
651 hammer_cmd_mirror_copy(char **av, int ac, int streaming)
674 if ((pid1 = fork()) == 0) {
679 if ((ptr = strchr(av[0], ':')) != NULL) {
684 xav[xac++] = "hammer";
704 snprintf(tbuf, sizeof(tbuf), "%d", TimeoutOpt);
709 xav[xac++] = "mirror-read-stream";
711 xav[xac++] = "mirror-read";
714 execv("/usr/bin/ssh", (void *)xav);
716 hammer_cmd_mirror_read(av, 1, streaming);
726 if ((pid2 = fork()) == 0) {
731 if ((ptr = strchr(av[1], ':')) != NULL) {
736 xav[xac++] = "hammer";
755 xav[xac++] = "mirror-write";
758 execv("/usr/bin/ssh", (void *)xav);
760 hammer_cmd_mirror_write(av + 1, 1);
769 while (waitpid(pid1, NULL, 0) <= 0)
771 while (waitpid(pid2, NULL, 0) <= 0)
776 * Read and return multiple mrecords
779 read_mrecords(int fd, char *buf, u_int size, hammer_ioc_mrecord_head_t pickup)
781 hammer_ioc_mrecord_any_t mrec;
789 while (size - count >= HAMMER_MREC_HEADSIZE) {
791 * Cached the record header in case we run out of buffer
795 if (pickup->signature == 0) {
796 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
797 i = read(fd, (char *)pickup + n,
798 HAMMER_MREC_HEADSIZE - n);
804 if (n != HAMMER_MREC_HEADSIZE) {
805 fprintf(stderr, "read_mrecords: short read on pipe\n");
808 if (pickup->signature != HAMMER_IOC_MIRROR_SIGNATURE) {
809 fprintf(stderr, "read_mrecords: malformed record on pipe, "
814 if (pickup->rec_size < HAMMER_MREC_HEADSIZE ||
815 pickup->rec_size > sizeof(*mrec) + HAMMER_XBUFSIZE) {
816 fprintf(stderr, "read_mrecords: malformed record on pipe, "
817 "illegal rec_size\n");
822 * Stop if we have insufficient space for the record and data.
824 bytes = HAMMER_HEAD_DOALIGN(pickup->rec_size);
825 if (size - count < bytes)
829 * Stop if the record type is not a REC, SKIP, or PASS,
830 * which are the only types the ioctl supports. Other types
831 * are used only by the userland protocol.
835 type = pickup->type & HAMMER_MRECF_TYPE_LOMASK;
836 if (type != HAMMER_MREC_TYPE_PFSD &&
837 type != HAMMER_MREC_TYPE_REC &&
838 type != HAMMER_MREC_TYPE_SKIP &&
839 type != HAMMER_MREC_TYPE_PASS) {
844 * Read the remainder and clear the pickup signature.
846 for (n = HAMMER_MREC_HEADSIZE; n < bytes; n += i) {
847 i = read(fd, buf + count + n, bytes - n);
852 fprintf(stderr, "read_mrecords: short read on pipe\n");
856 bcopy(pickup, buf + count, HAMMER_MREC_HEADSIZE);
857 pickup->signature = 0;
859 mrec = (void *)(buf + count);
862 * Validate the completed record
864 if (mrec->head.rec_crc !=
865 crc32((char *)mrec + HAMMER_MREC_CRCOFF,
866 mrec->head.rec_size - HAMMER_MREC_CRCOFF)) {
867 fprintf(stderr, "read_mrecords: malformed record "
868 "on pipe, bad crc\n");
873 * If its a B-Tree record validate the data crc.
875 * NOTE: If the VFS passes us an explicitly errorde mrec
876 * we just pass it through.
878 type = mrec->head.type & HAMMER_MRECF_TYPE_MASK;
880 if (type == HAMMER_MREC_TYPE_REC) {
881 if (mrec->head.rec_size <
882 sizeof(mrec->rec) + mrec->rec.leaf.data_len) {
884 "read_mrecords: malformed record on "
885 "pipe, illegal element data_len\n");
888 if (mrec->rec.leaf.data_len &&
889 mrec->rec.leaf.data_offset &&
890 hammer_crc_test_leaf(&mrec->rec + 1, &mrec->rec.leaf) == 0) {
892 "read_mrecords: data_crc did not "
893 "match data! obj=%016llx key=%016llx\n",
894 mrec->rec.leaf.base.obj_id,
895 mrec->rec.leaf.base.key);
897 "continuing, but there are problems\n");
906 * Read and return a single mrecord.
909 hammer_ioc_mrecord_any_t
910 read_mrecord(int fdin, int *errorp, hammer_ioc_mrecord_head_t pickup)
912 hammer_ioc_mrecord_any_t mrec;
913 struct hammer_ioc_mrecord_head mrechd;
918 if (pickup && pickup->type != 0) {
920 pickup->signature = 0;
922 n = HAMMER_MREC_HEADSIZE;
925 * Read in the PFSD header from the sender.
927 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
928 i = read(fdin, (char *)&mrechd + n, HAMMER_MREC_HEADSIZE - n);
933 *errorp = 0; /* EOF */
936 if (n != HAMMER_MREC_HEADSIZE) {
937 fprintf(stderr, "short read of mrecord header\n");
942 if (mrechd.signature != HAMMER_IOC_MIRROR_SIGNATURE) {
943 fprintf(stderr, "read_mrecord: bad signature\n");
947 bytes = HAMMER_HEAD_DOALIGN(mrechd.rec_size);
948 assert(bytes >= sizeof(mrechd));
949 mrec = malloc(bytes);
953 i = read(fdin, (char *)mrec + n, bytes - n);
959 fprintf(stderr, "read_mrecord: short read on payload\n");
963 if (mrec->head.rec_crc !=
964 crc32((char *)mrec + HAMMER_MREC_CRCOFF,
965 mrec->head.rec_size - HAMMER_MREC_CRCOFF)) {
966 fprintf(stderr, "read_mrecord: bad CRC\n");
976 write_mrecord(int fdout, u_int32_t type, hammer_ioc_mrecord_any_t mrec,
979 char zbuf[HAMMER_HEAD_ALIGN];
982 pad = HAMMER_HEAD_DOALIGN(bytes) - bytes;
984 assert(bytes >= (int)sizeof(mrec->head));
985 bzero(&mrec->head, sizeof(mrec->head));
986 mrec->head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
987 mrec->head.type = type;
988 mrec->head.rec_size = bytes;
989 mrec->head.rec_crc = crc32((char *)mrec + HAMMER_MREC_CRCOFF,
990 bytes - HAMMER_MREC_CRCOFF);
991 if (write(fdout, mrec, bytes) != bytes) {
992 fprintf(stderr, "write_mrecord: error %d (%s)\n",
993 errno, strerror(errno));
998 if (write(fdout, zbuf, pad) != pad) {
999 fprintf(stderr, "write_mrecord: error %d (%s)\n",
1000 errno, strerror(errno));
1007 * Generate a mirroring header with the pfs information of the
1008 * originating filesytem.
1011 generate_mrec_header(int fd, int fdout, int pfs_id,
1012 hammer_tid_t *tid_begp, hammer_tid_t *tid_endp)
1014 struct hammer_ioc_pseudofs_rw pfs;
1015 union hammer_ioc_mrecord_any mrec_tmp;
1017 bzero(&pfs, sizeof(pfs));
1018 bzero(&mrec_tmp, sizeof(mrec_tmp));
1019 pfs.pfs_id = pfs_id;
1020 pfs.ondisk = &mrec_tmp.pfs.pfsd;
1021 pfs.bytes = sizeof(mrec_tmp.pfs.pfsd);
1022 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1023 fprintf(stderr, "Mirror-read: not a HAMMER fs/pseudofs!\n");
1026 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1027 fprintf(stderr, "Mirror-read: HAMMER pfs version mismatch!\n");
1032 * sync_beg_tid - lowest TID on source after which a full history
1035 * sync_end_tid - highest fully synchronized TID from source.
1037 if (tid_begp && *tid_begp < mrec_tmp.pfs.pfsd.sync_beg_tid)
1038 *tid_begp = mrec_tmp.pfs.pfsd.sync_beg_tid;
1040 *tid_endp = mrec_tmp.pfs.pfsd.sync_end_tid;
1041 mrec_tmp.pfs.version = pfs.version;
1042 write_mrecord(fdout, HAMMER_MREC_TYPE_PFSD,
1043 &mrec_tmp, sizeof(mrec_tmp.pfs));
1047 * Validate the pfs information from the originating filesystem
1048 * against the target filesystem. shared_uuid must match.
1050 * return -1 if we got a TERM record
1053 validate_mrec_header(int fd, int fdin, int is_target, int pfs_id,
1054 struct hammer_ioc_mrecord_head *pickup,
1055 hammer_tid_t *tid_begp, hammer_tid_t *tid_endp)
1057 struct hammer_ioc_pseudofs_rw pfs;
1058 struct hammer_pseudofs_data pfsd;
1059 hammer_ioc_mrecord_any_t mrec;
1063 * Get the PFSD info from the target filesystem.
1065 bzero(&pfs, sizeof(pfs));
1066 bzero(&pfsd, sizeof(pfsd));
1067 pfs.pfs_id = pfs_id;
1069 pfs.bytes = sizeof(pfsd);
1070 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1071 fprintf(stderr, "mirror-write: not a HAMMER fs/pseudofs!\n");
1074 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1075 fprintf(stderr, "mirror-write: HAMMER pfs version mismatch!\n");
1079 mrec = read_mrecord(fdin, &error, pickup);
1082 fprintf(stderr, "validate_mrec_header: short read\n");
1085 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
1090 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
1091 fprintf(stderr, "validate_mrec_header: did not get expected "
1092 "PFSD record type\n");
1095 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
1096 fprintf(stderr, "validate_mrec_header: unexpected payload "
1100 if (mrec->pfs.version != pfs.version) {
1101 fprintf(stderr, "validate_mrec_header: Version mismatch\n");
1106 * Whew. Ok, is the read PFS info compatible with the target?
1108 if (bcmp(&mrec->pfs.pfsd.shared_uuid, &pfsd.shared_uuid,
1109 sizeof(pfsd.shared_uuid)) != 0) {
1111 "mirror-write: source and target have "
1112 "different shared-uuid's!\n");
1116 (pfsd.mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
1117 fprintf(stderr, "mirror-write: target must be in slave mode\n");
1121 *tid_begp = mrec->pfs.pfsd.sync_beg_tid;
1123 *tid_endp = mrec->pfs.pfsd.sync_end_tid;
1129 update_pfs_snapshot(int fd, hammer_tid_t snapshot_tid, int pfs_id)
1131 struct hammer_ioc_pseudofs_rw pfs;
1132 struct hammer_pseudofs_data pfsd;
1134 bzero(&pfs, sizeof(pfs));
1135 bzero(&pfsd, sizeof(pfsd));
1136 pfs.pfs_id = pfs_id;
1138 pfs.bytes = sizeof(pfsd);
1139 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1140 perror("update_pfs_snapshot (read)");
1143 if (pfsd.sync_end_tid != snapshot_tid) {
1144 pfsd.sync_end_tid = snapshot_tid;
1145 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) != 0) {
1146 perror("update_pfs_snapshot (rewrite)");
1149 if (VerboseOpt >= 2) {
1151 "Mirror-write: Completed, updated snapshot "
1159 * Bandwidth-limited write in chunks
1163 writebw(int fd, const void *buf, size_t nbytes,
1164 u_int64_t *bwcount, struct timeval *tv1)
1175 if (*bwcount + nbytes > BandwidthOpt)
1176 n = BandwidthOpt - *bwcount;
1180 r = write(fd, buf, n);
1184 buf = (const char *)buf + r;
1189 if (*bwcount >= BandwidthOpt) {
1190 gettimeofday(&tv2, NULL);
1191 usec = (int)(tv2.tv_sec - tv1->tv_sec) * 1000000 +
1192 (int)(tv2.tv_usec - tv1->tv_usec);
1193 if (usec >= 0 && usec < 1000000)
1194 usleep(1000000 - usec);
1195 gettimeofday(tv1, NULL);
1196 *bwcount -= BandwidthOpt;
1203 * Get a yes or no answer from the terminal. The program may be run as
1204 * part of a two-way pipe so we cannot use stdin for this operation.
1213 fp = fopen("/dev/tty", "r");
1215 fprintf(stderr, "No terminal for response\n");
1219 while (fgets(buf, sizeof(buf), fp) != NULL) {
1220 if (buf[0] == 'y' || buf[0] == 'Y') {
1224 if (buf[0] == 'n' || buf[0] == 'N') {
1228 fprintf(stderr, "Response not understood\n");
1236 mirror_usage(int code)
1239 "hammer mirror-read <filesystem> [begin-tid]\n"
1240 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
1241 "hammer mirror-write <filesystem>\n"
1242 "hammer mirror-dump\n"
1243 "hammer mirror-copy [[user@]host:]<filesystem>"
1244 " [[user@]host:]<filesystem>\n"
1245 "hammer mirror-stream [[user@]host:]<filesystem>"
1246 " [[user@]host:]<filesystem>\n"