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
37 #define SERIALBUF_SIZE (512 * 1024)
39 typedef struct histogram {
44 static int read_mrecords(int fd, char *buf, u_int size,
45 hammer_ioc_mrecord_head_t pickup);
46 static int generate_histogram(int fd, const char *filesystem,
47 histogram_t *histogram_ary,
48 struct hammer_ioc_mirror_rw *mirror_base,
50 static hammer_ioc_mrecord_any_t read_mrecord(int fdin, int *errorp,
51 hammer_ioc_mrecord_head_t pickup);
52 static void write_mrecord(int fdout, u_int32_t type,
53 hammer_ioc_mrecord_any_t mrec, int bytes);
54 static void generate_mrec_header(int fd, int pfs_id,
55 union hammer_ioc_mrecord_any *mrec_tmp);
56 static int validate_mrec_header(int fd, int fdin, int is_target, int pfs_id,
57 struct hammer_ioc_mrecord_head *pickup,
58 hammer_tid_t *tid_begp, hammer_tid_t *tid_endp);
59 static void update_pfs_snapshot(int fd, hammer_tid_t snapshot_tid, int pfs_id);
60 static ssize_t writebw(int fd, const void *buf, size_t nbytes,
61 u_int64_t *bwcount, struct timeval *tv1);
62 static int getyn(void);
63 static void mirror_usage(int code);
66 * Generate a mirroring data stream from the specific source over the
67 * entire key range, but restricted to the specified transaction range.
69 * The HAMMER VFS does most of the work, we add a few new mrecord
70 * types to negotiate the TID ranges and verify that the entire
71 * stream made it to the destination.
73 * streaming will be 0 for mirror-read, 1 for mirror-stream. The code will
74 * set up a fake value of -1 when running the histogram for mirror-read.
77 hammer_cmd_mirror_read(char **av, int ac, int streaming)
79 struct hammer_ioc_mirror_rw mirror;
80 struct hammer_ioc_pseudofs_rw pfs;
81 union hammer_ioc_mrecord_any mrec_tmp;
82 struct hammer_ioc_mrecord_head pickup;
83 hammer_ioc_mrecord_any_t mrec;
84 hammer_tid_t sync_tid;
85 histogram_t histogram_ary;
86 const char *filesystem;
87 char *buf = malloc(SERIALBUF_SIZE);
99 time_t base_t = time(NULL);
104 if (ac == 0 || ac > 2)
108 pickup.signature = 0;
113 histogram_ary = NULL;
117 bzero(&mirror, sizeof(mirror));
118 hammer_key_beg_init(&mirror.key_beg);
119 hammer_key_end_init(&mirror.key_end);
121 fd = getpfs(&pfs, filesystem);
123 if (streaming >= 0 && VerboseOpt && VerboseOpt < 2) {
124 fprintf(stderr, "%cRunning \b\b", (sameline ? '\r' : '\n'));
130 gettimeofday(&bwtv, NULL);
134 * Send initial header for the purpose of determining the
137 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
138 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
139 &mrec_tmp, sizeof(mrec_tmp.pfs));
142 * In 2-way mode the target will send us a PFS info packet
143 * first. Use the target's current snapshot TID as our default
148 n = validate_mrec_header(fd, 0, 0, pfs.pfs_id, &pickup,
149 NULL, &mirror.tid_beg);
150 if (n < 0) { /* got TERM record */
155 } else if (streaming && histogram) {
156 mirror.tid_beg = histogram_ary[histindex].tid + 1;
162 * Write out the PFS header, tid_beg will be updated if our PFS
163 * has a larger begin sync. tid_end is set to the latest source
164 * TID whos flush cycle has completed.
166 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
167 if (mirror.tid_beg < mrec_tmp.pfs.pfsd.sync_beg_tid)
168 mirror.tid_beg = mrec_tmp.pfs.pfsd.sync_beg_tid;
169 mirror.tid_end = mrec_tmp.pfs.pfsd.sync_end_tid;
171 mirror.size = SERIALBUF_SIZE;
172 mirror.pfs_id = pfs.pfs_id;
173 mirror.shared_uuid = pfs.ondisk->shared_uuid;
176 * XXX If the histogram is exhausted and the TID delta is large
177 * the stream might have been offline for a while and is
178 * now picking it up again. Do another histogram.
181 if (streaming && histogram && histindex == histend) {
182 if (mirror.tid_end - mirror.tid_beg > BULK_MINIMUM)
188 * Initial bulk startup control, try to do some incremental
189 * mirroring in order to allow the stream to be killed and
190 * restarted without having to start over.
192 if (histogram == 0 && BulkOpt == 0) {
193 if (VerboseOpt && repeat == 0) {
194 fprintf(stderr, "\n");
197 histmax = generate_histogram(fd, filesystem,
198 &histogram_ary, &mirror,
204 * Just stream the histogram, then stop
210 if (streaming && histogram) {
212 mirror.tid_end = histogram_ary[histindex].tid;
213 estbytes = histogram_ary[histindex-1].bytes;
214 mrec_tmp.pfs.pfsd.sync_end_tid = mirror.tid_end;
219 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
220 &mrec_tmp, sizeof(mrec_tmp.pfs));
223 * A cycle file overrides the beginning TID only if we are
224 * not operating in two-way or histogram mode.
226 if (TwoWayPipeOpt == 0 && histogram == 0) {
227 hammer_get_cycle(&mirror.key_beg, &mirror.tid_beg);
231 * An additional argument overrides the beginning TID regardless
232 * of what mode we are in. This is not recommending if operating
236 mirror.tid_beg = strtoull(av[1], NULL, 0);
238 if (streaming == 0 || VerboseOpt >= 2) {
240 "Mirror-read: Mirror %016jx to %016jx",
241 (uintmax_t)mirror.tid_beg, (uintmax_t)mirror.tid_end);
243 fprintf(stderr, " (bulk= %ju)", (uintmax_t)estbytes);
244 fprintf(stderr, "\n");
247 if (mirror.key_beg.obj_id != (int64_t)HAMMER_MIN_OBJID) {
248 fprintf(stderr, "Mirror-read: Resuming at object %016jx\n",
249 (uintmax_t)mirror.key_beg.obj_id);
253 * Nothing to do if begin equals end.
255 if (mirror.tid_beg >= mirror.tid_end) {
256 if (streaming == 0 || VerboseOpt >= 2)
257 fprintf(stderr, "Mirror-read: No work to do\n");
266 * Write out bulk records
269 mirror.size = SERIALBUF_SIZE;
273 mirror.pfs_id = pfs.pfs_id;
274 mirror.shared_uuid = pfs.ondisk->shared_uuid;
275 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
276 fprintf(stderr, "Mirror-read %s failed: %s\n",
277 filesystem, strerror(errno));
280 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
282 "Mirror-read %s fatal error %d\n",
283 filesystem, mirror.head.error);
288 n = writebw(1, mirror.ubuf, mirror.count,
291 n = write(1, mirror.ubuf, mirror.count);
293 if (n != mirror.count) {
294 fprintf(stderr, "Mirror-read %s failed: "
300 total_bytes += mirror.count;
301 if (streaming && VerboseOpt) {
303 "\rscan obj=%016jx tids=%016jx:%016jx %11jd",
304 (uintmax_t)mirror.key_cur.obj_id,
305 (uintmax_t)mirror.tid_beg,
306 (uintmax_t)mirror.tid_end,
307 (intmax_t)total_bytes);
311 mirror.key_beg = mirror.key_cur;
314 * Deal with time limit option
317 (unsigned)(time(NULL) - base_t) > (unsigned)TimeoutOpt) {
319 "Mirror-read %s interrupted by timer at"
322 (uintmax_t)mirror.key_cur.obj_id);
326 } while (mirror.count != 0);
329 if (streaming && VerboseOpt && sameline == 0) {
330 fprintf(stderr, "\n");
336 * Write out the termination sync record - only if not interrupted
338 if (interrupted == 0) {
340 write_mrecord(1, HAMMER_MREC_TYPE_SYNC,
341 &mrec_tmp, sizeof(mrec_tmp.sync));
343 write_mrecord(1, HAMMER_MREC_TYPE_IDLE,
344 &mrec_tmp, sizeof(mrec_tmp.sync));
349 * If the -2 option was given (automatic when doing mirror-copy),
350 * a two-way pipe is assumed and we expect a response mrec from
354 mrec = read_mrecord(0, &error, &pickup);
356 mrec->head.type != HAMMER_MREC_TYPE_UPDATE ||
357 mrec->head.rec_size != sizeof(mrec->update)) {
358 fprintf(stderr, "mirror_read: Did not get final "
359 "acknowledgement packet from target\n");
364 hammer_set_cycle(&mirror.key_cur,
366 fprintf(stderr, "Cyclefile %s updated for "
367 "continuation\n", CyclePath);
370 sync_tid = mrec->update.tid;
372 hammer_key_beg_init(&mirror.key_beg);
373 hammer_set_cycle(&mirror.key_beg, sync_tid);
375 "Cyclefile %s updated to 0x%016jx\n",
376 CyclePath, (uintmax_t)sync_tid);
379 } else if (CyclePath) {
380 /* NOTE! mirror.tid_beg cannot be updated */
381 fprintf(stderr, "Warning: cycle file (-c option) cannot be "
382 "fully updated unless you use mirror-copy\n");
383 hammer_set_cycle(&mirror.key_beg, mirror.tid_beg);
385 if (streaming && interrupted == 0) {
386 time_t t1 = time(NULL);
390 * Try to break down large bulk transfers into smaller ones
391 * so it can sync the transaction id on the slave. This
392 * way if we get interrupted a restart doesn't have to
393 * start from scratch.
395 if (streaming && histogram) {
396 if (histindex != histmax) {
397 if (VerboseOpt && VerboseOpt < 2 &&
399 fprintf(stderr, " (bulk incremental)");
406 if (VerboseOpt && streaming >= 0) {
407 fprintf(stderr, " W");
410 pfs.ondisk->sync_end_tid = mirror.tid_end;
413 * Fake streaming mode when using a histogram to
414 * break up a mirror-read, do not wait on source.
417 } else if (ioctl(fd, HAMMERIOC_WAI_PSEUDOFS, &pfs) < 0) {
418 fprintf(stderr, "Mirror-read %s: cannot stream: %s\n",
419 filesystem, strerror(errno));
421 t2 = time(NULL) - t1;
422 if (t2 >= 0 && t2 < DelayOpt) {
424 fprintf(stderr, "\bD");
427 sleep(DelayOpt - t2);
430 fprintf(stderr, "\b ");
437 write_mrecord(1, HAMMER_MREC_TYPE_TERM,
438 &mrec_tmp, sizeof(mrec_tmp.sync));
440 fprintf(stderr, "Mirror-read %s succeeded\n", filesystem);
444 * What we are trying to do here is figure out how much data is
445 * going to be sent for the TID range and to break the TID range
446 * down into reasonably-sized slices (from the point of view of
447 * data sent) so a lost connection can restart at a reasonable
448 * place and not all the way back at the beginning.
450 * An entry's TID serves as the end_tid for the prior entry
451 * So we have to offset the calculation by 1 so that TID falls into
452 * the previous entry when populating entries.
454 * Because the transaction id space is bursty we need a relatively
455 * large number of buckets (like a million) to do a reasonable job
456 * for things like an initial bulk mirrors on a very large filesystem.
458 #define HIST_COUNT (1024 * 1024)
461 generate_histogram(int fd, const char *filesystem,
462 histogram_t *histogram_ary,
463 struct hammer_ioc_mirror_rw *mirror_base,
466 struct hammer_ioc_mirror_rw mirror;
467 union hammer_ioc_mrecord_any *mrec;
468 hammer_tid_t tid_beg;
469 hammer_tid_t tid_end;
472 u_int64_t *tid_bytes;
480 mirror = *mirror_base;
481 tid_beg = mirror.tid_beg;
482 tid_end = mirror.tid_end;
483 mirror.head.flags |= HAMMER_IOC_MIRROR_NODATA;
485 if (*histogram_ary == NULL) {
486 *histogram_ary = malloc(sizeof(struct histogram) *
489 if (tid_beg >= tid_end)
493 tid_bytes = malloc(sizeof(*tid_bytes) * (HIST_COUNT + 2));
494 bzero(tid_bytes, sizeof(*tid_bytes) * (HIST_COUNT + 2));
497 fprintf(stderr, "Prescan to break up bulk transfer");
499 fprintf(stderr, " (%juMB chunks)",
500 (uintmax_t)(SplitupOpt / (1024 * 1024)));
501 fprintf(stderr, "\n");
505 * Note: (tid_beg,tid_end), range is inclusive of both beg & end.
507 * Note: Estimates can be off when the mirror is way behind due
514 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
515 fprintf(stderr, "Mirror-read %s failed: %s\n",
516 filesystem, strerror(errno));
519 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
521 "Mirror-read %s fatal error %d\n",
522 filesystem, mirror.head.error);
527 off += HAMMER_HEAD_DOALIGN(mrec->head.rec_size)
529 mrec = (void *)((char *)mirror.ubuf + off);
532 * We only care about general RECs and PASS
533 * records. We ignore SKIPs.
535 switch (mrec->head.type & HAMMER_MRECF_TYPE_LOMASK) {
536 case HAMMER_MREC_TYPE_REC:
537 case HAMMER_MREC_TYPE_PASS:
544 * Calculate for two indices, create_tid and
545 * delete_tid. Record data only applies to
548 * When tid is exactly on the boundary it really
549 * belongs to the previous entry because scans
550 * are inclusive of the ending entry.
552 tid = mrec->rec.leaf.base.delete_tid;
553 if (tid && tid >= tid_beg && tid <= tid_end) {
554 len = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
555 if (mrec->head.type ==
556 HAMMER_MREC_TYPE_REC) {
557 len -= HAMMER_HEAD_DOALIGN(
558 mrec->rec.leaf.data_len);
561 i = (tid - tid_beg) * HIST_COUNT /
563 tidx = tid_beg + i * (tid_end - tid_beg) /
565 if (tid == tidx && i)
567 assert(i >= 0 && i < HIST_COUNT);
573 tid = mrec->rec.leaf.base.create_tid;
574 if (tid && tid >= tid_beg && tid <= tid_end) {
575 len = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
576 if (mrec->head.type ==
577 HAMMER_MREC_TYPE_REC_NODATA) {
578 len += HAMMER_HEAD_DOALIGN(
579 mrec->rec.leaf.data_len);
581 i = (tid - tid_beg) * HIST_COUNT /
583 tidx = tid_beg + i * (tid_end - tid_beg) /
585 if (tid == tidx && i)
587 assert(i >= 0 && i < HIST_COUNT);
593 if (VerboseOpt > 1) {
594 if (*repeatp == 0 && accum > SplitupOpt) {
595 fprintf(stderr, ".");
600 if (mirror.count == 0)
602 mirror.key_beg = mirror.key_cur;
606 * Reduce to SplitupOpt (default 4GB) chunks. This code may
607 * use up to two additional elements. Do the array in-place.
609 * Inefficient degenerate cases can occur if we do not accumulate
610 * at least the requested split amount, so error on the side of
614 (*histogram_ary)[res].tid = tid_beg;
615 (*histogram_ary)[res].bytes = tid_bytes[0];
616 for (i = 1; i < HIST_COUNT; ++i) {
617 if ((*histogram_ary)[res].bytes >= SplitupOpt) {
619 (*histogram_ary)[res].tid = tid_beg +
620 i * (tid_end - tid_beg) /
622 (*histogram_ary)[res].bytes = 0;
625 (*histogram_ary)[res].bytes += tid_bytes[i];
628 (*histogram_ary)[res].tid = tid_end;
629 (*histogram_ary)[res].bytes = -1;
633 fprintf(stderr, "\n"); /* newline after ... */
634 fprintf(stderr, "Prescan %d chunks, total %ju MBytes (",
635 res, (uintmax_t)total / (1024 * 1024));
636 for (i = 0; i < res && i < 3; ++i) {
638 fprintf(stderr, ", ");
639 fprintf(stderr, "%ju",
640 (uintmax_t)(*histogram_ary)[i].bytes);
643 fprintf(stderr, ", ...");
644 fprintf(stderr, ")\n");
646 assert(res <= HIST_COUNT);
654 create_pfs(const char *filesystem, uuid_t *s_uuid)
656 if (ForceYesOpt == 1) {
657 fprintf(stderr, "PFS slave %s does not exist. "
658 "Auto create new slave PFS!\n", filesystem);
661 fprintf(stderr, "PFS slave %s does not exist.\n"
662 "Do you want to create a new slave PFS? (yes|no) ",
666 fprintf(stderr, "Aborting operation\n");
672 char *shared_uuid = NULL;
673 uuid_to_string(s_uuid, &shared_uuid, &status);
676 asprintf(&cmd, "/sbin/hammer pfs-slave '%s' shared-uuid=%s 1>&2",
677 filesystem, shared_uuid);
681 fprintf(stderr, "Failed to alloc memory\n");
684 if (system(cmd) != 0) {
685 fprintf(stderr, "Failed to create PFS\n");
691 * Pipe the mirroring data stream on stdin to the HAMMER VFS, adding
692 * some additional packet types to negotiate TID ranges and to verify
693 * completion. The HAMMER VFS does most of the work.
695 * It is important to note that the mirror.key_{beg,end} range must
696 * match the ranged used by the original. For now both sides use
697 * range the entire key space.
699 * It is even more important that the records in the stream conform
700 * to the TID range also supplied in the stream. The HAMMER VFS will
701 * use the REC, PASS, and SKIP record types to track the portions of
702 * the B-Tree being scanned in order to be able to proactively delete
703 * records on the target within those active areas that are not mentioned
706 * The mirror.key_cur field is used by the VFS to do this tracking. It
707 * must be initialized to key_beg but then is persistently updated by
708 * the HAMMER VFS on each successive ioctl() call. If you blow up this
709 * field you will blow up the mirror target, possibly to the point of
710 * deleting everything. As a safety measure the HAMMER VFS simply marks
711 * the records that the source has destroyed as deleted on the target,
712 * and normal pruning operations will deal with their final disposition
713 * at some later time.
716 hammer_cmd_mirror_write(char **av, int ac)
718 struct hammer_ioc_mirror_rw mirror;
719 const char *filesystem;
720 char *buf = malloc(SERIALBUF_SIZE);
721 struct hammer_ioc_pseudofs_rw pfs;
722 struct hammer_ioc_mrecord_head pickup;
723 struct hammer_ioc_synctid synctid;
724 union hammer_ioc_mrecord_any mrec_tmp;
725 hammer_ioc_mrecord_any_t mrec;
735 pickup.signature = 0;
739 bzero(&mirror, sizeof(mirror));
740 hammer_key_beg_init(&mirror.key_beg);
741 hammer_key_end_init(&mirror.key_end);
742 mirror.key_end = mirror.key_beg;
745 * Read initial packet
747 mrec = read_mrecord(0, &error, &pickup);
750 fprintf(stderr, "validate_mrec_header: short read\n");
756 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
759 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
760 fprintf(stderr, "validate_mrec_header: did not get expected "
761 "PFSD record type\n");
764 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
765 fprintf(stderr, "validate_mrec_header: unexpected payload "
771 * Create slave PFS if it doesn't yet exist
773 if (lstat(filesystem, &st) != 0) {
774 create_pfs(filesystem, &mrec->pfs.pfsd.shared_uuid);
779 fd = getpfs(&pfs, filesystem);
782 * In two-way mode the target writes out a PFS packet first.
783 * The source uses our tid_end as its tid_beg by default,
784 * picking up where it left off.
788 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
789 if (mirror.tid_beg < mrec_tmp.pfs.pfsd.sync_beg_tid)
790 mirror.tid_beg = mrec_tmp.pfs.pfsd.sync_beg_tid;
791 mirror.tid_end = mrec_tmp.pfs.pfsd.sync_end_tid;
792 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
793 &mrec_tmp, sizeof(mrec_tmp.pfs));
797 * Read and process the PFS header. The source informs us of
798 * the TID range the stream represents.
800 n = validate_mrec_header(fd, 0, 1, pfs.pfs_id, &pickup,
801 &mirror.tid_beg, &mirror.tid_end);
802 if (n < 0) { /* got TERM record */
808 mirror.size = SERIALBUF_SIZE;
811 * Read and process bulk records (REC, PASS, and SKIP types).
813 * On your life, do NOT mess with mirror.key_cur or your mirror
814 * target may become history.
818 mirror.pfs_id = pfs.pfs_id;
819 mirror.shared_uuid = pfs.ondisk->shared_uuid;
820 mirror.size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
821 if (mirror.size <= 0)
823 if (ioctl(fd, HAMMERIOC_MIRROR_WRITE, &mirror) < 0) {
824 fprintf(stderr, "Mirror-write %s failed: %s\n",
825 filesystem, strerror(errno));
828 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
830 "Mirror-write %s fatal error %d\n",
831 filesystem, mirror.head.error);
835 if (mirror.head.flags & HAMMER_IOC_HEAD_INTR) {
837 "Mirror-write %s interrupted by timer at"
840 mirror.key_cur.obj_id);
847 * Read and process the termination sync record.
849 mrec = read_mrecord(0, &error, &pickup);
851 if (mrec && mrec->head.type == HAMMER_MREC_TYPE_TERM) {
852 fprintf(stderr, "Mirror-write: received termination request\n");
858 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
859 mrec->head.type != HAMMER_MREC_TYPE_IDLE) ||
860 mrec->head.rec_size != sizeof(mrec->sync)) {
861 fprintf(stderr, "Mirror-write %s: Did not get termination "
862 "sync record, or rec_size is wrong rt=%d\n",
863 filesystem, mrec->head.type);
868 * Update the PFS info on the target so the user has visibility
869 * into the new snapshot, and sync the target filesystem.
871 if (mrec->head.type == HAMMER_MREC_TYPE_SYNC) {
872 update_pfs_snapshot(fd, mirror.tid_end, pfs.pfs_id);
874 bzero(&synctid, sizeof(synctid));
875 synctid.op = HAMMER_SYNCTID_SYNC2;
876 ioctl(fd, HAMMERIOC_SYNCTID, &synctid);
878 if (VerboseOpt >= 2) {
879 fprintf(stderr, "Mirror-write %s: succeeded\n",
888 * Report back to the originator.
891 mrec_tmp.update.tid = mirror.tid_end;
892 write_mrecord(1, HAMMER_MREC_TYPE_UPDATE,
893 &mrec_tmp, sizeof(mrec_tmp.update));
895 printf("Source can update synctid to 0x%016jx\n",
896 (uintmax_t)mirror.tid_end);
903 hammer_cmd_mirror_dump(void)
905 char *buf = malloc(SERIALBUF_SIZE);
906 struct hammer_ioc_mrecord_head pickup;
907 hammer_ioc_mrecord_any_t mrec;
914 * Read and process the PFS header
916 pickup.signature = 0;
919 mrec = read_mrecord(0, &error, &pickup);
923 * Read and process bulk records
926 size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
930 while (offset < size) {
931 mrec = (void *)((char *)buf + offset);
932 bytes = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
933 if (offset + bytes > size) {
934 fprintf(stderr, "Misaligned record\n");
938 switch(mrec->head.type & HAMMER_MRECF_TYPE_MASK) {
939 case HAMMER_MREC_TYPE_REC_BADCRC:
940 case HAMMER_MREC_TYPE_REC:
941 printf("Record obj=%016jx key=%016jx "
943 (uintmax_t)mrec->rec.leaf.base.obj_id,
944 (uintmax_t)mrec->rec.leaf.base.key,
945 mrec->rec.leaf.base.rec_type,
946 mrec->rec.leaf.base.obj_type);
947 if (mrec->head.type ==
948 HAMMER_MREC_TYPE_REC_BADCRC) {
949 printf(" (BAD CRC)");
952 printf(" tids %016jx:%016jx data=%d\n",
953 (uintmax_t)mrec->rec.leaf.base.create_tid,
954 (uintmax_t)mrec->rec.leaf.base.delete_tid,
955 mrec->rec.leaf.data_len);
957 case HAMMER_MREC_TYPE_PASS:
958 printf("Pass obj=%016jx key=%016jx "
960 (uintmax_t)mrec->rec.leaf.base.obj_id,
961 (uintmax_t)mrec->rec.leaf.base.key,
962 mrec->rec.leaf.base.rec_type,
963 mrec->rec.leaf.base.obj_type);
964 printf(" tids %016jx:%016jx data=%d\n",
965 (uintmax_t)mrec->rec.leaf.base.create_tid,
966 (uintmax_t)mrec->rec.leaf.base.delete_tid,
967 mrec->rec.leaf.data_len);
969 case HAMMER_MREC_TYPE_SKIP:
970 printf("Skip obj=%016jx key=%016jx rt=%02x to\n"
971 " obj=%016jx key=%016jx rt=%02x\n",
972 (uintmax_t)mrec->skip.skip_beg.obj_id,
973 (uintmax_t)mrec->skip.skip_beg.key,
974 mrec->skip.skip_beg.rec_type,
975 (uintmax_t)mrec->skip.skip_end.obj_id,
976 (uintmax_t)mrec->skip.skip_end.key,
977 mrec->skip.skip_end.rec_type);
986 * Read and process the termination sync record.
988 mrec = read_mrecord(0, &error, &pickup);
990 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
991 mrec->head.type != HAMMER_MREC_TYPE_IDLE)
993 fprintf(stderr, "Mirror-dump: Did not get termination "
998 * Continue with more batches until EOF.
1000 mrec = read_mrecord(0, &error, &pickup);
1006 hammer_cmd_mirror_copy(char **av, int ac, int streaming)
1011 const char *xav[32];
1020 signal(SIGPIPE, SIG_IGN);
1023 if (pipe(fds) < 0) {
1031 if ((pid1 = fork()) == 0) {
1032 signal(SIGPIPE, SIG_DFL);
1037 if ((ptr = strchr(av[0], ':')) != NULL) {
1045 xav[xac++] = SshPort;
1048 xav[xac++] = "hammer";
1050 switch(VerboseOpt) {
1060 xav[xac++] = "-vvv";
1068 snprintf(tbuf, sizeof(tbuf), "%d", TimeoutOpt);
1072 if (SplitupOptStr) {
1074 xav[xac++] = SplitupOptStr;
1077 xav[xac++] = "mirror-read-stream";
1079 xav[xac++] = "mirror-read";
1082 execv("/usr/bin/ssh", (void *)xav);
1084 hammer_cmd_mirror_read(av, 1, streaming);
1094 if ((pid2 = fork()) == 0) {
1095 signal(SIGPIPE, SIG_DFL);
1100 if ((ptr = strchr(av[1], ':')) != NULL) {
1108 xav[xac++] = SshPort;
1111 xav[xac++] = "hammer";
1113 switch(VerboseOpt) {
1123 xav[xac++] = "-vvv";
1130 xav[xac++] = "mirror-write";
1133 execv("/usr/bin/ssh", (void *)xav);
1135 hammer_cmd_mirror_write(av + 1, 1);
1144 while (waitpid(pid1, NULL, 0) <= 0)
1146 while (waitpid(pid2, NULL, 0) <= 0)
1150 * If the link is lost restart
1154 fprintf(stderr, "\nLost Link\n");
1157 sleep(15 + DelayOpt);
1164 * Read and return multiple mrecords
1167 read_mrecords(int fd, char *buf, u_int size, hammer_ioc_mrecord_head_t pickup)
1169 hammer_ioc_mrecord_any_t mrec;
1177 while (size - count >= HAMMER_MREC_HEADSIZE) {
1179 * Cached the record header in case we run out of buffer
1183 if (pickup->signature == 0) {
1184 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
1185 i = read(fd, (char *)pickup + n,
1186 HAMMER_MREC_HEADSIZE - n);
1192 if (n != HAMMER_MREC_HEADSIZE) {
1193 fprintf(stderr, "read_mrecords: short read on pipe\n");
1196 if (pickup->signature != HAMMER_IOC_MIRROR_SIGNATURE) {
1197 fprintf(stderr, "read_mrecords: malformed record on pipe, "
1202 if (pickup->rec_size < HAMMER_MREC_HEADSIZE ||
1203 pickup->rec_size > sizeof(*mrec) + HAMMER_XBUFSIZE) {
1204 fprintf(stderr, "read_mrecords: malformed record on pipe, "
1205 "illegal rec_size\n");
1210 * Stop if we have insufficient space for the record and data.
1212 bytes = HAMMER_HEAD_DOALIGN(pickup->rec_size);
1213 if (size - count < bytes)
1217 * Stop if the record type is not a REC, SKIP, or PASS,
1218 * which are the only types the ioctl supports. Other types
1219 * are used only by the userland protocol.
1223 type = pickup->type & HAMMER_MRECF_TYPE_LOMASK;
1224 if (type != HAMMER_MREC_TYPE_PFSD &&
1225 type != HAMMER_MREC_TYPE_REC &&
1226 type != HAMMER_MREC_TYPE_SKIP &&
1227 type != HAMMER_MREC_TYPE_PASS) {
1232 * Read the remainder and clear the pickup signature.
1234 for (n = HAMMER_MREC_HEADSIZE; n < bytes; n += i) {
1235 i = read(fd, buf + count + n, bytes - n);
1240 fprintf(stderr, "read_mrecords: short read on pipe\n");
1244 bcopy(pickup, buf + count, HAMMER_MREC_HEADSIZE);
1245 pickup->signature = 0;
1247 mrec = (void *)(buf + count);
1250 * Validate the completed record
1252 if (mrec->head.rec_crc !=
1253 crc32((char *)mrec + HAMMER_MREC_CRCOFF,
1254 mrec->head.rec_size - HAMMER_MREC_CRCOFF)) {
1255 fprintf(stderr, "read_mrecords: malformed record "
1256 "on pipe, bad crc\n");
1261 * If its a B-Tree record validate the data crc.
1263 * NOTE: If the VFS passes us an explicitly errorde mrec
1264 * we just pass it through.
1266 type = mrec->head.type & HAMMER_MRECF_TYPE_MASK;
1268 if (type == HAMMER_MREC_TYPE_REC) {
1269 if (mrec->head.rec_size <
1270 sizeof(mrec->rec) + mrec->rec.leaf.data_len) {
1272 "read_mrecords: malformed record on "
1273 "pipe, illegal element data_len\n");
1276 if (mrec->rec.leaf.data_len &&
1277 mrec->rec.leaf.data_offset &&
1278 hammer_crc_test_leaf(&mrec->rec + 1, &mrec->rec.leaf) == 0) {
1280 "read_mrecords: data_crc did not "
1281 "match data! obj=%016jx key=%016jx\n",
1282 (uintmax_t)mrec->rec.leaf.base.obj_id,
1283 (uintmax_t)mrec->rec.leaf.base.key);
1285 "continuing, but there are problems\n");
1294 * Read and return a single mrecord.
1297 hammer_ioc_mrecord_any_t
1298 read_mrecord(int fdin, int *errorp, hammer_ioc_mrecord_head_t pickup)
1300 hammer_ioc_mrecord_any_t mrec;
1301 struct hammer_ioc_mrecord_head mrechd;
1306 if (pickup && pickup->type != 0) {
1308 pickup->signature = 0;
1310 n = HAMMER_MREC_HEADSIZE;
1313 * Read in the PFSD header from the sender.
1315 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
1316 i = read(fdin, (char *)&mrechd + n, HAMMER_MREC_HEADSIZE - n);
1321 *errorp = 0; /* EOF */
1324 if (n != HAMMER_MREC_HEADSIZE) {
1325 fprintf(stderr, "short read of mrecord header\n");
1330 if (mrechd.signature != HAMMER_IOC_MIRROR_SIGNATURE) {
1331 fprintf(stderr, "read_mrecord: bad signature\n");
1335 bytes = HAMMER_HEAD_DOALIGN(mrechd.rec_size);
1336 assert(bytes >= sizeof(mrechd));
1337 mrec = malloc(bytes);
1338 mrec->head = mrechd;
1341 i = read(fdin, (char *)mrec + n, bytes - n);
1347 fprintf(stderr, "read_mrecord: short read on payload\n");
1351 if (mrec->head.rec_crc !=
1352 crc32((char *)mrec + HAMMER_MREC_CRCOFF,
1353 mrec->head.rec_size - HAMMER_MREC_CRCOFF)) {
1354 fprintf(stderr, "read_mrecord: bad CRC\n");
1364 write_mrecord(int fdout, u_int32_t type, hammer_ioc_mrecord_any_t mrec,
1367 char zbuf[HAMMER_HEAD_ALIGN];
1370 pad = HAMMER_HEAD_DOALIGN(bytes) - bytes;
1372 assert(bytes >= (int)sizeof(mrec->head));
1373 bzero(&mrec->head, sizeof(mrec->head));
1374 mrec->head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
1375 mrec->head.type = type;
1376 mrec->head.rec_size = bytes;
1377 mrec->head.rec_crc = crc32((char *)mrec + HAMMER_MREC_CRCOFF,
1378 bytes - HAMMER_MREC_CRCOFF);
1379 if (write(fdout, mrec, bytes) != bytes) {
1380 fprintf(stderr, "write_mrecord: error %d (%s)\n",
1381 errno, strerror(errno));
1386 if (write(fdout, zbuf, pad) != pad) {
1387 fprintf(stderr, "write_mrecord: error %d (%s)\n",
1388 errno, strerror(errno));
1395 * Generate a mirroring header with the pfs information of the
1396 * originating filesytem.
1399 generate_mrec_header(int fd, int pfs_id,
1400 union hammer_ioc_mrecord_any *mrec_tmp)
1402 struct hammer_ioc_pseudofs_rw pfs;
1404 bzero(&pfs, sizeof(pfs));
1405 bzero(mrec_tmp, sizeof(*mrec_tmp));
1406 pfs.pfs_id = pfs_id;
1407 pfs.ondisk = &mrec_tmp->pfs.pfsd;
1408 pfs.bytes = sizeof(mrec_tmp->pfs.pfsd);
1409 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1410 fprintf(stderr, "Mirror-read: not a HAMMER fs/pseudofs!\n");
1413 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1414 fprintf(stderr, "Mirror-read: HAMMER pfs version mismatch!\n");
1417 mrec_tmp->pfs.version = pfs.version;
1421 * Validate the pfs information from the originating filesystem
1422 * against the target filesystem. shared_uuid must match.
1424 * return -1 if we got a TERM record
1427 validate_mrec_header(int fd, int fdin, int is_target, int pfs_id,
1428 struct hammer_ioc_mrecord_head *pickup,
1429 hammer_tid_t *tid_begp, hammer_tid_t *tid_endp)
1431 struct hammer_ioc_pseudofs_rw pfs;
1432 struct hammer_pseudofs_data pfsd;
1433 hammer_ioc_mrecord_any_t mrec;
1437 * Get the PFSD info from the target filesystem.
1439 bzero(&pfs, sizeof(pfs));
1440 bzero(&pfsd, sizeof(pfsd));
1441 pfs.pfs_id = pfs_id;
1443 pfs.bytes = sizeof(pfsd);
1444 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1445 fprintf(stderr, "mirror-write: not a HAMMER fs/pseudofs!\n");
1448 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1449 fprintf(stderr, "mirror-write: HAMMER pfs version mismatch!\n");
1453 mrec = read_mrecord(fdin, &error, pickup);
1456 fprintf(stderr, "validate_mrec_header: short read\n");
1459 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
1464 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
1465 fprintf(stderr, "validate_mrec_header: did not get expected "
1466 "PFSD record type\n");
1469 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
1470 fprintf(stderr, "validate_mrec_header: unexpected payload "
1474 if (mrec->pfs.version != pfs.version) {
1475 fprintf(stderr, "validate_mrec_header: Version mismatch\n");
1480 * Whew. Ok, is the read PFS info compatible with the target?
1482 if (bcmp(&mrec->pfs.pfsd.shared_uuid, &pfsd.shared_uuid,
1483 sizeof(pfsd.shared_uuid)) != 0) {
1485 "mirror-write: source and target have "
1486 "different shared-uuid's!\n");
1490 (pfsd.mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
1491 fprintf(stderr, "mirror-write: target must be in slave mode\n");
1495 *tid_begp = mrec->pfs.pfsd.sync_beg_tid;
1497 *tid_endp = mrec->pfs.pfsd.sync_end_tid;
1503 update_pfs_snapshot(int fd, hammer_tid_t snapshot_tid, int pfs_id)
1505 struct hammer_ioc_pseudofs_rw pfs;
1506 struct hammer_pseudofs_data pfsd;
1508 bzero(&pfs, sizeof(pfs));
1509 bzero(&pfsd, sizeof(pfsd));
1510 pfs.pfs_id = pfs_id;
1512 pfs.bytes = sizeof(pfsd);
1513 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1514 perror("update_pfs_snapshot (read)");
1517 if (pfsd.sync_end_tid != snapshot_tid) {
1518 pfsd.sync_end_tid = snapshot_tid;
1519 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) != 0) {
1520 perror("update_pfs_snapshot (rewrite)");
1523 if (VerboseOpt >= 2) {
1525 "Mirror-write: Completed, updated snapshot "
1527 (uintmax_t)snapshot_tid);
1534 * Bandwidth-limited write in chunks
1538 writebw(int fd, const void *buf, size_t nbytes,
1539 u_int64_t *bwcount, struct timeval *tv1)
1550 if (*bwcount + nbytes > BandwidthOpt)
1551 n = BandwidthOpt - *bwcount;
1555 r = write(fd, buf, n);
1559 buf = (const char *)buf + r;
1564 if (*bwcount >= BandwidthOpt) {
1565 gettimeofday(&tv2, NULL);
1566 usec = (int)(tv2.tv_sec - tv1->tv_sec) * 1000000 +
1567 (int)(tv2.tv_usec - tv1->tv_usec);
1568 if (usec >= 0 && usec < 1000000)
1569 usleep(1000000 - usec);
1570 gettimeofday(tv1, NULL);
1571 *bwcount -= BandwidthOpt;
1578 * Get a yes or no answer from the terminal. The program may be run as
1579 * part of a two-way pipe so we cannot use stdin for this operation.
1588 fp = fopen("/dev/tty", "r");
1590 fprintf(stderr, "No terminal for response\n");
1594 while (fgets(buf, sizeof(buf), fp) != NULL) {
1595 if (buf[0] == 'y' || buf[0] == 'Y') {
1599 if (buf[0] == 'n' || buf[0] == 'N') {
1603 fprintf(stderr, "Response not understood\n");
1611 mirror_usage(int code)
1614 "hammer mirror-read <filesystem> [begin-tid]\n"
1615 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
1616 "hammer mirror-write <filesystem>\n"
1617 "hammer mirror-dump\n"
1618 "hammer mirror-copy [[user@]host:]<filesystem>"
1619 " [[user@]host:]<filesystem>\n"
1620 "hammer mirror-stream [[user@]host:]<filesystem>"
1621 " [[user@]host:]<filesystem>\n"