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
41 #define SERIALBUF_SIZE (512 * 1024)
43 typedef struct histogram {
48 static int read_mrecords(int fd, char *buf, u_int size,
49 hammer_ioc_mrecord_head_t pickup);
50 static int generate_histogram(int fd, const char *filesystem,
51 histogram_t *histogram_ary,
52 struct hammer_ioc_mirror_rw *mirror_base,
54 static hammer_ioc_mrecord_any_t read_mrecord(int fdin, int *errorp,
55 hammer_ioc_mrecord_head_t pickup);
56 static void write_mrecord(int fdout, u_int32_t type,
57 hammer_ioc_mrecord_any_t mrec, int bytes);
58 static void generate_mrec_header(int fd, int pfs_id,
59 union hammer_ioc_mrecord_any *mrec_tmp);
60 static int validate_mrec_header(int fd, int fdin, int is_target, int pfs_id,
61 struct hammer_ioc_mrecord_head *pickup,
62 hammer_tid_t *tid_begp, hammer_tid_t *tid_endp);
63 static void update_pfs_snapshot(int fd, hammer_tid_t snapshot_tid, int pfs_id);
64 static ssize_t writebw(int fd, const void *buf, size_t nbytes,
65 u_int64_t *bwcount, struct timeval *tv1);
66 static int getyn(void);
67 static void mirror_usage(int code);
70 * Generate a mirroring data stream from the specific source over the
71 * entire key range, but restricted to the specified transaction range.
73 * The HAMMER VFS does most of the work, we add a few new mrecord
74 * types to negotiate the TID ranges and verify that the entire
75 * stream made it to the destination.
77 * streaming will be 0 for mirror-read, 1 for mirror-stream. The code will
78 * set up a fake value of -1 when running the histogram for mirror-read.
81 hammer_cmd_mirror_read(char **av, int ac, int streaming)
83 struct hammer_ioc_mirror_rw mirror;
84 struct hammer_ioc_pseudofs_rw pfs;
85 union hammer_ioc_mrecord_any mrec_tmp;
86 struct hammer_ioc_mrecord_head pickup;
87 hammer_ioc_mrecord_any_t mrec;
88 hammer_tid_t sync_tid;
89 histogram_t histogram_ary;
90 const char *filesystem;
91 char *buf = malloc(SERIALBUF_SIZE);
103 time_t base_t = time(NULL);
108 if (ac == 0 || ac > 2)
111 hammer_check_restrict(filesystem);
113 pickup.signature = 0;
118 histogram_ary = NULL;
122 bzero(&mirror, sizeof(mirror));
123 hammer_key_beg_init(&mirror.key_beg);
124 hammer_key_end_init(&mirror.key_end);
126 fd = getpfs(&pfs, filesystem);
129 score_printf(LINE1, "Running");
131 if (streaming >= 0 && VerboseOpt && VerboseOpt < 2) {
132 fprintf(stderr, "%cRunning \b\b", (sameline ? '\r' : '\n'));
138 gettimeofday(&bwtv, NULL);
142 * Send initial header for the purpose of determining the
145 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
146 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
147 &mrec_tmp, sizeof(mrec_tmp.pfs));
150 * In 2-way mode the target will send us a PFS info packet
151 * first. Use the target's current snapshot TID as our default
156 n = validate_mrec_header(fd, 0, 0, pfs.pfs_id, &pickup,
157 NULL, &mirror.tid_beg);
158 if (n < 0) { /* got TERM record */
163 } else if (streaming && histogram) {
164 mirror.tid_beg = histogram_ary[histindex].tid + 1;
170 * Write out the PFS header, tid_beg will be updated if our PFS
171 * has a larger begin sync. tid_end is set to the latest source
172 * TID whos flush cycle has completed.
174 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
175 if (mirror.tid_beg < mrec_tmp.pfs.pfsd.sync_beg_tid)
176 mirror.tid_beg = mrec_tmp.pfs.pfsd.sync_beg_tid;
177 mirror.tid_end = mrec_tmp.pfs.pfsd.sync_end_tid;
179 mirror.size = SERIALBUF_SIZE;
180 mirror.pfs_id = pfs.pfs_id;
181 mirror.shared_uuid = pfs.ondisk->shared_uuid;
184 * XXX If the histogram is exhausted and the TID delta is large
185 * the stream might have been offline for a while and is
186 * now picking it up again. Do another histogram.
189 if (streaming && histogram && histindex == histend) {
190 if (mirror.tid_end - mirror.tid_beg > BULK_MINIMUM)
196 * Initial bulk startup control, try to do some incremental
197 * mirroring in order to allow the stream to be killed and
198 * restarted without having to start over.
200 if (histogram == 0 && BulkOpt == 0) {
201 if (VerboseOpt && repeat == 0) {
202 fprintf(stderr, "\n");
205 histmax = generate_histogram(fd, filesystem,
206 &histogram_ary, &mirror,
212 * Just stream the histogram, then stop
218 if (streaming && histogram) {
220 mirror.tid_end = histogram_ary[histindex].tid;
221 estbytes = histogram_ary[histindex-1].bytes;
222 mrec_tmp.pfs.pfsd.sync_end_tid = mirror.tid_end;
227 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
228 &mrec_tmp, sizeof(mrec_tmp.pfs));
231 * A cycle file overrides the beginning TID only if we are
232 * not operating in two-way or histogram mode.
234 if (TwoWayPipeOpt == 0 && histogram == 0) {
235 hammer_get_cycle(&mirror.key_beg, &mirror.tid_beg);
239 * An additional argument overrides the beginning TID regardless
240 * of what mode we are in. This is not recommending if operating
244 mirror.tid_beg = strtoull(av[1], NULL, 0);
246 if (streaming == 0 || VerboseOpt >= 2) {
248 "Mirror-read: Mirror %016jx to %016jx",
249 (uintmax_t)mirror.tid_beg, (uintmax_t)mirror.tid_end);
251 fprintf(stderr, " (bulk= %ju)", (uintmax_t)estbytes);
252 fprintf(stderr, "\n");
255 if (mirror.key_beg.obj_id != (int64_t)HAMMER_MIN_OBJID) {
256 fprintf(stderr, "Mirror-read: Resuming at object %016jx\n",
257 (uintmax_t)mirror.key_beg.obj_id);
261 * Nothing to do if begin equals end.
263 if (mirror.tid_beg >= mirror.tid_end) {
264 if (streaming == 0 || VerboseOpt >= 2)
265 fprintf(stderr, "Mirror-read: No work to do\n");
274 * Write out bulk records
277 mirror.size = SERIALBUF_SIZE;
281 mirror.pfs_id = pfs.pfs_id;
282 mirror.shared_uuid = pfs.ondisk->shared_uuid;
283 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
284 score_printf(LINE3, "Mirror-read %s failed: %s",
285 filesystem, strerror(errno));
286 fprintf(stderr, "Mirror-read %s failed: %s\n",
287 filesystem, strerror(errno));
290 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
291 score_printf(LINE3, "Mirror-read %s fatal error %d",
292 filesystem, mirror.head.error);
294 "Mirror-read %s fatal error %d\n",
295 filesystem, mirror.head.error);
300 n = writebw(1, mirror.ubuf, mirror.count,
303 n = write(1, mirror.ubuf, mirror.count);
305 if (n != mirror.count) {
307 "Mirror-read %s failed: "
311 "Mirror-read %s failed: "
317 total_bytes += mirror.count;
318 if (streaming && VerboseOpt) {
320 "\rscan obj=%016jx tids=%016jx:%016jx %11jd",
321 (uintmax_t)mirror.key_cur.obj_id,
322 (uintmax_t)mirror.tid_beg,
323 (uintmax_t)mirror.tid_end,
324 (intmax_t)total_bytes);
327 } else if (streaming) {
329 "obj=%016jx tids=%016jx:%016jx %11jd",
330 (uintmax_t)mirror.key_cur.obj_id,
331 (uintmax_t)mirror.tid_beg,
332 (uintmax_t)mirror.tid_end,
333 (intmax_t)total_bytes);
335 mirror.key_beg = mirror.key_cur;
338 * Deal with time limit option
341 (unsigned)(time(NULL) - base_t) > (unsigned)TimeoutOpt) {
343 "Mirror-read %s interrupted by timer at"
346 (uintmax_t)mirror.key_cur.obj_id);
348 "Mirror-read %s interrupted by timer at"
351 (uintmax_t)mirror.key_cur.obj_id);
355 } while (mirror.count != 0);
358 if (streaming && VerboseOpt && sameline == 0) {
359 fprintf(stderr, "\n");
365 * Write out the termination sync record - only if not interrupted
367 if (interrupted == 0) {
369 write_mrecord(1, HAMMER_MREC_TYPE_SYNC,
370 &mrec_tmp, sizeof(mrec_tmp.sync));
372 write_mrecord(1, HAMMER_MREC_TYPE_IDLE,
373 &mrec_tmp, sizeof(mrec_tmp.sync));
378 * If the -2 option was given (automatic when doing mirror-copy),
379 * a two-way pipe is assumed and we expect a response mrec from
383 mrec = read_mrecord(0, &error, &pickup);
385 mrec->head.type != HAMMER_MREC_TYPE_UPDATE ||
386 mrec->head.rec_size != sizeof(mrec->update)) {
387 fprintf(stderr, "mirror_read: Did not get final "
388 "acknowledgement packet from target\n");
393 hammer_set_cycle(&mirror.key_cur,
395 fprintf(stderr, "Cyclefile %s updated for "
396 "continuation\n", CyclePath);
399 sync_tid = mrec->update.tid;
401 hammer_key_beg_init(&mirror.key_beg);
402 hammer_set_cycle(&mirror.key_beg, sync_tid);
404 "Cyclefile %s updated to 0x%016jx\n",
405 CyclePath, (uintmax_t)sync_tid);
408 } else if (CyclePath) {
409 /* NOTE! mirror.tid_beg cannot be updated */
410 fprintf(stderr, "Warning: cycle file (-c option) cannot be "
411 "fully updated unless you use mirror-copy\n");
412 hammer_set_cycle(&mirror.key_beg, mirror.tid_beg);
414 if (streaming && interrupted == 0) {
415 time_t t1 = time(NULL);
419 * Try to break down large bulk transfers into smaller ones
420 * so it can sync the transaction id on the slave. This
421 * way if we get interrupted a restart doesn't have to
422 * start from scratch.
424 if (streaming && histogram) {
425 if (histindex != histmax) {
426 if (VerboseOpt && VerboseOpt < 2 &&
428 fprintf(stderr, " (bulk incremental)");
435 if (VerboseOpt && streaming >= 0) {
436 fprintf(stderr, " W");
438 } else if (streaming >= 0) {
439 score_printf(LINE1, "Waiting");
441 pfs.ondisk->sync_end_tid = mirror.tid_end;
444 * Fake streaming mode when using a histogram to
445 * break up a mirror-read, do not wait on source.
448 } else if (ioctl(fd, HAMMERIOC_WAI_PSEUDOFS, &pfs) < 0) {
450 "Mirror-read %s: cannot stream: %s\n",
451 filesystem, strerror(errno));
453 "Mirror-read %s: cannot stream: %s\n",
454 filesystem, strerror(errno));
456 t2 = time(NULL) - t1;
457 if (t2 >= 0 && t2 < DelayOpt) {
459 fprintf(stderr, "\bD");
462 sleep(DelayOpt - t2);
465 fprintf(stderr, "\b ");
472 write_mrecord(1, HAMMER_MREC_TYPE_TERM,
473 &mrec_tmp, sizeof(mrec_tmp.sync));
475 fprintf(stderr, "Mirror-read %s succeeded\n", filesystem);
479 * What we are trying to do here is figure out how much data is
480 * going to be sent for the TID range and to break the TID range
481 * down into reasonably-sized slices (from the point of view of
482 * data sent) so a lost connection can restart at a reasonable
483 * place and not all the way back at the beginning.
485 * An entry's TID serves as the end_tid for the prior entry
486 * So we have to offset the calculation by 1 so that TID falls into
487 * the previous entry when populating entries.
489 * Because the transaction id space is bursty we need a relatively
490 * large number of buckets (like a million) to do a reasonable job
491 * for things like an initial bulk mirrors on a very large filesystem.
493 #define HIST_COUNT (1024 * 1024)
496 generate_histogram(int fd, const char *filesystem,
497 histogram_t *histogram_ary,
498 struct hammer_ioc_mirror_rw *mirror_base,
501 struct hammer_ioc_mirror_rw mirror;
502 union hammer_ioc_mrecord_any *mrec;
503 hammer_tid_t tid_beg;
504 hammer_tid_t tid_end;
507 u_int64_t *tid_bytes;
516 mirror = *mirror_base;
517 tid_beg = mirror.tid_beg;
518 tid_end = mirror.tid_end;
519 mirror.head.flags |= HAMMER_IOC_MIRROR_NODATA;
521 if (*histogram_ary == NULL) {
522 *histogram_ary = malloc(sizeof(struct histogram) *
525 if (tid_beg >= tid_end)
529 tid_bytes = malloc(sizeof(*tid_bytes) * (HIST_COUNT + 2));
530 bzero(tid_bytes, sizeof(*tid_bytes) * (HIST_COUNT + 2));
533 fprintf(stderr, "Prescan to break up bulk transfer");
535 fprintf(stderr, " (%juMB chunks)",
536 (uintmax_t)(SplitupOpt / (1024 * 1024)));
537 fprintf(stderr, "\n");
541 * Note: (tid_beg,tid_end), range is inclusive of both beg & end.
543 * Note: Estimates can be off when the mirror is way behind due
551 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
552 fprintf(stderr, "Mirror-read %s failed: %s\n",
553 filesystem, strerror(errno));
556 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
558 "Mirror-read %s fatal error %d\n",
559 filesystem, mirror.head.error);
564 off += HAMMER_HEAD_DOALIGN(mrec->head.rec_size)
566 mrec = (void *)((char *)mirror.ubuf + off);
569 * We only care about general RECs and PASS
570 * records. We ignore SKIPs.
572 switch (mrec->head.type & HAMMER_MRECF_TYPE_LOMASK) {
573 case HAMMER_MREC_TYPE_REC:
574 case HAMMER_MREC_TYPE_PASS:
581 * Calculate for two indices, create_tid and
582 * delete_tid. Record data only applies to
585 * When tid is exactly on the boundary it really
586 * belongs to the previous entry because scans
587 * are inclusive of the ending entry.
589 tid = mrec->rec.leaf.base.delete_tid;
590 if (tid && tid >= tid_beg && tid <= tid_end) {
591 len = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
592 if (mrec->head.type ==
593 HAMMER_MREC_TYPE_REC) {
594 len -= HAMMER_HEAD_DOALIGN(
595 mrec->rec.leaf.data_len);
598 i = (tid - tid_beg) * HIST_COUNT /
600 tidx = tid_beg + i * (tid_end - tid_beg) /
602 if (tid == tidx && i)
604 assert(i >= 0 && i < HIST_COUNT);
610 tid = mrec->rec.leaf.base.create_tid;
611 if (tid && tid >= tid_beg && tid <= tid_end) {
612 len = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
613 if (mrec->head.type ==
614 HAMMER_MREC_TYPE_REC_NODATA) {
615 len += HAMMER_HEAD_DOALIGN(
616 mrec->rec.leaf.data_len);
618 i = (tid - tid_beg) * HIST_COUNT /
620 tidx = tid_beg + i * (tid_end - tid_beg) /
622 if (tid == tidx && i)
624 assert(i >= 0 && i < HIST_COUNT);
630 if (*repeatp == 0 && accum > SplitupOpt) {
631 if (VerboseOpt > 1) {
632 fprintf(stderr, ".");
636 score_printf(LINE2, "Prescan chunk %d", chunkno);
639 if (mirror.count == 0)
641 mirror.key_beg = mirror.key_cur;
645 * Reduce to SplitupOpt (default 4GB) chunks. This code may
646 * use up to two additional elements. Do the array in-place.
648 * Inefficient degenerate cases can occur if we do not accumulate
649 * at least the requested split amount, so error on the side of
653 (*histogram_ary)[res].tid = tid_beg;
654 (*histogram_ary)[res].bytes = tid_bytes[0];
655 for (i = 1; i < HIST_COUNT; ++i) {
656 if ((*histogram_ary)[res].bytes >= SplitupOpt) {
658 (*histogram_ary)[res].tid = tid_beg +
659 i * (tid_end - tid_beg) /
661 (*histogram_ary)[res].bytes = 0;
664 (*histogram_ary)[res].bytes += tid_bytes[i];
667 (*histogram_ary)[res].tid = tid_end;
668 (*histogram_ary)[res].bytes = -1;
672 fprintf(stderr, "\n"); /* newline after ... */
673 score_printf(LINE3, "Prescan %d chunks, total %ju MBytes",
674 res, (uintmax_t)total / (1024 * 1024));
675 fprintf(stderr, "Prescan %d chunks, total %ju MBytes (",
676 res, (uintmax_t)total / (1024 * 1024));
677 for (i = 0; i < res && i < 3; ++i) {
679 fprintf(stderr, ", ");
680 fprintf(stderr, "%ju",
681 (uintmax_t)(*histogram_ary)[i].bytes);
684 fprintf(stderr, ", ...");
685 fprintf(stderr, ")\n");
687 assert(res <= HIST_COUNT);
695 create_pfs(const char *filesystem, uuid_t *s_uuid)
697 if (ForceYesOpt == 1) {
698 fprintf(stderr, "PFS slave %s does not exist. "
699 "Auto create new slave PFS!\n", filesystem);
702 fprintf(stderr, "PFS slave %s does not exist.\n"
703 "Do you want to create a new slave PFS? (yes|no) ",
707 fprintf(stderr, "Aborting operation\n");
713 char *shared_uuid = NULL;
714 uuid_to_string(s_uuid, &shared_uuid, &status);
717 asprintf(&cmd, "/sbin/hammer pfs-slave '%s' shared-uuid=%s 1>&2",
718 filesystem, shared_uuid);
722 fprintf(stderr, "Failed to alloc memory\n");
725 if (system(cmd) != 0) {
726 fprintf(stderr, "Failed to create PFS\n");
732 * Pipe the mirroring data stream on stdin to the HAMMER VFS, adding
733 * some additional packet types to negotiate TID ranges and to verify
734 * completion. The HAMMER VFS does most of the work.
736 * It is important to note that the mirror.key_{beg,end} range must
737 * match the ranged used by the original. For now both sides use
738 * range the entire key space.
740 * It is even more important that the records in the stream conform
741 * to the TID range also supplied in the stream. The HAMMER VFS will
742 * use the REC, PASS, and SKIP record types to track the portions of
743 * the B-Tree being scanned in order to be able to proactively delete
744 * records on the target within those active areas that are not mentioned
747 * The mirror.key_cur field is used by the VFS to do this tracking. It
748 * must be initialized to key_beg but then is persistently updated by
749 * the HAMMER VFS on each successive ioctl() call. If you blow up this
750 * field you will blow up the mirror target, possibly to the point of
751 * deleting everything. As a safety measure the HAMMER VFS simply marks
752 * the records that the source has destroyed as deleted on the target,
753 * and normal pruning operations will deal with their final disposition
754 * at some later time.
757 hammer_cmd_mirror_write(char **av, int ac)
759 struct hammer_ioc_mirror_rw mirror;
760 const char *filesystem;
761 char *buf = malloc(SERIALBUF_SIZE);
762 struct hammer_ioc_pseudofs_rw pfs;
763 struct hammer_ioc_mrecord_head pickup;
764 struct hammer_ioc_synctid synctid;
765 union hammer_ioc_mrecord_any mrec_tmp;
766 hammer_ioc_mrecord_any_t mrec;
775 hammer_check_restrict(filesystem);
777 pickup.signature = 0;
781 bzero(&mirror, sizeof(mirror));
782 hammer_key_beg_init(&mirror.key_beg);
783 hammer_key_end_init(&mirror.key_end);
784 mirror.key_end = mirror.key_beg;
787 * Read initial packet
789 mrec = read_mrecord(0, &error, &pickup);
792 fprintf(stderr, "validate_mrec_header: short read\n");
798 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
801 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
802 fprintf(stderr, "validate_mrec_header: did not get expected "
803 "PFSD record type\n");
806 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
807 fprintf(stderr, "validate_mrec_header: unexpected payload "
813 * Create slave PFS if it doesn't yet exist
815 if (lstat(filesystem, &st) != 0) {
816 create_pfs(filesystem, &mrec->pfs.pfsd.shared_uuid);
821 fd = getpfs(&pfs, filesystem);
824 * In two-way mode the target writes out a PFS packet first.
825 * The source uses our tid_end as its tid_beg by default,
826 * picking up where it left off.
830 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
831 if (mirror.tid_beg < mrec_tmp.pfs.pfsd.sync_beg_tid)
832 mirror.tid_beg = mrec_tmp.pfs.pfsd.sync_beg_tid;
833 mirror.tid_end = mrec_tmp.pfs.pfsd.sync_end_tid;
834 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
835 &mrec_tmp, sizeof(mrec_tmp.pfs));
839 * Read and process the PFS header. The source informs us of
840 * the TID range the stream represents.
842 n = validate_mrec_header(fd, 0, 1, pfs.pfs_id, &pickup,
843 &mirror.tid_beg, &mirror.tid_end);
844 if (n < 0) { /* got TERM record */
850 mirror.size = SERIALBUF_SIZE;
853 * Read and process bulk records (REC, PASS, and SKIP types).
855 * On your life, do NOT mess with mirror.key_cur or your mirror
856 * target may become history.
860 mirror.pfs_id = pfs.pfs_id;
861 mirror.shared_uuid = pfs.ondisk->shared_uuid;
862 mirror.size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
863 if (mirror.size <= 0)
865 if (ioctl(fd, HAMMERIOC_MIRROR_WRITE, &mirror) < 0) {
866 fprintf(stderr, "Mirror-write %s failed: %s\n",
867 filesystem, strerror(errno));
870 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
872 "Mirror-write %s fatal error %d\n",
873 filesystem, mirror.head.error);
877 if (mirror.head.flags & HAMMER_IOC_HEAD_INTR) {
879 "Mirror-write %s interrupted by timer at"
882 mirror.key_cur.obj_id);
889 * Read and process the termination sync record.
891 mrec = read_mrecord(0, &error, &pickup);
893 if (mrec && mrec->head.type == HAMMER_MREC_TYPE_TERM) {
894 fprintf(stderr, "Mirror-write: received termination request\n");
900 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
901 mrec->head.type != HAMMER_MREC_TYPE_IDLE) ||
902 mrec->head.rec_size != sizeof(mrec->sync)) {
903 fprintf(stderr, "Mirror-write %s: Did not get termination "
904 "sync record, or rec_size is wrong rt=%d\n",
906 (mrec ? (int)mrec->head.type : -1));
911 * Update the PFS info on the target so the user has visibility
912 * into the new snapshot, and sync the target filesystem.
914 if (mrec->head.type == HAMMER_MREC_TYPE_SYNC) {
915 update_pfs_snapshot(fd, mirror.tid_end, pfs.pfs_id);
917 bzero(&synctid, sizeof(synctid));
918 synctid.op = HAMMER_SYNCTID_SYNC2;
919 ioctl(fd, HAMMERIOC_SYNCTID, &synctid);
921 if (VerboseOpt >= 2) {
922 fprintf(stderr, "Mirror-write %s: succeeded\n",
931 * Report back to the originator.
934 mrec_tmp.update.tid = mirror.tid_end;
935 write_mrecord(1, HAMMER_MREC_TYPE_UPDATE,
936 &mrec_tmp, sizeof(mrec_tmp.update));
938 printf("Source can update synctid to 0x%016jx\n",
939 (uintmax_t)mirror.tid_end);
946 hammer_cmd_mirror_dump(void)
948 char *buf = malloc(SERIALBUF_SIZE);
949 struct hammer_ioc_mrecord_head pickup;
950 hammer_ioc_mrecord_any_t mrec;
957 * Read and process the PFS header
959 pickup.signature = 0;
962 mrec = read_mrecord(0, &error, &pickup);
966 * Read and process bulk records
969 size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
973 while (offset < size) {
974 mrec = (void *)((char *)buf + offset);
975 bytes = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
976 if (offset + bytes > size) {
977 fprintf(stderr, "Misaligned record\n");
981 switch(mrec->head.type & HAMMER_MRECF_TYPE_MASK) {
982 case HAMMER_MREC_TYPE_REC_BADCRC:
983 case HAMMER_MREC_TYPE_REC:
984 printf("Record obj=%016jx key=%016jx "
986 (uintmax_t)mrec->rec.leaf.base.obj_id,
987 (uintmax_t)mrec->rec.leaf.base.key,
988 mrec->rec.leaf.base.rec_type,
989 mrec->rec.leaf.base.obj_type);
990 if (mrec->head.type ==
991 HAMMER_MREC_TYPE_REC_BADCRC) {
992 printf(" (BAD CRC)");
995 printf(" tids %016jx:%016jx data=%d\n",
996 (uintmax_t)mrec->rec.leaf.base.create_tid,
997 (uintmax_t)mrec->rec.leaf.base.delete_tid,
998 mrec->rec.leaf.data_len);
1000 case HAMMER_MREC_TYPE_PASS:
1001 printf("Pass obj=%016jx key=%016jx "
1002 "rt=%02x ot=%02x\n",
1003 (uintmax_t)mrec->rec.leaf.base.obj_id,
1004 (uintmax_t)mrec->rec.leaf.base.key,
1005 mrec->rec.leaf.base.rec_type,
1006 mrec->rec.leaf.base.obj_type);
1007 printf(" tids %016jx:%016jx data=%d\n",
1008 (uintmax_t)mrec->rec.leaf.base.create_tid,
1009 (uintmax_t)mrec->rec.leaf.base.delete_tid,
1010 mrec->rec.leaf.data_len);
1012 case HAMMER_MREC_TYPE_SKIP:
1013 printf("Skip obj=%016jx key=%016jx rt=%02x to\n"
1014 " obj=%016jx key=%016jx rt=%02x\n",
1015 (uintmax_t)mrec->skip.skip_beg.obj_id,
1016 (uintmax_t)mrec->skip.skip_beg.key,
1017 mrec->skip.skip_beg.rec_type,
1018 (uintmax_t)mrec->skip.skip_end.obj_id,
1019 (uintmax_t)mrec->skip.skip_end.key,
1020 mrec->skip.skip_end.rec_type);
1029 * Read and process the termination sync record.
1031 mrec = read_mrecord(0, &error, &pickup);
1033 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
1034 mrec->head.type != HAMMER_MREC_TYPE_IDLE)
1036 fprintf(stderr, "Mirror-dump: Did not get termination "
1041 * Continue with more batches until EOF.
1043 mrec = read_mrecord(0, &error, &pickup);
1049 hammer_cmd_mirror_copy(char **av, int ac, int streaming)
1054 const char *xav[32];
1056 char *sh, *user, *host, *rfs;
1063 signal(SIGPIPE, SIG_IGN);
1066 if (pipe(fds) < 0) {
1074 if ((pid1 = fork()) == 0) {
1075 signal(SIGPIPE, SIG_DFL);
1080 if ((rfs = strchr(av[0], ':')) != NULL) {
1083 if((sh = getenv("HAMMER_RSH")) == NULL)
1091 user = strndup(av[0], (rfs - av[0]));
1093 if ((host = strchr(av[0], '@')) != NULL) {
1094 user = strndup( av[0], (host++ - av[0]));
1095 host = strndup( host, (rfs++ - host));
1101 host = strndup( av[0], (rfs++ - av[0]));
1109 xav[xac++] = SshPort;
1112 xav[xac++] = "hammer";
1114 switch(VerboseOpt) {
1124 xav[xac++] = "-vvv";
1132 snprintf(tbuf, sizeof(tbuf), "%d", TimeoutOpt);
1136 if (SplitupOptStr) {
1138 xav[xac++] = SplitupOptStr;
1141 xav[xac++] = "mirror-read-stream";
1143 xav[xac++] = "mirror-read";
1146 execvp(*xav, (void *)xav);
1148 hammer_cmd_mirror_read(av, 1, streaming);
1158 if ((pid2 = fork()) == 0) {
1159 signal(SIGPIPE, SIG_DFL);
1164 if ((rfs = strchr(av[1], ':')) != NULL) {
1167 if((sh = getenv("HAMMER_RSH")) == NULL)
1175 user = strndup(av[1], (rfs - av[1]));
1177 if ((host = strchr(av[1], '@')) != NULL) {
1178 user = strndup( av[1], (host++ - av[1]));
1179 host = strndup( host, (rfs++ - host));
1185 host = strndup( av[1], (rfs++ - av[1]));
1192 xav[xac++] = SshPort;
1195 xav[xac++] = "hammer";
1197 switch(VerboseOpt) {
1207 xav[xac++] = "-vvv";
1214 xav[xac++] = "mirror-write";
1217 execvp(*xav, (void *)xav);
1219 hammer_cmd_mirror_write(av + 1, 1);
1228 while (waitpid(pid1, NULL, 0) <= 0)
1230 while (waitpid(pid2, NULL, 0) <= 0)
1234 * If the link is lost restart
1238 fprintf(stderr, "\nLost Link\n");
1241 sleep(15 + DelayOpt);
1248 * Read and return multiple mrecords
1251 read_mrecords(int fd, char *buf, u_int size, hammer_ioc_mrecord_head_t pickup)
1253 hammer_ioc_mrecord_any_t mrec;
1261 while (size - count >= HAMMER_MREC_HEADSIZE) {
1263 * Cached the record header in case we run out of buffer
1267 if (pickup->signature == 0) {
1268 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
1269 i = read(fd, (char *)pickup + n,
1270 HAMMER_MREC_HEADSIZE - n);
1276 if (n != HAMMER_MREC_HEADSIZE) {
1277 fprintf(stderr, "read_mrecords: short read on pipe\n");
1280 if (pickup->signature != HAMMER_IOC_MIRROR_SIGNATURE) {
1281 fprintf(stderr, "read_mrecords: malformed record on pipe, "
1286 if (pickup->rec_size < HAMMER_MREC_HEADSIZE ||
1287 pickup->rec_size > sizeof(*mrec) + HAMMER_XBUFSIZE) {
1288 fprintf(stderr, "read_mrecords: malformed record on pipe, "
1289 "illegal rec_size\n");
1294 * Stop if we have insufficient space for the record and data.
1296 bytes = HAMMER_HEAD_DOALIGN(pickup->rec_size);
1297 if (size - count < bytes)
1301 * Stop if the record type is not a REC, SKIP, or PASS,
1302 * which are the only types the ioctl supports. Other types
1303 * are used only by the userland protocol.
1307 type = pickup->type & HAMMER_MRECF_TYPE_LOMASK;
1308 if (type != HAMMER_MREC_TYPE_PFSD &&
1309 type != HAMMER_MREC_TYPE_REC &&
1310 type != HAMMER_MREC_TYPE_SKIP &&
1311 type != HAMMER_MREC_TYPE_PASS) {
1316 * Read the remainder and clear the pickup signature.
1318 for (n = HAMMER_MREC_HEADSIZE; n < bytes; n += i) {
1319 i = read(fd, buf + count + n, bytes - n);
1324 fprintf(stderr, "read_mrecords: short read on pipe\n");
1328 bcopy(pickup, buf + count, HAMMER_MREC_HEADSIZE);
1329 pickup->signature = 0;
1331 mrec = (void *)(buf + count);
1334 * Validate the completed record
1336 if (mrec->head.rec_crc !=
1337 crc32((char *)mrec + HAMMER_MREC_CRCOFF,
1338 mrec->head.rec_size - HAMMER_MREC_CRCOFF)) {
1339 fprintf(stderr, "read_mrecords: malformed record "
1340 "on pipe, bad crc\n");
1345 * If its a B-Tree record validate the data crc.
1347 * NOTE: If the VFS passes us an explicitly errorde mrec
1348 * we just pass it through.
1350 type = mrec->head.type & HAMMER_MRECF_TYPE_MASK;
1352 if (type == HAMMER_MREC_TYPE_REC) {
1353 if (mrec->head.rec_size <
1354 sizeof(mrec->rec) + mrec->rec.leaf.data_len) {
1356 "read_mrecords: malformed record on "
1357 "pipe, illegal element data_len\n");
1360 if (mrec->rec.leaf.data_len &&
1361 mrec->rec.leaf.data_offset &&
1362 hammer_crc_test_leaf(&mrec->rec + 1, &mrec->rec.leaf) == 0) {
1364 "read_mrecords: data_crc did not "
1365 "match data! obj=%016jx key=%016jx\n",
1366 (uintmax_t)mrec->rec.leaf.base.obj_id,
1367 (uintmax_t)mrec->rec.leaf.base.key);
1369 "continuing, but there are problems\n");
1378 * Read and return a single mrecord.
1381 hammer_ioc_mrecord_any_t
1382 read_mrecord(int fdin, int *errorp, hammer_ioc_mrecord_head_t pickup)
1384 hammer_ioc_mrecord_any_t mrec;
1385 struct hammer_ioc_mrecord_head mrechd;
1390 if (pickup && pickup->type != 0) {
1392 pickup->signature = 0;
1394 n = HAMMER_MREC_HEADSIZE;
1397 * Read in the PFSD header from the sender.
1399 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
1400 i = read(fdin, (char *)&mrechd + n, HAMMER_MREC_HEADSIZE - n);
1405 *errorp = 0; /* EOF */
1408 if (n != HAMMER_MREC_HEADSIZE) {
1409 fprintf(stderr, "short read of mrecord header\n");
1414 if (mrechd.signature != HAMMER_IOC_MIRROR_SIGNATURE) {
1415 fprintf(stderr, "read_mrecord: bad signature\n");
1419 bytes = HAMMER_HEAD_DOALIGN(mrechd.rec_size);
1420 assert(bytes >= sizeof(mrechd));
1421 mrec = malloc(bytes);
1422 mrec->head = mrechd;
1425 i = read(fdin, (char *)mrec + n, bytes - n);
1431 fprintf(stderr, "read_mrecord: short read on payload\n");
1435 if (mrec->head.rec_crc !=
1436 crc32((char *)mrec + HAMMER_MREC_CRCOFF,
1437 mrec->head.rec_size - HAMMER_MREC_CRCOFF)) {
1438 fprintf(stderr, "read_mrecord: bad CRC\n");
1448 write_mrecord(int fdout, u_int32_t type, hammer_ioc_mrecord_any_t mrec,
1451 char zbuf[HAMMER_HEAD_ALIGN];
1454 pad = HAMMER_HEAD_DOALIGN(bytes) - bytes;
1456 assert(bytes >= (int)sizeof(mrec->head));
1457 bzero(&mrec->head, sizeof(mrec->head));
1458 mrec->head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
1459 mrec->head.type = type;
1460 mrec->head.rec_size = bytes;
1461 mrec->head.rec_crc = crc32((char *)mrec + HAMMER_MREC_CRCOFF,
1462 bytes - HAMMER_MREC_CRCOFF);
1463 if (write(fdout, mrec, bytes) != bytes) {
1464 fprintf(stderr, "write_mrecord: error %d (%s)\n",
1465 errno, strerror(errno));
1470 if (write(fdout, zbuf, pad) != pad) {
1471 fprintf(stderr, "write_mrecord: error %d (%s)\n",
1472 errno, strerror(errno));
1479 * Generate a mirroring header with the pfs information of the
1480 * originating filesytem.
1483 generate_mrec_header(int fd, int pfs_id,
1484 union hammer_ioc_mrecord_any *mrec_tmp)
1486 struct hammer_ioc_pseudofs_rw pfs;
1488 bzero(&pfs, sizeof(pfs));
1489 bzero(mrec_tmp, sizeof(*mrec_tmp));
1490 pfs.pfs_id = pfs_id;
1491 pfs.ondisk = &mrec_tmp->pfs.pfsd;
1492 pfs.bytes = sizeof(mrec_tmp->pfs.pfsd);
1493 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1494 fprintf(stderr, "Mirror-read: not a HAMMER fs/pseudofs!\n");
1497 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1498 fprintf(stderr, "Mirror-read: HAMMER pfs version mismatch!\n");
1501 mrec_tmp->pfs.version = pfs.version;
1505 * Validate the pfs information from the originating filesystem
1506 * against the target filesystem. shared_uuid must match.
1508 * return -1 if we got a TERM record
1511 validate_mrec_header(int fd, int fdin, int is_target, int pfs_id,
1512 struct hammer_ioc_mrecord_head *pickup,
1513 hammer_tid_t *tid_begp, hammer_tid_t *tid_endp)
1515 struct hammer_ioc_pseudofs_rw pfs;
1516 struct hammer_pseudofs_data pfsd;
1517 hammer_ioc_mrecord_any_t mrec;
1521 * Get the PFSD info from the target filesystem.
1523 bzero(&pfs, sizeof(pfs));
1524 bzero(&pfsd, sizeof(pfsd));
1525 pfs.pfs_id = pfs_id;
1527 pfs.bytes = sizeof(pfsd);
1528 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1529 fprintf(stderr, "mirror-write: not a HAMMER fs/pseudofs!\n");
1532 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1533 fprintf(stderr, "mirror-write: HAMMER pfs version mismatch!\n");
1537 mrec = read_mrecord(fdin, &error, pickup);
1540 fprintf(stderr, "validate_mrec_header: short read\n");
1543 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
1548 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
1549 fprintf(stderr, "validate_mrec_header: did not get expected "
1550 "PFSD record type\n");
1553 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
1554 fprintf(stderr, "validate_mrec_header: unexpected payload "
1558 if (mrec->pfs.version != pfs.version) {
1559 fprintf(stderr, "validate_mrec_header: Version mismatch\n");
1564 * Whew. Ok, is the read PFS info compatible with the target?
1566 if (bcmp(&mrec->pfs.pfsd.shared_uuid, &pfsd.shared_uuid,
1567 sizeof(pfsd.shared_uuid)) != 0) {
1569 "mirror-write: source and target have "
1570 "different shared-uuid's!\n");
1574 (pfsd.mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
1575 fprintf(stderr, "mirror-write: target must be in slave mode\n");
1579 *tid_begp = mrec->pfs.pfsd.sync_beg_tid;
1581 *tid_endp = mrec->pfs.pfsd.sync_end_tid;
1587 update_pfs_snapshot(int fd, hammer_tid_t snapshot_tid, int pfs_id)
1589 struct hammer_ioc_pseudofs_rw pfs;
1590 struct hammer_pseudofs_data pfsd;
1592 bzero(&pfs, sizeof(pfs));
1593 bzero(&pfsd, sizeof(pfsd));
1594 pfs.pfs_id = pfs_id;
1596 pfs.bytes = sizeof(pfsd);
1597 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1598 perror("update_pfs_snapshot (read)");
1601 if (pfsd.sync_end_tid != snapshot_tid) {
1602 pfsd.sync_end_tid = snapshot_tid;
1603 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) != 0) {
1604 perror("update_pfs_snapshot (rewrite)");
1607 if (VerboseOpt >= 2) {
1609 "Mirror-write: Completed, updated snapshot "
1611 (uintmax_t)snapshot_tid);
1618 * Bandwidth-limited write in chunks
1622 writebw(int fd, const void *buf, size_t nbytes,
1623 u_int64_t *bwcount, struct timeval *tv1)
1634 if (*bwcount + nbytes > BandwidthOpt)
1635 n = BandwidthOpt - *bwcount;
1639 r = write(fd, buf, n);
1643 buf = (const char *)buf + r;
1648 if (*bwcount >= BandwidthOpt) {
1649 gettimeofday(&tv2, NULL);
1650 usec = (int)(tv2.tv_sec - tv1->tv_sec) * 1000000 +
1651 (int)(tv2.tv_usec - tv1->tv_usec);
1652 if (usec >= 0 && usec < 1000000)
1653 usleep(1000000 - usec);
1654 gettimeofday(tv1, NULL);
1655 *bwcount -= BandwidthOpt;
1662 * Get a yes or no answer from the terminal. The program may be run as
1663 * part of a two-way pipe so we cannot use stdin for this operation.
1672 fp = fopen("/dev/tty", "r");
1674 fprintf(stderr, "No terminal for response\n");
1678 while (fgets(buf, sizeof(buf), fp) != NULL) {
1679 if (buf[0] == 'y' || buf[0] == 'Y') {
1683 if (buf[0] == 'n' || buf[0] == 'N') {
1687 fprintf(stderr, "Response not understood\n");
1695 mirror_usage(int code)
1698 "hammer mirror-read <filesystem> [begin-tid]\n"
1699 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
1700 "hammer mirror-write <filesystem>\n"
1701 "hammer mirror-dump\n"
1702 "hammer mirror-copy [[user@]host:]<filesystem>"
1703 " [[user@]host:]<filesystem>\n"
1704 "hammer mirror-stream [[user@]host:]<filesystem>"
1705 " [[user@]host:]<filesystem>\n"