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 struct hammer_ioc_mrecord_head *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 struct hammer_ioc_mrecord_head *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;
95 const char *filesystem;
96 char *buf = malloc(SERIALBUF_SIZE);
108 time_t base_t = time(NULL);
113 if (ac == 0 || ac > 2) {
118 hammer_check_restrict(filesystem);
120 pickup.signature = 0;
125 histogram_ary = NULL;
129 bzero(&mirror, sizeof(mirror));
130 hammer_key_beg_init(&mirror.key_beg);
131 hammer_key_end_init(&mirror.key_end);
133 fd = getpfs(&pfs, filesystem);
136 score_printf(LINE1, "Running");
138 if (streaming >= 0 && VerboseOpt && VerboseOpt < 2) {
139 fprintf(stderr, "%cRunning \b\b", (sameline ? '\r' : '\n'));
145 gettimeofday(&bwtv, NULL);
149 * Send initial header for the purpose of determining the
152 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
153 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
154 &mrec_tmp, sizeof(mrec_tmp.pfs));
157 * In 2-way mode the target will send us a PFS info packet
158 * first. Use the target's current snapshot TID as our default
163 n = validate_mrec_header(fd, 0, 0, pfs.pfs_id, &pickup,
164 NULL, &mirror.tid_beg);
165 if (n < 0) { /* got TERM record */
172 } else if (streaming && histogram) {
173 mirror.tid_beg = histogram_ary[histindex].tid + 1;
179 * Write out the PFS header, tid_beg will be updated if our PFS
180 * has a larger begin sync. tid_end is set to the latest source
181 * TID whos flush cycle has completed.
183 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
184 if (mirror.tid_beg < mrec_tmp.pfs.pfsd.sync_beg_tid)
185 mirror.tid_beg = mrec_tmp.pfs.pfsd.sync_beg_tid;
186 mirror.tid_end = mrec_tmp.pfs.pfsd.sync_end_tid;
188 mirror.size = SERIALBUF_SIZE;
189 mirror.pfs_id = pfs.pfs_id;
190 mirror.shared_uuid = pfs.ondisk->shared_uuid;
193 * XXX If the histogram is exhausted and the TID delta is large
194 * the stream might have been offline for a while and is
195 * now picking it up again. Do another histogram.
198 if (streaming && histogram && histindex == histend) {
199 if (mirror.tid_end - mirror.tid_beg > BULK_MINIMUM)
205 * Initial bulk startup control, try to do some incremental
206 * mirroring in order to allow the stream to be killed and
207 * restarted without having to start over.
209 if (histogram == 0 && BulkOpt == 0) {
210 if (VerboseOpt && repeat == 0) {
211 fprintf(stderr, "\n");
214 histmax = generate_histogram(fd, filesystem,
215 &histogram_ary, &mirror,
221 * Just stream the histogram, then stop
227 if (streaming && histogram) {
229 mirror.tid_end = histogram_ary[histindex].tid;
230 estbytes = histogram_ary[histindex-1].bytes;
231 mrec_tmp.pfs.pfsd.sync_end_tid = mirror.tid_end;
236 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
237 &mrec_tmp, sizeof(mrec_tmp.pfs));
240 * A cycle file overrides the beginning TID only if we are
241 * not operating in two-way or histogram mode.
243 if (TwoWayPipeOpt == 0 && histogram == 0)
244 hammer_get_cycle(&mirror.key_beg, &mirror.tid_beg);
247 * An additional argument overrides the beginning TID regardless
248 * of what mode we are in. This is not recommending if operating
252 mirror.tid_beg = strtoull(av[1], NULL, 0);
254 if (streaming == 0 || VerboseOpt >= 2) {
256 "Mirror-read: Mirror %016jx to %016jx",
257 (uintmax_t)mirror.tid_beg, (uintmax_t)mirror.tid_end);
259 fprintf(stderr, " (bulk= %ju)", (uintmax_t)estbytes);
260 fprintf(stderr, "\n");
263 if (mirror.key_beg.obj_id != (int64_t)HAMMER_MIN_OBJID) {
264 fprintf(stderr, "Mirror-read: Resuming at object %016jx\n",
265 (uintmax_t)mirror.key_beg.obj_id);
269 * Nothing to do if begin equals end.
271 if (mirror.tid_beg >= mirror.tid_end) {
272 if (streaming == 0 || VerboseOpt >= 2)
273 fprintf(stderr, "Mirror-read: No work to do\n");
282 * Write out bulk records
285 mirror.size = SERIALBUF_SIZE;
289 mirror.pfs_id = pfs.pfs_id;
290 mirror.shared_uuid = pfs.ondisk->shared_uuid;
291 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
292 score_printf(LINE3, "Mirror-read %s failed: %s",
293 filesystem, strerror(errno));
294 err(1, "Mirror-read %s failed", filesystem);
297 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
298 score_printf(LINE3, "Mirror-read %s fatal error %d",
299 filesystem, mirror.head.error);
300 errx(1, "Mirror-read %s fatal error %d",
301 filesystem, mirror.head.error);
306 n = writebw(1, mirror.ubuf, mirror.count,
309 n = write(1, mirror.ubuf, mirror.count);
311 if (n != mirror.count) {
313 "Mirror-read %s failed: "
316 errx(1, "Mirror-read %s failed: short write",
321 total_bytes += mirror.count;
322 if (streaming && VerboseOpt) {
324 "\rscan obj=%016jx tids=%016jx:%016jx %11jd",
325 (uintmax_t)mirror.key_cur.obj_id,
326 (uintmax_t)mirror.tid_beg,
327 (uintmax_t)mirror.tid_end,
328 (intmax_t)total_bytes);
331 } else if (streaming) {
333 "obj=%016jx tids=%016jx:%016jx %11jd",
334 (uintmax_t)mirror.key_cur.obj_id,
335 (uintmax_t)mirror.tid_beg,
336 (uintmax_t)mirror.tid_end,
337 (intmax_t)total_bytes);
339 mirror.key_beg = mirror.key_cur;
342 * Deal with time limit option
345 (unsigned)(time(NULL) - base_t) > (unsigned)TimeoutOpt) {
347 "Mirror-read %s interrupted by timer at"
350 (uintmax_t)mirror.key_cur.obj_id);
352 "Mirror-read %s interrupted by timer at"
355 (uintmax_t)mirror.key_cur.obj_id);
359 } while (mirror.count != 0);
362 if (streaming && VerboseOpt && sameline == 0) {
363 fprintf(stderr, "\n");
369 * Write out the termination sync record - only if not interrupted
371 if (interrupted == 0) {
373 write_mrecord(1, HAMMER_MREC_TYPE_SYNC,
374 &mrec_tmp, sizeof(mrec_tmp.sync));
376 write_mrecord(1, HAMMER_MREC_TYPE_IDLE,
377 &mrec_tmp, sizeof(mrec_tmp.sync));
382 * If the -2 option was given (automatic when doing mirror-copy),
383 * a two-way pipe is assumed and we expect a response mrec from
387 mrec = read_mrecord(0, &error, &pickup);
389 mrec->head.type != HAMMER_MREC_TYPE_UPDATE ||
390 mrec->head.rec_size != sizeof(mrec->update)) {
391 errx(1, "mirror_read: Did not get final "
392 "acknowledgement packet from target");
397 hammer_set_cycle(&mirror.key_cur,
399 fprintf(stderr, "Cyclefile %s updated for "
400 "continuation\n", CyclePath);
403 sync_tid = mrec->update.tid;
405 hammer_key_beg_init(&mirror.key_beg);
406 hammer_set_cycle(&mirror.key_beg, sync_tid);
408 "Cyclefile %s updated to 0x%016jx\n",
409 CyclePath, (uintmax_t)sync_tid);
413 } else if (CyclePath) {
414 /* NOTE! mirror.tid_beg cannot be updated */
415 fprintf(stderr, "Warning: cycle file (-c option) cannot be "
416 "fully updated unless you use mirror-copy\n");
417 hammer_set_cycle(&mirror.key_beg, mirror.tid_beg);
419 if (streaming && interrupted == 0) {
420 time_t t1 = time(NULL);
424 * Try to break down large bulk transfers into smaller ones
425 * so it can sync the transaction id on the slave. This
426 * way if we get interrupted a restart doesn't have to
427 * start from scratch.
429 if (streaming && histogram) {
430 if (histindex != histmax) {
431 if (VerboseOpt && VerboseOpt < 2 &&
433 fprintf(stderr, " (bulk incremental)");
440 if (VerboseOpt && streaming >= 0) {
441 fprintf(stderr, " W");
443 } else if (streaming >= 0) {
444 score_printf(LINE1, "Waiting");
446 pfs.ondisk->sync_end_tid = mirror.tid_end;
449 * Fake streaming mode when using a histogram to
450 * break up a mirror-read, do not wait on source.
453 } else if (ioctl(fd, HAMMERIOC_WAI_PSEUDOFS, &pfs) < 0) {
455 "Mirror-read %s: cannot stream: %s\n",
456 filesystem, strerror(errno));
458 "Mirror-read %s: cannot stream: %s\n",
459 filesystem, strerror(errno));
461 t2 = time(NULL) - t1;
462 if (t2 >= 0 && t2 < DelayOpt) {
464 fprintf(stderr, "\bD");
467 sleep(DelayOpt - t2);
470 fprintf(stderr, "\b ");
477 write_mrecord(1, HAMMER_MREC_TYPE_TERM,
478 &mrec_tmp, sizeof(mrec_tmp.sync));
482 fprintf(stderr, "Mirror-read %s succeeded\n", filesystem);
486 * What we are trying to do here is figure out how much data is
487 * going to be sent for the TID range and to break the TID range
488 * down into reasonably-sized slices (from the point of view of
489 * data sent) so a lost connection can restart at a reasonable
490 * place and not all the way back at the beginning.
492 * An entry's TID serves as the end_tid for the prior entry
493 * So we have to offset the calculation by 1 so that TID falls into
494 * the previous entry when populating entries.
496 * Because the transaction id space is bursty we need a relatively
497 * large number of buckets (like a million) to do a reasonable job
498 * for things like an initial bulk mirrors on a very large filesystem.
500 #define HIST_COUNT (1024 * 1024)
504 generate_histogram(int fd, const char *filesystem,
505 histogram_t *histogram_ary,
506 struct hammer_ioc_mirror_rw *mirror_base,
509 struct hammer_ioc_mirror_rw mirror;
510 union hammer_ioc_mrecord_any *mrec;
511 hammer_tid_t tid_beg;
512 hammer_tid_t tid_end;
524 mirror = *mirror_base;
525 tid_beg = mirror.tid_beg;
526 tid_end = mirror.tid_end;
527 mirror.head.flags |= HAMMER_IOC_MIRROR_NODATA;
529 if (*histogram_ary == NULL) {
530 *histogram_ary = malloc(sizeof(struct histogram) *
533 if (tid_beg >= tid_end)
537 tid_bytes = malloc(sizeof(*tid_bytes) * (HIST_COUNT + 2));
538 bzero(tid_bytes, sizeof(*tid_bytes) * (HIST_COUNT + 2));
541 fprintf(stderr, "Prescan to break up bulk transfer");
542 if (VerboseOpt > 1) {
543 fprintf(stderr, " (%juMB chunks)",
544 (uintmax_t)(SplitupOpt / (1024 * 1024)));
546 fprintf(stderr, "\n");
550 * Note: (tid_beg,tid_end), range is inclusive of both beg & end.
552 * Note: Estimates can be off when the mirror is way behind due
560 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
561 err(1, "Mirror-read %s failed", filesystem);
564 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
565 errx(1, "Mirror-read %s fatal error %d",
566 filesystem, mirror.head.error);
571 off += HAMMER_HEAD_DOALIGN(mrec->head.rec_size)) {
572 mrec = (void *)((char *)mirror.ubuf + off);
575 * We only care about general RECs and PASS
576 * records. We ignore SKIPs.
578 switch (mrec->head.type & HAMMER_MRECF_TYPE_LOMASK) {
579 case HAMMER_MREC_TYPE_REC:
580 case HAMMER_MREC_TYPE_PASS:
587 * Calculate for two indices, create_tid and
588 * delete_tid. Record data only applies to
591 * When tid is exactly on the boundary it really
592 * belongs to the previous entry because scans
593 * are inclusive of the ending entry.
595 tid = mrec->rec.leaf.base.delete_tid;
596 if (tid && tid >= tid_beg && tid <= tid_end) {
597 len = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
598 if (mrec->head.type ==
599 HAMMER_MREC_TYPE_REC) {
600 len -= HAMMER_HEAD_DOALIGN(
601 mrec->rec.leaf.data_len);
604 i = (tid - tid_beg) * HIST_COUNT /
606 tidx = tid_beg + i * (tid_end - tid_beg) /
608 if (tid == tidx && i)
610 assert(i >= 0 && i < HIST_COUNT);
616 tid = mrec->rec.leaf.base.create_tid;
617 if (tid && tid >= tid_beg && tid <= tid_end) {
618 len = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
619 if (mrec->head.type ==
620 HAMMER_MREC_TYPE_REC_NODATA) {
621 len += HAMMER_HEAD_DOALIGN(
622 mrec->rec.leaf.data_len);
624 i = (tid - tid_beg) * HIST_COUNT /
626 tidx = tid_beg + i * (tid_end - tid_beg) /
628 if (tid == tidx && i)
630 assert(i >= 0 && i < HIST_COUNT);
636 if (*repeatp == 0 && accum > SplitupOpt) {
637 if (VerboseOpt > 1) {
638 fprintf(stderr, ".");
642 score_printf(LINE2, "Prescan chunk %d", chunkno);
645 if (mirror.count == 0)
647 mirror.key_beg = mirror.key_cur;
651 * Reduce to SplitupOpt (default 4GB) chunks. This code may
652 * use up to two additional elements. Do the array in-place.
654 * Inefficient degenerate cases can occur if we do not accumulate
655 * at least the requested split amount, so error on the side of
659 (*histogram_ary)[res].tid = tid_beg;
660 (*histogram_ary)[res].bytes = tid_bytes[0];
661 for (i = 1; i < HIST_COUNT; ++i) {
662 if ((*histogram_ary)[res].bytes >= SplitupOpt) {
664 (*histogram_ary)[res].tid = tid_beg +
665 i * (tid_end - tid_beg) /
667 (*histogram_ary)[res].bytes = 0;
670 (*histogram_ary)[res].bytes += tid_bytes[i];
673 (*histogram_ary)[res].tid = tid_end;
674 (*histogram_ary)[res].bytes = -1;
678 fprintf(stderr, "\n"); /* newline after ... */
679 score_printf(LINE3, "Prescan %d chunks, total %ju MBytes",
680 res, (uintmax_t)total / (1024 * 1024));
681 fprintf(stderr, "Prescan %d chunks, total %ju MBytes (",
682 res, (uintmax_t)total / (1024 * 1024));
683 for (i = 0; i < res && i < 3; ++i) {
685 fprintf(stderr, ", ");
686 fprintf(stderr, "%ju",
687 (uintmax_t)(*histogram_ary)[i].bytes);
690 fprintf(stderr, ", ...");
691 fprintf(stderr, ")\n");
693 assert(res <= HIST_COUNT);
702 create_pfs(const char *filesystem, hammer_uuid_t *s_uuid)
704 if (ForceYesOpt == 1) {
705 fprintf(stderr, "PFS slave %s does not exist. "
706 "Auto create new slave PFS!\n", filesystem);
709 fprintf(stderr, "PFS slave %s does not exist.\n"
710 "Do you want to create a new slave PFS? [y/n] ",
713 if (getyntty() != 1) {
714 errx(1, "Aborting operation");
719 char *shared_uuid = NULL;
720 hammer_uuid_to_string(s_uuid, &shared_uuid);
723 asprintf(&cmd, "/sbin/hammer pfs-slave '%s' shared-uuid=%s 1>&2",
724 filesystem, shared_uuid);
728 errx(1, "Failed to alloc memory");
731 if (system(cmd) != 0)
732 fprintf(stderr, "Failed to create PFS\n");
737 * Pipe the mirroring data stream on stdin to the HAMMER VFS, adding
738 * some additional packet types to negotiate TID ranges and to verify
739 * completion. The HAMMER VFS does most of the work.
741 * It is important to note that the mirror.key_{beg,end} range must
742 * match the ranged used by the original. For now both sides use
743 * range the entire key space.
745 * It is even more important that the records in the stream conform
746 * to the TID range also supplied in the stream. The HAMMER VFS will
747 * use the REC, PASS, and SKIP record types to track the portions of
748 * the B-Tree being scanned in order to be able to proactively delete
749 * records on the target within those active areas that are not mentioned
752 * The mirror.key_cur field is used by the VFS to do this tracking. It
753 * must be initialized to key_beg but then is persistently updated by
754 * the HAMMER VFS on each successive ioctl() call. If you blow up this
755 * field you will blow up the mirror target, possibly to the point of
756 * deleting everything. As a safety measure the HAMMER VFS simply marks
757 * the records that the source has destroyed as deleted on the target,
758 * and normal pruning operations will deal with their final disposition
759 * at some later time.
762 hammer_cmd_mirror_write(char **av, int ac)
764 struct hammer_ioc_mirror_rw mirror;
765 const char *filesystem;
766 char *buf = malloc(SERIALBUF_SIZE);
767 struct hammer_ioc_pseudofs_rw pfs;
768 struct hammer_ioc_mrecord_head pickup;
769 struct hammer_ioc_synctid synctid;
770 union hammer_ioc_mrecord_any mrec_tmp;
771 hammer_ioc_mrecord_any_t mrec;
782 hammer_check_restrict(filesystem);
784 pickup.signature = 0;
788 bzero(&mirror, sizeof(mirror));
789 hammer_key_beg_init(&mirror.key_beg);
790 hammer_key_end_init(&mirror.key_end);
791 mirror.key_end = mirror.key_beg;
794 * Read initial packet
796 mrec = read_mrecord(0, &error, &pickup);
799 errx(1, "validate_mrec_header: short read");
807 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
811 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
812 errx(1, "validate_mrec_header: did not get expected "
816 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
817 errx(1, "validate_mrec_header: unexpected payload size");
822 * Create slave PFS if it doesn't yet exist
824 if (lstat(filesystem, &st) != 0)
825 create_pfs(filesystem, &mrec->pfs.pfsd.shared_uuid);
829 fd = getpfs(&pfs, filesystem);
832 * In two-way mode the target writes out a PFS packet first.
833 * The source uses our tid_end as its tid_beg by default,
834 * picking up where it left off.
838 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
839 if (mirror.tid_beg < mrec_tmp.pfs.pfsd.sync_beg_tid)
840 mirror.tid_beg = mrec_tmp.pfs.pfsd.sync_beg_tid;
841 mirror.tid_end = mrec_tmp.pfs.pfsd.sync_end_tid;
842 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
843 &mrec_tmp, sizeof(mrec_tmp.pfs));
847 * Read and process the PFS header. The source informs us of
848 * the TID range the stream represents.
850 n = validate_mrec_header(fd, 0, 1, pfs.pfs_id, &pickup,
851 &mirror.tid_beg, &mirror.tid_end);
852 if (n < 0) { /* got TERM record */
859 mirror.size = SERIALBUF_SIZE;
862 * Read and process bulk records (REC, PASS, and SKIP types).
864 * On your life, do NOT mess with mirror.key_cur or your mirror
865 * target may become history.
869 mirror.pfs_id = pfs.pfs_id;
870 mirror.shared_uuid = pfs.ondisk->shared_uuid;
871 mirror.size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
872 if (mirror.size <= 0)
874 if (ioctl(fd, HAMMERIOC_MIRROR_WRITE, &mirror) < 0) {
875 err(1, "Mirror-write %s failed", filesystem);
878 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
879 errx(1, "Mirror-write %s fatal error %d",
880 filesystem, mirror.head.error);
884 if (mirror.head.flags & HAMMER_IOC_HEAD_INTR) {
885 errx(1, "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 errx(1, "Mirror-write %s: Did not get termination "
912 "sync record, or rec_size is wrong rt=%d",
913 filesystem, (mrec ? (int)mrec->head.type : -1));
918 * Update the PFS info on the target so the user has visibility
919 * into the new snapshot, and sync the target filesystem.
921 if (mrec->head.type == HAMMER_MREC_TYPE_SYNC) {
922 update_pfs_snapshot(fd, mirror.tid_end, pfs.pfs_id);
924 bzero(&synctid, sizeof(synctid));
925 synctid.op = HAMMER_SYNCTID_SYNC2;
926 ioctl(fd, HAMMERIOC_SYNCTID, &synctid);
928 if (VerboseOpt >= 2) {
929 fprintf(stderr, "Mirror-write %s: succeeded\n",
938 * Report back to the originator.
941 mrec_tmp.update.tid = mirror.tid_end;
942 write_mrecord(1, HAMMER_MREC_TYPE_UPDATE,
943 &mrec_tmp, sizeof(mrec_tmp.update));
945 printf("Source can update synctid to 0x%016jx\n",
946 (uintmax_t)mirror.tid_end);
953 hammer_cmd_mirror_dump(char **av, int ac)
955 char *buf = malloc(SERIALBUF_SIZE);
956 struct hammer_ioc_mrecord_head pickup;
957 hammer_ioc_mrecord_any_t mrec;
964 if (ac == 1 && strcmp(*av, "header") == 0) {
966 } else if (ac != 0) {
972 * Read and process the PFS header
974 pickup.signature = 0;
977 mrec = read_mrecord(0, &error, &pickup);
980 * Dump the PFS header. mirror-dump takes its input from the output
981 * of a mirror-read so getpfs() can't be used to get a fd to be passed
984 if (header_only && mrec != NULL) {
985 dump_pfsd(&mrec->pfs.pfsd, -1);
994 * Read and process bulk records
997 size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
1001 while (offset < size) {
1002 mrec = (void *)((char *)buf + offset);
1003 bytes = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
1004 if (offset + bytes > size) {
1005 errx(1, "Misaligned record");
1009 switch(mrec->head.type & HAMMER_MRECF_TYPE_MASK) {
1010 case HAMMER_MREC_TYPE_REC_BADCRC:
1011 case HAMMER_MREC_TYPE_REC:
1012 printf("Record lo=%08x obj=%016jx key=%016jx "
1014 mrec->rec.leaf.base.localization,
1015 (uintmax_t)mrec->rec.leaf.base.obj_id,
1016 (uintmax_t)mrec->rec.leaf.base.key,
1017 mrec->rec.leaf.base.rec_type,
1018 mrec->rec.leaf.base.obj_type);
1019 if (mrec->head.type ==
1020 HAMMER_MREC_TYPE_REC_BADCRC) {
1021 printf(" (BAD CRC)");
1024 printf(" tids %016jx:%016jx data=%d\n",
1025 (uintmax_t)mrec->rec.leaf.base.create_tid,
1026 (uintmax_t)mrec->rec.leaf.base.delete_tid,
1027 mrec->rec.leaf.data_len);
1029 case HAMMER_MREC_TYPE_PASS:
1030 printf("Pass lo=%08x obj=%016jx key=%016jx "
1031 "rt=%02x ot=%02x\n",
1032 mrec->rec.leaf.base.localization,
1033 (uintmax_t)mrec->rec.leaf.base.obj_id,
1034 (uintmax_t)mrec->rec.leaf.base.key,
1035 mrec->rec.leaf.base.rec_type,
1036 mrec->rec.leaf.base.obj_type);
1037 printf(" tids %016jx:%016jx data=%d\n",
1038 (uintmax_t)mrec->rec.leaf.base.create_tid,
1039 (uintmax_t)mrec->rec.leaf.base.delete_tid,
1040 mrec->rec.leaf.data_len);
1042 case HAMMER_MREC_TYPE_SKIP:
1043 printf("Skip lo=%08x obj=%016jx key=%016jx rt=%02x to\n"
1044 " lo=%08x obj=%016jx key=%016jx rt=%02x\n",
1045 mrec->skip.skip_beg.localization,
1046 (uintmax_t)mrec->skip.skip_beg.obj_id,
1047 (uintmax_t)mrec->skip.skip_beg.key,
1048 mrec->skip.skip_beg.rec_type,
1049 mrec->skip.skip_end.localization,
1050 (uintmax_t)mrec->skip.skip_end.obj_id,
1051 (uintmax_t)mrec->skip.skip_end.key,
1052 mrec->skip.skip_end.rec_type);
1061 * Read and process the termination sync record.
1063 mrec = read_mrecord(0, &error, &pickup);
1065 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
1066 mrec->head.type != HAMMER_MREC_TYPE_IDLE)) {
1067 fprintf(stderr, "Mirror-dump: Did not get termination "
1073 * Continue with more batches until EOF.
1075 mrec = read_mrecord(0, &error, &pickup);
1084 hammer_cmd_mirror_copy(char **av, int ac, int streaming)
1089 const char *xav[32];
1091 char *sh, *user, *host, *rfs;
1100 signal(SIGPIPE, SIG_IGN);
1103 if (pipe(fds) < 0) {
1111 if ((pid1 = fork()) == 0) {
1112 signal(SIGPIPE, SIG_DFL);
1117 if ((rfs = strchr(av[0], ':')) != NULL) {
1120 if((sh = getenv("HAMMER_RSH")) == NULL)
1128 if ((host = strchr(av[0], '@')) != NULL) {
1129 user = strndup( av[0], (host++ - av[0]));
1130 host = strndup( host, (rfs++ - host));
1135 host = strndup( av[0], (rfs++ - av[0]));
1143 xav[xac++] = SshPort;
1146 xav[xac++] = "hammer";
1148 switch(VerboseOpt) {
1158 xav[xac++] = "-vvv";
1165 snprintf(tbuf, sizeof(tbuf), "%d", TimeoutOpt);
1169 if (SplitupOptStr) {
1171 xav[xac++] = SplitupOptStr;
1174 xav[xac++] = "mirror-read-stream";
1176 xav[xac++] = "mirror-read";
1179 execvp(*xav, (void *)xav);
1181 hammer_cmd_mirror_read(av, 1, streaming);
1191 if ((pid2 = fork()) == 0) {
1192 signal(SIGPIPE, SIG_DFL);
1197 if ((rfs = strchr(av[1], ':')) != NULL) {
1200 if((sh = getenv("HAMMER_RSH")) == NULL)
1208 if ((host = strchr(av[1], '@')) != NULL) {
1209 user = strndup( av[1], (host++ - av[1]));
1210 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";
1243 xav[xac++] = "mirror-write";
1246 execvp(*xav, (void *)xav);
1248 hammer_cmd_mirror_write(av + 1, 1);
1257 while (waitpid(pid1, NULL, 0) <= 0)
1259 while (waitpid(pid2, NULL, 0) <= 0)
1263 * If the link is lost restart
1267 fprintf(stderr, "\nLost Link\n");
1270 sleep(15 + DelayOpt);
1277 * Read and return multiple mrecords
1281 read_mrecords(int fd, char *buf, u_int size, struct hammer_ioc_mrecord_head *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 errx(1, "read_mrecords: short read on pipe");
1310 if (pickup->signature != HAMMER_IOC_MIRROR_SIGNATURE) {
1311 errx(1, "read_mrecords: malformed record on pipe, "
1316 if (pickup->rec_size < HAMMER_MREC_HEADSIZE ||
1317 pickup->rec_size > sizeof(*mrec) + HAMMER_XBUFSIZE) {
1318 errx(1, "read_mrecords: malformed record on pipe, "
1319 "illegal rec_size");
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 errx(1, "read_mrecords: short read on pipe");
1358 bcopy(pickup, buf + count, HAMMER_MREC_HEADSIZE);
1359 pickup->signature = 0;
1361 mrec = (void *)(buf + count);
1364 * Validate the completed record
1366 if (!hammer_crc_test_mrec_head(&mrec->head, mrec->head.rec_size)) {
1367 errx(1, "read_mrecords: malformed record on pipe, bad crc");
1372 * If its a B-Tree record validate the data crc.
1374 * NOTE: If the VFS passes us an explicitly errorde mrec
1375 * we just pass it through.
1377 type = mrec->head.type & HAMMER_MRECF_TYPE_MASK;
1379 if (type == HAMMER_MREC_TYPE_REC) {
1380 if (mrec->head.rec_size <
1381 sizeof(mrec->rec) + mrec->rec.leaf.data_len) {
1382 errx(1, "read_mrecords: malformed record on "
1383 "pipe, illegal element data_len");
1386 if (mrec->rec.leaf.data_len &&
1387 mrec->rec.leaf.data_offset &&
1388 hammer_crc_test_leaf(HammerVersion, &mrec->rec + 1, &mrec->rec.leaf) == 0) {
1390 "read_mrecords: data_crc did not "
1391 "match data! obj=%016jx key=%016jx\n",
1392 (uintmax_t)mrec->rec.leaf.base.obj_id,
1393 (uintmax_t)mrec->rec.leaf.base.key);
1395 "continuing, but there are problems\n");
1404 * Read and return a single mrecord.
1407 hammer_ioc_mrecord_any_t
1408 read_mrecord(int fdin, int *errorp, struct hammer_ioc_mrecord_head *pickup)
1410 hammer_ioc_mrecord_any_t mrec;
1411 struct hammer_ioc_mrecord_head mrechd;
1416 if (pickup && pickup->type != 0) {
1418 pickup->signature = 0;
1420 n = HAMMER_MREC_HEADSIZE;
1423 * Read in the PFSD header from the sender.
1425 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
1426 i = read(fdin, (char *)&mrechd + n, HAMMER_MREC_HEADSIZE - n);
1431 *errorp = 0; /* EOF */
1434 if (n != HAMMER_MREC_HEADSIZE) {
1435 fprintf(stderr, "short read of mrecord header\n");
1440 if (mrechd.signature != HAMMER_IOC_MIRROR_SIGNATURE) {
1441 fprintf(stderr, "read_mrecord: bad signature\n");
1445 bytes = HAMMER_HEAD_DOALIGN(mrechd.rec_size);
1446 assert(bytes >= sizeof(mrechd));
1447 mrec = malloc(bytes);
1448 mrec->head = mrechd;
1451 i = read(fdin, (char *)mrec + n, bytes - n);
1457 fprintf(stderr, "read_mrecord: short read on payload\n");
1461 if (!hammer_crc_test_mrec_head(&mrec->head, mrec->head.rec_size)) {
1462 fprintf(stderr, "read_mrecord: bad CRC\n");
1472 write_mrecord(int fdout, uint32_t type, hammer_ioc_mrecord_any_t mrec,
1475 char zbuf[HAMMER_HEAD_ALIGN];
1478 pad = HAMMER_HEAD_DOALIGN(bytes) - bytes;
1480 assert(bytes >= (int)sizeof(mrec->head));
1481 bzero(&mrec->head, sizeof(mrec->head));
1482 mrec->head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
1483 mrec->head.type = type;
1484 mrec->head.rec_size = bytes;
1485 hammer_crc_set_mrec_head(&mrec->head, bytes);
1486 if (write(fdout, mrec, bytes) != bytes) {
1487 err(1, "write_mrecord");
1492 if (write(fdout, zbuf, pad) != pad) {
1493 err(1, "write_mrecord");
1500 * Generate a mirroring header with the pfs information of the
1501 * originating filesytem.
1505 generate_mrec_header(int fd, int pfs_id,
1506 union hammer_ioc_mrecord_any *mrec_tmp)
1508 struct hammer_ioc_pseudofs_rw pfs;
1510 bzero(mrec_tmp, sizeof(*mrec_tmp));
1511 clrpfs(&pfs, &mrec_tmp->pfs.pfsd, pfs_id);
1513 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1514 err(1, "Mirror-read: not a HAMMER fs/pseudofs!");
1517 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1518 errx(1, "Mirror-read: HAMMER PFS version mismatch!");
1521 mrec_tmp->pfs.version = pfs.version;
1525 * Validate the pfs information from the originating filesystem
1526 * against the target filesystem. shared_uuid must match.
1528 * return -1 if we got a TERM record
1532 validate_mrec_header(int fd, int fdin, int is_target, int pfs_id,
1533 struct hammer_ioc_mrecord_head *pickup,
1534 hammer_tid_t *tid_begp, hammer_tid_t *tid_endp)
1536 struct hammer_ioc_pseudofs_rw pfs;
1537 struct hammer_pseudofs_data pfsd;
1538 hammer_ioc_mrecord_any_t mrec;
1542 * Get the PFSD info from the target filesystem.
1544 clrpfs(&pfs, &pfsd, pfs_id);
1545 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1546 err(1, "mirror-write: not a HAMMER fs/pseudofs!");
1549 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1550 errx(1, "mirror-write: HAMMER PFS version mismatch!");
1554 mrec = read_mrecord(fdin, &error, pickup);
1557 errx(1, "validate_mrec_header: short read");
1562 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
1567 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
1568 errx(1, "validate_mrec_header: did not get expected "
1569 "PFSD record type");
1572 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
1573 errx(1, "validate_mrec_header: unexpected payload size");
1576 if (mrec->pfs.version != pfs.version) {
1577 errx(1, "validate_mrec_header: Version mismatch");
1582 * Whew. Ok, is the read PFS info compatible with the target?
1584 if (hammer_uuid_compare(&mrec->pfs.pfsd.shared_uuid, &pfsd.shared_uuid)) {
1585 errx(1, "mirror-write: source and target have "
1586 "different shared-uuid's!");
1589 if (is_target && hammer_is_pfs_master(&pfsd)) {
1590 errx(1, "mirror-write: target must be in slave mode");
1594 *tid_begp = mrec->pfs.pfsd.sync_beg_tid;
1596 *tid_endp = mrec->pfs.pfsd.sync_end_tid;
1603 update_pfs_snapshot(int fd, hammer_tid_t snapshot_tid, int pfs_id)
1605 struct hammer_ioc_pseudofs_rw pfs;
1606 struct hammer_pseudofs_data pfsd;
1608 clrpfs(&pfs, &pfsd, pfs_id);
1609 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1610 err(1, "update_pfs_snapshot (read)");
1614 if (pfsd.sync_end_tid != snapshot_tid) {
1615 pfsd.sync_end_tid = snapshot_tid;
1616 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) != 0) {
1617 err(1, "update_pfs_snapshot (rewrite)");
1620 if (VerboseOpt >= 2) {
1622 "Mirror-write: Completed, updated snapshot "
1624 (uintmax_t)snapshot_tid);
1631 * Bandwidth-limited write in chunks
1635 writebw(int fd, const void *buf, size_t nbytes,
1636 uint64_t *bwcount, struct timeval *tv1)
1647 if (*bwcount + nbytes > BandwidthOpt)
1648 n = BandwidthOpt - *bwcount;
1652 r = write(fd, buf, n);
1656 buf = (const char *)buf + r;
1661 if (*bwcount >= BandwidthOpt) {
1662 gettimeofday(&tv2, NULL);
1663 usec = (int)(tv2.tv_sec - tv1->tv_sec) * 1000000 +
1664 (int)(tv2.tv_usec - tv1->tv_usec);
1665 if (usec >= 0 && usec < 1000000)
1666 usleep(1000000 - usec);
1667 gettimeofday(tv1, NULL);
1668 *bwcount -= BandwidthOpt;
1675 * Get a yes or no answer from the terminal. The program may be run as
1676 * part of a two-way pipe so we cannot use stdin for this operation.
1686 fp = fopen("/dev/tty", "r");
1688 fprintf(stderr, "No terminal for response\n");
1692 while (fgets(buf, sizeof(buf), fp) != NULL) {
1693 if (buf[0] == 'y' || buf[0] == 'Y') {
1697 if (buf[0] == 'n' || buf[0] == 'N') {
1701 fprintf(stderr, "Response not understood\n");
1710 score_printf(size_t i, size_t w, const char *ctl, ...)
1714 static size_t SSize;
1715 static int SFd = -1;
1716 static char ScoreBuf[1024];
1718 if (ScoreBoardFile == NULL)
1720 assert(i + w < sizeof(ScoreBuf));
1722 SFd = open(ScoreBoardFile, O_RDWR|O_CREAT|O_TRUNC, 0644);
1727 for (n = 0; n < i; ++n) {
1728 if (ScoreBuf[n] == 0)
1732 vsnprintf(ScoreBuf + i, w - 1, ctl, va);
1734 n = strlen(ScoreBuf + i);
1736 ScoreBuf[i + n] = ' ';
1739 ScoreBuf[i + n] = '\n';
1742 pwrite(SFd, ScoreBuf, SSize, 0);
1747 hammer_check_restrict(const char *filesystem)
1752 if (RestrictTarget == NULL)
1754 rlen = strlen(RestrictTarget);
1755 if (strncmp(filesystem, RestrictTarget, rlen) != 0) {
1756 errx(1, "hammer-remote: restricted target");
1761 while (filesystem[rlen]) {
1763 filesystem[rlen] == '.' &&
1764 filesystem[rlen+1] == '.') {
1765 errx(1, "hammer-remote: '..' not allowed");
1768 if (filesystem[rlen] == '/')
1778 mirror_usage(int code)
1781 "hammer mirror-read <filesystem> [begin-tid]\n"
1782 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
1783 "hammer mirror-write <filesystem>\n"
1784 "hammer mirror-dump [header]\n"
1785 "hammer mirror-copy [[user@]host:]<filesystem>"
1786 " [[user@]host:]<filesystem>\n"
1787 "hammer mirror-stream [[user@]host:]<filesystem>"
1788 " [[user@]host:]<filesystem>\n"