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 const char *ScoreBoardFile;
49 const char *RestrictTarget;
51 static int read_mrecords(int fd, char *buf, u_int size,
52 hammer_ioc_mrecord_head_t pickup);
53 static int generate_histogram(int fd, const char *filesystem,
54 histogram_t *histogram_ary,
55 struct hammer_ioc_mirror_rw *mirror_base,
57 static hammer_ioc_mrecord_any_t read_mrecord(int fdin, int *errorp,
58 hammer_ioc_mrecord_head_t pickup);
59 static void write_mrecord(int fdout, uint32_t type,
60 hammer_ioc_mrecord_any_t mrec, int bytes);
61 static void generate_mrec_header(int fd, int pfs_id,
62 union hammer_ioc_mrecord_any *mrec_tmp);
63 static int validate_mrec_header(int fd, int fdin, int is_target, int pfs_id,
64 struct hammer_ioc_mrecord_head *pickup,
65 hammer_tid_t *tid_begp, hammer_tid_t *tid_endp);
66 static void update_pfs_snapshot(int fd, hammer_tid_t snapshot_tid, int pfs_id);
67 static ssize_t writebw(int fd, const void *buf, size_t nbytes,
68 uint64_t *bwcount, struct timeval *tv1);
69 static int getyntty(void);
70 static void score_printf(size_t i, size_t w, const char *ctl, ...);
71 static void hammer_check_restrict(const char *filesystem);
72 static void mirror_usage(int code);
75 * Generate a mirroring data stream from the specific source over the
76 * entire key range, but restricted to the specified transaction range.
78 * The HAMMER VFS does most of the work, we add a few new mrecord
79 * types to negotiate the TID ranges and verify that the entire
80 * stream made it to the destination.
82 * streaming will be 0 for mirror-read, 1 for mirror-stream. The code will
83 * set up a fake value of -1 when running the histogram for mirror-read.
86 hammer_cmd_mirror_read(char **av, int ac, int streaming)
88 struct hammer_ioc_mirror_rw mirror;
89 struct hammer_ioc_pseudofs_rw pfs;
90 union hammer_ioc_mrecord_any mrec_tmp;
91 struct hammer_ioc_mrecord_head pickup;
92 hammer_ioc_mrecord_any_t mrec;
93 hammer_tid_t sync_tid;
94 histogram_t histogram_ary;
96 char *buf = malloc(SERIALBUF_SIZE);
108 time_t base_t = time(NULL);
113 if (ac == 0 || ac > 2)
116 hammer_check_restrict(filesystem);
118 pickup.signature = 0;
123 histogram_ary = NULL;
127 bzero(&mirror, sizeof(mirror));
128 hammer_key_beg_init(&mirror.key_beg);
129 hammer_key_end_init(&mirror.key_end);
131 fd = getpfs(&pfs, filesystem);
134 score_printf(LINE1, "Running");
136 if (streaming >= 0 && VerboseOpt && VerboseOpt < 2) {
137 fprintf(stderr, "%cRunning \b\b", (sameline ? '\r' : '\n'));
143 gettimeofday(&bwtv, NULL);
147 * Send initial header for the purpose of determining the
150 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
151 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
152 &mrec_tmp, sizeof(mrec_tmp.pfs));
155 * In 2-way mode the target will send us a PFS info packet
156 * first. Use the target's current snapshot TID as our default
161 n = validate_mrec_header(fd, 0, 0, pfs.pfs_id, &pickup,
162 NULL, &mirror.tid_beg);
163 if (n < 0) { /* got TERM record */
170 } else if (streaming && histogram) {
171 mirror.tid_beg = histogram_ary[histindex].tid + 1;
177 * Write out the PFS header, tid_beg will be updated if our PFS
178 * has a larger begin sync. tid_end is set to the latest source
179 * TID whos flush cycle has completed.
181 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
182 if (mirror.tid_beg < mrec_tmp.pfs.pfsd.sync_beg_tid)
183 mirror.tid_beg = mrec_tmp.pfs.pfsd.sync_beg_tid;
184 mirror.tid_end = mrec_tmp.pfs.pfsd.sync_end_tid;
186 mirror.size = SERIALBUF_SIZE;
187 mirror.pfs_id = pfs.pfs_id;
188 mirror.shared_uuid = pfs.ondisk->shared_uuid;
191 * XXX If the histogram is exhausted and the TID delta is large
192 * the stream might have been offline for a while and is
193 * now picking it up again. Do another histogram.
196 if (streaming && histogram && histindex == histend) {
197 if (mirror.tid_end - mirror.tid_beg > BULK_MINIMUM)
203 * Initial bulk startup control, try to do some incremental
204 * mirroring in order to allow the stream to be killed and
205 * restarted without having to start over.
207 if (histogram == 0 && BulkOpt == 0) {
208 if (VerboseOpt && repeat == 0) {
209 fprintf(stderr, "\n");
212 histmax = generate_histogram(fd, filesystem,
213 &histogram_ary, &mirror,
219 * Just stream the histogram, then stop
225 if (streaming && histogram) {
227 mirror.tid_end = histogram_ary[histindex].tid;
228 estbytes = histogram_ary[histindex-1].bytes;
229 mrec_tmp.pfs.pfsd.sync_end_tid = mirror.tid_end;
234 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
235 &mrec_tmp, sizeof(mrec_tmp.pfs));
238 * A cycle file overrides the beginning TID only if we are
239 * not operating in two-way or histogram mode.
241 if (TwoWayPipeOpt == 0 && histogram == 0) {
242 hammer_get_cycle(&mirror.key_beg, &mirror.tid_beg);
246 * An additional argument overrides the beginning TID regardless
247 * of what mode we are in. This is not recommending if operating
251 mirror.tid_beg = strtoull(av[1], NULL, 0);
253 if (streaming == 0 || VerboseOpt >= 2) {
255 "Mirror-read: Mirror %016jx to %016jx",
256 (uintmax_t)mirror.tid_beg, (uintmax_t)mirror.tid_end);
258 fprintf(stderr, " (bulk= %ju)", (uintmax_t)estbytes);
259 fprintf(stderr, "\n");
262 if (mirror.key_beg.obj_id != (int64_t)HAMMER_MIN_OBJID) {
263 fprintf(stderr, "Mirror-read: Resuming at object %016jx\n",
264 (uintmax_t)mirror.key_beg.obj_id);
268 * Nothing to do if begin equals end.
270 if (mirror.tid_beg >= mirror.tid_end) {
271 if (streaming == 0 || VerboseOpt >= 2)
272 fprintf(stderr, "Mirror-read: No work to do\n");
281 * Write out bulk records
284 mirror.size = SERIALBUF_SIZE;
288 mirror.pfs_id = pfs.pfs_id;
289 mirror.shared_uuid = pfs.ondisk->shared_uuid;
290 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
291 score_printf(LINE3, "Mirror-read %s failed: %s",
292 filesystem, strerror(errno));
293 fprintf(stderr, "Mirror-read %s failed: %s\n",
294 filesystem, strerror(errno));
297 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
298 score_printf(LINE3, "Mirror-read %s fatal error %d",
299 filesystem, mirror.head.error);
301 "Mirror-read %s fatal error %d\n",
302 filesystem, mirror.head.error);
307 n = writebw(1, mirror.ubuf, mirror.count,
310 n = write(1, mirror.ubuf, mirror.count);
312 if (n != mirror.count) {
314 "Mirror-read %s failed: "
318 "Mirror-read %s failed: "
324 total_bytes += mirror.count;
325 if (streaming && VerboseOpt) {
327 "\rscan obj=%016jx tids=%016jx:%016jx %11jd",
328 (uintmax_t)mirror.key_cur.obj_id,
329 (uintmax_t)mirror.tid_beg,
330 (uintmax_t)mirror.tid_end,
331 (intmax_t)total_bytes);
334 } else if (streaming) {
336 "obj=%016jx tids=%016jx:%016jx %11jd",
337 (uintmax_t)mirror.key_cur.obj_id,
338 (uintmax_t)mirror.tid_beg,
339 (uintmax_t)mirror.tid_end,
340 (intmax_t)total_bytes);
342 mirror.key_beg = mirror.key_cur;
345 * Deal with time limit option
348 (unsigned)(time(NULL) - base_t) > (unsigned)TimeoutOpt) {
350 "Mirror-read %s interrupted by timer at"
353 (uintmax_t)mirror.key_cur.obj_id);
355 "Mirror-read %s interrupted by timer at"
358 (uintmax_t)mirror.key_cur.obj_id);
362 } while (mirror.count != 0);
365 if (streaming && VerboseOpt && sameline == 0) {
366 fprintf(stderr, "\n");
372 * Write out the termination sync record - only if not interrupted
374 if (interrupted == 0) {
376 write_mrecord(1, HAMMER_MREC_TYPE_SYNC,
377 &mrec_tmp, sizeof(mrec_tmp.sync));
379 write_mrecord(1, HAMMER_MREC_TYPE_IDLE,
380 &mrec_tmp, sizeof(mrec_tmp.sync));
385 * If the -2 option was given (automatic when doing mirror-copy),
386 * a two-way pipe is assumed and we expect a response mrec from
390 mrec = read_mrecord(0, &error, &pickup);
392 mrec->head.type != HAMMER_MREC_TYPE_UPDATE ||
393 mrec->head.rec_size != sizeof(mrec->update)) {
394 fprintf(stderr, "mirror_read: Did not get final "
395 "acknowledgement packet from target\n");
400 hammer_set_cycle(&mirror.key_cur,
402 fprintf(stderr, "Cyclefile %s updated for "
403 "continuation\n", CyclePath);
406 sync_tid = mrec->update.tid;
408 hammer_key_beg_init(&mirror.key_beg);
409 hammer_set_cycle(&mirror.key_beg, sync_tid);
411 "Cyclefile %s updated to 0x%016jx\n",
412 CyclePath, (uintmax_t)sync_tid);
416 } else if (CyclePath) {
417 /* NOTE! mirror.tid_beg cannot be updated */
418 fprintf(stderr, "Warning: cycle file (-c option) cannot be "
419 "fully updated unless you use mirror-copy\n");
420 hammer_set_cycle(&mirror.key_beg, mirror.tid_beg);
422 if (streaming && interrupted == 0) {
423 time_t t1 = time(NULL);
427 * Try to break down large bulk transfers into smaller ones
428 * so it can sync the transaction id on the slave. This
429 * way if we get interrupted a restart doesn't have to
430 * start from scratch.
432 if (streaming && histogram) {
433 if (histindex != histmax) {
434 if (VerboseOpt && VerboseOpt < 2 &&
436 fprintf(stderr, " (bulk incremental)");
443 if (VerboseOpt && streaming >= 0) {
444 fprintf(stderr, " W");
446 } else if (streaming >= 0) {
447 score_printf(LINE1, "Waiting");
449 pfs.ondisk->sync_end_tid = mirror.tid_end;
452 * Fake streaming mode when using a histogram to
453 * break up a mirror-read, do not wait on source.
456 } else if (ioctl(fd, HAMMERIOC_WAI_PSEUDOFS, &pfs) < 0) {
458 "Mirror-read %s: cannot stream: %s\n",
459 filesystem, strerror(errno));
461 "Mirror-read %s: cannot stream: %s\n",
462 filesystem, strerror(errno));
464 t2 = time(NULL) - t1;
465 if (t2 >= 0 && t2 < DelayOpt) {
467 fprintf(stderr, "\bD");
470 sleep(DelayOpt - t2);
473 fprintf(stderr, "\b ");
480 write_mrecord(1, HAMMER_MREC_TYPE_TERM,
481 &mrec_tmp, sizeof(mrec_tmp.sync));
485 fprintf(stderr, "Mirror-read %s succeeded\n", filesystem);
489 * What we are trying to do here is figure out how much data is
490 * going to be sent for the TID range and to break the TID range
491 * down into reasonably-sized slices (from the point of view of
492 * data sent) so a lost connection can restart at a reasonable
493 * place and not all the way back at the beginning.
495 * An entry's TID serves as the end_tid for the prior entry
496 * So we have to offset the calculation by 1 so that TID falls into
497 * the previous entry when populating entries.
499 * Because the transaction id space is bursty we need a relatively
500 * large number of buckets (like a million) to do a reasonable job
501 * for things like an initial bulk mirrors on a very large filesystem.
503 #define HIST_COUNT (1024 * 1024)
506 generate_histogram(int fd, const char *filesystem,
507 histogram_t *histogram_ary,
508 struct hammer_ioc_mirror_rw *mirror_base,
511 struct hammer_ioc_mirror_rw mirror;
512 union hammer_ioc_mrecord_any *mrec;
513 hammer_tid_t tid_beg;
514 hammer_tid_t tid_end;
526 mirror = *mirror_base;
527 tid_beg = mirror.tid_beg;
528 tid_end = mirror.tid_end;
529 mirror.head.flags |= HAMMER_IOC_MIRROR_NODATA;
531 if (*histogram_ary == NULL) {
532 *histogram_ary = malloc(sizeof(struct histogram) *
535 if (tid_beg >= tid_end)
539 tid_bytes = malloc(sizeof(*tid_bytes) * (HIST_COUNT + 2));
540 bzero(tid_bytes, sizeof(*tid_bytes) * (HIST_COUNT + 2));
543 fprintf(stderr, "Prescan to break up bulk transfer");
545 fprintf(stderr, " (%juMB chunks)",
546 (uintmax_t)(SplitupOpt / (1024 * 1024)));
547 fprintf(stderr, "\n");
551 * Note: (tid_beg,tid_end), range is inclusive of both beg & end.
553 * Note: Estimates can be off when the mirror is way behind due
561 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
562 fprintf(stderr, "Mirror-read %s failed: %s\n",
563 filesystem, strerror(errno));
566 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
568 "Mirror-read %s fatal error %d\n",
569 filesystem, mirror.head.error);
574 off += HAMMER_HEAD_DOALIGN(mrec->head.rec_size)) {
575 mrec = (void *)((char *)mirror.ubuf + off);
578 * We only care about general RECs and PASS
579 * records. We ignore SKIPs.
581 switch (mrec->head.type & HAMMER_MRECF_TYPE_LOMASK) {
582 case HAMMER_MREC_TYPE_REC:
583 case HAMMER_MREC_TYPE_PASS:
590 * Calculate for two indices, create_tid and
591 * delete_tid. Record data only applies to
594 * When tid is exactly on the boundary it really
595 * belongs to the previous entry because scans
596 * are inclusive of the ending entry.
598 tid = mrec->rec.leaf.base.delete_tid;
599 if (tid && tid >= tid_beg && tid <= tid_end) {
600 len = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
601 if (mrec->head.type ==
602 HAMMER_MREC_TYPE_REC) {
603 len -= HAMMER_HEAD_DOALIGN(
604 mrec->rec.leaf.data_len);
607 i = (tid - tid_beg) * HIST_COUNT /
609 tidx = tid_beg + i * (tid_end - tid_beg) /
611 if (tid == tidx && i)
613 assert(i >= 0 && i < HIST_COUNT);
619 tid = mrec->rec.leaf.base.create_tid;
620 if (tid && tid >= tid_beg && tid <= tid_end) {
621 len = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
622 if (mrec->head.type ==
623 HAMMER_MREC_TYPE_REC_NODATA) {
624 len += HAMMER_HEAD_DOALIGN(
625 mrec->rec.leaf.data_len);
627 i = (tid - tid_beg) * HIST_COUNT /
629 tidx = tid_beg + i * (tid_end - tid_beg) /
631 if (tid == tidx && i)
633 assert(i >= 0 && i < HIST_COUNT);
639 if (*repeatp == 0 && accum > SplitupOpt) {
640 if (VerboseOpt > 1) {
641 fprintf(stderr, ".");
645 score_printf(LINE2, "Prescan chunk %d", chunkno);
648 if (mirror.count == 0)
650 mirror.key_beg = mirror.key_cur;
654 * Reduce to SplitupOpt (default 4GB) chunks. This code may
655 * use up to two additional elements. Do the array in-place.
657 * Inefficient degenerate cases can occur if we do not accumulate
658 * at least the requested split amount, so error on the side of
662 (*histogram_ary)[res].tid = tid_beg;
663 (*histogram_ary)[res].bytes = tid_bytes[0];
664 for (i = 1; i < HIST_COUNT; ++i) {
665 if ((*histogram_ary)[res].bytes >= SplitupOpt) {
667 (*histogram_ary)[res].tid = tid_beg +
668 i * (tid_end - tid_beg) /
670 (*histogram_ary)[res].bytes = 0;
673 (*histogram_ary)[res].bytes += tid_bytes[i];
676 (*histogram_ary)[res].tid = tid_end;
677 (*histogram_ary)[res].bytes = -1;
681 fprintf(stderr, "\n"); /* newline after ... */
682 score_printf(LINE3, "Prescan %d chunks, total %ju MBytes",
683 res, (uintmax_t)total / (1024 * 1024));
684 fprintf(stderr, "Prescan %d chunks, total %ju MBytes (",
685 res, (uintmax_t)total / (1024 * 1024));
686 for (i = 0; i < res && i < 3; ++i) {
688 fprintf(stderr, ", ");
689 fprintf(stderr, "%ju",
690 (uintmax_t)(*histogram_ary)[i].bytes);
693 fprintf(stderr, ", ...");
694 fprintf(stderr, ")\n");
696 assert(res <= HIST_COUNT);
704 create_pfs(const char *filesystem, uuid_t *s_uuid)
706 if (ForceYesOpt == 1) {
707 fprintf(stderr, "PFS slave %s does not exist. "
708 "Auto create new slave PFS!\n", filesystem);
711 fprintf(stderr, "PFS slave %s does not exist.\n"
712 "Do you want to create a new slave PFS? (yes|no) ",
715 if (getyntty() != 1) {
716 fprintf(stderr, "Aborting operation\n");
722 char *shared_uuid = NULL;
723 uuid_to_string(s_uuid, &shared_uuid, &status);
726 asprintf(&cmd, "/sbin/hammer pfs-slave '%s' shared-uuid=%s 1>&2",
727 filesystem, shared_uuid);
731 fprintf(stderr, "Failed to alloc memory\n");
734 if (system(cmd) != 0) {
735 fprintf(stderr, "Failed to create PFS\n");
741 * Pipe the mirroring data stream on stdin to the HAMMER VFS, adding
742 * some additional packet types to negotiate TID ranges and to verify
743 * completion. The HAMMER VFS does most of the work.
745 * It is important to note that the mirror.key_{beg,end} range must
746 * match the ranged used by the original. For now both sides use
747 * range the entire key space.
749 * It is even more important that the records in the stream conform
750 * to the TID range also supplied in the stream. The HAMMER VFS will
751 * use the REC, PASS, and SKIP record types to track the portions of
752 * the B-Tree being scanned in order to be able to proactively delete
753 * records on the target within those active areas that are not mentioned
756 * The mirror.key_cur field is used by the VFS to do this tracking. It
757 * must be initialized to key_beg but then is persistently updated by
758 * the HAMMER VFS on each successive ioctl() call. If you blow up this
759 * field you will blow up the mirror target, possibly to the point of
760 * deleting everything. As a safety measure the HAMMER VFS simply marks
761 * the records that the source has destroyed as deleted on the target,
762 * and normal pruning operations will deal with their final disposition
763 * at some later time.
766 hammer_cmd_mirror_write(char **av, int ac)
768 struct hammer_ioc_mirror_rw mirror;
770 char *buf = malloc(SERIALBUF_SIZE);
771 struct hammer_ioc_pseudofs_rw pfs;
772 struct hammer_ioc_mrecord_head pickup;
773 struct hammer_ioc_synctid synctid;
774 union hammer_ioc_mrecord_any mrec_tmp;
775 hammer_ioc_mrecord_any_t mrec;
784 hammer_check_restrict(filesystem);
786 pickup.signature = 0;
790 bzero(&mirror, sizeof(mirror));
791 hammer_key_beg_init(&mirror.key_beg);
792 hammer_key_end_init(&mirror.key_end);
793 mirror.key_end = mirror.key_beg;
796 * Read initial packet
798 mrec = read_mrecord(0, &error, &pickup);
801 fprintf(stderr, "validate_mrec_header: short read\n");
807 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
811 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
812 fprintf(stderr, "validate_mrec_header: did not get expected "
813 "PFSD record type\n");
816 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
817 fprintf(stderr, "validate_mrec_header: unexpected payload "
823 * Create slave PFS if it doesn't yet exist
825 if (lstat(filesystem, &st) != 0) {
826 create_pfs(filesystem, &mrec->pfs.pfsd.shared_uuid);
831 fd = getpfs(&pfs, filesystem);
834 * In two-way mode the target writes out a PFS packet first.
835 * The source uses our tid_end as its tid_beg by default,
836 * picking up where it left off.
840 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
841 if (mirror.tid_beg < mrec_tmp.pfs.pfsd.sync_beg_tid)
842 mirror.tid_beg = mrec_tmp.pfs.pfsd.sync_beg_tid;
843 mirror.tid_end = mrec_tmp.pfs.pfsd.sync_end_tid;
844 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
845 &mrec_tmp, sizeof(mrec_tmp.pfs));
849 * Read and process the PFS header. The source informs us of
850 * the TID range the stream represents.
852 n = validate_mrec_header(fd, 0, 1, pfs.pfs_id, &pickup,
853 &mirror.tid_beg, &mirror.tid_end);
854 if (n < 0) { /* got TERM record */
861 mirror.size = SERIALBUF_SIZE;
864 * Read and process bulk records (REC, PASS, and SKIP types).
866 * On your life, do NOT mess with mirror.key_cur or your mirror
867 * target may become history.
871 mirror.pfs_id = pfs.pfs_id;
872 mirror.shared_uuid = pfs.ondisk->shared_uuid;
873 mirror.size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
874 if (mirror.size <= 0)
876 if (ioctl(fd, HAMMERIOC_MIRROR_WRITE, &mirror) < 0) {
877 fprintf(stderr, "Mirror-write %s failed: %s\n",
878 filesystem, strerror(errno));
881 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
883 "Mirror-write %s fatal error %d\n",
884 filesystem, mirror.head.error);
888 if (mirror.head.flags & HAMMER_IOC_HEAD_INTR) {
890 "Mirror-write %s interrupted by timer at"
893 mirror.key_cur.obj_id);
900 * Read and process the termination sync record.
902 mrec = read_mrecord(0, &error, &pickup);
904 if (mrec && mrec->head.type == HAMMER_MREC_TYPE_TERM) {
905 fprintf(stderr, "Mirror-write: received termination request\n");
913 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
914 mrec->head.type != HAMMER_MREC_TYPE_IDLE) ||
915 mrec->head.rec_size != sizeof(mrec->sync)) {
916 fprintf(stderr, "Mirror-write %s: Did not get termination "
917 "sync record, or rec_size is wrong rt=%d\n",
919 (mrec ? (int)mrec->head.type : -1));
924 * Update the PFS info on the target so the user has visibility
925 * into the new snapshot, and sync the target filesystem.
927 if (mrec->head.type == HAMMER_MREC_TYPE_SYNC) {
928 update_pfs_snapshot(fd, mirror.tid_end, pfs.pfs_id);
930 bzero(&synctid, sizeof(synctid));
931 synctid.op = HAMMER_SYNCTID_SYNC2;
932 ioctl(fd, HAMMERIOC_SYNCTID, &synctid);
934 if (VerboseOpt >= 2) {
935 fprintf(stderr, "Mirror-write %s: succeeded\n",
944 * Report back to the originator.
947 mrec_tmp.update.tid = mirror.tid_end;
948 write_mrecord(1, HAMMER_MREC_TYPE_UPDATE,
949 &mrec_tmp, sizeof(mrec_tmp.update));
951 printf("Source can update synctid to 0x%016jx\n",
952 (uintmax_t)mirror.tid_end);
959 hammer_cmd_mirror_dump(char **av, int ac)
961 char *buf = malloc(SERIALBUF_SIZE);
962 struct hammer_ioc_mrecord_head pickup;
963 hammer_ioc_mrecord_any_t mrec;
970 if (ac == 1 && strcmp(*av, "header") == 0)
976 * Read and process the PFS header
978 pickup.signature = 0;
981 mrec = read_mrecord(0, &error, &pickup);
984 * Dump the PFS header. mirror-dump takes its input from the output
985 * of a mirror-read so getpfs() can't be used to get a fd to be passed
988 if (header_only && mrec != NULL) {
989 dump_pfsd(&mrec->pfs.pfsd, -1);
998 * Read and process bulk records
1001 size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
1005 while (offset < size) {
1006 mrec = (void *)((char *)buf + offset);
1007 bytes = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
1008 if (offset + bytes > size) {
1009 fprintf(stderr, "Misaligned record\n");
1013 switch(mrec->head.type & HAMMER_MRECF_TYPE_MASK) {
1014 case HAMMER_MREC_TYPE_REC_BADCRC:
1015 case HAMMER_MREC_TYPE_REC:
1016 printf("Record lo=%08x obj=%016jx key=%016jx "
1018 mrec->rec.leaf.base.localization,
1019 (uintmax_t)mrec->rec.leaf.base.obj_id,
1020 (uintmax_t)mrec->rec.leaf.base.key,
1021 mrec->rec.leaf.base.rec_type,
1022 mrec->rec.leaf.base.obj_type);
1023 if (mrec->head.type ==
1024 HAMMER_MREC_TYPE_REC_BADCRC) {
1025 printf(" (BAD CRC)");
1028 printf(" tids %016jx:%016jx data=%d\n",
1029 (uintmax_t)mrec->rec.leaf.base.create_tid,
1030 (uintmax_t)mrec->rec.leaf.base.delete_tid,
1031 mrec->rec.leaf.data_len);
1033 case HAMMER_MREC_TYPE_PASS:
1034 printf("Pass lo=%08x obj=%016jx key=%016jx "
1035 "rt=%02x ot=%02x\n",
1036 mrec->rec.leaf.base.localization,
1037 (uintmax_t)mrec->rec.leaf.base.obj_id,
1038 (uintmax_t)mrec->rec.leaf.base.key,
1039 mrec->rec.leaf.base.rec_type,
1040 mrec->rec.leaf.base.obj_type);
1041 printf(" tids %016jx:%016jx data=%d\n",
1042 (uintmax_t)mrec->rec.leaf.base.create_tid,
1043 (uintmax_t)mrec->rec.leaf.base.delete_tid,
1044 mrec->rec.leaf.data_len);
1046 case HAMMER_MREC_TYPE_SKIP:
1047 printf("Skip lo=%08x obj=%016jx key=%016jx rt=%02x to\n"
1048 " lo=%08x obj=%016jx key=%016jx rt=%02x\n",
1049 mrec->skip.skip_beg.localization,
1050 (uintmax_t)mrec->skip.skip_beg.obj_id,
1051 (uintmax_t)mrec->skip.skip_beg.key,
1052 mrec->skip.skip_beg.rec_type,
1053 mrec->skip.skip_end.localization,
1054 (uintmax_t)mrec->skip.skip_end.obj_id,
1055 (uintmax_t)mrec->skip.skip_end.key,
1056 mrec->skip.skip_end.rec_type);
1065 * Read and process the termination sync record.
1067 mrec = read_mrecord(0, &error, &pickup);
1069 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
1070 mrec->head.type != HAMMER_MREC_TYPE_IDLE)) {
1071 fprintf(stderr, "Mirror-dump: Did not get termination "
1077 * Continue with more batches until EOF.
1079 mrec = read_mrecord(0, &error, &pickup);
1088 hammer_cmd_mirror_copy(char **av, int ac, int streaming)
1093 const char *xav[32];
1095 char *sh, *user, *host, *rfs;
1102 signal(SIGPIPE, SIG_IGN);
1105 if (pipe(fds) < 0) {
1113 if ((pid1 = fork()) == 0) {
1114 signal(SIGPIPE, SIG_DFL);
1119 if ((rfs = strchr(av[0], ':')) != NULL) {
1122 if((sh = getenv("HAMMER_RSH")) == NULL)
1130 if ((host = strchr(av[0], '@')) != NULL) {
1131 user = strndup( av[0], (host++ - av[0]));
1132 host = strndup( host, (rfs++ - host));
1138 host = strndup( av[0], (rfs++ - av[0]));
1146 xav[xac++] = SshPort;
1149 xav[xac++] = "hammer";
1151 switch(VerboseOpt) {
1161 xav[xac++] = "-vvv";
1169 snprintf(tbuf, sizeof(tbuf), "%d", TimeoutOpt);
1173 if (SplitupOptStr) {
1175 xav[xac++] = SplitupOptStr;
1178 xav[xac++] = "mirror-read-stream";
1180 xav[xac++] = "mirror-read";
1183 execvp(*xav, (void *)xav);
1185 hammer_cmd_mirror_read(av, 1, streaming);
1195 if ((pid2 = fork()) == 0) {
1196 signal(SIGPIPE, SIG_DFL);
1201 if ((rfs = strchr(av[1], ':')) != NULL) {
1204 if((sh = getenv("HAMMER_RSH")) == NULL)
1212 if ((host = strchr(av[1], '@')) != NULL) {
1213 user = strndup( av[1], (host++ - av[1]));
1214 host = strndup( host, (rfs++ - host));
1220 host = strndup( av[1], (rfs++ - av[1]));
1227 xav[xac++] = SshPort;
1230 xav[xac++] = "hammer";
1232 switch(VerboseOpt) {
1242 xav[xac++] = "-vvv";
1249 xav[xac++] = "mirror-write";
1252 execvp(*xav, (void *)xav);
1254 hammer_cmd_mirror_write(av + 1, 1);
1263 while (waitpid(pid1, NULL, 0) <= 0)
1265 while (waitpid(pid2, NULL, 0) <= 0)
1269 * If the link is lost restart
1273 fprintf(stderr, "\nLost Link\n");
1276 sleep(15 + DelayOpt);
1283 * Read and return multiple mrecords
1286 read_mrecords(int fd, char *buf, u_int size, hammer_ioc_mrecord_head_t pickup)
1288 hammer_ioc_mrecord_any_t mrec;
1296 while (size - count >= HAMMER_MREC_HEADSIZE) {
1298 * Cached the record header in case we run out of buffer
1302 if (pickup->signature == 0) {
1303 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
1304 i = read(fd, (char *)pickup + n,
1305 HAMMER_MREC_HEADSIZE - n);
1311 if (n != HAMMER_MREC_HEADSIZE) {
1312 fprintf(stderr, "read_mrecords: short read on pipe\n");
1315 if (pickup->signature != HAMMER_IOC_MIRROR_SIGNATURE) {
1316 fprintf(stderr, "read_mrecords: malformed record on pipe, "
1321 if (pickup->rec_size < HAMMER_MREC_HEADSIZE ||
1322 pickup->rec_size > sizeof(*mrec) + HAMMER_XBUFSIZE) {
1323 fprintf(stderr, "read_mrecords: malformed record on pipe, "
1324 "illegal rec_size\n");
1329 * Stop if we have insufficient space for the record and data.
1331 bytes = HAMMER_HEAD_DOALIGN(pickup->rec_size);
1332 if (size - count < bytes)
1336 * Stop if the record type is not a REC, SKIP, or PASS,
1337 * which are the only types the ioctl supports. Other types
1338 * are used only by the userland protocol.
1342 type = pickup->type & HAMMER_MRECF_TYPE_LOMASK;
1343 if (type != HAMMER_MREC_TYPE_PFSD &&
1344 type != HAMMER_MREC_TYPE_REC &&
1345 type != HAMMER_MREC_TYPE_SKIP &&
1346 type != HAMMER_MREC_TYPE_PASS) {
1351 * Read the remainder and clear the pickup signature.
1353 for (n = HAMMER_MREC_HEADSIZE; n < bytes; n += i) {
1354 i = read(fd, buf + count + n, bytes - n);
1359 fprintf(stderr, "read_mrecords: short read on pipe\n");
1363 bcopy(pickup, buf + count, HAMMER_MREC_HEADSIZE);
1364 pickup->signature = 0;
1366 mrec = (void *)(buf + count);
1369 * Validate the completed record
1371 if (!hammer_crc_test_mrec_head(&mrec->head, mrec->head.rec_size)) {
1372 fprintf(stderr, "read_mrecords: malformed record "
1373 "on pipe, bad crc\n");
1378 * If its a B-Tree record validate the data crc.
1380 * NOTE: If the VFS passes us an explicitly errorde mrec
1381 * we just pass it through.
1383 type = mrec->head.type & HAMMER_MRECF_TYPE_MASK;
1385 if (type == HAMMER_MREC_TYPE_REC) {
1386 if (mrec->head.rec_size <
1387 sizeof(mrec->rec) + mrec->rec.leaf.data_len) {
1389 "read_mrecords: malformed record on "
1390 "pipe, illegal element data_len\n");
1393 if (mrec->rec.leaf.data_len &&
1394 mrec->rec.leaf.data_offset &&
1395 hammer_crc_test_leaf(&mrec->rec + 1, &mrec->rec.leaf) == 0) {
1397 "read_mrecords: data_crc did not "
1398 "match data! obj=%016jx key=%016jx\n",
1399 (uintmax_t)mrec->rec.leaf.base.obj_id,
1400 (uintmax_t)mrec->rec.leaf.base.key);
1402 "continuing, but there are problems\n");
1411 * Read and return a single mrecord.
1414 hammer_ioc_mrecord_any_t
1415 read_mrecord(int fdin, int *errorp, hammer_ioc_mrecord_head_t pickup)
1417 hammer_ioc_mrecord_any_t mrec;
1418 struct hammer_ioc_mrecord_head mrechd;
1423 if (pickup && pickup->type != 0) {
1425 pickup->signature = 0;
1427 n = HAMMER_MREC_HEADSIZE;
1430 * Read in the PFSD header from the sender.
1432 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
1433 i = read(fdin, (char *)&mrechd + n, HAMMER_MREC_HEADSIZE - n);
1438 *errorp = 0; /* EOF */
1441 if (n != HAMMER_MREC_HEADSIZE) {
1442 fprintf(stderr, "short read of mrecord header\n");
1447 if (mrechd.signature != HAMMER_IOC_MIRROR_SIGNATURE) {
1448 fprintf(stderr, "read_mrecord: bad signature\n");
1452 bytes = HAMMER_HEAD_DOALIGN(mrechd.rec_size);
1453 assert(bytes >= sizeof(mrechd));
1454 mrec = malloc(bytes);
1455 mrec->head = mrechd;
1458 i = read(fdin, (char *)mrec + n, bytes - n);
1464 fprintf(stderr, "read_mrecord: short read on payload\n");
1468 if (!hammer_crc_test_mrec_head(&mrec->head, mrec->head.rec_size)) {
1469 fprintf(stderr, "read_mrecord: bad CRC\n");
1479 write_mrecord(int fdout, uint32_t type, hammer_ioc_mrecord_any_t mrec,
1482 char zbuf[HAMMER_HEAD_ALIGN];
1485 pad = HAMMER_HEAD_DOALIGN(bytes) - bytes;
1487 assert(bytes >= (int)sizeof(mrec->head));
1488 bzero(&mrec->head, sizeof(mrec->head));
1489 mrec->head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
1490 mrec->head.type = type;
1491 mrec->head.rec_size = bytes;
1492 hammer_crc_set_mrec_head(&mrec->head, bytes);
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 score_printf(size_t i, size_t w, const char *ctl, ...)
1728 static size_t SSize;
1729 static int SFd = -1;
1730 static char ScoreBuf[1024];
1732 if (ScoreBoardFile == NULL)
1734 assert(i + w < sizeof(ScoreBuf));
1736 SFd = open(ScoreBoardFile, O_RDWR|O_CREAT|O_TRUNC, 0644);
1741 for (n = 0; n < i; ++n) {
1742 if (ScoreBuf[n] == 0)
1746 vsnprintf(ScoreBuf + i, w - 1, ctl, va);
1748 n = strlen(ScoreBuf + i);
1750 ScoreBuf[i + n] = ' ';
1753 ScoreBuf[i + n] = '\n';
1756 pwrite(SFd, ScoreBuf, SSize, 0);
1760 hammer_check_restrict(const char *filesystem)
1765 if (RestrictTarget == NULL)
1767 rlen = strlen(RestrictTarget);
1768 if (strncmp(filesystem, RestrictTarget, rlen) != 0) {
1769 fprintf(stderr, "hammer-remote: restricted target\n");
1773 while (filesystem[rlen]) {
1775 filesystem[rlen] == '.' &&
1776 filesystem[rlen+1] == '.') {
1777 fprintf(stderr, "hammer-remote: '..' not allowed\n");
1780 if (filesystem[rlen] == '/')
1789 mirror_usage(int code)
1792 "hammer mirror-read <filesystem> [begin-tid]\n"
1793 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
1794 "hammer mirror-write <filesystem>\n"
1795 "hammer mirror-dump [header]\n"
1796 "hammer mirror-copy [[user@]host:]<filesystem>"
1797 " [[user@]host:]<filesystem>\n"
1798 "hammer mirror-stream [[user@]host:]<filesystem>"
1799 " [[user@]host:]<filesystem>\n"