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, uint32_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 uint64_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;
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 */
165 } else if (streaming && histogram) {
166 mirror.tid_beg = histogram_ary[histindex].tid + 1;
172 * Write out the PFS header, tid_beg will be updated if our PFS
173 * has a larger begin sync. tid_end is set to the latest source
174 * TID whos flush cycle has completed.
176 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
177 if (mirror.tid_beg < mrec_tmp.pfs.pfsd.sync_beg_tid)
178 mirror.tid_beg = mrec_tmp.pfs.pfsd.sync_beg_tid;
179 mirror.tid_end = mrec_tmp.pfs.pfsd.sync_end_tid;
181 mirror.size = SERIALBUF_SIZE;
182 mirror.pfs_id = pfs.pfs_id;
183 mirror.shared_uuid = pfs.ondisk->shared_uuid;
186 * XXX If the histogram is exhausted and the TID delta is large
187 * the stream might have been offline for a while and is
188 * now picking it up again. Do another histogram.
191 if (streaming && histogram && histindex == histend) {
192 if (mirror.tid_end - mirror.tid_beg > BULK_MINIMUM)
198 * Initial bulk startup control, try to do some incremental
199 * mirroring in order to allow the stream to be killed and
200 * restarted without having to start over.
202 if (histogram == 0 && BulkOpt == 0) {
203 if (VerboseOpt && repeat == 0) {
204 fprintf(stderr, "\n");
207 histmax = generate_histogram(fd, filesystem,
208 &histogram_ary, &mirror,
214 * Just stream the histogram, then stop
220 if (streaming && histogram) {
222 mirror.tid_end = histogram_ary[histindex].tid;
223 estbytes = histogram_ary[histindex-1].bytes;
224 mrec_tmp.pfs.pfsd.sync_end_tid = mirror.tid_end;
229 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
230 &mrec_tmp, sizeof(mrec_tmp.pfs));
233 * A cycle file overrides the beginning TID only if we are
234 * not operating in two-way or histogram mode.
236 if (TwoWayPipeOpt == 0 && histogram == 0) {
237 hammer_get_cycle(&mirror.key_beg, &mirror.tid_beg);
241 * An additional argument overrides the beginning TID regardless
242 * of what mode we are in. This is not recommending if operating
246 mirror.tid_beg = strtoull(av[1], NULL, 0);
248 if (streaming == 0 || VerboseOpt >= 2) {
250 "Mirror-read: Mirror %016jx to %016jx",
251 (uintmax_t)mirror.tid_beg, (uintmax_t)mirror.tid_end);
253 fprintf(stderr, " (bulk= %ju)", (uintmax_t)estbytes);
254 fprintf(stderr, "\n");
257 if (mirror.key_beg.obj_id != (int64_t)HAMMER_MIN_OBJID) {
258 fprintf(stderr, "Mirror-read: Resuming at object %016jx\n",
259 (uintmax_t)mirror.key_beg.obj_id);
263 * Nothing to do if begin equals end.
265 if (mirror.tid_beg >= mirror.tid_end) {
266 if (streaming == 0 || VerboseOpt >= 2)
267 fprintf(stderr, "Mirror-read: No work to do\n");
276 * Write out bulk records
279 mirror.size = SERIALBUF_SIZE;
283 mirror.pfs_id = pfs.pfs_id;
284 mirror.shared_uuid = pfs.ondisk->shared_uuid;
285 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
286 score_printf(LINE3, "Mirror-read %s failed: %s",
287 filesystem, strerror(errno));
288 fprintf(stderr, "Mirror-read %s failed: %s\n",
289 filesystem, strerror(errno));
292 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
293 score_printf(LINE3, "Mirror-read %s fatal error %d",
294 filesystem, mirror.head.error);
296 "Mirror-read %s fatal error %d\n",
297 filesystem, mirror.head.error);
302 n = writebw(1, mirror.ubuf, mirror.count,
305 n = write(1, mirror.ubuf, mirror.count);
307 if (n != mirror.count) {
309 "Mirror-read %s failed: "
313 "Mirror-read %s failed: "
319 total_bytes += mirror.count;
320 if (streaming && VerboseOpt) {
322 "\rscan obj=%016jx tids=%016jx:%016jx %11jd",
323 (uintmax_t)mirror.key_cur.obj_id,
324 (uintmax_t)mirror.tid_beg,
325 (uintmax_t)mirror.tid_end,
326 (intmax_t)total_bytes);
329 } else if (streaming) {
331 "obj=%016jx tids=%016jx:%016jx %11jd",
332 (uintmax_t)mirror.key_cur.obj_id,
333 (uintmax_t)mirror.tid_beg,
334 (uintmax_t)mirror.tid_end,
335 (intmax_t)total_bytes);
337 mirror.key_beg = mirror.key_cur;
340 * Deal with time limit option
343 (unsigned)(time(NULL) - base_t) > (unsigned)TimeoutOpt) {
345 "Mirror-read %s interrupted by timer at"
348 (uintmax_t)mirror.key_cur.obj_id);
350 "Mirror-read %s interrupted by timer at"
353 (uintmax_t)mirror.key_cur.obj_id);
357 } while (mirror.count != 0);
360 if (streaming && VerboseOpt && sameline == 0) {
361 fprintf(stderr, "\n");
367 * Write out the termination sync record - only if not interrupted
369 if (interrupted == 0) {
371 write_mrecord(1, HAMMER_MREC_TYPE_SYNC,
372 &mrec_tmp, sizeof(mrec_tmp.sync));
374 write_mrecord(1, HAMMER_MREC_TYPE_IDLE,
375 &mrec_tmp, sizeof(mrec_tmp.sync));
380 * If the -2 option was given (automatic when doing mirror-copy),
381 * a two-way pipe is assumed and we expect a response mrec from
385 mrec = read_mrecord(0, &error, &pickup);
387 mrec->head.type != HAMMER_MREC_TYPE_UPDATE ||
388 mrec->head.rec_size != sizeof(mrec->update)) {
389 fprintf(stderr, "mirror_read: Did not get final "
390 "acknowledgement packet from target\n");
395 hammer_set_cycle(&mirror.key_cur,
397 fprintf(stderr, "Cyclefile %s updated for "
398 "continuation\n", CyclePath);
401 sync_tid = mrec->update.tid;
403 hammer_key_beg_init(&mirror.key_beg);
404 hammer_set_cycle(&mirror.key_beg, sync_tid);
406 "Cyclefile %s updated to 0x%016jx\n",
407 CyclePath, (uintmax_t)sync_tid);
411 } else if (CyclePath) {
412 /* NOTE! mirror.tid_beg cannot be updated */
413 fprintf(stderr, "Warning: cycle file (-c option) cannot be "
414 "fully updated unless you use mirror-copy\n");
415 hammer_set_cycle(&mirror.key_beg, mirror.tid_beg);
417 if (streaming && interrupted == 0) {
418 time_t t1 = time(NULL);
422 * Try to break down large bulk transfers into smaller ones
423 * so it can sync the transaction id on the slave. This
424 * way if we get interrupted a restart doesn't have to
425 * start from scratch.
427 if (streaming && histogram) {
428 if (histindex != histmax) {
429 if (VerboseOpt && VerboseOpt < 2 &&
431 fprintf(stderr, " (bulk incremental)");
438 if (VerboseOpt && streaming >= 0) {
439 fprintf(stderr, " W");
441 } else if (streaming >= 0) {
442 score_printf(LINE1, "Waiting");
444 pfs.ondisk->sync_end_tid = mirror.tid_end;
447 * Fake streaming mode when using a histogram to
448 * break up a mirror-read, do not wait on source.
451 } else if (ioctl(fd, HAMMERIOC_WAI_PSEUDOFS, &pfs) < 0) {
453 "Mirror-read %s: cannot stream: %s\n",
454 filesystem, strerror(errno));
456 "Mirror-read %s: cannot stream: %s\n",
457 filesystem, strerror(errno));
459 t2 = time(NULL) - t1;
460 if (t2 >= 0 && t2 < DelayOpt) {
462 fprintf(stderr, "\bD");
465 sleep(DelayOpt - t2);
468 fprintf(stderr, "\b ");
475 write_mrecord(1, HAMMER_MREC_TYPE_TERM,
476 &mrec_tmp, sizeof(mrec_tmp.sync));
480 fprintf(stderr, "Mirror-read %s succeeded\n", filesystem);
484 * What we are trying to do here is figure out how much data is
485 * going to be sent for the TID range and to break the TID range
486 * down into reasonably-sized slices (from the point of view of
487 * data sent) so a lost connection can restart at a reasonable
488 * place and not all the way back at the beginning.
490 * An entry's TID serves as the end_tid for the prior entry
491 * So we have to offset the calculation by 1 so that TID falls into
492 * the previous entry when populating entries.
494 * Because the transaction id space is bursty we need a relatively
495 * large number of buckets (like a million) to do a reasonable job
496 * for things like an initial bulk mirrors on a very large filesystem.
498 #define HIST_COUNT (1024 * 1024)
501 generate_histogram(int fd, const char *filesystem,
502 histogram_t *histogram_ary,
503 struct hammer_ioc_mirror_rw *mirror_base,
506 struct hammer_ioc_mirror_rw mirror;
507 union hammer_ioc_mrecord_any *mrec;
508 hammer_tid_t tid_beg;
509 hammer_tid_t tid_end;
521 mirror = *mirror_base;
522 tid_beg = mirror.tid_beg;
523 tid_end = mirror.tid_end;
524 mirror.head.flags |= HAMMER_IOC_MIRROR_NODATA;
526 if (*histogram_ary == NULL) {
527 *histogram_ary = malloc(sizeof(struct histogram) *
530 if (tid_beg >= tid_end)
534 tid_bytes = malloc(sizeof(*tid_bytes) * (HIST_COUNT + 2));
535 bzero(tid_bytes, sizeof(*tid_bytes) * (HIST_COUNT + 2));
538 fprintf(stderr, "Prescan to break up bulk transfer");
540 fprintf(stderr, " (%juMB chunks)",
541 (uintmax_t)(SplitupOpt / (1024 * 1024)));
542 fprintf(stderr, "\n");
546 * Note: (tid_beg,tid_end), range is inclusive of both beg & end.
548 * Note: Estimates can be off when the mirror is way behind due
556 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
557 fprintf(stderr, "Mirror-read %s failed: %s\n",
558 filesystem, strerror(errno));
561 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
563 "Mirror-read %s fatal error %d\n",
564 filesystem, mirror.head.error);
569 off += HAMMER_HEAD_DOALIGN(mrec->head.rec_size)) {
570 mrec = (void *)((char *)mirror.ubuf + off);
573 * We only care about general RECs and PASS
574 * records. We ignore SKIPs.
576 switch (mrec->head.type & HAMMER_MRECF_TYPE_LOMASK) {
577 case HAMMER_MREC_TYPE_REC:
578 case HAMMER_MREC_TYPE_PASS:
585 * Calculate for two indices, create_tid and
586 * delete_tid. Record data only applies to
589 * When tid is exactly on the boundary it really
590 * belongs to the previous entry because scans
591 * are inclusive of the ending entry.
593 tid = mrec->rec.leaf.base.delete_tid;
594 if (tid && tid >= tid_beg && tid <= tid_end) {
595 len = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
596 if (mrec->head.type ==
597 HAMMER_MREC_TYPE_REC) {
598 len -= HAMMER_HEAD_DOALIGN(
599 mrec->rec.leaf.data_len);
602 i = (tid - tid_beg) * HIST_COUNT /
604 tidx = tid_beg + i * (tid_end - tid_beg) /
606 if (tid == tidx && i)
608 assert(i >= 0 && i < HIST_COUNT);
614 tid = mrec->rec.leaf.base.create_tid;
615 if (tid && tid >= tid_beg && tid <= tid_end) {
616 len = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
617 if (mrec->head.type ==
618 HAMMER_MREC_TYPE_REC_NODATA) {
619 len += HAMMER_HEAD_DOALIGN(
620 mrec->rec.leaf.data_len);
622 i = (tid - tid_beg) * HIST_COUNT /
624 tidx = tid_beg + i * (tid_end - tid_beg) /
626 if (tid == tidx && i)
628 assert(i >= 0 && i < HIST_COUNT);
634 if (*repeatp == 0 && accum > SplitupOpt) {
635 if (VerboseOpt > 1) {
636 fprintf(stderr, ".");
640 score_printf(LINE2, "Prescan chunk %d", chunkno);
643 if (mirror.count == 0)
645 mirror.key_beg = mirror.key_cur;
649 * Reduce to SplitupOpt (default 4GB) chunks. This code may
650 * use up to two additional elements. Do the array in-place.
652 * Inefficient degenerate cases can occur if we do not accumulate
653 * at least the requested split amount, so error on the side of
657 (*histogram_ary)[res].tid = tid_beg;
658 (*histogram_ary)[res].bytes = tid_bytes[0];
659 for (i = 1; i < HIST_COUNT; ++i) {
660 if ((*histogram_ary)[res].bytes >= SplitupOpt) {
662 (*histogram_ary)[res].tid = tid_beg +
663 i * (tid_end - tid_beg) /
665 (*histogram_ary)[res].bytes = 0;
668 (*histogram_ary)[res].bytes += tid_bytes[i];
671 (*histogram_ary)[res].tid = tid_end;
672 (*histogram_ary)[res].bytes = -1;
676 fprintf(stderr, "\n"); /* newline after ... */
677 score_printf(LINE3, "Prescan %d chunks, total %ju MBytes",
678 res, (uintmax_t)total / (1024 * 1024));
679 fprintf(stderr, "Prescan %d chunks, total %ju MBytes (",
680 res, (uintmax_t)total / (1024 * 1024));
681 for (i = 0; i < res && i < 3; ++i) {
683 fprintf(stderr, ", ");
684 fprintf(stderr, "%ju",
685 (uintmax_t)(*histogram_ary)[i].bytes);
688 fprintf(stderr, ", ...");
689 fprintf(stderr, ")\n");
691 assert(res <= HIST_COUNT);
699 create_pfs(const char *filesystem, uuid_t *s_uuid)
701 if (ForceYesOpt == 1) {
702 fprintf(stderr, "PFS slave %s does not exist. "
703 "Auto create new slave PFS!\n", filesystem);
706 fprintf(stderr, "PFS slave %s does not exist.\n"
707 "Do you want to create a new slave PFS? (yes|no) ",
711 fprintf(stderr, "Aborting operation\n");
717 char *shared_uuid = NULL;
718 uuid_to_string(s_uuid, &shared_uuid, &status);
721 asprintf(&cmd, "/sbin/hammer pfs-slave '%s' shared-uuid=%s 1>&2",
722 filesystem, shared_uuid);
726 fprintf(stderr, "Failed to alloc memory\n");
729 if (system(cmd) != 0) {
730 fprintf(stderr, "Failed to create PFS\n");
736 * Pipe the mirroring data stream on stdin to the HAMMER VFS, adding
737 * some additional packet types to negotiate TID ranges and to verify
738 * completion. The HAMMER VFS does most of the work.
740 * It is important to note that the mirror.key_{beg,end} range must
741 * match the ranged used by the original. For now both sides use
742 * range the entire key space.
744 * It is even more important that the records in the stream conform
745 * to the TID range also supplied in the stream. The HAMMER VFS will
746 * use the REC, PASS, and SKIP record types to track the portions of
747 * the B-Tree being scanned in order to be able to proactively delete
748 * records on the target within those active areas that are not mentioned
751 * The mirror.key_cur field is used by the VFS to do this tracking. It
752 * must be initialized to key_beg but then is persistently updated by
753 * the HAMMER VFS on each successive ioctl() call. If you blow up this
754 * field you will blow up the mirror target, possibly to the point of
755 * deleting everything. As a safety measure the HAMMER VFS simply marks
756 * the records that the source has destroyed as deleted on the target,
757 * and normal pruning operations will deal with their final disposition
758 * at some later time.
761 hammer_cmd_mirror_write(char **av, int ac)
763 struct hammer_ioc_mirror_rw mirror;
765 char *buf = malloc(SERIALBUF_SIZE);
766 struct hammer_ioc_pseudofs_rw pfs;
767 struct hammer_ioc_mrecord_head pickup;
768 struct hammer_ioc_synctid synctid;
769 union hammer_ioc_mrecord_any mrec_tmp;
770 hammer_ioc_mrecord_any_t mrec;
779 hammer_check_restrict(filesystem);
781 pickup.signature = 0;
785 bzero(&mirror, sizeof(mirror));
786 hammer_key_beg_init(&mirror.key_beg);
787 hammer_key_end_init(&mirror.key_end);
788 mirror.key_end = mirror.key_beg;
791 * Read initial packet
793 mrec = read_mrecord(0, &error, &pickup);
796 fprintf(stderr, "validate_mrec_header: short read\n");
802 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
806 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
807 fprintf(stderr, "validate_mrec_header: did not get expected "
808 "PFSD record type\n");
811 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
812 fprintf(stderr, "validate_mrec_header: unexpected payload "
818 * Create slave PFS if it doesn't yet exist
820 if (lstat(filesystem, &st) != 0) {
821 create_pfs(filesystem, &mrec->pfs.pfsd.shared_uuid);
826 fd = getpfs(&pfs, filesystem);
829 * In two-way mode the target writes out a PFS packet first.
830 * The source uses our tid_end as its tid_beg by default,
831 * picking up where it left off.
835 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
836 if (mirror.tid_beg < mrec_tmp.pfs.pfsd.sync_beg_tid)
837 mirror.tid_beg = mrec_tmp.pfs.pfsd.sync_beg_tid;
838 mirror.tid_end = mrec_tmp.pfs.pfsd.sync_end_tid;
839 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
840 &mrec_tmp, sizeof(mrec_tmp.pfs));
844 * Read and process the PFS header. The source informs us of
845 * the TID range the stream represents.
847 n = validate_mrec_header(fd, 0, 1, pfs.pfs_id, &pickup,
848 &mirror.tid_beg, &mirror.tid_end);
849 if (n < 0) { /* got TERM record */
856 mirror.size = SERIALBUF_SIZE;
859 * Read and process bulk records (REC, PASS, and SKIP types).
861 * On your life, do NOT mess with mirror.key_cur or your mirror
862 * target may become history.
866 mirror.pfs_id = pfs.pfs_id;
867 mirror.shared_uuid = pfs.ondisk->shared_uuid;
868 mirror.size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
869 if (mirror.size <= 0)
871 if (ioctl(fd, HAMMERIOC_MIRROR_WRITE, &mirror) < 0) {
872 fprintf(stderr, "Mirror-write %s failed: %s\n",
873 filesystem, strerror(errno));
876 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
878 "Mirror-write %s fatal error %d\n",
879 filesystem, mirror.head.error);
883 if (mirror.head.flags & HAMMER_IOC_HEAD_INTR) {
885 "Mirror-write %s interrupted by timer at"
888 mirror.key_cur.obj_id);
895 * Read and process the termination sync record.
897 mrec = read_mrecord(0, &error, &pickup);
899 if (mrec && mrec->head.type == HAMMER_MREC_TYPE_TERM) {
900 fprintf(stderr, "Mirror-write: received termination request\n");
908 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
909 mrec->head.type != HAMMER_MREC_TYPE_IDLE) ||
910 mrec->head.rec_size != sizeof(mrec->sync)) {
911 fprintf(stderr, "Mirror-write %s: Did not get termination "
912 "sync record, or rec_size is wrong rt=%d\n",
914 (mrec ? (int)mrec->head.type : -1));
919 * Update the PFS info on the target so the user has visibility
920 * into the new snapshot, and sync the target filesystem.
922 if (mrec->head.type == HAMMER_MREC_TYPE_SYNC) {
923 update_pfs_snapshot(fd, mirror.tid_end, pfs.pfs_id);
925 bzero(&synctid, sizeof(synctid));
926 synctid.op = HAMMER_SYNCTID_SYNC2;
927 ioctl(fd, HAMMERIOC_SYNCTID, &synctid);
929 if (VerboseOpt >= 2) {
930 fprintf(stderr, "Mirror-write %s: succeeded\n",
939 * Report back to the originator.
942 mrec_tmp.update.tid = mirror.tid_end;
943 write_mrecord(1, HAMMER_MREC_TYPE_UPDATE,
944 &mrec_tmp, sizeof(mrec_tmp.update));
946 printf("Source can update synctid to 0x%016jx\n",
947 (uintmax_t)mirror.tid_end);
954 hammer_cmd_mirror_dump(char **av, int ac)
956 char *buf = malloc(SERIALBUF_SIZE);
957 struct hammer_ioc_mrecord_head pickup;
958 hammer_ioc_mrecord_any_t mrec;
965 if (ac == 1 && strcmp(*av, "header") == 0)
971 * Read and process the PFS header
973 pickup.signature = 0;
976 mrec = read_mrecord(0, &error, &pickup);
979 * Dump the PFS header. mirror-dump takes its input from the output
980 * of a mirror-read so getpfs() can't be used to get a fd to be passed
983 if (header_only && mrec != NULL) {
984 dump_pfsd(&mrec->pfs.pfsd, -1);
993 * Read and process bulk records
996 size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
1000 while (offset < size) {
1001 mrec = (void *)((char *)buf + offset);
1002 bytes = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
1003 if (offset + bytes > size) {
1004 fprintf(stderr, "Misaligned record\n");
1008 switch(mrec->head.type & HAMMER_MRECF_TYPE_MASK) {
1009 case HAMMER_MREC_TYPE_REC_BADCRC:
1010 case HAMMER_MREC_TYPE_REC:
1011 printf("Record lo=%08x obj=%016jx key=%016jx "
1013 mrec->rec.leaf.base.localization,
1014 (uintmax_t)mrec->rec.leaf.base.obj_id,
1015 (uintmax_t)mrec->rec.leaf.base.key,
1016 mrec->rec.leaf.base.rec_type,
1017 mrec->rec.leaf.base.obj_type);
1018 if (mrec->head.type ==
1019 HAMMER_MREC_TYPE_REC_BADCRC) {
1020 printf(" (BAD CRC)");
1023 printf(" tids %016jx:%016jx data=%d\n",
1024 (uintmax_t)mrec->rec.leaf.base.create_tid,
1025 (uintmax_t)mrec->rec.leaf.base.delete_tid,
1026 mrec->rec.leaf.data_len);
1028 case HAMMER_MREC_TYPE_PASS:
1029 printf("Pass lo=%08x obj=%016jx key=%016jx "
1030 "rt=%02x ot=%02x\n",
1031 mrec->rec.leaf.base.localization,
1032 (uintmax_t)mrec->rec.leaf.base.obj_id,
1033 (uintmax_t)mrec->rec.leaf.base.key,
1034 mrec->rec.leaf.base.rec_type,
1035 mrec->rec.leaf.base.obj_type);
1036 printf(" tids %016jx:%016jx data=%d\n",
1037 (uintmax_t)mrec->rec.leaf.base.create_tid,
1038 (uintmax_t)mrec->rec.leaf.base.delete_tid,
1039 mrec->rec.leaf.data_len);
1041 case HAMMER_MREC_TYPE_SKIP:
1042 printf("Skip lo=%08x obj=%016jx key=%016jx rt=%02x to\n"
1043 " lo=%08x obj=%016jx key=%016jx rt=%02x\n",
1044 mrec->skip.skip_beg.localization,
1045 (uintmax_t)mrec->skip.skip_beg.obj_id,
1046 (uintmax_t)mrec->skip.skip_beg.key,
1047 mrec->skip.skip_beg.rec_type,
1048 mrec->skip.skip_end.localization,
1049 (uintmax_t)mrec->skip.skip_end.obj_id,
1050 (uintmax_t)mrec->skip.skip_end.key,
1051 mrec->skip.skip_end.rec_type);
1060 * Read and process the termination sync record.
1062 mrec = read_mrecord(0, &error, &pickup);
1064 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
1065 mrec->head.type != HAMMER_MREC_TYPE_IDLE)) {
1066 fprintf(stderr, "Mirror-dump: Did not get termination "
1072 * Continue with more batches until EOF.
1074 mrec = read_mrecord(0, &error, &pickup);
1083 hammer_cmd_mirror_copy(char **av, int ac, int streaming)
1088 const char *xav[32];
1090 char *sh, *user, *host, *rfs;
1097 signal(SIGPIPE, SIG_IGN);
1100 if (pipe(fds) < 0) {
1108 if ((pid1 = fork()) == 0) {
1109 signal(SIGPIPE, SIG_DFL);
1114 if ((rfs = strchr(av[0], ':')) != NULL) {
1117 if((sh = getenv("HAMMER_RSH")) == NULL)
1125 if ((host = strchr(av[0], '@')) != NULL) {
1126 user = strndup( av[0], (host++ - av[0]));
1127 host = strndup( host, (rfs++ - host));
1133 host = strndup( av[0], (rfs++ - av[0]));
1141 xav[xac++] = SshPort;
1144 xav[xac++] = "hammer";
1146 switch(VerboseOpt) {
1156 xav[xac++] = "-vvv";
1164 snprintf(tbuf, sizeof(tbuf), "%d", TimeoutOpt);
1168 if (SplitupOptStr) {
1170 xav[xac++] = SplitupOptStr;
1173 xav[xac++] = "mirror-read-stream";
1175 xav[xac++] = "mirror-read";
1178 execvp(*xav, (void *)xav);
1180 hammer_cmd_mirror_read(av, 1, streaming);
1190 if ((pid2 = fork()) == 0) {
1191 signal(SIGPIPE, SIG_DFL);
1196 if ((rfs = strchr(av[1], ':')) != NULL) {
1199 if((sh = getenv("HAMMER_RSH")) == NULL)
1207 if ((host = strchr(av[1], '@')) != NULL) {
1208 user = strndup( av[1], (host++ - av[1]));
1209 host = strndup( host, (rfs++ - host));
1215 host = strndup( av[1], (rfs++ - av[1]));
1222 xav[xac++] = SshPort;
1225 xav[xac++] = "hammer";
1227 switch(VerboseOpt) {
1237 xav[xac++] = "-vvv";
1244 xav[xac++] = "mirror-write";
1247 execvp(*xav, (void *)xav);
1249 hammer_cmd_mirror_write(av + 1, 1);
1258 while (waitpid(pid1, NULL, 0) <= 0)
1260 while (waitpid(pid2, NULL, 0) <= 0)
1264 * If the link is lost restart
1268 fprintf(stderr, "\nLost Link\n");
1271 sleep(15 + DelayOpt);
1278 * Read and return multiple mrecords
1281 read_mrecords(int fd, char *buf, u_int size, hammer_ioc_mrecord_head_t pickup)
1283 hammer_ioc_mrecord_any_t mrec;
1291 while (size - count >= HAMMER_MREC_HEADSIZE) {
1293 * Cached the record header in case we run out of buffer
1297 if (pickup->signature == 0) {
1298 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
1299 i = read(fd, (char *)pickup + n,
1300 HAMMER_MREC_HEADSIZE - n);
1306 if (n != HAMMER_MREC_HEADSIZE) {
1307 fprintf(stderr, "read_mrecords: short read on pipe\n");
1310 if (pickup->signature != HAMMER_IOC_MIRROR_SIGNATURE) {
1311 fprintf(stderr, "read_mrecords: malformed record on pipe, "
1316 if (pickup->rec_size < HAMMER_MREC_HEADSIZE ||
1317 pickup->rec_size > sizeof(*mrec) + HAMMER_XBUFSIZE) {
1318 fprintf(stderr, "read_mrecords: malformed record on pipe, "
1319 "illegal rec_size\n");
1324 * Stop if we have insufficient space for the record and data.
1326 bytes = HAMMER_HEAD_DOALIGN(pickup->rec_size);
1327 if (size - count < bytes)
1331 * Stop if the record type is not a REC, SKIP, or PASS,
1332 * which are the only types the ioctl supports. Other types
1333 * are used only by the userland protocol.
1337 type = pickup->type & HAMMER_MRECF_TYPE_LOMASK;
1338 if (type != HAMMER_MREC_TYPE_PFSD &&
1339 type != HAMMER_MREC_TYPE_REC &&
1340 type != HAMMER_MREC_TYPE_SKIP &&
1341 type != HAMMER_MREC_TYPE_PASS) {
1346 * Read the remainder and clear the pickup signature.
1348 for (n = HAMMER_MREC_HEADSIZE; n < bytes; n += i) {
1349 i = read(fd, buf + count + n, bytes - n);
1354 fprintf(stderr, "read_mrecords: short read on pipe\n");
1358 bcopy(pickup, buf + count, HAMMER_MREC_HEADSIZE);
1359 pickup->signature = 0;
1361 mrec = (void *)(buf + count);
1364 * Validate the completed record
1366 if (mrec->head.rec_crc !=
1367 crc32((char *)mrec + HAMMER_MREC_CRCOFF,
1368 mrec->head.rec_size - HAMMER_MREC_CRCOFF)) {
1369 fprintf(stderr, "read_mrecords: malformed record "
1370 "on pipe, bad crc\n");
1375 * If its a B-Tree record validate the data crc.
1377 * NOTE: If the VFS passes us an explicitly errorde mrec
1378 * we just pass it through.
1380 type = mrec->head.type & HAMMER_MRECF_TYPE_MASK;
1382 if (type == HAMMER_MREC_TYPE_REC) {
1383 if (mrec->head.rec_size <
1384 sizeof(mrec->rec) + mrec->rec.leaf.data_len) {
1386 "read_mrecords: malformed record on "
1387 "pipe, illegal element data_len\n");
1390 if (mrec->rec.leaf.data_len &&
1391 mrec->rec.leaf.data_offset &&
1392 hammer_crc_test_leaf(&mrec->rec + 1, &mrec->rec.leaf) == 0) {
1394 "read_mrecords: data_crc did not "
1395 "match data! obj=%016jx key=%016jx\n",
1396 (uintmax_t)mrec->rec.leaf.base.obj_id,
1397 (uintmax_t)mrec->rec.leaf.base.key);
1399 "continuing, but there are problems\n");
1408 * Read and return a single mrecord.
1411 hammer_ioc_mrecord_any_t
1412 read_mrecord(int fdin, int *errorp, hammer_ioc_mrecord_head_t pickup)
1414 hammer_ioc_mrecord_any_t mrec;
1415 struct hammer_ioc_mrecord_head mrechd;
1420 if (pickup && pickup->type != 0) {
1422 pickup->signature = 0;
1424 n = HAMMER_MREC_HEADSIZE;
1427 * Read in the PFSD header from the sender.
1429 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
1430 i = read(fdin, (char *)&mrechd + n, HAMMER_MREC_HEADSIZE - n);
1435 *errorp = 0; /* EOF */
1438 if (n != HAMMER_MREC_HEADSIZE) {
1439 fprintf(stderr, "short read of mrecord header\n");
1444 if (mrechd.signature != HAMMER_IOC_MIRROR_SIGNATURE) {
1445 fprintf(stderr, "read_mrecord: bad signature\n");
1449 bytes = HAMMER_HEAD_DOALIGN(mrechd.rec_size);
1450 assert(bytes >= sizeof(mrechd));
1451 mrec = malloc(bytes);
1452 mrec->head = mrechd;
1455 i = read(fdin, (char *)mrec + n, bytes - n);
1461 fprintf(stderr, "read_mrecord: short read on payload\n");
1465 if (mrec->head.rec_crc !=
1466 crc32((char *)mrec + HAMMER_MREC_CRCOFF,
1467 mrec->head.rec_size - HAMMER_MREC_CRCOFF)) {
1468 fprintf(stderr, "read_mrecord: bad CRC\n");
1478 write_mrecord(int fdout, uint32_t type, hammer_ioc_mrecord_any_t mrec,
1481 char zbuf[HAMMER_HEAD_ALIGN];
1484 pad = HAMMER_HEAD_DOALIGN(bytes) - bytes;
1486 assert(bytes >= (int)sizeof(mrec->head));
1487 bzero(&mrec->head, sizeof(mrec->head));
1488 mrec->head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
1489 mrec->head.type = type;
1490 mrec->head.rec_size = bytes;
1491 mrec->head.rec_crc = crc32((char *)mrec + HAMMER_MREC_CRCOFF,
1492 bytes - HAMMER_MREC_CRCOFF);
1493 if (write(fdout, mrec, bytes) != bytes) {
1494 fprintf(stderr, "write_mrecord: error %d (%s)\n",
1495 errno, strerror(errno));
1500 if (write(fdout, zbuf, pad) != pad) {
1501 fprintf(stderr, "write_mrecord: error %d (%s)\n",
1502 errno, strerror(errno));
1509 * Generate a mirroring header with the pfs information of the
1510 * originating filesytem.
1513 generate_mrec_header(int fd, int pfs_id,
1514 union hammer_ioc_mrecord_any *mrec_tmp)
1516 struct hammer_ioc_pseudofs_rw pfs;
1518 bzero(&pfs, sizeof(pfs));
1519 bzero(mrec_tmp, sizeof(*mrec_tmp));
1520 pfs.pfs_id = pfs_id;
1521 pfs.ondisk = &mrec_tmp->pfs.pfsd;
1522 pfs.bytes = sizeof(mrec_tmp->pfs.pfsd);
1523 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1524 fprintf(stderr, "Mirror-read: not a HAMMER fs/pseudofs!\n");
1527 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1528 fprintf(stderr, "Mirror-read: HAMMER PFS version mismatch!\n");
1531 mrec_tmp->pfs.version = pfs.version;
1535 * Validate the pfs information from the originating filesystem
1536 * against the target filesystem. shared_uuid must match.
1538 * return -1 if we got a TERM record
1541 validate_mrec_header(int fd, int fdin, int is_target, int pfs_id,
1542 struct hammer_ioc_mrecord_head *pickup,
1543 hammer_tid_t *tid_begp, hammer_tid_t *tid_endp)
1545 struct hammer_ioc_pseudofs_rw pfs;
1546 struct hammer_pseudofs_data pfsd;
1547 hammer_ioc_mrecord_any_t mrec;
1551 * Get the PFSD info from the target filesystem.
1553 bzero(&pfs, sizeof(pfs));
1554 bzero(&pfsd, sizeof(pfsd));
1555 pfs.pfs_id = pfs_id;
1557 pfs.bytes = sizeof(pfsd);
1558 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1559 fprintf(stderr, "mirror-write: not a HAMMER fs/pseudofs!\n");
1562 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1563 fprintf(stderr, "mirror-write: HAMMER PFS version mismatch!\n");
1567 mrec = read_mrecord(fdin, &error, pickup);
1570 fprintf(stderr, "validate_mrec_header: short read\n");
1573 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
1578 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
1579 fprintf(stderr, "validate_mrec_header: did not get expected "
1580 "PFSD record type\n");
1583 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
1584 fprintf(stderr, "validate_mrec_header: unexpected payload "
1588 if (mrec->pfs.version != pfs.version) {
1589 fprintf(stderr, "validate_mrec_header: Version mismatch\n");
1594 * Whew. Ok, is the read PFS info compatible with the target?
1596 if (bcmp(&mrec->pfs.pfsd.shared_uuid, &pfsd.shared_uuid,
1597 sizeof(pfsd.shared_uuid)) != 0) {
1599 "mirror-write: source and target have "
1600 "different shared-uuid's!\n");
1603 if (is_target && hammer_is_pfs_master(&pfsd)) {
1604 fprintf(stderr, "mirror-write: target must be in slave mode\n");
1608 *tid_begp = mrec->pfs.pfsd.sync_beg_tid;
1610 *tid_endp = mrec->pfs.pfsd.sync_end_tid;
1616 update_pfs_snapshot(int fd, hammer_tid_t snapshot_tid, int pfs_id)
1618 struct hammer_ioc_pseudofs_rw pfs;
1619 struct hammer_pseudofs_data pfsd;
1621 bzero(&pfs, sizeof(pfs));
1622 bzero(&pfsd, sizeof(pfsd));
1623 pfs.pfs_id = pfs_id;
1625 pfs.bytes = sizeof(pfsd);
1626 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1627 perror("update_pfs_snapshot (read)");
1630 if (pfsd.sync_end_tid != snapshot_tid) {
1631 pfsd.sync_end_tid = snapshot_tid;
1632 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) != 0) {
1633 perror("update_pfs_snapshot (rewrite)");
1636 if (VerboseOpt >= 2) {
1638 "Mirror-write: Completed, updated snapshot "
1640 (uintmax_t)snapshot_tid);
1647 * Bandwidth-limited write in chunks
1651 writebw(int fd, const void *buf, size_t nbytes,
1652 uint64_t *bwcount, struct timeval *tv1)
1663 if (*bwcount + nbytes > BandwidthOpt)
1664 n = BandwidthOpt - *bwcount;
1668 r = write(fd, buf, n);
1672 buf = (const char *)buf + r;
1677 if (*bwcount >= BandwidthOpt) {
1678 gettimeofday(&tv2, NULL);
1679 usec = (int)(tv2.tv_sec - tv1->tv_sec) * 1000000 +
1680 (int)(tv2.tv_usec - tv1->tv_usec);
1681 if (usec >= 0 && usec < 1000000)
1682 usleep(1000000 - usec);
1683 gettimeofday(tv1, NULL);
1684 *bwcount -= BandwidthOpt;
1691 * Get a yes or no answer from the terminal. The program may be run as
1692 * part of a two-way pipe so we cannot use stdin for this operation.
1701 fp = fopen("/dev/tty", "r");
1703 fprintf(stderr, "No terminal for response\n");
1707 while (fgets(buf, sizeof(buf), fp) != NULL) {
1708 if (buf[0] == 'y' || buf[0] == 'Y') {
1712 if (buf[0] == 'n' || buf[0] == 'N') {
1716 fprintf(stderr, "Response not understood\n");
1724 mirror_usage(int code)
1727 "hammer mirror-read <filesystem> [begin-tid]\n"
1728 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
1729 "hammer mirror-write <filesystem>\n"
1730 "hammer mirror-dump [header]\n"
1731 "hammer mirror-copy [[user@]host:]<filesystem>"
1732 " [[user@]host:]<filesystem>\n"
1733 "hammer mirror-stream [[user@]host:]<filesystem>"
1734 " [[user@]host:]<filesystem>\n"