sbin/hammer: Add /* not reached */
[dragonfly.git] / sbin / hammer / cmd_mirror.c
blob56a61a922362b0d7044dbca06070f6fa0a07570b
1 /*
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
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
35 #include "hammer.h"
37 #define LINE1 0,20
38 #define LINE2 20,78
39 #define LINE3 90,70
41 #define SERIALBUF_SIZE (512 * 1024)
43 typedef struct histogram {
44 hammer_tid_t tid;
45 uint64_t bytes;
46 } *histogram_t;
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,
56 int *repeatp);
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.
85 void
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);
97 int interrupted = 0;
98 int error;
99 int fd;
100 int n;
101 int didwork;
102 int histogram;
103 int histindex;
104 int histmax;
105 int repeat = 0;
106 int sameline;
107 int64_t total_bytes;
108 time_t base_t = time(NULL);
109 struct timeval bwtv;
110 uint64_t bwcount;
111 uint64_t estbytes;
113 if (ac == 0 || ac > 2)
114 mirror_usage(1);
115 filesystem = av[0];
116 hammer_check_restrict(filesystem);
118 pickup.signature = 0;
119 pickup.type = 0;
120 histogram = 0;
121 histindex = 0;
122 histmax = 0;
123 histogram_ary = NULL;
124 sameline = 0;
126 again:
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);
133 if (streaming >= 0)
134 score_printf(LINE1, "Running");
136 if (streaming >= 0 && VerboseOpt && VerboseOpt < 2) {
137 fprintf(stderr, "%cRunning \b\b", (sameline ? '\r' : '\n'));
138 fflush(stderr);
139 sameline = 1;
141 sameline = 1;
142 total_bytes = 0;
143 gettimeofday(&bwtv, NULL);
144 bwcount = 0;
147 * Send initial header for the purpose of determining the
148 * shared-uuid.
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
157 * begin TID.
159 if (TwoWayPipeOpt) {
160 mirror.tid_beg = 0;
161 n = validate_mrec_header(fd, 0, 0, pfs.pfs_id, &pickup,
162 NULL, &mirror.tid_beg);
163 if (n < 0) { /* got TERM record */
164 relpfs(fd, &pfs);
165 free(buf);
166 free(histogram_ary);
167 return;
169 ++mirror.tid_beg;
170 } else if (streaming && histogram) {
171 mirror.tid_beg = histogram_ary[histindex].tid + 1;
172 } else {
173 mirror.tid_beg = 0;
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;
185 mirror.ubuf = buf;
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.
195 #if 0
196 if (streaming && histogram && histindex == histend) {
197 if (mirror.tid_end - mirror.tid_beg > BULK_MINIMUM)
198 histogram = 0;
200 #endif
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");
210 sameline = 0;
212 histmax = generate_histogram(fd, filesystem,
213 &histogram_ary, &mirror,
214 &repeat);
215 histindex = 0;
216 histogram = 1;
219 * Just stream the histogram, then stop
221 if (streaming == 0)
222 streaming = -1;
225 if (streaming && histogram) {
226 ++histindex;
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;
230 } else {
231 estbytes = 0;
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);
245 * An additional argument overrides the beginning TID regardless
246 * of what mode we are in. This is not recommending if operating
247 * in two-way mode.
249 if (ac == 2)
250 mirror.tid_beg = strtoull(av[1], NULL, 0);
252 if (streaming == 0 || VerboseOpt >= 2) {
253 fprintf(stderr,
254 "Mirror-read: Mirror %016jx to %016jx",
255 (uintmax_t)mirror.tid_beg, (uintmax_t)mirror.tid_end);
256 if (histogram)
257 fprintf(stderr, " (bulk= %ju)", (uintmax_t)estbytes);
258 fprintf(stderr, "\n");
259 fflush(stderr);
261 if (mirror.key_beg.obj_id != (int64_t)HAMMER_MIN_OBJID) {
262 fprintf(stderr, "Mirror-read: Resuming at object %016jx\n",
263 (uintmax_t)mirror.key_beg.obj_id);
267 * Nothing to do if begin equals end.
269 if (mirror.tid_beg >= mirror.tid_end) {
270 if (streaming == 0 || VerboseOpt >= 2)
271 fprintf(stderr, "Mirror-read: No work to do\n");
272 sleep(DelayOpt);
273 didwork = 0;
274 histogram = 0;
275 goto done;
277 didwork = 1;
280 * Write out bulk records
282 mirror.ubuf = buf;
283 mirror.size = SERIALBUF_SIZE;
285 do {
286 mirror.count = 0;
287 mirror.pfs_id = pfs.pfs_id;
288 mirror.shared_uuid = pfs.ondisk->shared_uuid;
289 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
290 score_printf(LINE3, "Mirror-read %s failed: %s",
291 filesystem, strerror(errno));
292 err(1, "Mirror-read %s failed", filesystem);
293 /* not reached */
295 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
296 score_printf(LINE3, "Mirror-read %s fatal error %d",
297 filesystem, mirror.head.error);
298 errx(1, "Mirror-read %s fatal error %d",
299 filesystem, mirror.head.error);
300 /* not reached */
302 if (mirror.count) {
303 if (BandwidthOpt) {
304 n = writebw(1, mirror.ubuf, mirror.count,
305 &bwcount, &bwtv);
306 } else {
307 n = write(1, mirror.ubuf, mirror.count);
309 if (n != mirror.count) {
310 score_printf(LINE3,
311 "Mirror-read %s failed: "
312 "short write",
313 filesystem);
314 errx(1, "Mirror-read %s failed: short write",
315 filesystem);
316 /* not reached */
319 total_bytes += mirror.count;
320 if (streaming && VerboseOpt) {
321 fprintf(stderr,
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);
327 fflush(stderr);
328 sameline = 0;
329 } else if (streaming) {
330 score_printf(LINE2,
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
342 if (TimeoutOpt &&
343 (unsigned)(time(NULL) - base_t) > (unsigned)TimeoutOpt) {
344 score_printf(LINE3,
345 "Mirror-read %s interrupted by timer at"
346 " %016jx",
347 filesystem,
348 (uintmax_t)mirror.key_cur.obj_id);
349 fprintf(stderr,
350 "Mirror-read %s interrupted by timer at"
351 " %016jx\n",
352 filesystem,
353 (uintmax_t)mirror.key_cur.obj_id);
354 interrupted = 1;
355 break;
357 } while (mirror.count != 0);
359 done:
360 if (streaming && VerboseOpt && sameline == 0) {
361 fprintf(stderr, "\n");
362 fflush(stderr);
363 sameline = 1;
367 * Write out the termination sync record - only if not interrupted
369 if (interrupted == 0) {
370 if (didwork) {
371 write_mrecord(1, HAMMER_MREC_TYPE_SYNC,
372 &mrec_tmp, sizeof(mrec_tmp.sync));
373 } else {
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
382 * the target.
384 if (TwoWayPipeOpt) {
385 mrec = read_mrecord(0, &error, &pickup);
386 if (mrec == NULL ||
387 mrec->head.type != HAMMER_MREC_TYPE_UPDATE ||
388 mrec->head.rec_size != sizeof(mrec->update)) {
389 errx(1, "mirror_read: Did not get final "
390 "acknowledgement packet from target");
391 /* not reached */
393 if (interrupted) {
394 if (CyclePath) {
395 hammer_set_cycle(&mirror.key_cur,
396 mirror.tid_beg);
397 fprintf(stderr, "Cyclefile %s updated for "
398 "continuation\n", CyclePath);
400 } else {
401 sync_tid = mrec->update.tid;
402 if (CyclePath) {
403 hammer_key_beg_init(&mirror.key_beg);
404 hammer_set_cycle(&mirror.key_beg, sync_tid);
405 fprintf(stderr,
406 "Cyclefile %s updated to 0x%016jx\n",
407 CyclePath, (uintmax_t)sync_tid);
410 free(mrec);
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);
419 time_t t2;
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 &&
430 streaming >= 0) {
431 fprintf(stderr, " (bulk incremental)");
433 relpfs(fd, &pfs);
434 goto again;
438 if (VerboseOpt && streaming >= 0) {
439 fprintf(stderr, " W");
440 fflush(stderr);
441 } else if (streaming >= 0) {
442 score_printf(LINE1, "Waiting");
444 pfs.ondisk->sync_end_tid = mirror.tid_end;
445 if (streaming < 0) {
447 * Fake streaming mode when using a histogram to
448 * break up a mirror-read, do not wait on source.
450 streaming = 0;
451 } else if (ioctl(fd, HAMMERIOC_WAI_PSEUDOFS, &pfs) < 0) {
452 score_printf(LINE3,
453 "Mirror-read %s: cannot stream: %s\n",
454 filesystem, strerror(errno));
455 fprintf(stderr,
456 "Mirror-read %s: cannot stream: %s\n",
457 filesystem, strerror(errno));
458 } else {
459 t2 = time(NULL) - t1;
460 if (t2 >= 0 && t2 < DelayOpt) {
461 if (VerboseOpt) {
462 fprintf(stderr, "\bD");
463 fflush(stderr);
465 sleep(DelayOpt - t2);
467 if (VerboseOpt) {
468 fprintf(stderr, "\b ");
469 fflush(stderr);
471 relpfs(fd, &pfs);
472 goto again;
475 write_mrecord(1, HAMMER_MREC_TYPE_TERM,
476 &mrec_tmp, sizeof(mrec_tmp.sync));
477 relpfs(fd, &pfs);
478 free(buf);
479 free(histogram_ary);
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)
500 static int
501 generate_histogram(int fd, const char *filesystem,
502 histogram_t *histogram_ary,
503 struct hammer_ioc_mirror_rw *mirror_base,
504 int *repeatp)
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;
510 hammer_tid_t tid;
511 hammer_tid_t tidx;
512 uint64_t *tid_bytes;
513 uint64_t total;
514 uint64_t accum;
515 int chunkno;
516 int i;
517 int res;
518 int off;
519 int len;
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) *
528 (HIST_COUNT + 2));
530 if (tid_beg >= tid_end)
531 return(0);
533 /* needs 2 extra */
534 tid_bytes = malloc(sizeof(*tid_bytes) * (HIST_COUNT + 2));
535 bzero(tid_bytes, sizeof(*tid_bytes) * (HIST_COUNT + 2));
537 if (*repeatp == 0) {
538 fprintf(stderr, "Prescan to break up bulk transfer");
539 if (VerboseOpt > 1)
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
549 * to skips.
551 total = 0;
552 accum = 0;
553 chunkno = 0;
554 for (;;) {
555 mirror.count = 0;
556 if (ioctl(fd, HAMMERIOC_MIRROR_READ, &mirror) < 0) {
557 err(1, "Mirror-read %s failed", filesystem);
558 /* not reached */
560 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
561 errx(1, "Mirror-read %s fatal error %d",
562 filesystem, mirror.head.error);
563 /* not reached */
565 for (off = 0;
566 off < mirror.count;
567 off += HAMMER_HEAD_DOALIGN(mrec->head.rec_size)) {
568 mrec = (void *)((char *)mirror.ubuf + off);
571 * We only care about general RECs and PASS
572 * records. We ignore SKIPs.
574 switch (mrec->head.type & HAMMER_MRECF_TYPE_LOMASK) {
575 case HAMMER_MREC_TYPE_REC:
576 case HAMMER_MREC_TYPE_PASS:
577 break;
578 default:
579 continue;
583 * Calculate for two indices, create_tid and
584 * delete_tid. Record data only applies to
585 * the create_tid.
587 * When tid is exactly on the boundary it really
588 * belongs to the previous entry because scans
589 * are inclusive of the ending entry.
591 tid = mrec->rec.leaf.base.delete_tid;
592 if (tid && tid >= tid_beg && tid <= tid_end) {
593 len = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
594 if (mrec->head.type ==
595 HAMMER_MREC_TYPE_REC) {
596 len -= HAMMER_HEAD_DOALIGN(
597 mrec->rec.leaf.data_len);
598 assert(len > 0);
600 i = (tid - tid_beg) * HIST_COUNT /
601 (tid_end - tid_beg);
602 tidx = tid_beg + i * (tid_end - tid_beg) /
603 HIST_COUNT;
604 if (tid == tidx && i)
605 --i;
606 assert(i >= 0 && i < HIST_COUNT);
607 tid_bytes[i] += len;
608 total += len;
609 accum += len;
612 tid = mrec->rec.leaf.base.create_tid;
613 if (tid && tid >= tid_beg && tid <= tid_end) {
614 len = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
615 if (mrec->head.type ==
616 HAMMER_MREC_TYPE_REC_NODATA) {
617 len += HAMMER_HEAD_DOALIGN(
618 mrec->rec.leaf.data_len);
620 i = (tid - tid_beg) * HIST_COUNT /
621 (tid_end - tid_beg);
622 tidx = tid_beg + i * (tid_end - tid_beg) /
623 HIST_COUNT;
624 if (tid == tidx && i)
625 --i;
626 assert(i >= 0 && i < HIST_COUNT);
627 tid_bytes[i] += len;
628 total += len;
629 accum += len;
632 if (*repeatp == 0 && accum > SplitupOpt) {
633 if (VerboseOpt > 1) {
634 fprintf(stderr, ".");
635 fflush(stderr);
637 ++chunkno;
638 score_printf(LINE2, "Prescan chunk %d", chunkno);
639 accum = 0;
641 if (mirror.count == 0)
642 break;
643 mirror.key_beg = mirror.key_cur;
647 * Reduce to SplitupOpt (default 4GB) chunks. This code may
648 * use up to two additional elements. Do the array in-place.
650 * Inefficient degenerate cases can occur if we do not accumulate
651 * at least the requested split amount, so error on the side of
652 * going over a bit.
654 res = 0;
655 (*histogram_ary)[res].tid = tid_beg;
656 (*histogram_ary)[res].bytes = tid_bytes[0];
657 for (i = 1; i < HIST_COUNT; ++i) {
658 if ((*histogram_ary)[res].bytes >= SplitupOpt) {
659 ++res;
660 (*histogram_ary)[res].tid = tid_beg +
661 i * (tid_end - tid_beg) /
662 HIST_COUNT;
663 (*histogram_ary)[res].bytes = 0;
666 (*histogram_ary)[res].bytes += tid_bytes[i];
668 ++res;
669 (*histogram_ary)[res].tid = tid_end;
670 (*histogram_ary)[res].bytes = -1;
672 if (*repeatp == 0) {
673 if (VerboseOpt > 1)
674 fprintf(stderr, "\n"); /* newline after ... */
675 score_printf(LINE3, "Prescan %d chunks, total %ju MBytes",
676 res, (uintmax_t)total / (1024 * 1024));
677 fprintf(stderr, "Prescan %d chunks, total %ju MBytes (",
678 res, (uintmax_t)total / (1024 * 1024));
679 for (i = 0; i < res && i < 3; ++i) {
680 if (i)
681 fprintf(stderr, ", ");
682 fprintf(stderr, "%ju",
683 (uintmax_t)(*histogram_ary)[i].bytes);
685 if (i < res)
686 fprintf(stderr, ", ...");
687 fprintf(stderr, ")\n");
689 assert(res <= HIST_COUNT);
690 *repeatp = 1;
692 free(tid_bytes);
693 return(res);
696 static void
697 create_pfs(const char *filesystem, uuid_t *s_uuid)
699 if (ForceYesOpt == 1) {
700 fprintf(stderr, "PFS slave %s does not exist. "
701 "Auto create new slave PFS!\n", filesystem);
703 } else {
704 fprintf(stderr, "PFS slave %s does not exist.\n"
705 "Do you want to create a new slave PFS? [y/n] ",
706 filesystem);
707 fflush(stderr);
708 if (getyntty() != 1) {
709 errx(1, "Aborting operation");
710 /* not reached */
714 uint32_t status;
715 char *shared_uuid = NULL;
716 uuid_to_string(s_uuid, &shared_uuid, &status);
718 char *cmd = NULL;
719 asprintf(&cmd, "/sbin/hammer pfs-slave '%s' shared-uuid=%s 1>&2",
720 filesystem, shared_uuid);
721 free(shared_uuid);
723 if (cmd == NULL) {
724 errx(1, "Failed to alloc memory");
725 /* not reached */
727 if (system(cmd) != 0)
728 fprintf(stderr, "Failed to create PFS\n");
729 free(cmd);
733 * Pipe the mirroring data stream on stdin to the HAMMER VFS, adding
734 * some additional packet types to negotiate TID ranges and to verify
735 * completion. The HAMMER VFS does most of the work.
737 * It is important to note that the mirror.key_{beg,end} range must
738 * match the ranged used by the original. For now both sides use
739 * range the entire key space.
741 * It is even more important that the records in the stream conform
742 * to the TID range also supplied in the stream. The HAMMER VFS will
743 * use the REC, PASS, and SKIP record types to track the portions of
744 * the B-Tree being scanned in order to be able to proactively delete
745 * records on the target within those active areas that are not mentioned
746 * by the source.
748 * The mirror.key_cur field is used by the VFS to do this tracking. It
749 * must be initialized to key_beg but then is persistently updated by
750 * the HAMMER VFS on each successive ioctl() call. If you blow up this
751 * field you will blow up the mirror target, possibly to the point of
752 * deleting everything. As a safety measure the HAMMER VFS simply marks
753 * the records that the source has destroyed as deleted on the target,
754 * and normal pruning operations will deal with their final disposition
755 * at some later time.
757 void
758 hammer_cmd_mirror_write(char **av, int ac)
760 struct hammer_ioc_mirror_rw mirror;
761 const char *filesystem;
762 char *buf = malloc(SERIALBUF_SIZE);
763 struct hammer_ioc_pseudofs_rw pfs;
764 struct hammer_ioc_mrecord_head pickup;
765 struct hammer_ioc_synctid synctid;
766 union hammer_ioc_mrecord_any mrec_tmp;
767 hammer_ioc_mrecord_any_t mrec;
768 struct stat st;
769 int error;
770 int fd;
771 int n;
773 if (ac != 1)
774 mirror_usage(1);
775 filesystem = av[0];
776 hammer_check_restrict(filesystem);
778 pickup.signature = 0;
779 pickup.type = 0;
781 again:
782 bzero(&mirror, sizeof(mirror));
783 hammer_key_beg_init(&mirror.key_beg);
784 hammer_key_end_init(&mirror.key_end);
785 mirror.key_end = mirror.key_beg;
788 * Read initial packet
790 mrec = read_mrecord(0, &error, &pickup);
791 if (mrec == NULL) {
792 if (error == 0) {
793 errx(1, "validate_mrec_header: short read");
794 /* not reached */
796 exit(1);
799 * Validate packet
801 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
802 free(buf);
803 return;
805 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
806 errx(1, "validate_mrec_header: did not get expected "
807 "PFSD record type");
808 /* not reached */
810 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
811 errx(1, "validate_mrec_header: unexpected payload size");
812 /* not reached */
816 * Create slave PFS if it doesn't yet exist
818 if (lstat(filesystem, &st) != 0)
819 create_pfs(filesystem, &mrec->pfs.pfsd.shared_uuid);
820 free(mrec);
821 mrec = NULL;
823 fd = getpfs(&pfs, filesystem);
826 * In two-way mode the target writes out a PFS packet first.
827 * The source uses our tid_end as its tid_beg by default,
828 * picking up where it left off.
830 mirror.tid_beg = 0;
831 if (TwoWayPipeOpt) {
832 generate_mrec_header(fd, pfs.pfs_id, &mrec_tmp);
833 if (mirror.tid_beg < mrec_tmp.pfs.pfsd.sync_beg_tid)
834 mirror.tid_beg = mrec_tmp.pfs.pfsd.sync_beg_tid;
835 mirror.tid_end = mrec_tmp.pfs.pfsd.sync_end_tid;
836 write_mrecord(1, HAMMER_MREC_TYPE_PFSD,
837 &mrec_tmp, sizeof(mrec_tmp.pfs));
841 * Read and process the PFS header. The source informs us of
842 * the TID range the stream represents.
844 n = validate_mrec_header(fd, 0, 1, pfs.pfs_id, &pickup,
845 &mirror.tid_beg, &mirror.tid_end);
846 if (n < 0) { /* got TERM record */
847 relpfs(fd, &pfs);
848 free(buf);
849 return;
852 mirror.ubuf = buf;
853 mirror.size = SERIALBUF_SIZE;
856 * Read and process bulk records (REC, PASS, and SKIP types).
858 * On your life, do NOT mess with mirror.key_cur or your mirror
859 * target may become history.
861 for (;;) {
862 mirror.count = 0;
863 mirror.pfs_id = pfs.pfs_id;
864 mirror.shared_uuid = pfs.ondisk->shared_uuid;
865 mirror.size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
866 if (mirror.size <= 0)
867 break;
868 if (ioctl(fd, HAMMERIOC_MIRROR_WRITE, &mirror) < 0) {
869 err(1, "Mirror-write %s failed", filesystem);
870 /* not reached */
872 if (mirror.head.flags & HAMMER_IOC_HEAD_ERROR) {
873 errx(1, "Mirror-write %s fatal error %d",
874 filesystem, mirror.head.error);
875 /* not reached */
877 #if 0
878 if (mirror.head.flags & HAMMER_IOC_HEAD_INTR) {
879 errx(1, "Mirror-write %s interrupted by timer at"
880 " %016llx",
881 filesystem,
882 mirror.key_cur.obj_id);
883 /* not reached */
885 #endif
889 * Read and process the termination sync record.
891 mrec = read_mrecord(0, &error, &pickup);
893 if (mrec && mrec->head.type == HAMMER_MREC_TYPE_TERM) {
894 fprintf(stderr, "Mirror-write: received termination request\n");
895 relpfs(fd, &pfs);
896 free(mrec);
897 free(buf);
898 return;
901 if (mrec == NULL ||
902 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
903 mrec->head.type != HAMMER_MREC_TYPE_IDLE) ||
904 mrec->head.rec_size != sizeof(mrec->sync)) {
905 errx(1, "Mirror-write %s: Did not get termination "
906 "sync record, or rec_size is wrong rt=%d",
907 filesystem, (mrec ? (int)mrec->head.type : -1));
908 /* not reached */
912 * Update the PFS info on the target so the user has visibility
913 * into the new snapshot, and sync the target filesystem.
915 if (mrec->head.type == HAMMER_MREC_TYPE_SYNC) {
916 update_pfs_snapshot(fd, mirror.tid_end, pfs.pfs_id);
918 bzero(&synctid, sizeof(synctid));
919 synctid.op = HAMMER_SYNCTID_SYNC2;
920 ioctl(fd, HAMMERIOC_SYNCTID, &synctid);
922 if (VerboseOpt >= 2) {
923 fprintf(stderr, "Mirror-write %s: succeeded\n",
924 filesystem);
928 free(mrec);
929 mrec = NULL;
932 * Report back to the originator.
934 if (TwoWayPipeOpt) {
935 mrec_tmp.update.tid = mirror.tid_end;
936 write_mrecord(1, HAMMER_MREC_TYPE_UPDATE,
937 &mrec_tmp, sizeof(mrec_tmp.update));
938 } else {
939 printf("Source can update synctid to 0x%016jx\n",
940 (uintmax_t)mirror.tid_end);
942 relpfs(fd, &pfs);
943 goto again;
946 void
947 hammer_cmd_mirror_dump(char **av, int ac)
949 char *buf = malloc(SERIALBUF_SIZE);
950 struct hammer_ioc_mrecord_head pickup;
951 hammer_ioc_mrecord_any_t mrec;
952 int error;
953 int size;
954 int offset;
955 int bytes;
956 int header_only = 0;
958 if (ac == 1 && strcmp(*av, "header") == 0)
959 header_only = 1;
960 else if (ac != 0)
961 mirror_usage(1);
964 * Read and process the PFS header
966 pickup.signature = 0;
967 pickup.type = 0;
969 mrec = read_mrecord(0, &error, &pickup);
972 * Dump the PFS header. mirror-dump takes its input from the output
973 * of a mirror-read so getpfs() can't be used to get a fd to be passed
974 * to dump_pfsd().
976 if (header_only && mrec != NULL) {
977 dump_pfsd(&mrec->pfs.pfsd, -1);
978 free(mrec);
979 free(buf);
980 return;
982 free(mrec);
984 again:
986 * Read and process bulk records
988 for (;;) {
989 size = read_mrecords(0, buf, SERIALBUF_SIZE, &pickup);
990 if (size <= 0)
991 break;
992 offset = 0;
993 while (offset < size) {
994 mrec = (void *)((char *)buf + offset);
995 bytes = HAMMER_HEAD_DOALIGN(mrec->head.rec_size);
996 if (offset + bytes > size) {
997 errx(1, "Misaligned record");
998 /* not reached */
1001 switch(mrec->head.type & HAMMER_MRECF_TYPE_MASK) {
1002 case HAMMER_MREC_TYPE_REC_BADCRC:
1003 case HAMMER_MREC_TYPE_REC:
1004 printf("Record lo=%08x obj=%016jx key=%016jx "
1005 "rt=%02x ot=%02x",
1006 mrec->rec.leaf.base.localization,
1007 (uintmax_t)mrec->rec.leaf.base.obj_id,
1008 (uintmax_t)mrec->rec.leaf.base.key,
1009 mrec->rec.leaf.base.rec_type,
1010 mrec->rec.leaf.base.obj_type);
1011 if (mrec->head.type ==
1012 HAMMER_MREC_TYPE_REC_BADCRC) {
1013 printf(" (BAD CRC)");
1015 printf("\n");
1016 printf(" tids %016jx:%016jx data=%d\n",
1017 (uintmax_t)mrec->rec.leaf.base.create_tid,
1018 (uintmax_t)mrec->rec.leaf.base.delete_tid,
1019 mrec->rec.leaf.data_len);
1020 break;
1021 case HAMMER_MREC_TYPE_PASS:
1022 printf("Pass lo=%08x obj=%016jx key=%016jx "
1023 "rt=%02x ot=%02x\n",
1024 mrec->rec.leaf.base.localization,
1025 (uintmax_t)mrec->rec.leaf.base.obj_id,
1026 (uintmax_t)mrec->rec.leaf.base.key,
1027 mrec->rec.leaf.base.rec_type,
1028 mrec->rec.leaf.base.obj_type);
1029 printf(" tids %016jx:%016jx data=%d\n",
1030 (uintmax_t)mrec->rec.leaf.base.create_tid,
1031 (uintmax_t)mrec->rec.leaf.base.delete_tid,
1032 mrec->rec.leaf.data_len);
1033 break;
1034 case HAMMER_MREC_TYPE_SKIP:
1035 printf("Skip lo=%08x obj=%016jx key=%016jx rt=%02x to\n"
1036 " lo=%08x obj=%016jx key=%016jx rt=%02x\n",
1037 mrec->skip.skip_beg.localization,
1038 (uintmax_t)mrec->skip.skip_beg.obj_id,
1039 (uintmax_t)mrec->skip.skip_beg.key,
1040 mrec->skip.skip_beg.rec_type,
1041 mrec->skip.skip_end.localization,
1042 (uintmax_t)mrec->skip.skip_end.obj_id,
1043 (uintmax_t)mrec->skip.skip_end.key,
1044 mrec->skip.skip_end.rec_type);
1045 default:
1046 break;
1048 offset += bytes;
1053 * Read and process the termination sync record.
1055 mrec = read_mrecord(0, &error, &pickup);
1056 if (mrec == NULL ||
1057 (mrec->head.type != HAMMER_MREC_TYPE_SYNC &&
1058 mrec->head.type != HAMMER_MREC_TYPE_IDLE)) {
1059 fprintf(stderr, "Mirror-dump: Did not get termination "
1060 "sync record\n");
1062 free(mrec);
1065 * Continue with more batches until EOF.
1067 mrec = read_mrecord(0, &error, &pickup);
1068 if (mrec) {
1069 free(mrec);
1070 goto again;
1072 free(buf);
1075 void
1076 hammer_cmd_mirror_copy(char **av, int ac, int streaming)
1078 pid_t pid1;
1079 pid_t pid2;
1080 int fds[2];
1081 const char *xav[32];
1082 char tbuf[16];
1083 char *sh, *user, *host, *rfs;
1084 int xac;
1086 if (ac != 2)
1087 mirror_usage(1);
1089 TwoWayPipeOpt = 1;
1090 signal(SIGPIPE, SIG_IGN);
1092 again:
1093 if (pipe(fds) < 0) {
1094 err(1, "pipe");
1095 /* not reached */
1099 * Source
1101 if ((pid1 = fork()) == 0) {
1102 signal(SIGPIPE, SIG_DFL);
1103 dup2(fds[0], 0);
1104 dup2(fds[0], 1);
1105 close(fds[0]);
1106 close(fds[1]);
1107 if ((rfs = strchr(av[0], ':')) != NULL) {
1108 xac = 0;
1110 if((sh = getenv("HAMMER_RSH")) == NULL)
1111 xav[xac++] = "ssh";
1112 else
1113 xav[xac++] = sh;
1115 if (CompressOpt)
1116 xav[xac++] = "-C";
1118 if ((host = strchr(av[0], '@')) != NULL) {
1119 user = strndup( av[0], (host++ - av[0]));
1120 host = strndup( host, (rfs++ - host));
1121 xav[xac++] = "-l";
1122 xav[xac++] = user;
1123 xav[xac++] = host;
1124 } else {
1125 host = strndup( av[0], (rfs++ - av[0]));
1126 user = NULL;
1127 xav[xac++] = host;
1131 if (SshPort) {
1132 xav[xac++] = "-p";
1133 xav[xac++] = SshPort;
1136 xav[xac++] = "hammer";
1138 switch(VerboseOpt) {
1139 case 0:
1140 break;
1141 case 1:
1142 xav[xac++] = "-v";
1143 break;
1144 case 2:
1145 xav[xac++] = "-vv";
1146 break;
1147 default:
1148 xav[xac++] = "-vvv";
1149 break;
1151 if (ForceYesOpt)
1152 xav[xac++] = "-y";
1153 xav[xac++] = "-2";
1154 if (TimeoutOpt) {
1155 snprintf(tbuf, sizeof(tbuf), "%d", TimeoutOpt);
1156 xav[xac++] = "-t";
1157 xav[xac++] = tbuf;
1159 if (SplitupOptStr) {
1160 xav[xac++] = "-S";
1161 xav[xac++] = SplitupOptStr;
1163 if (streaming)
1164 xav[xac++] = "mirror-read-stream";
1165 else
1166 xav[xac++] = "mirror-read";
1167 xav[xac++] = rfs;
1168 xav[xac++] = NULL;
1169 execvp(*xav, (void *)xav);
1170 } else {
1171 hammer_cmd_mirror_read(av, 1, streaming);
1172 fflush(stdout);
1173 fflush(stderr);
1175 _exit(1);
1179 * Target
1181 if ((pid2 = fork()) == 0) {
1182 signal(SIGPIPE, SIG_DFL);
1183 dup2(fds[1], 0);
1184 dup2(fds[1], 1);
1185 close(fds[0]);
1186 close(fds[1]);
1187 if ((rfs = strchr(av[1], ':')) != NULL) {
1188 xac = 0;
1190 if((sh = getenv("HAMMER_RSH")) == NULL)
1191 xav[xac++] = "ssh";
1192 else
1193 xav[xac++] = sh;
1195 if (CompressOpt)
1196 xav[xac++] = "-C";
1198 if ((host = strchr(av[1], '@')) != NULL) {
1199 user = strndup( av[1], (host++ - av[1]));
1200 host = strndup( host, (rfs++ - host));
1201 xav[xac++] = "-l";
1202 xav[xac++] = user;
1203 xav[xac++] = host;
1204 } else {
1205 host = strndup( av[1], (rfs++ - av[1]));
1206 user = NULL;
1207 xav[xac++] = host;
1210 if (SshPort) {
1211 xav[xac++] = "-p";
1212 xav[xac++] = SshPort;
1215 xav[xac++] = "hammer";
1217 switch(VerboseOpt) {
1218 case 0:
1219 break;
1220 case 1:
1221 xav[xac++] = "-v";
1222 break;
1223 case 2:
1224 xav[xac++] = "-vv";
1225 break;
1226 default:
1227 xav[xac++] = "-vvv";
1228 break;
1230 if (ForceYesOpt)
1231 xav[xac++] = "-y";
1232 xav[xac++] = "-2";
1233 xav[xac++] = "mirror-write";
1234 xav[xac++] = rfs;
1235 xav[xac++] = NULL;
1236 execvp(*xav, (void *)xav);
1237 } else {
1238 hammer_cmd_mirror_write(av + 1, 1);
1239 fflush(stdout);
1240 fflush(stderr);
1242 _exit(1);
1244 close(fds[0]);
1245 close(fds[1]);
1247 while (waitpid(pid1, NULL, 0) <= 0)
1249 while (waitpid(pid2, NULL, 0) <= 0)
1253 * If the link is lost restart
1255 if (streaming) {
1256 if (VerboseOpt) {
1257 fprintf(stderr, "\nLost Link\n");
1258 fflush(stderr);
1260 sleep(15 + DelayOpt);
1261 goto again;
1267 * Read and return multiple mrecords
1269 static int
1270 read_mrecords(int fd, char *buf, u_int size, hammer_ioc_mrecord_head_t pickup)
1272 hammer_ioc_mrecord_any_t mrec;
1273 u_int count;
1274 size_t n;
1275 size_t i;
1276 size_t bytes;
1277 int type;
1279 count = 0;
1280 while (size - count >= HAMMER_MREC_HEADSIZE) {
1282 * Cached the record header in case we run out of buffer
1283 * space.
1285 fflush(stdout);
1286 if (pickup->signature == 0) {
1287 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
1288 i = read(fd, (char *)pickup + n,
1289 HAMMER_MREC_HEADSIZE - n);
1290 if (i <= 0)
1291 break;
1293 if (n == 0)
1294 break;
1295 if (n != HAMMER_MREC_HEADSIZE) {
1296 errx(1, "read_mrecords: short read on pipe");
1297 /* not reached */
1299 if (pickup->signature != HAMMER_IOC_MIRROR_SIGNATURE) {
1300 errx(1, "read_mrecords: malformed record on pipe, "
1301 "bad signature");
1302 /* not reached */
1305 if (pickup->rec_size < HAMMER_MREC_HEADSIZE ||
1306 pickup->rec_size > sizeof(*mrec) + HAMMER_XBUFSIZE) {
1307 errx(1, "read_mrecords: malformed record on pipe, "
1308 "illegal rec_size");
1309 /* not reached */
1313 * Stop if we have insufficient space for the record and data.
1315 bytes = HAMMER_HEAD_DOALIGN(pickup->rec_size);
1316 if (size - count < bytes)
1317 break;
1320 * Stop if the record type is not a REC, SKIP, or PASS,
1321 * which are the only types the ioctl supports. Other types
1322 * are used only by the userland protocol.
1324 * Ignore all flags.
1326 type = pickup->type & HAMMER_MRECF_TYPE_LOMASK;
1327 if (type != HAMMER_MREC_TYPE_PFSD &&
1328 type != HAMMER_MREC_TYPE_REC &&
1329 type != HAMMER_MREC_TYPE_SKIP &&
1330 type != HAMMER_MREC_TYPE_PASS) {
1331 break;
1335 * Read the remainder and clear the pickup signature.
1337 for (n = HAMMER_MREC_HEADSIZE; n < bytes; n += i) {
1338 i = read(fd, buf + count + n, bytes - n);
1339 if (i <= 0)
1340 break;
1342 if (n != bytes) {
1343 errx(1, "read_mrecords: short read on pipe");
1344 /* not reached */
1347 bcopy(pickup, buf + count, HAMMER_MREC_HEADSIZE);
1348 pickup->signature = 0;
1349 pickup->type = 0;
1350 mrec = (void *)(buf + count);
1353 * Validate the completed record
1355 if (!hammer_crc_test_mrec_head(&mrec->head, mrec->head.rec_size)) {
1356 errx(1, "read_mrecords: malformed record on pipe, bad crc");
1357 /* not reached */
1361 * If its a B-Tree record validate the data crc.
1363 * NOTE: If the VFS passes us an explicitly errorde mrec
1364 * we just pass it through.
1366 type = mrec->head.type & HAMMER_MRECF_TYPE_MASK;
1368 if (type == HAMMER_MREC_TYPE_REC) {
1369 if (mrec->head.rec_size <
1370 sizeof(mrec->rec) + mrec->rec.leaf.data_len) {
1371 errx(1, "read_mrecords: malformed record on "
1372 "pipe, illegal element data_len");
1373 /* not reached */
1375 if (mrec->rec.leaf.data_len &&
1376 mrec->rec.leaf.data_offset &&
1377 hammer_crc_test_leaf(HammerVersion, &mrec->rec + 1, &mrec->rec.leaf) == 0) {
1378 fprintf(stderr,
1379 "read_mrecords: data_crc did not "
1380 "match data! obj=%016jx key=%016jx\n",
1381 (uintmax_t)mrec->rec.leaf.base.obj_id,
1382 (uintmax_t)mrec->rec.leaf.base.key);
1383 fprintf(stderr,
1384 "continuing, but there are problems\n");
1387 count += bytes;
1389 return(count);
1393 * Read and return a single mrecord.
1395 static
1396 hammer_ioc_mrecord_any_t
1397 read_mrecord(int fdin, int *errorp, hammer_ioc_mrecord_head_t pickup)
1399 hammer_ioc_mrecord_any_t mrec;
1400 struct hammer_ioc_mrecord_head mrechd;
1401 size_t bytes;
1402 size_t n;
1403 size_t i;
1405 if (pickup && pickup->type != 0) {
1406 mrechd = *pickup;
1407 pickup->signature = 0;
1408 pickup->type = 0;
1409 n = HAMMER_MREC_HEADSIZE;
1410 } else {
1412 * Read in the PFSD header from the sender.
1414 for (n = 0; n < HAMMER_MREC_HEADSIZE; n += i) {
1415 i = read(fdin, (char *)&mrechd + n, HAMMER_MREC_HEADSIZE - n);
1416 if (i <= 0)
1417 break;
1419 if (n == 0) {
1420 *errorp = 0; /* EOF */
1421 return(NULL);
1423 if (n != HAMMER_MREC_HEADSIZE) {
1424 fprintf(stderr, "short read of mrecord header\n");
1425 *errorp = EPIPE;
1426 return(NULL);
1429 if (mrechd.signature != HAMMER_IOC_MIRROR_SIGNATURE) {
1430 fprintf(stderr, "read_mrecord: bad signature\n");
1431 *errorp = EINVAL;
1432 return(NULL);
1434 bytes = HAMMER_HEAD_DOALIGN(mrechd.rec_size);
1435 assert(bytes >= sizeof(mrechd));
1436 mrec = malloc(bytes);
1437 mrec->head = mrechd;
1439 while (n < bytes) {
1440 i = read(fdin, (char *)mrec + n, bytes - n);
1441 if (i <= 0)
1442 break;
1443 n += i;
1445 if (n != bytes) {
1446 fprintf(stderr, "read_mrecord: short read on payload\n");
1447 *errorp = EPIPE;
1448 return(NULL);
1450 if (!hammer_crc_test_mrec_head(&mrec->head, mrec->head.rec_size)) {
1451 fprintf(stderr, "read_mrecord: bad CRC\n");
1452 *errorp = EINVAL;
1453 return(NULL);
1455 *errorp = 0;
1456 return(mrec);
1459 static
1460 void
1461 write_mrecord(int fdout, uint32_t type, hammer_ioc_mrecord_any_t mrec,
1462 int bytes)
1464 char zbuf[HAMMER_HEAD_ALIGN];
1465 int pad;
1467 pad = HAMMER_HEAD_DOALIGN(bytes) - bytes;
1469 assert(bytes >= (int)sizeof(mrec->head));
1470 bzero(&mrec->head, sizeof(mrec->head));
1471 mrec->head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
1472 mrec->head.type = type;
1473 mrec->head.rec_size = bytes;
1474 hammer_crc_set_mrec_head(&mrec->head, bytes);
1475 if (write(fdout, mrec, bytes) != bytes) {
1476 err(1, "write_mrecord");
1477 /* not reached */
1479 if (pad) {
1480 bzero(zbuf, pad);
1481 if (write(fdout, zbuf, pad) != pad) {
1482 err(1, "write_mrecord");
1483 /* not reached */
1489 * Generate a mirroring header with the pfs information of the
1490 * originating filesytem.
1492 static void
1493 generate_mrec_header(int fd, int pfs_id,
1494 union hammer_ioc_mrecord_any *mrec_tmp)
1496 struct hammer_ioc_pseudofs_rw pfs;
1498 bzero(mrec_tmp, sizeof(*mrec_tmp));
1499 clrpfs(&pfs, &mrec_tmp->pfs.pfsd, pfs_id);
1501 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1502 err(1, "Mirror-read: not a HAMMER fs/pseudofs!");
1503 /* not reached */
1505 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1506 errx(1, "Mirror-read: HAMMER PFS version mismatch!");
1507 /* not reached */
1509 mrec_tmp->pfs.version = pfs.version;
1513 * Validate the pfs information from the originating filesystem
1514 * against the target filesystem. shared_uuid must match.
1516 * return -1 if we got a TERM record
1518 static int
1519 validate_mrec_header(int fd, int fdin, int is_target, int pfs_id,
1520 struct hammer_ioc_mrecord_head *pickup,
1521 hammer_tid_t *tid_begp, hammer_tid_t *tid_endp)
1523 struct hammer_ioc_pseudofs_rw pfs;
1524 struct hammer_pseudofs_data pfsd;
1525 hammer_ioc_mrecord_any_t mrec;
1526 int error;
1529 * Get the PFSD info from the target filesystem.
1531 clrpfs(&pfs, &pfsd, pfs_id);
1532 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1533 err(1, "mirror-write: not a HAMMER fs/pseudofs!");
1534 /* not reached */
1536 if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1537 errx(1, "mirror-write: HAMMER PFS version mismatch!");
1538 /* not reached */
1541 mrec = read_mrecord(fdin, &error, pickup);
1542 if (mrec == NULL) {
1543 if (error == 0) {
1544 errx(1, "validate_mrec_header: short read");
1545 /* not reached */
1547 exit(1);
1549 if (mrec->head.type == HAMMER_MREC_TYPE_TERM) {
1550 free(mrec);
1551 return(-1);
1554 if (mrec->head.type != HAMMER_MREC_TYPE_PFSD) {
1555 errx(1, "validate_mrec_header: did not get expected "
1556 "PFSD record type");
1557 /* not reached */
1559 if (mrec->head.rec_size != sizeof(mrec->pfs)) {
1560 errx(1, "validate_mrec_header: unexpected payload size");
1561 /* not reached */
1563 if (mrec->pfs.version != pfs.version) {
1564 errx(1, "validate_mrec_header: Version mismatch");
1565 /* not reached */
1569 * Whew. Ok, is the read PFS info compatible with the target?
1571 if (bcmp(&mrec->pfs.pfsd.shared_uuid, &pfsd.shared_uuid,
1572 sizeof(pfsd.shared_uuid)) != 0) {
1573 errx(1, "mirror-write: source and target have "
1574 "different shared-uuid's!");
1575 /* not reached */
1577 if (is_target && hammer_is_pfs_master(&pfsd)) {
1578 errx(1, "mirror-write: target must be in slave mode");
1579 /* not reached */
1581 if (tid_begp)
1582 *tid_begp = mrec->pfs.pfsd.sync_beg_tid;
1583 if (tid_endp)
1584 *tid_endp = mrec->pfs.pfsd.sync_end_tid;
1585 free(mrec);
1586 return(0);
1589 static void
1590 update_pfs_snapshot(int fd, hammer_tid_t snapshot_tid, int pfs_id)
1592 struct hammer_ioc_pseudofs_rw pfs;
1593 struct hammer_pseudofs_data pfsd;
1595 clrpfs(&pfs, &pfsd, pfs_id);
1596 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1597 err(1, "update_pfs_snapshot (read)");
1598 /* not reached */
1601 if (pfsd.sync_end_tid != snapshot_tid) {
1602 pfsd.sync_end_tid = snapshot_tid;
1603 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) != 0) {
1604 err(1, "update_pfs_snapshot (rewrite)");
1605 /* not reached */
1607 if (VerboseOpt >= 2) {
1608 fprintf(stderr,
1609 "Mirror-write: Completed, updated snapshot "
1610 "to %016jx\n",
1611 (uintmax_t)snapshot_tid);
1612 fflush(stderr);
1618 * Bandwidth-limited write in chunks
1620 static
1621 ssize_t
1622 writebw(int fd, const void *buf, size_t nbytes,
1623 uint64_t *bwcount, struct timeval *tv1)
1625 struct timeval tv2;
1626 size_t n;
1627 ssize_t r;
1628 ssize_t a;
1629 int usec;
1631 a = 0;
1632 r = 0;
1633 while (nbytes) {
1634 if (*bwcount + nbytes > BandwidthOpt)
1635 n = BandwidthOpt - *bwcount;
1636 else
1637 n = nbytes;
1638 if (n)
1639 r = write(fd, buf, n);
1640 if (r >= 0) {
1641 a += r;
1642 nbytes -= r;
1643 buf = (const char *)buf + r;
1645 if ((size_t)r != n)
1646 break;
1647 *bwcount += n;
1648 if (*bwcount >= BandwidthOpt) {
1649 gettimeofday(&tv2, NULL);
1650 usec = (int)(tv2.tv_sec - tv1->tv_sec) * 1000000 +
1651 (int)(tv2.tv_usec - tv1->tv_usec);
1652 if (usec >= 0 && usec < 1000000)
1653 usleep(1000000 - usec);
1654 gettimeofday(tv1, NULL);
1655 *bwcount -= BandwidthOpt;
1658 return(a ? a : r);
1662 * Get a yes or no answer from the terminal. The program may be run as
1663 * part of a two-way pipe so we cannot use stdin for this operation.
1665 static int
1666 getyntty(void)
1668 char buf[256];
1669 FILE *fp;
1670 int result;
1672 fp = fopen("/dev/tty", "r");
1673 if (fp == NULL) {
1674 fprintf(stderr, "No terminal for response\n");
1675 return(-1);
1677 result = -1;
1678 while (fgets(buf, sizeof(buf), fp) != NULL) {
1679 if (buf[0] == 'y' || buf[0] == 'Y') {
1680 result = 1;
1681 break;
1683 if (buf[0] == 'n' || buf[0] == 'N') {
1684 result = 0;
1685 break;
1687 fprintf(stderr, "Response not understood\n");
1688 break;
1690 fclose(fp);
1691 return(result);
1694 static void
1695 score_printf(size_t i, size_t w, const char *ctl, ...)
1697 va_list va;
1698 size_t n;
1699 static size_t SSize;
1700 static int SFd = -1;
1701 static char ScoreBuf[1024];
1703 if (ScoreBoardFile == NULL)
1704 return;
1705 assert(i + w < sizeof(ScoreBuf));
1706 if (SFd < 0) {
1707 SFd = open(ScoreBoardFile, O_RDWR|O_CREAT|O_TRUNC, 0644);
1708 if (SFd < 0)
1709 return;
1710 SSize = 0;
1712 for (n = 0; n < i; ++n) {
1713 if (ScoreBuf[n] == 0)
1714 ScoreBuf[n] = ' ';
1716 va_start(va, ctl);
1717 vsnprintf(ScoreBuf + i, w - 1, ctl, va);
1718 va_end(va);
1719 n = strlen(ScoreBuf + i);
1720 while (n < w - 1) {
1721 ScoreBuf[i + n] = ' ';
1722 ++n;
1724 ScoreBuf[i + n] = '\n';
1725 if (SSize < i + w)
1726 SSize = i + w;
1727 pwrite(SFd, ScoreBuf, SSize, 0);
1730 static void
1731 hammer_check_restrict(const char *filesystem)
1733 size_t rlen;
1734 int atslash;
1736 if (RestrictTarget == NULL)
1737 return;
1738 rlen = strlen(RestrictTarget);
1739 if (strncmp(filesystem, RestrictTarget, rlen) != 0) {
1740 errx(1, "hammer-remote: restricted target");
1741 /* not reached */
1744 atslash = 1;
1745 while (filesystem[rlen]) {
1746 if (atslash &&
1747 filesystem[rlen] == '.' &&
1748 filesystem[rlen+1] == '.') {
1749 errx(1, "hammer-remote: '..' not allowed");
1750 /* not reached */
1752 if (filesystem[rlen] == '/')
1753 atslash = 1;
1754 else
1755 atslash = 0;
1756 ++rlen;
1760 static void
1761 mirror_usage(int code)
1763 fprintf(stderr,
1764 "hammer mirror-read <filesystem> [begin-tid]\n"
1765 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
1766 "hammer mirror-write <filesystem>\n"
1767 "hammer mirror-dump [header]\n"
1768 "hammer mirror-copy [[user@]host:]<filesystem>"
1769 " [[user@]host:]<filesystem>\n"
1770 "hammer mirror-stream [[user@]host:]<filesystem>"
1771 " [[user@]host:]<filesystem>\n"
1773 exit(code);