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
34 * $DragonFly: src/sbin/hammer/cmd_mirror.c,v 1.16 2008/11/09 05:22:56 dillon Exp $
39 #define SERIALBUF_SIZE (512 * 1024)
41 typedef struct histogram
{
46 static int read_mrecords(int fd
, char *buf
, u_int size
,
47 hammer_ioc_mrecord_head_t pickup
);
48 static int generate_histogram(int fd
, const char *filesystem
,
49 histogram_t
*histogram_ary
,
50 struct hammer_ioc_mirror_rw
*mirror_base
);
51 static hammer_ioc_mrecord_any_t
read_mrecord(int fdin
, int *errorp
,
52 hammer_ioc_mrecord_head_t pickup
);
53 static void write_mrecord(int fdout
, u_int32_t type
,
54 hammer_ioc_mrecord_any_t mrec
, int bytes
);
55 static void generate_mrec_header(int fd
, int pfs_id
,
56 union hammer_ioc_mrecord_any
*mrec_tmp
);
57 static int validate_mrec_header(int fd
, int fdin
, int is_target
, int pfs_id
,
58 struct hammer_ioc_mrecord_head
*pickup
,
59 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
);
60 static void update_pfs_snapshot(int fd
, hammer_tid_t snapshot_tid
, int pfs_id
);
61 static ssize_t
writebw(int fd
, const void *buf
, size_t nbytes
,
62 u_int64_t
*bwcount
, struct timeval
*tv1
);
63 static int getyn(void);
64 static void mirror_usage(int code
);
67 * Generate a mirroring data stream from the specific source over the
68 * entire key range, but restricted to the specified transaction range.
70 * The HAMMER VFS does most of the work, we add a few new mrecord
71 * types to negotiate the TID ranges and verify that the entire
72 * stream made it to the destination.
75 hammer_cmd_mirror_read(char **av
, int ac
, int streaming
)
77 struct hammer_ioc_mirror_rw mirror
;
78 struct hammer_ioc_pseudofs_rw pfs
;
79 union hammer_ioc_mrecord_any mrec_tmp
;
80 struct hammer_ioc_mrecord_head pickup
;
81 hammer_ioc_mrecord_any_t mrec
;
82 hammer_tid_t sync_tid
;
83 histogram_t histogram_ary
;
84 const char *filesystem
;
85 char *buf
= malloc(SERIALBUF_SIZE
);
95 time_t base_t
= time(NULL
);
100 if (ac
== 0 || ac
> 2)
104 pickup
.signature
= 0;
109 histogram_ary
= NULL
;
112 bzero(&mirror
, sizeof(mirror
));
113 hammer_key_beg_init(&mirror
.key_beg
);
114 hammer_key_end_init(&mirror
.key_end
);
116 fd
= getpfs(&pfs
, filesystem
);
118 if (streaming
&& VerboseOpt
&& VerboseOpt
< 2) {
119 fprintf(stderr
, "\nRunning");
123 gettimeofday(&bwtv
, NULL
);
127 * Send initial header for the purpose of determining the
130 generate_mrec_header(fd
, pfs
.pfs_id
, &mrec_tmp
);
131 write_mrecord(1, HAMMER_MREC_TYPE_PFSD
,
132 &mrec_tmp
, sizeof(mrec_tmp
.pfs
));
135 * In 2-way mode the target will send us a PFS info packet
136 * first. Use the target's current snapshot TID as our default
141 n
= validate_mrec_header(fd
, 0, 0, pfs
.pfs_id
, &pickup
,
142 NULL
, &mirror
.tid_beg
);
143 if (n
< 0) { /* got TERM record */
151 * Write out the PFS header, tid_beg will be updated if our PFS
152 * has a larger begin sync. tid_end is set to the latest source
153 * TID whos flush cycle has completed.
155 generate_mrec_header(fd
, pfs
.pfs_id
, &mrec_tmp
);
156 if (mirror
.tid_beg
< mrec_tmp
.pfs
.pfsd
.sync_beg_tid
)
157 mirror
.tid_beg
= mrec_tmp
.pfs
.pfsd
.sync_beg_tid
;
158 mirror
.tid_end
= mrec_tmp
.pfs
.pfsd
.sync_end_tid
;
160 mirror
.size
= SERIALBUF_SIZE
;
161 mirror
.pfs_id
= pfs
.pfs_id
;
162 mirror
.shared_uuid
= pfs
.ondisk
->shared_uuid
;
165 * XXX If the histogram is exhausted and the TID delta is large
166 * the stream might have been offline for a while and is
167 * now picking it up again. Do another histogram.
170 if (TwoWayPipeOpt
&& streaming
&& histogram
&& histindex
== histend
) {
171 if (mirror
.tid_end
- mirror
.tid_beg
> BULK_MINIMUM
)
177 * Initial bulk startup control, try to do some incremental
178 * mirroring in order to allow the stream to be killed and
179 * restarted without having to start over.
181 if (histogram
== 0 && BulkOpt
== 0) {
183 fprintf(stderr
, "\n");
184 histmax
= generate_histogram(fd
, filesystem
,
185 &histogram_ary
, &mirror
);
190 if (TwoWayPipeOpt
&& streaming
&& histogram
) {
192 mirror
.tid_end
= histogram_ary
[histindex
].tid
;
193 estbytes
= histogram_ary
[histindex
-1].bytes
;
194 mrec_tmp
.pfs
.pfsd
.sync_end_tid
= mirror
.tid_end
;
199 write_mrecord(1, HAMMER_MREC_TYPE_PFSD
,
200 &mrec_tmp
, sizeof(mrec_tmp
.pfs
));
203 * A cycle file overrides the beginning TID only if we are
204 * not operating in two-way mode.
206 if (TwoWayPipeOpt
== 0) {
207 hammer_get_cycle(&mirror
.key_beg
, &mirror
.tid_beg
);
211 * An additional argument overrides the beginning TID regardless
212 * of what mode we are in. This is not recommending if operating
216 mirror
.tid_beg
= strtoull(av
[1], NULL
, 0);
218 if (streaming
== 0 || VerboseOpt
>= 2) {
220 "Mirror-read: Mirror %016jx to %016jx",
221 (uintmax_t)mirror
.tid_beg
, (uintmax_t)mirror
.tid_end
);
223 fprintf(stderr
, " (bulk= %ju)", (uintmax_t)estbytes
);
224 fprintf(stderr
, "\n");
227 if (mirror
.key_beg
.obj_id
!= (int64_t)HAMMER_MIN_OBJID
) {
228 fprintf(stderr
, "Mirror-read: Resuming at object %016jx\n",
229 (uintmax_t)mirror
.key_beg
.obj_id
);
233 * Nothing to do if begin equals end.
235 if (mirror
.tid_beg
>= mirror
.tid_end
) {
236 if (streaming
== 0 || VerboseOpt
>= 2)
237 fprintf(stderr
, "Mirror-read: No work to do\n");
244 * Write out bulk records
247 mirror
.size
= SERIALBUF_SIZE
;
251 mirror
.pfs_id
= pfs
.pfs_id
;
252 mirror
.shared_uuid
= pfs
.ondisk
->shared_uuid
;
253 if (ioctl(fd
, HAMMERIOC_MIRROR_READ
, &mirror
) < 0) {
254 fprintf(stderr
, "Mirror-read %s failed: %s\n",
255 filesystem
, strerror(errno
));
258 if (mirror
.head
.flags
& HAMMER_IOC_HEAD_ERROR
) {
260 "Mirror-read %s fatal error %d\n",
261 filesystem
, mirror
.head
.error
);
266 n
= writebw(1, mirror
.ubuf
, mirror
.count
,
269 n
= write(1, mirror
.ubuf
, mirror
.count
);
271 if (n
!= mirror
.count
) {
272 fprintf(stderr
, "Mirror-read %s failed: "
278 total_bytes
+= mirror
.count
;
279 if (streaming
&& VerboseOpt
) {
281 "\robj=%016jx tids=%016jx:%016jx %11jd",
282 (uintmax_t)mirror
.key_cur
.obj_id
,
283 (uintmax_t)mirror
.tid_beg
,
284 (uintmax_t)mirror
.tid_end
,
285 (intmax_t)total_bytes
);
288 mirror
.key_beg
= mirror
.key_cur
;
291 * Deal with time limit option
294 (unsigned)(time(NULL
) - base_t
) > (unsigned)TimeoutOpt
) {
296 "Mirror-read %s interrupted by timer at"
299 (uintmax_t)mirror
.key_cur
.obj_id
);
303 } while (mirror
.count
!= 0);
306 if (streaming
&& VerboseOpt
) {
307 fprintf(stderr
, "\n");
312 * Write out the termination sync record - only if not interrupted
314 if (interrupted
== 0) {
316 write_mrecord(1, HAMMER_MREC_TYPE_SYNC
,
317 &mrec_tmp
, sizeof(mrec_tmp
.sync
));
319 write_mrecord(1, HAMMER_MREC_TYPE_IDLE
,
320 &mrec_tmp
, sizeof(mrec_tmp
.sync
));
325 * If the -2 option was given (automatic when doing mirror-copy),
326 * a two-way pipe is assumed and we expect a response mrec from
330 mrec
= read_mrecord(0, &error
, &pickup
);
332 mrec
->head
.type
!= HAMMER_MREC_TYPE_UPDATE
||
333 mrec
->head
.rec_size
!= sizeof(mrec
->update
)) {
334 fprintf(stderr
, "mirror_read: Did not get final "
335 "acknowledgement packet from target\n");
340 hammer_set_cycle(&mirror
.key_cur
,
342 fprintf(stderr
, "Cyclefile %s updated for "
343 "continuation\n", CyclePath
);
346 sync_tid
= mrec
->update
.tid
;
348 hammer_key_beg_init(&mirror
.key_beg
);
349 hammer_set_cycle(&mirror
.key_beg
, sync_tid
);
351 "Cyclefile %s updated to 0x%016jx\n",
352 CyclePath
, (uintmax_t)sync_tid
);
355 } else if (CyclePath
) {
356 /* NOTE! mirror.tid_beg cannot be updated */
357 fprintf(stderr
, "Warning: cycle file (-c option) cannot be "
358 "fully updated unless you use mirror-copy\n");
359 hammer_set_cycle(&mirror
.key_beg
, mirror
.tid_beg
);
361 if (streaming
&& interrupted
== 0) {
362 time_t t1
= time(NULL
);
366 * Two way streaming tries to break down large bulk
367 * transfers into smaller ones so it can sync the
368 * transaction id on the slave. This way if we get
369 * interrupted a restart doesn't have to start from
372 if (TwoWayPipeOpt
&& streaming
&& histogram
) {
373 if (histindex
!= histmax
) {
374 if (VerboseOpt
&& VerboseOpt
< 2)
375 fprintf(stderr
, " (bulk incremental)");
381 fprintf(stderr
, " W");
384 pfs
.ondisk
->sync_end_tid
= mirror
.tid_end
;
385 if (ioctl(fd
, HAMMERIOC_WAI_PSEUDOFS
, &pfs
) < 0) {
386 fprintf(stderr
, "Mirror-read %s: cannot stream: %s\n",
387 filesystem
, strerror(errno
));
389 t2
= time(NULL
) - t1
;
390 if (t2
>= 0 && t2
< DelayOpt
) {
392 fprintf(stderr
, "\bD");
395 sleep(DelayOpt
- t2
);
398 fprintf(stderr
, "\b ");
405 write_mrecord(1, HAMMER_MREC_TYPE_TERM
,
406 &mrec_tmp
, sizeof(mrec_tmp
.sync
));
408 fprintf(stderr
, "Mirror-read %s succeeded\n", filesystem
);
412 * What we are trying to do here is figure out how much data is
413 * going to be sent for the TID range and to break the TID range
414 * down into reasonably-sized slices (from the point of view of
415 * data sent) so a lost connection can restart at a reasonable
416 * place and not all the way back at the beginning.
418 * An entry's TID serves as the end_tid for the prior entry
419 * So we have to offset the calculation by 1 so that TID falls into
420 * the previous entry when populating entries.
422 * Because the transaction id space is bursty we need a relatively
423 * large number of buckets (like a million) to do a reasonable job
424 * for things like an initial bulk mirrors on a very large filesystem.
426 #define HIST_COUNT (1024 * 1024)
429 generate_histogram(int fd
, const char *filesystem
,
430 histogram_t
*histogram_ary
,
431 struct hammer_ioc_mirror_rw
*mirror_base
)
433 struct hammer_ioc_mirror_rw mirror
;
434 union hammer_ioc_mrecord_any
*mrec
;
435 hammer_tid_t tid_beg
;
436 hammer_tid_t tid_end
;
439 u_int64_t
*tid_bytes
;
447 mirror
= *mirror_base
;
448 tid_beg
= mirror
.tid_beg
;
449 tid_end
= mirror
.tid_end
;
450 mirror
.head
.flags
|= HAMMER_IOC_MIRROR_NODATA
;
452 if (*histogram_ary
== NULL
) {
453 *histogram_ary
= malloc(sizeof(struct histogram
) *
456 if (tid_beg
>= tid_end
)
460 tid_bytes
= malloc(sizeof(*tid_bytes
) * (HIST_COUNT
+ 2));
461 bzero(tid_bytes
, sizeof(tid_bytes
));
463 fprintf(stderr
, "Prescan to break up bulk transfer");
465 fprintf(stderr
, " (%juMB chunks)",
466 (uintmax_t)(SplitupOpt
/ (1024 * 1024)));
467 fprintf(stderr
, "\n");
470 * Note: (tid_beg,tid_end), range is inclusive of both beg & end.
472 * Note: Estimates can be off when the mirror is way behind due
479 if (ioctl(fd
, HAMMERIOC_MIRROR_READ
, &mirror
) < 0) {
480 fprintf(stderr
, "Mirror-read %s failed: %s\n",
481 filesystem
, strerror(errno
));
484 if (mirror
.head
.flags
& HAMMER_IOC_HEAD_ERROR
) {
486 "Mirror-read %s fatal error %d\n",
487 filesystem
, mirror
.head
.error
);
492 off
+= HAMMER_HEAD_DOALIGN(mrec
->head
.rec_size
)
494 mrec
= (void *)((char *)mirror
.ubuf
+ off
);
497 * We only care about general RECs and PASS
498 * records. We ignore SKIPs.
500 switch (mrec
->head
.type
& HAMMER_MRECF_TYPE_LOMASK
) {
501 case HAMMER_MREC_TYPE_REC
:
502 case HAMMER_MREC_TYPE_PASS
:
509 * Calculate for two indices, create_tid and
510 * delete_tid. Record data only applies to
513 * When tid is exactly on the boundary it really
514 * belongs to the previous entry because scans
515 * are inclusive of the ending entry.
517 tid
= mrec
->rec
.leaf
.base
.delete_tid
;
518 if (tid
&& tid
>= tid_beg
&& tid
<= tid_end
) {
519 len
= HAMMER_HEAD_DOALIGN(mrec
->head
.rec_size
);
520 if (mrec
->head
.type
==
521 HAMMER_MREC_TYPE_REC
) {
522 len
-= HAMMER_HEAD_DOALIGN(
523 mrec
->rec
.leaf
.data_len
);
526 i
= (tid
- tid_beg
) * HIST_COUNT
/
528 tidx
= tid_beg
+ i
* (tid_end
- tid_beg
) /
530 if (tid
== tidx
&& i
)
532 assert(i
>= 0 && i
< HIST_COUNT
);
538 tid
= mrec
->rec
.leaf
.base
.create_tid
;
539 if (tid
&& tid
>= tid_beg
&& tid
<= tid_end
) {
540 len
= HAMMER_HEAD_DOALIGN(mrec
->head
.rec_size
);
541 if (mrec
->head
.type
==
542 HAMMER_MREC_TYPE_REC_NODATA
) {
543 len
+= HAMMER_HEAD_DOALIGN(
544 mrec
->rec
.leaf
.data_len
);
546 i
= (tid
- tid_beg
) * HIST_COUNT
/
548 tidx
= tid_beg
+ i
* (tid_end
- tid_beg
) /
550 if (tid
== tidx
&& i
)
552 assert(i
>= 0 && i
< HIST_COUNT
);
558 if (VerboseOpt
> 1) {
559 if (accum
> SplitupOpt
) {
560 fprintf(stderr
, ".");
565 if (mirror
.count
== 0)
567 mirror
.key_beg
= mirror
.key_cur
;
571 * Reduce to SplitupOpt (default 100MB) chunks. This code may
572 * use up to two additional elements. Do the array in-place.
574 * Inefficient degenerate cases can occur if we do not accumulate
575 * at least the requested split amount, so error on the side of
579 (*histogram_ary
)[res
].tid
= tid_beg
;
580 (*histogram_ary
)[res
].bytes
= tid_bytes
[0];
581 for (i
= 1; i
< HIST_COUNT
; ++i
) {
582 if ((*histogram_ary
)[res
].bytes
>= SplitupOpt
) {
584 (*histogram_ary
)[res
].tid
= tid_beg
+
585 i
* (tid_end
- tid_beg
) /
587 (*histogram_ary
)[res
].bytes
= 0;
590 (*histogram_ary
)[res
].bytes
+= tid_bytes
[i
];
593 (*histogram_ary
)[res
].tid
= tid_end
;
594 (*histogram_ary
)[res
].bytes
= -1;
597 fprintf(stderr
, "\n"); /* newline after ... */
598 assert(res
<= HIST_COUNT
);
599 fprintf(stderr
, "Prescan %d chunks, total %ju MBytes (",
600 res
, (uintmax_t)total
/ (1024 * 1024));
601 for (i
= 0; i
< res
&& i
< 3; ++i
) {
603 fprintf(stderr
, ", ");
604 fprintf(stderr
, "%ju", (uintmax_t)(*histogram_ary
)[i
].bytes
);
607 fprintf(stderr
, ", ...");
608 fprintf(stderr
, ")\n");
615 create_pfs(const char *filesystem
, uuid_t
*s_uuid
)
617 if (ForceYesOpt
== 1) {
618 fprintf(stderr
, "PFS slave %s does not exist. "
619 "Auto create new slave PFS!\n", filesystem
);
622 fprintf(stderr
, "PFS slave %s does not exist.\n"
623 "Do you want to create a new slave PFS? (yes|no) ",
627 fprintf(stderr
, "Aborting operation\n");
633 char *shared_uuid
= NULL
;
634 uuid_to_string(s_uuid
, &shared_uuid
, &status
);
637 asprintf(&cmd
, "/sbin/hammer pfs-slave '%s' shared-uuid=%s 1>&2",
638 filesystem
, shared_uuid
);
642 fprintf(stderr
, "Failed to alloc memory\n");
645 if (system(cmd
) != 0) {
646 fprintf(stderr
, "Failed to create PFS\n");
652 * Pipe the mirroring data stream on stdin to the HAMMER VFS, adding
653 * some additional packet types to negotiate TID ranges and to verify
654 * completion. The HAMMER VFS does most of the work.
656 * It is important to note that the mirror.key_{beg,end} range must
657 * match the ranged used by the original. For now both sides use
658 * range the entire key space.
660 * It is even more important that the records in the stream conform
661 * to the TID range also supplied in the stream. The HAMMER VFS will
662 * use the REC, PASS, and SKIP record types to track the portions of
663 * the B-Tree being scanned in order to be able to proactively delete
664 * records on the target within those active areas that are not mentioned
667 * The mirror.key_cur field is used by the VFS to do this tracking. It
668 * must be initialized to key_beg but then is persistently updated by
669 * the HAMMER VFS on each successive ioctl() call. If you blow up this
670 * field you will blow up the mirror target, possibly to the point of
671 * deleting everything. As a safety measure the HAMMER VFS simply marks
672 * the records that the source has destroyed as deleted on the target,
673 * and normal pruning operations will deal with their final disposition
674 * at some later time.
677 hammer_cmd_mirror_write(char **av
, int ac
)
679 struct hammer_ioc_mirror_rw mirror
;
680 const char *filesystem
;
681 char *buf
= malloc(SERIALBUF_SIZE
);
682 struct hammer_ioc_pseudofs_rw pfs
;
683 struct hammer_ioc_mrecord_head pickup
;
684 struct hammer_ioc_synctid synctid
;
685 union hammer_ioc_mrecord_any mrec_tmp
;
686 hammer_ioc_mrecord_any_t mrec
;
696 pickup
.signature
= 0;
700 bzero(&mirror
, sizeof(mirror
));
701 hammer_key_beg_init(&mirror
.key_beg
);
702 hammer_key_end_init(&mirror
.key_end
);
703 mirror
.key_end
= mirror
.key_beg
;
706 * Read initial packet
708 mrec
= read_mrecord(0, &error
, &pickup
);
711 fprintf(stderr
, "validate_mrec_header: short read\n");
717 if (mrec
->head
.type
== HAMMER_MREC_TYPE_TERM
) {
720 if (mrec
->head
.type
!= HAMMER_MREC_TYPE_PFSD
) {
721 fprintf(stderr
, "validate_mrec_header: did not get expected "
722 "PFSD record type\n");
725 if (mrec
->head
.rec_size
!= sizeof(mrec
->pfs
)) {
726 fprintf(stderr
, "validate_mrec_header: unexpected payload "
732 * Create slave PFS if it doesn't yet exist
734 if (lstat(filesystem
, &st
) != 0) {
735 create_pfs(filesystem
, &mrec
->pfs
.pfsd
.shared_uuid
);
740 fd
= getpfs(&pfs
, filesystem
);
743 * In two-way mode the target writes out a PFS packet first.
744 * The source uses our tid_end as its tid_beg by default,
745 * picking up where it left off.
749 generate_mrec_header(fd
, pfs
.pfs_id
, &mrec_tmp
);
750 if (mirror
.tid_beg
< mrec_tmp
.pfs
.pfsd
.sync_beg_tid
)
751 mirror
.tid_beg
= mrec_tmp
.pfs
.pfsd
.sync_beg_tid
;
752 mirror
.tid_end
= mrec_tmp
.pfs
.pfsd
.sync_end_tid
;
753 write_mrecord(1, HAMMER_MREC_TYPE_PFSD
,
754 &mrec_tmp
, sizeof(mrec_tmp
.pfs
));
758 * Read and process the PFS header. The source informs us of
759 * the TID range the stream represents.
761 n
= validate_mrec_header(fd
, 0, 1, pfs
.pfs_id
, &pickup
,
762 &mirror
.tid_beg
, &mirror
.tid_end
);
763 if (n
< 0) { /* got TERM record */
769 mirror
.size
= SERIALBUF_SIZE
;
772 * Read and process bulk records (REC, PASS, and SKIP types).
774 * On your life, do NOT mess with mirror.key_cur or your mirror
775 * target may become history.
779 mirror
.pfs_id
= pfs
.pfs_id
;
780 mirror
.shared_uuid
= pfs
.ondisk
->shared_uuid
;
781 mirror
.size
= read_mrecords(0, buf
, SERIALBUF_SIZE
, &pickup
);
782 if (mirror
.size
<= 0)
784 if (ioctl(fd
, HAMMERIOC_MIRROR_WRITE
, &mirror
) < 0) {
785 fprintf(stderr
, "Mirror-write %s failed: %s\n",
786 filesystem
, strerror(errno
));
789 if (mirror
.head
.flags
& HAMMER_IOC_HEAD_ERROR
) {
791 "Mirror-write %s fatal error %d\n",
792 filesystem
, mirror
.head
.error
);
796 if (mirror
.head
.flags
& HAMMER_IOC_HEAD_INTR
) {
798 "Mirror-write %s interrupted by timer at"
801 mirror
.key_cur
.obj_id
);
808 * Read and process the termination sync record.
810 mrec
= read_mrecord(0, &error
, &pickup
);
812 if (mrec
&& mrec
->head
.type
== HAMMER_MREC_TYPE_TERM
) {
813 fprintf(stderr
, "Mirror-write: received termination request\n");
819 (mrec
->head
.type
!= HAMMER_MREC_TYPE_SYNC
&&
820 mrec
->head
.type
!= HAMMER_MREC_TYPE_IDLE
) ||
821 mrec
->head
.rec_size
!= sizeof(mrec
->sync
)) {
822 fprintf(stderr
, "Mirror-write %s: Did not get termination "
823 "sync record, or rec_size is wrong rt=%d\n",
824 filesystem
, mrec
->head
.type
);
829 * Update the PFS info on the target so the user has visibility
830 * into the new snapshot, and sync the target filesystem.
832 if (mrec
->head
.type
== HAMMER_MREC_TYPE_SYNC
) {
833 update_pfs_snapshot(fd
, mirror
.tid_end
, pfs
.pfs_id
);
835 bzero(&synctid
, sizeof(synctid
));
836 synctid
.op
= HAMMER_SYNCTID_SYNC2
;
837 ioctl(fd
, HAMMERIOC_SYNCTID
, &synctid
);
839 if (VerboseOpt
>= 2) {
840 fprintf(stderr
, "Mirror-write %s: succeeded\n",
849 * Report back to the originator.
852 mrec_tmp
.update
.tid
= mirror
.tid_end
;
853 write_mrecord(1, HAMMER_MREC_TYPE_UPDATE
,
854 &mrec_tmp
, sizeof(mrec_tmp
.update
));
856 printf("Source can update synctid to 0x%016jx\n",
857 (uintmax_t)mirror
.tid_end
);
864 hammer_cmd_mirror_dump(void)
866 char *buf
= malloc(SERIALBUF_SIZE
);
867 struct hammer_ioc_mrecord_head pickup
;
868 hammer_ioc_mrecord_any_t mrec
;
875 * Read and process the PFS header
877 pickup
.signature
= 0;
880 mrec
= read_mrecord(0, &error
, &pickup
);
883 * Read and process bulk records
886 size
= read_mrecords(0, buf
, SERIALBUF_SIZE
, &pickup
);
890 while (offset
< size
) {
891 mrec
= (void *)((char *)buf
+ offset
);
892 bytes
= HAMMER_HEAD_DOALIGN(mrec
->head
.rec_size
);
893 if (offset
+ bytes
> size
) {
894 fprintf(stderr
, "Misaligned record\n");
898 switch(mrec
->head
.type
& HAMMER_MRECF_TYPE_MASK
) {
899 case HAMMER_MREC_TYPE_REC_BADCRC
:
900 case HAMMER_MREC_TYPE_REC
:
901 printf("Record obj=%016jx key=%016jx "
903 (uintmax_t)mrec
->rec
.leaf
.base
.obj_id
,
904 (uintmax_t)mrec
->rec
.leaf
.base
.key
,
905 mrec
->rec
.leaf
.base
.rec_type
,
906 mrec
->rec
.leaf
.base
.obj_type
);
907 if (mrec
->head
.type
==
908 HAMMER_MREC_TYPE_REC_BADCRC
) {
909 printf(" (BAD CRC)");
912 printf(" tids %016jx:%016jx data=%d\n",
913 (uintmax_t)mrec
->rec
.leaf
.base
.create_tid
,
914 (uintmax_t)mrec
->rec
.leaf
.base
.delete_tid
,
915 mrec
->rec
.leaf
.data_len
);
917 case HAMMER_MREC_TYPE_PASS
:
918 printf("Pass obj=%016jx key=%016jx "
920 (uintmax_t)mrec
->rec
.leaf
.base
.obj_id
,
921 (uintmax_t)mrec
->rec
.leaf
.base
.key
,
922 mrec
->rec
.leaf
.base
.rec_type
,
923 mrec
->rec
.leaf
.base
.obj_type
);
924 printf(" tids %016jx:%016jx data=%d\n",
925 (uintmax_t)mrec
->rec
.leaf
.base
.create_tid
,
926 (uintmax_t)mrec
->rec
.leaf
.base
.delete_tid
,
927 mrec
->rec
.leaf
.data_len
);
929 case HAMMER_MREC_TYPE_SKIP
:
930 printf("Skip obj=%016jx key=%016jx rt=%02x to\n"
931 " obj=%016jx key=%016jx rt=%02x\n",
932 (uintmax_t)mrec
->skip
.skip_beg
.obj_id
,
933 (uintmax_t)mrec
->skip
.skip_beg
.key
,
934 mrec
->skip
.skip_beg
.rec_type
,
935 (uintmax_t)mrec
->skip
.skip_end
.obj_id
,
936 (uintmax_t)mrec
->skip
.skip_end
.key
,
937 mrec
->skip
.skip_end
.rec_type
);
946 * Read and process the termination sync record.
948 mrec
= read_mrecord(0, &error
, &pickup
);
950 (mrec
->head
.type
!= HAMMER_MREC_TYPE_SYNC
&&
951 mrec
->head
.type
!= HAMMER_MREC_TYPE_IDLE
)
953 fprintf(stderr
, "Mirror-dump: Did not get termination "
959 hammer_cmd_mirror_copy(char **av
, int ac
, int streaming
)
973 signal(SIGPIPE
, SIG_IGN
);
984 if ((pid1
= fork()) == 0) {
985 signal(SIGPIPE
, SIG_DFL
);
990 if ((ptr
= strchr(av
[0], ':')) != NULL
) {
998 xav
[xac
++] = SshPort
;
1001 xav
[xac
++] = "hammer";
1003 switch(VerboseOpt
) {
1013 xav
[xac
++] = "-vvv";
1021 snprintf(tbuf
, sizeof(tbuf
), "%d", TimeoutOpt
);
1026 xav
[xac
++] = "mirror-read-stream";
1028 xav
[xac
++] = "mirror-read";
1031 execv("/usr/bin/ssh", (void *)xav
);
1033 hammer_cmd_mirror_read(av
, 1, streaming
);
1043 if ((pid2
= fork()) == 0) {
1044 signal(SIGPIPE
, SIG_DFL
);
1049 if ((ptr
= strchr(av
[1], ':')) != NULL
) {
1057 xav
[xac
++] = SshPort
;
1060 xav
[xac
++] = "hammer";
1062 switch(VerboseOpt
) {
1072 xav
[xac
++] = "-vvv";
1079 xav
[xac
++] = "mirror-write";
1082 execv("/usr/bin/ssh", (void *)xav
);
1084 hammer_cmd_mirror_write(av
+ 1, 1);
1093 while (waitpid(pid1
, NULL
, 0) <= 0)
1095 while (waitpid(pid2
, NULL
, 0) <= 0)
1099 * If the link is lost restart
1103 fprintf(stderr
, "\nLost Link\n");
1106 sleep(15 + DelayOpt
);
1113 * Read and return multiple mrecords
1116 read_mrecords(int fd
, char *buf
, u_int size
, hammer_ioc_mrecord_head_t pickup
)
1118 hammer_ioc_mrecord_any_t mrec
;
1126 while (size
- count
>= HAMMER_MREC_HEADSIZE
) {
1128 * Cached the record header in case we run out of buffer
1132 if (pickup
->signature
== 0) {
1133 for (n
= 0; n
< HAMMER_MREC_HEADSIZE
; n
+= i
) {
1134 i
= read(fd
, (char *)pickup
+ n
,
1135 HAMMER_MREC_HEADSIZE
- n
);
1141 if (n
!= HAMMER_MREC_HEADSIZE
) {
1142 fprintf(stderr
, "read_mrecords: short read on pipe\n");
1145 if (pickup
->signature
!= HAMMER_IOC_MIRROR_SIGNATURE
) {
1146 fprintf(stderr
, "read_mrecords: malformed record on pipe, "
1151 if (pickup
->rec_size
< HAMMER_MREC_HEADSIZE
||
1152 pickup
->rec_size
> sizeof(*mrec
) + HAMMER_XBUFSIZE
) {
1153 fprintf(stderr
, "read_mrecords: malformed record on pipe, "
1154 "illegal rec_size\n");
1159 * Stop if we have insufficient space for the record and data.
1161 bytes
= HAMMER_HEAD_DOALIGN(pickup
->rec_size
);
1162 if (size
- count
< bytes
)
1166 * Stop if the record type is not a REC, SKIP, or PASS,
1167 * which are the only types the ioctl supports. Other types
1168 * are used only by the userland protocol.
1172 type
= pickup
->type
& HAMMER_MRECF_TYPE_LOMASK
;
1173 if (type
!= HAMMER_MREC_TYPE_PFSD
&&
1174 type
!= HAMMER_MREC_TYPE_REC
&&
1175 type
!= HAMMER_MREC_TYPE_SKIP
&&
1176 type
!= HAMMER_MREC_TYPE_PASS
) {
1181 * Read the remainder and clear the pickup signature.
1183 for (n
= HAMMER_MREC_HEADSIZE
; n
< bytes
; n
+= i
) {
1184 i
= read(fd
, buf
+ count
+ n
, bytes
- n
);
1189 fprintf(stderr
, "read_mrecords: short read on pipe\n");
1193 bcopy(pickup
, buf
+ count
, HAMMER_MREC_HEADSIZE
);
1194 pickup
->signature
= 0;
1196 mrec
= (void *)(buf
+ count
);
1199 * Validate the completed record
1201 if (mrec
->head
.rec_crc
!=
1202 crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
1203 mrec
->head
.rec_size
- HAMMER_MREC_CRCOFF
)) {
1204 fprintf(stderr
, "read_mrecords: malformed record "
1205 "on pipe, bad crc\n");
1210 * If its a B-Tree record validate the data crc.
1212 * NOTE: If the VFS passes us an explicitly errorde mrec
1213 * we just pass it through.
1215 type
= mrec
->head
.type
& HAMMER_MRECF_TYPE_MASK
;
1217 if (type
== HAMMER_MREC_TYPE_REC
) {
1218 if (mrec
->head
.rec_size
<
1219 sizeof(mrec
->rec
) + mrec
->rec
.leaf
.data_len
) {
1221 "read_mrecords: malformed record on "
1222 "pipe, illegal element data_len\n");
1225 if (mrec
->rec
.leaf
.data_len
&&
1226 mrec
->rec
.leaf
.data_offset
&&
1227 hammer_crc_test_leaf(&mrec
->rec
+ 1, &mrec
->rec
.leaf
) == 0) {
1229 "read_mrecords: data_crc did not "
1230 "match data! obj=%016jx key=%016jx\n",
1231 (uintmax_t)mrec
->rec
.leaf
.base
.obj_id
,
1232 (uintmax_t)mrec
->rec
.leaf
.base
.key
);
1234 "continuing, but there are problems\n");
1243 * Read and return a single mrecord.
1246 hammer_ioc_mrecord_any_t
1247 read_mrecord(int fdin
, int *errorp
, hammer_ioc_mrecord_head_t pickup
)
1249 hammer_ioc_mrecord_any_t mrec
;
1250 struct hammer_ioc_mrecord_head mrechd
;
1255 if (pickup
&& pickup
->type
!= 0) {
1257 pickup
->signature
= 0;
1259 n
= HAMMER_MREC_HEADSIZE
;
1262 * Read in the PFSD header from the sender.
1264 for (n
= 0; n
< HAMMER_MREC_HEADSIZE
; n
+= i
) {
1265 i
= read(fdin
, (char *)&mrechd
+ n
, HAMMER_MREC_HEADSIZE
- n
);
1270 *errorp
= 0; /* EOF */
1273 if (n
!= HAMMER_MREC_HEADSIZE
) {
1274 fprintf(stderr
, "short read of mrecord header\n");
1279 if (mrechd
.signature
!= HAMMER_IOC_MIRROR_SIGNATURE
) {
1280 fprintf(stderr
, "read_mrecord: bad signature\n");
1284 bytes
= HAMMER_HEAD_DOALIGN(mrechd
.rec_size
);
1285 assert(bytes
>= sizeof(mrechd
));
1286 mrec
= malloc(bytes
);
1287 mrec
->head
= mrechd
;
1290 i
= read(fdin
, (char *)mrec
+ n
, bytes
- n
);
1296 fprintf(stderr
, "read_mrecord: short read on payload\n");
1300 if (mrec
->head
.rec_crc
!=
1301 crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
1302 mrec
->head
.rec_size
- HAMMER_MREC_CRCOFF
)) {
1303 fprintf(stderr
, "read_mrecord: bad CRC\n");
1313 write_mrecord(int fdout
, u_int32_t type
, hammer_ioc_mrecord_any_t mrec
,
1316 char zbuf
[HAMMER_HEAD_ALIGN
];
1319 pad
= HAMMER_HEAD_DOALIGN(bytes
) - bytes
;
1321 assert(bytes
>= (int)sizeof(mrec
->head
));
1322 bzero(&mrec
->head
, sizeof(mrec
->head
));
1323 mrec
->head
.signature
= HAMMER_IOC_MIRROR_SIGNATURE
;
1324 mrec
->head
.type
= type
;
1325 mrec
->head
.rec_size
= bytes
;
1326 mrec
->head
.rec_crc
= crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
1327 bytes
- HAMMER_MREC_CRCOFF
);
1328 if (write(fdout
, mrec
, bytes
) != bytes
) {
1329 fprintf(stderr
, "write_mrecord: error %d (%s)\n",
1330 errno
, strerror(errno
));
1335 if (write(fdout
, zbuf
, pad
) != pad
) {
1336 fprintf(stderr
, "write_mrecord: error %d (%s)\n",
1337 errno
, strerror(errno
));
1344 * Generate a mirroring header with the pfs information of the
1345 * originating filesytem.
1348 generate_mrec_header(int fd
, int pfs_id
,
1349 union hammer_ioc_mrecord_any
*mrec_tmp
)
1351 struct hammer_ioc_pseudofs_rw pfs
;
1353 bzero(&pfs
, sizeof(pfs
));
1354 bzero(mrec_tmp
, sizeof(*mrec_tmp
));
1355 pfs
.pfs_id
= pfs_id
;
1356 pfs
.ondisk
= &mrec_tmp
->pfs
.pfsd
;
1357 pfs
.bytes
= sizeof(mrec_tmp
->pfs
.pfsd
);
1358 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
1359 fprintf(stderr
, "Mirror-read: not a HAMMER fs/pseudofs!\n");
1362 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
1363 fprintf(stderr
, "Mirror-read: HAMMER pfs version mismatch!\n");
1366 mrec_tmp
->pfs
.version
= pfs
.version
;
1370 * Validate the pfs information from the originating filesystem
1371 * against the target filesystem. shared_uuid must match.
1373 * return -1 if we got a TERM record
1376 validate_mrec_header(int fd
, int fdin
, int is_target
, int pfs_id
,
1377 struct hammer_ioc_mrecord_head
*pickup
,
1378 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
)
1380 struct hammer_ioc_pseudofs_rw pfs
;
1381 struct hammer_pseudofs_data pfsd
;
1382 hammer_ioc_mrecord_any_t mrec
;
1386 * Get the PFSD info from the target filesystem.
1388 bzero(&pfs
, sizeof(pfs
));
1389 bzero(&pfsd
, sizeof(pfsd
));
1390 pfs
.pfs_id
= pfs_id
;
1392 pfs
.bytes
= sizeof(pfsd
);
1393 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
1394 fprintf(stderr
, "mirror-write: not a HAMMER fs/pseudofs!\n");
1397 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
1398 fprintf(stderr
, "mirror-write: HAMMER pfs version mismatch!\n");
1402 mrec
= read_mrecord(fdin
, &error
, pickup
);
1405 fprintf(stderr
, "validate_mrec_header: short read\n");
1408 if (mrec
->head
.type
== HAMMER_MREC_TYPE_TERM
) {
1413 if (mrec
->head
.type
!= HAMMER_MREC_TYPE_PFSD
) {
1414 fprintf(stderr
, "validate_mrec_header: did not get expected "
1415 "PFSD record type\n");
1418 if (mrec
->head
.rec_size
!= sizeof(mrec
->pfs
)) {
1419 fprintf(stderr
, "validate_mrec_header: unexpected payload "
1423 if (mrec
->pfs
.version
!= pfs
.version
) {
1424 fprintf(stderr
, "validate_mrec_header: Version mismatch\n");
1429 * Whew. Ok, is the read PFS info compatible with the target?
1431 if (bcmp(&mrec
->pfs
.pfsd
.shared_uuid
, &pfsd
.shared_uuid
,
1432 sizeof(pfsd
.shared_uuid
)) != 0) {
1434 "mirror-write: source and target have "
1435 "different shared-uuid's!\n");
1439 (pfsd
.mirror_flags
& HAMMER_PFSD_SLAVE
) == 0) {
1440 fprintf(stderr
, "mirror-write: target must be in slave mode\n");
1444 *tid_begp
= mrec
->pfs
.pfsd
.sync_beg_tid
;
1446 *tid_endp
= mrec
->pfs
.pfsd
.sync_end_tid
;
1452 update_pfs_snapshot(int fd
, hammer_tid_t snapshot_tid
, int pfs_id
)
1454 struct hammer_ioc_pseudofs_rw pfs
;
1455 struct hammer_pseudofs_data pfsd
;
1457 bzero(&pfs
, sizeof(pfs
));
1458 bzero(&pfsd
, sizeof(pfsd
));
1459 pfs
.pfs_id
= pfs_id
;
1461 pfs
.bytes
= sizeof(pfsd
);
1462 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
1463 perror("update_pfs_snapshot (read)");
1466 if (pfsd
.sync_end_tid
!= snapshot_tid
) {
1467 pfsd
.sync_end_tid
= snapshot_tid
;
1468 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) != 0) {
1469 perror("update_pfs_snapshot (rewrite)");
1472 if (VerboseOpt
>= 2) {
1474 "Mirror-write: Completed, updated snapshot "
1476 (uintmax_t)snapshot_tid
);
1483 * Bandwidth-limited write in chunks
1487 writebw(int fd
, const void *buf
, size_t nbytes
,
1488 u_int64_t
*bwcount
, struct timeval
*tv1
)
1499 if (*bwcount
+ nbytes
> BandwidthOpt
)
1500 n
= BandwidthOpt
- *bwcount
;
1504 r
= write(fd
, buf
, n
);
1508 buf
= (const char *)buf
+ r
;
1513 if (*bwcount
>= BandwidthOpt
) {
1514 gettimeofday(&tv2
, NULL
);
1515 usec
= (int)(tv2
.tv_sec
- tv1
->tv_sec
) * 1000000 +
1516 (int)(tv2
.tv_usec
- tv1
->tv_usec
);
1517 if (usec
>= 0 && usec
< 1000000)
1518 usleep(1000000 - usec
);
1519 gettimeofday(tv1
, NULL
);
1520 *bwcount
-= BandwidthOpt
;
1527 * Get a yes or no answer from the terminal. The program may be run as
1528 * part of a two-way pipe so we cannot use stdin for this operation.
1537 fp
= fopen("/dev/tty", "r");
1539 fprintf(stderr
, "No terminal for response\n");
1543 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
1544 if (buf
[0] == 'y' || buf
[0] == 'Y') {
1548 if (buf
[0] == 'n' || buf
[0] == 'N') {
1552 fprintf(stderr
, "Response not understood\n");
1560 mirror_usage(int code
)
1563 "hammer mirror-read <filesystem> [begin-tid]\n"
1564 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
1565 "hammer mirror-write <filesystem>\n"
1566 "hammer mirror-dump\n"
1567 "hammer mirror-copy [[user@]host:]<filesystem>"
1568 " [[user@]host:]<filesystem>\n"
1569 "hammer mirror-stream [[user@]host:]<filesystem>"
1570 " [[user@]host:]<filesystem>\n"